Update row endpoints

This commit is contained in:
Andrew Kingston 2024-12-05 15:59:48 +00:00
parent b025878834
commit 923859b799
No known key found for this signature in database
1 changed files with 18 additions and 19 deletions

View File

@ -1,20 +1,21 @@
import { import {
DeleteRow, DeleteRowRequest,
DeleteRows,
ExportRowsRequest, ExportRowsRequest,
GetRowResponse, FindRowResponse,
PatchRowRequest, PatchRowRequest,
PatchRowResponse, PatchRowResponse,
Row, Row,
SaveRowRequest,
SaveRowResponse,
} from "@budibase/types" } from "@budibase/types"
import { BaseAPIClient } from "./types" import { BaseAPIClient } from "./types"
export interface RowEndpoints { export interface RowEndpoints {
fetchRow: (tableId: string, rowId: string) => Promise<GetRowResponse> fetchRow: (tableId: string, rowId: string) => Promise<FindRowResponse>
saveRow: ( saveRow: (
row: Row, row: SaveRowRequest,
suppressErrors?: boolean suppressErrors?: boolean
) => Promise<PatchRowResponse | undefined> ) => Promise<SaveRowResponse>
patchRow: ( patchRow: (
row: PatchRowRequest, row: PatchRowRequest,
suppressErrors?: boolean suppressErrors?: boolean
@ -30,13 +31,13 @@ export interface RowEndpoints {
export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({ export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({
/** /**
* Fetches data about a certain row in a table. * Fetches data about a certain row in a data source.
* @param tableId the ID of the table to fetch from * @param sourceId the ID of the table or view to fetch from
* @param rowId the ID of the row to fetch * @param rowId the ID of the row to fetch
*/ */
fetchRow: async (tableId, rowId) => { fetchRow: async (sourceId, rowId) => {
return await API.get({ return await API.get({
url: `/api/${tableId}/rows/${rowId}`, url: `/api/${sourceId}/rows/${rowId}`,
}) })
}, },
@ -46,12 +47,9 @@ export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({
* @param suppressErrors whether or not to suppress error notifications * @param suppressErrors whether or not to suppress error notifications
*/ */
saveRow: async (row, suppressErrors = false) => { saveRow: async (row, suppressErrors = false) => {
const resourceId = row?._viewId || row?.tableId const sourceId = row._viewId || row.tableId
if (!resourceId) {
return
}
return await API.post({ return await API.post({
url: `/api/${resourceId}/rows`, url: `/api/${sourceId}/rows`,
body: row, body: row,
suppressErrors, suppressErrors,
}) })
@ -63,8 +61,9 @@ export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({
* @param suppressErrors whether or not to suppress error notifications * @param suppressErrors whether or not to suppress error notifications
*/ */
patchRow: async (row, suppressErrors = false) => { patchRow: async (row, suppressErrors = false) => {
const sourceId = row._viewId || row.tableId
return await API.patch({ return await API.patch({
url: `/api/${row.tableId}/rows`, url: `/api/${sourceId}/rows`,
body: row, body: row,
suppressErrors, suppressErrors,
}) })
@ -76,7 +75,7 @@ export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({
* @param rowId the ID of the row to delete * @param rowId the ID of the row to delete
*/ */
deleteRow: async (sourceId, rowId) => { deleteRow: async (sourceId, rowId) => {
return await API.delete<DeleteRow>({ return await API.delete<DeleteRowRequest>({
url: `/api/${sourceId}/rows`, url: `/api/${sourceId}/rows`,
body: { body: {
_id: rowId, _id: rowId,
@ -90,12 +89,12 @@ export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({
* @param rows the array of rows to delete * @param rows the array of rows to delete
*/ */
deleteRows: async (sourceId, rows) => { deleteRows: async (sourceId, rows) => {
rows?.forEach((row: Row | string) => { rows.forEach(row => {
if (typeof row === "object") { if (typeof row === "object") {
delete row?._viewId delete row?._viewId
} }
}) })
return await API.delete<DeleteRows>({ return await API.delete<DeleteRowRequest>({
url: `/api/${sourceId}/rows`, url: `/api/${sourceId}/rows`,
body: { body: {
rows, rows,