From a26eb571152bb0aa9b3fbb0cf6a9289239e24b2a Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 26 Nov 2024 09:50:53 +0000 Subject: [PATCH] Type frontend core app endpoints --- .../frontend-core/src/api/{app.js => app.ts} | 66 ++++++++++++++++++- packages/frontend-core/src/api/index.ts | 7 +- packages/frontend-core/src/api/types.ts | 10 ++- 3 files changed, 75 insertions(+), 8 deletions(-) rename packages/frontend-core/src/api/{app.js => app.ts} (69%) diff --git a/packages/frontend-core/src/api/app.js b/packages/frontend-core/src/api/app.ts similarity index 69% rename from packages/frontend-core/src/api/app.js rename to packages/frontend-core/src/api/app.ts index de1703373b..b884f618bb 100644 --- a/packages/frontend-core/src/api/app.js +++ b/packages/frontend-core/src/api/app.ts @@ -1,6 +1,56 @@ import { sdk } from "@budibase/shared-core" +import { BaseAPIClient } from "./types" +import { + App, + CreateAppRequest, + DuplicateAppRequest, + DuplicateAppResponse, + FetchAppPackageResponse, + GetDiagnosticsResponse, + UpdateAppRequest, + UpdateAppResponse, +} from "@budibase/types" -export const buildAppEndpoints = API => ({ +export type AppEndpoints = { + fetchAppPackage: (appId: string) => Promise + saveAppMetadata: ( + appId: string, + metadata: UpdateAppRequest + ) => Promise + unpublishApp: (appId: string) => Promise + createApp: (app: CreateAppRequest) => Promise + deleteApp: (appId: string) => Promise + duplicateApp: ( + appId: string, + app: DuplicateAppRequest + ) => Promise + updateAppFromExport: ( + appId: string, + body: any + ) => Promise<{ message: string }> + fetchSystemDebugInfo: () => Promise + syncApp: (appId: string) => Promise<{ message: string }> + getApps: () => Promise + fetchComponentLibDefinitions: ( + appId: string + ) => Promise<{ [key: string]: any }> + setRevertableVersion: ( + appId: string, + revertableVersion: string + ) => Promise + addSampleData: (appId: string) => Promise + + // Untyped - TODO: + publishAppChanges: (appId: string) => Promise + revertAppChanges: (appId: string) => Promise + updateAppClientVersion: (appId: string) => Promise + revertAppClientVersion: (appId: string) => Promise + importApps: (apps: any) => Promise + releaseAppLock: (appId: string) => Promise + getAppDeployments: () => Promise +} + +export const buildAppEndpoints = (API: BaseAPIClient): AppEndpoints => ({ /** * Fetches screen definition for an app. * @param appId the ID of the app to fetch from @@ -16,7 +66,7 @@ export const buildAppEndpoints = API => ({ * @param appId the ID of the app to update * @param metadata the app metadata to save */ - saveAppMetadata: async ({ appId, metadata }) => { + saveAppMetadata: async (appId, metadata) => { return await API.put({ url: `/api/applications/${appId}`, body: metadata, @@ -87,7 +137,7 @@ export const buildAppEndpoints = API => ({ * Duplicate an existing app * @param app the app to dupe */ - duplicateApp: async (app, appId) => { + duplicateApp: async (appId, app) => { return await API.post({ url: `/api/applications/${appId}/duplicate`, body: app, @@ -192,12 +242,22 @@ export const buildAppEndpoints = API => ({ }) }, + /** + * Adds sample data to an app + * @param appId the app ID + */ addSampleData: async appId => { return await API.post({ url: `/api/applications/${appId}/sample`, }) }, + /** + * Sets the revertable version of an app. + * Used when manually reverting to older client versions. + * @param appId the app ID + * @param revertableVersion the version number + */ setRevertableVersion: async (appId, revertableVersion) => { return await API.post({ url: `/api/applications/${appId}/setRevertableVersion`, diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index cb4753d7cd..47900ba029 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -4,7 +4,8 @@ import { APIClientConfig, APIClient, APICallConfig, -} from "../types" + BaseAPIClient, +} from "./types" import { Helpers } from "@budibase/bbui" import { Header } from "@budibase/shared-core" import { ApiVersion } from "../constants" @@ -55,7 +56,7 @@ export const APISessionID = Helpers.uuid() /** * Constructs an API client with the provided configuration. */ -export const createAPIClient = (config: APIClientConfig = {}) => { +export const createAPIClient = (config: APIClientConfig = {}): APIClient => { let cache: Record = {} // Generates an error object from an API response @@ -220,7 +221,7 @@ export const createAPIClient = (config: APIClientConfig = {}) => { } // Build the underlying core API methods - let API: APIClient = { + let API: BaseAPIClient = { post: requestApiCall(HTTPMethod.POST), get: requestApiCall(HTTPMethod.GET), patch: requestApiCall(HTTPMethod.PATCH), diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index 4406deb31b..ec6b604340 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -1,3 +1,5 @@ +import { AppEndpoints } from "./app" + export enum HTTPMethod { POST = "POST", PATCH = "PATCH", @@ -26,11 +28,15 @@ export type APICallConfig = { export type APICallParams = Pick & Partial -export type APIClient = { +export type BaseAPIClient = { post: (params: APICallParams) => Promise get: (params: APICallParams) => Promise put: (params: APICallParams) => Promise delete: (params: APICallParams) => Promise patch: (params: APICallParams) => Promise - [key: string]: any + error: (message: string) => void + invalidateCache: () => void + getAppID: () => string } + +export type APIClient = BaseAPIClient & AppEndpoints & { [key: string]: any }