2023-11-18 04:06:01 +13:00
|
|
|
import { Table, SearchInputParams, CreateTableParams } from "../../../types"
|
2022-09-15 11:58:08 +12:00
|
|
|
import { HeadersInit, Response } from "node-fetch"
|
2023-04-06 02:33:56 +12:00
|
|
|
import { generateTable } from "../../fixtures/tables"
|
|
|
|
import BudibasePublicAPIClient from "../BudibasePublicAPIClient"
|
2022-09-15 11:58:08 +12:00
|
|
|
|
2023-04-06 02:33:56 +12:00
|
|
|
export default class TableAPI {
|
2022-09-15 11:58:08 +12:00
|
|
|
headers?: HeadersInit
|
|
|
|
|
2023-04-06 02:33:56 +12:00
|
|
|
client: BudibasePublicAPIClient
|
|
|
|
|
|
|
|
constructor(client: BudibasePublicAPIClient) {
|
|
|
|
this.client = client
|
2022-09-15 11:58:08 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
async seed() {
|
|
|
|
return this.create(generateTable())
|
|
|
|
}
|
|
|
|
|
|
|
|
async create(body: CreateTableParams): Promise<[Response, Table]> {
|
2023-04-06 02:33:56 +12:00
|
|
|
const [response, json] = await this.client.post(`/tables`, {
|
2022-09-15 11:58:08 +12:00
|
|
|
body,
|
|
|
|
})
|
|
|
|
return [response, json.data]
|
|
|
|
}
|
|
|
|
|
|
|
|
async read(id: string): Promise<[Response, Table]> {
|
2023-04-06 02:33:56 +12:00
|
|
|
const [response, json] = await this.client.get(`/tables/${id}`)
|
2022-09-15 11:58:08 +12:00
|
|
|
return [response, json.data]
|
|
|
|
}
|
|
|
|
|
2022-09-16 06:51:11 +12:00
|
|
|
async search(body: SearchInputParams): Promise<[Response, [Table]]> {
|
2023-04-06 02:33:56 +12:00
|
|
|
const [response, json] = await this.client.post(`/tables/search`, { body })
|
2022-09-15 11:58:08 +12:00
|
|
|
return [response, json.data]
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(id: string, body: Table): Promise<[Response, Table]> {
|
2023-04-06 02:33:56 +12:00
|
|
|
const [response, json] = await this.client.put(`/tables/${id}`, { body })
|
2022-09-15 11:58:08 +12:00
|
|
|
return [response, json.data]
|
|
|
|
}
|
|
|
|
}
|