Type frontend core app endpoints

This commit is contained in:
Andrew Kingston 2024-11-26 09:50:53 +00:00
parent 6776a55356
commit a26eb57115
No known key found for this signature in database
3 changed files with 75 additions and 8 deletions

View File

@ -1,6 +1,56 @@
import { sdk } from "@budibase/shared-core" 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<FetchAppPackageResponse>
saveAppMetadata: (
appId: string,
metadata: UpdateAppRequest
) => Promise<UpdateAppResponse>
unpublishApp: (appId: string) => Promise<void>
createApp: (app: CreateAppRequest) => Promise<App>
deleteApp: (appId: string) => Promise<void>
duplicateApp: (
appId: string,
app: DuplicateAppRequest
) => Promise<DuplicateAppResponse>
updateAppFromExport: (
appId: string,
body: any
) => Promise<{ message: string }>
fetchSystemDebugInfo: () => Promise<GetDiagnosticsResponse>
syncApp: (appId: string) => Promise<{ message: string }>
getApps: () => Promise<App[]>
fetchComponentLibDefinitions: (
appId: string
) => Promise<{ [key: string]: any }>
setRevertableVersion: (
appId: string,
revertableVersion: string
) => Promise<App>
addSampleData: (appId: string) => Promise<void>
// Untyped - TODO:
publishAppChanges: (appId: string) => Promise<any>
revertAppChanges: (appId: string) => Promise<any>
updateAppClientVersion: (appId: string) => Promise<any>
revertAppClientVersion: (appId: string) => Promise<any>
importApps: (apps: any) => Promise<any>
releaseAppLock: (appId: string) => Promise<any>
getAppDeployments: () => Promise<any>
}
export const buildAppEndpoints = (API: BaseAPIClient): AppEndpoints => ({
/** /**
* Fetches screen definition for an app. * Fetches screen definition for an app.
* @param appId the ID of the app to fetch from * @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 appId the ID of the app to update
* @param metadata the app metadata to save * @param metadata the app metadata to save
*/ */
saveAppMetadata: async ({ appId, metadata }) => { saveAppMetadata: async (appId, metadata) => {
return await API.put({ return await API.put({
url: `/api/applications/${appId}`, url: `/api/applications/${appId}`,
body: metadata, body: metadata,
@ -87,7 +137,7 @@ export const buildAppEndpoints = API => ({
* Duplicate an existing app * Duplicate an existing app
* @param app the app to dupe * @param app the app to dupe
*/ */
duplicateApp: async (app, appId) => { duplicateApp: async (appId, app) => {
return await API.post({ return await API.post({
url: `/api/applications/${appId}/duplicate`, url: `/api/applications/${appId}/duplicate`,
body: app, body: app,
@ -192,12 +242,22 @@ export const buildAppEndpoints = API => ({
}) })
}, },
/**
* Adds sample data to an app
* @param appId the app ID
*/
addSampleData: async appId => { addSampleData: async appId => {
return await API.post({ return await API.post({
url: `/api/applications/${appId}/sample`, 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) => { setRevertableVersion: async (appId, revertableVersion) => {
return await API.post({ return await API.post({
url: `/api/applications/${appId}/setRevertableVersion`, url: `/api/applications/${appId}/setRevertableVersion`,

View File

@ -4,7 +4,8 @@ import {
APIClientConfig, APIClientConfig,
APIClient, APIClient,
APICallConfig, APICallConfig,
} from "../types" BaseAPIClient,
} from "./types"
import { Helpers } from "@budibase/bbui" import { Helpers } from "@budibase/bbui"
import { Header } from "@budibase/shared-core" import { Header } from "@budibase/shared-core"
import { ApiVersion } from "../constants" import { ApiVersion } from "../constants"
@ -55,7 +56,7 @@ export const APISessionID = Helpers.uuid()
/** /**
* Constructs an API client with the provided configuration. * Constructs an API client with the provided configuration.
*/ */
export const createAPIClient = (config: APIClientConfig = {}) => { export const createAPIClient = (config: APIClientConfig = {}): APIClient => {
let cache: Record<string, any> = {} let cache: Record<string, any> = {}
// Generates an error object from an API response // Generates an error object from an API response
@ -220,7 +221,7 @@ export const createAPIClient = (config: APIClientConfig = {}) => {
} }
// Build the underlying core API methods // Build the underlying core API methods
let API: APIClient = { let API: BaseAPIClient = {
post: requestApiCall(HTTPMethod.POST), post: requestApiCall(HTTPMethod.POST),
get: requestApiCall(HTTPMethod.GET), get: requestApiCall(HTTPMethod.GET),
patch: requestApiCall(HTTPMethod.PATCH), patch: requestApiCall(HTTPMethod.PATCH),

View File

@ -1,3 +1,5 @@
import { AppEndpoints } from "./app"
export enum HTTPMethod { export enum HTTPMethod {
POST = "POST", POST = "POST",
PATCH = "PATCH", PATCH = "PATCH",
@ -26,11 +28,15 @@ export type APICallConfig = {
export type APICallParams = Pick<APICallConfig, "url"> & Partial<APICallConfig> export type APICallParams = Pick<APICallConfig, "url"> & Partial<APICallConfig>
export type APIClient = { export type BaseAPIClient = {
post: <T>(params: APICallParams) => Promise<T> post: <T>(params: APICallParams) => Promise<T>
get: <T>(params: APICallParams) => Promise<T> get: <T>(params: APICallParams) => Promise<T>
put: <T>(params: APICallParams) => Promise<T> put: <T>(params: APICallParams) => Promise<T>
delete: <T>(params: APICallParams) => Promise<T> delete: <T>(params: APICallParams) => Promise<T>
patch: <T>(params: APICallParams) => Promise<T> patch: <T>(params: APICallParams) => Promise<T>
[key: string]: any error: (message: string) => void
invalidateCache: () => void
getAppID: () => string
} }
export type APIClient = BaseAPIClient & AppEndpoints & { [key: string]: any }