From ac05ccd6a8e2449a50d9c9ddd8373084900a4e21 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 3 Dec 2024 09:59:08 +0000 Subject: [PATCH] Type query endpoints --- .../builder/src/stores/builder/queries.js | 5 +-- packages/client/src/utils/buttonActions.js | 7 +-- .../src/api/{queries.js => queries.ts} | 43 +++++++++++++++---- packages/frontend-core/src/api/types.ts | 4 +- .../frontend-core/src/fetch/QueryFetch.js | 4 +- 5 files changed, 43 insertions(+), 20 deletions(-) rename packages/frontend-core/src/api/{queries.js => queries.ts} (56%) diff --git a/packages/builder/src/stores/builder/queries.js b/packages/builder/src/stores/builder/queries.js index b717a17f97..a3c599785d 100644 --- a/packages/builder/src/stores/builder/queries.js +++ b/packages/builder/src/stores/builder/queries.js @@ -62,10 +62,7 @@ export function createQueriesStore() { } const importQueries = async ({ data, datasourceId }) => { - return await API.importQueries({ - datasourceId, - data, - }) + return await API.importQueries(datasourceId, data) } const select = id => { diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 63354d3119..561c12556a 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -251,17 +251,14 @@ const navigationHandler = action => { } const queryExecutionHandler = async action => { - const { datasourceId, queryId, queryParams, notificationOverride } = - action.parameters + const { queryId, queryParams, notificationOverride } = action.parameters try { const query = await API.fetchQueryDefinition(queryId) if (query?.datasourceId == null) { notificationStore.actions.error("That query couldn't be found") return false } - const result = await API.executeQuery({ - datasourceId, - queryId, + const result = await API.executeQuery(queryId, { parameters: queryParams, }) diff --git a/packages/frontend-core/src/api/queries.js b/packages/frontend-core/src/api/queries.ts similarity index 56% rename from packages/frontend-core/src/api/queries.js rename to packages/frontend-core/src/api/queries.ts index f18ec7c4ec..02b1dc38ec 100644 --- a/packages/frontend-core/src/api/queries.js +++ b/packages/frontend-core/src/api/queries.ts @@ -1,12 +1,39 @@ -export const buildQueryEndpoints = API => ({ +import { + ExecuteQueryRequest, + ExecuteQueryResponse, + PreviewQueryRequest, + PreviewQueryResponse, + Query, +} from "@budibase/types" +import { BaseAPIClient } from "./types" + +export interface QueryEndpoints { + executeQuery: ( + queryId: string, + opts?: ExecuteQueryRequest + ) => Promise[]> + fetchQueryDefinition: (queryId: string) => Promise + getQueries: () => Promise + saveQuery: (query: Query) => Promise + deleteQuery: (id: string, rev: string) => Promise + previewQuery: (query: PreviewQueryRequest) => Promise + + // Missing request or response types + importQueries: (datasourceId: string, data: any) => Promise +} + +export const buildQueryEndpoints = (API: BaseAPIClient): QueryEndpoints => ({ /** * Executes a query against an external data connector. * @param queryId the ID of the query to execute * @param pagination pagination info for the query * @param parameters parameters for the query */ - executeQuery: async ({ queryId, pagination, parameters }) => { - return await API.post({ + executeQuery: async (queryId, { pagination, parameters } = {}) => { + return await API.post< + ExecuteQueryRequest, + ExecuteQueryResponse | Record[] + >({ url: `/api/v2/queries/${queryId}`, body: { parameters, @@ -48,12 +75,12 @@ export const buildQueryEndpoints = API => ({ /** * Deletes a query - * @param queryId the ID of the query to delete - * @param queryRev the rev of the query to delete + * @param id the ID of the query to delete + * @param rev the rev of the query to delete */ - deleteQuery: async ({ queryId, queryRev }) => { + deleteQuery: async (id, rev) => { return await API.delete({ - url: `/api/queries/${queryId}/${queryRev}`, + url: `/api/queries/${id}/${rev}`, }) }, @@ -62,7 +89,7 @@ export const buildQueryEndpoints = API => ({ * @param datasourceId the datasource ID to import queries into * @param data the data string of the content to import */ - importQueries: async ({ datasourceId, data }) => { + importQueries: async (datasourceId, data) => { return await API.post({ url: "/api/queries/import", body: { diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index ebea541c34..5908d37a25 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -19,6 +19,7 @@ import { MigrationEndpoints } from "./migrations" import { OtherEndpoints } from "./other" import { PermissionEndpoints } from "./permissions" import { PluginEndpoins } from "./plugins" +import { QueryEndpoints } from "./queries" export enum HTTPMethod { POST = "POST", @@ -109,4 +110,5 @@ export type APIClient = BaseAPIClient & MigrationEndpoints & OtherEndpoints & PermissionEndpoints & - PluginEndpoins & { [key: string]: any } + PluginEndpoins & + QueryEndpoints & { [key: string]: any } diff --git a/packages/frontend-core/src/fetch/QueryFetch.js b/packages/frontend-core/src/fetch/QueryFetch.js index 6420893515..9fac9704d3 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.js +++ b/packages/frontend-core/src/fetch/QueryFetch.js @@ -48,7 +48,7 @@ export default class QueryFetch extends DataFetch { } // Add pagination to query if supported - let queryPayload = { queryId: datasource?._id, parameters } + let queryPayload = { parameters } if (paginate && supportsPagination) { const requestCursor = type === "page" ? parseInt(cursor || 1) : cursor queryPayload.pagination = { page: requestCursor, limit } @@ -56,7 +56,7 @@ export default class QueryFetch extends DataFetch { // Execute query try { - const res = await this.API.executeQuery(queryPayload) + const res = await this.API.executeQuery(datasource?._id, queryPayload) const { data, pagination, ...rest } = res // Derive pagination info from response