diff --git a/packages/frontend-core/src/api/relationships.js b/packages/frontend-core/src/api/relationships.js deleted file mode 100644 index 45595750a8..0000000000 --- a/packages/frontend-core/src/api/relationships.js +++ /dev/null @@ -1,21 +0,0 @@ -export const buildRelationshipEndpoints = API => ({ - /** - * Fetches related rows for a certain field of a certain row. - * @param tableId the ID of the table to fetch from - * @param rowId the ID of the row to fetch related rows for - * @param fieldName the name of the relationship field - */ - fetchRelationshipData: async ({ tableId, rowId, fieldName }) => { - if (!tableId || !rowId) { - return [] - } - const response = await API.get({ - url: `/api/${tableId}/${rowId}/enrich?field=${fieldName}`, - }) - if (!fieldName) { - return response || [] - } else { - return response[fieldName] || [] - } - }, -}) diff --git a/packages/frontend-core/src/api/relationships.ts b/packages/frontend-core/src/api/relationships.ts new file mode 100644 index 0000000000..4579557a0a --- /dev/null +++ b/packages/frontend-core/src/api/relationships.ts @@ -0,0 +1,31 @@ +import { Row } from "@budibase/types" +import { BaseAPIClient } from "./types" + +export interface RelationshipEndpoints { + fetchRelationshipData: ( + sourceId: string, + rowId: string, + fieldName?: string + ) => Promise +} + +export const buildRelationshipEndpoints = ( + API: BaseAPIClient +): RelationshipEndpoints => ({ + /** + * Fetches related rows for a certain field of a certain row. + * @param sourceId the ID of the table to fetch from + * @param rowId the ID of the row to fetch related rows for + * @param fieldName the name of the relationship field + */ + fetchRelationshipData: async (sourceId, rowId, fieldName) => { + const response = await API.get({ + url: `/api/${sourceId}/${rowId}/enrich?field=${fieldName}`, + }) + if (!fieldName) { + return [response] + } else { + return response[fieldName] || [] + } + }, +}) diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index 5908d37a25..6d2088cfba 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -20,6 +20,7 @@ import { OtherEndpoints } from "./other" import { PermissionEndpoints } from "./permissions" import { PluginEndpoins } from "./plugins" import { QueryEndpoints } from "./queries" +import { RelationshipEndpoints } from "./relationships" export enum HTTPMethod { POST = "POST", @@ -111,4 +112,5 @@ export type APIClient = BaseAPIClient & OtherEndpoints & PermissionEndpoints & PluginEndpoins & - QueryEndpoints & { [key: string]: any } + QueryEndpoints & + RelationshipEndpoints & { [key: string]: any } diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.js b/packages/frontend-core/src/fetch/RelationshipFetch.js index 04797fcdf1..0dec535724 100644 --- a/packages/frontend-core/src/fetch/RelationshipFetch.js +++ b/packages/frontend-core/src/fetch/RelationshipFetch.js @@ -3,13 +3,16 @@ import DataFetch from "./DataFetch.js" export default class RelationshipFetch extends DataFetch { async getData() { const { datasource } = this.options + if (!datasource?.rowId || !datasource?.rowTableId) { + return { rows: [] } + } try { - const res = await this.API.fetchRelationshipData({ - rowId: datasource?.rowId, - tableId: datasource?.rowTableId, - fieldName: datasource?.fieldName, - }) - return { rows: res || [] } + const res = await this.API.fetchRelationshipData( + datasource.rowTableId, + datasource.rowId, + datasource.fieldName + ) + return { rows: res } } catch (error) { return { rows: [] } }