Type query endpoints

This commit is contained in:
Andrew Kingston 2024-12-03 09:59:08 +00:00
parent abddbdeea7
commit ac05ccd6a8
No known key found for this signature in database
5 changed files with 43 additions and 20 deletions

View File

@ -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 => {

View File

@ -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,
})

View File

@ -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<ExecuteQueryResponse | Record<string, any>[]>
fetchQueryDefinition: (queryId: string) => Promise<Query>
getQueries: () => Promise<Query[]>
saveQuery: (query: Query) => Promise<Query>
deleteQuery: (id: string, rev: string) => Promise<void>
previewQuery: (query: PreviewQueryRequest) => Promise<PreviewQueryResponse>
// Missing request or response types
importQueries: (datasourceId: string, data: any) => Promise<any>
}
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<string, any>[]
>({
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: {

View File

@ -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 }

View File

@ -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