Api key, application and analytics API typing.

This commit is contained in:
mike12345567 2024-11-29 16:03:53 +00:00
parent c372d5e64e
commit e4fd2c94d4
14 changed files with 126 additions and 26 deletions

View File

@ -1,16 +1,22 @@
import { events, context } from "@budibase/backend-core" import { events, context } from "@budibase/backend-core"
import { AnalyticsPingRequest, App, PingSource } from "@budibase/types" import {
AnalyticsPingRequest,
App,
PingSource,
Ctx,
AnalyticsEnabledResponse,
} from "@budibase/types"
import { DocumentType, isDevAppID } from "../../db/utils" import { DocumentType, isDevAppID } from "../../db/utils"
export const isEnabled = async (ctx: any) => { export const isEnabled = async (ctx: Ctx<void, AnalyticsEnabledResponse>) => {
const enabled = await events.analytics.enabled() const enabled = await events.analytics.enabled()
ctx.body = { ctx.body = {
enabled, enabled,
} }
} }
export const ping = async (ctx: any) => { export const ping = async (ctx: Ctx<AnalyticsPingRequest, void>) => {
const body = ctx.request.body as AnalyticsPingRequest const body = ctx.request.body
switch (body.source) { switch (body.source) {
case PingSource.APP: { case PingSource.APP: {

View File

@ -1,18 +1,25 @@
import { db as dbCore, tenancy } from "@budibase/backend-core" import { db as dbCore, tenancy } from "@budibase/backend-core"
import { BBContext, Document } from "@budibase/types" import {
Document,
UserCtx,
ApiKeyDoc,
ApiKeyFetchResponse,
ApiKeyUpdateRequest,
ApiKeyUpdateResponse,
} from "@budibase/types"
const KEYS_DOC = dbCore.StaticDatabases.GLOBAL.docs.apiKeys const KEYS_DOC = dbCore.StaticDatabases.GLOBAL.docs.apiKeys
async function getBuilderMainDoc() { async function getBuilderMainDoc() {
const db = tenancy.getGlobalDB() const db = tenancy.getGlobalDB()
try { const doc = await db.tryGet<ApiKeyDoc>(KEYS_DOC)
return await db.get<any>(KEYS_DOC) if (!doc) {
} catch (err) {
// doesn't exist yet, nothing to get
return { return {
_id: KEYS_DOC, _id: KEYS_DOC,
apiKeys: {},
} }
} }
return doc
} }
async function setBuilderMainDoc(doc: Document) { async function setBuilderMainDoc(doc: Document) {
@ -22,7 +29,7 @@ async function setBuilderMainDoc(doc: Document) {
return db.put(doc) return db.put(doc)
} }
export async function fetch(ctx: BBContext) { export async function fetch(ctx: UserCtx<void, ApiKeyFetchResponse>) {
try { try {
const mainDoc = await getBuilderMainDoc() const mainDoc = await getBuilderMainDoc()
ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {} ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {}
@ -32,7 +39,9 @@ export async function fetch(ctx: BBContext) {
} }
} }
export async function update(ctx: BBContext) { export async function update(
ctx: UserCtx<ApiKeyUpdateRequest, ApiKeyUpdateResponse>
) {
const key = ctx.params.key const key = ctx.params.key
const value = ctx.request.body.value const value = ctx.request.body.value

View File

@ -59,6 +59,16 @@ import {
BBReferenceFieldSubType, BBReferenceFieldSubType,
Row, Row,
BBRequest, BBRequest,
SyncAppResponse,
CreateAppResponse,
FetchAppsResponse,
UpdateAppClientResponse,
RevertAppClientResponse,
DestroyAppResponse,
ImportToUpdateAppRequest,
ImportToUpdateAppResponse,
SetRevertableAppVersionRequest,
SetRevertableAppVersionResponse,
} from "@budibase/types" } from "@budibase/types"
import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts" import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts"
import sdk from "../../sdk" import sdk from "../../sdk"
@ -166,7 +176,7 @@ async function createInstance(appId: string, template: AppTemplate) {
return { _id: appId } return { _id: appId }
} }
export const addSampleData = async (ctx: UserCtx) => { export const addSampleData = async (ctx: UserCtx<void, void>) => {
const db = context.getAppDB() const db = context.getAppDB()
try { try {
@ -182,7 +192,7 @@ export const addSampleData = async (ctx: UserCtx) => {
ctx.status = 200 ctx.status = 200
} }
export async function fetch(ctx: UserCtx<void, App[]>) { export async function fetch(ctx: UserCtx<void, FetchAppsResponse>) {
ctx.body = await sdk.applications.fetch( ctx.body = await sdk.applications.fetch(
ctx.query.status as AppStatus, ctx.query.status as AppStatus,
ctx.user ctx.user
@ -242,7 +252,9 @@ export async function fetchAppPackage(
} }
} }
async function performAppCreate(ctx: UserCtx<CreateAppRequest, App>) { async function performAppCreate(
ctx: UserCtx<CreateAppRequest, CreateAppResponse>
) {
const apps = (await dbCore.getAllApps({ dev: true })) as App[] const apps = (await dbCore.getAllApps({ dev: true })) as App[]
const { body } = ctx.request const { body } = ctx.request
const { name, url, encryptionPassword, templateKey } = body const { name, url, encryptionPassword, templateKey } = body
@ -510,7 +522,9 @@ async function appPostCreate(ctx: UserCtx<CreateAppRequest, App>, app: App) {
} }
} }
export async function create(ctx: UserCtx<CreateAppRequest, App>) { export async function create(
ctx: UserCtx<CreateAppRequest, CreateAppResponse>
) {
const newApplication = await quotas.addApp(() => performAppCreate(ctx)) const newApplication = await quotas.addApp(() => performAppCreate(ctx))
await appPostCreate(ctx, newApplication) await appPostCreate(ctx, newApplication)
await cache.bustCache(cache.CacheKey.CHECKLIST) await cache.bustCache(cache.CacheKey.CHECKLIST)
@ -553,7 +567,9 @@ export async function update(
}) })
} }
export async function updateClient(ctx: UserCtx) { export async function updateClient(
ctx: UserCtx<void, UpdateAppClientResponse>
) {
// Get current app version // Get current app version
const application = await sdk.applications.metadata.get() const application = await sdk.applications.metadata.get()
const currentVersion = application.version const currentVersion = application.version
@ -581,7 +597,9 @@ export async function updateClient(ctx: UserCtx) {
ctx.body = app ctx.body = app
} }
export async function revertClient(ctx: UserCtx) { export async function revertClient(
ctx: UserCtx<void, RevertAppClientResponse>
) {
// Check app can be reverted // Check app can be reverted
const application = await sdk.applications.metadata.get() const application = await sdk.applications.metadata.get()
if (!application.revertableVersion) { if (!application.revertableVersion) {
@ -668,7 +686,7 @@ async function postDestroyApp(ctx: UserCtx) {
} }
} }
export async function destroy(ctx: UserCtx) { export async function destroy(ctx: UserCtx<void, DestroyAppResponse>) {
await preDestroyApp(ctx) await preDestroyApp(ctx)
const result = await destroyApp(ctx) const result = await destroyApp(ctx)
await postDestroyApp(ctx) await postDestroyApp(ctx)
@ -676,7 +694,7 @@ export async function destroy(ctx: UserCtx) {
ctx.body = result ctx.body = result
} }
export async function unpublish(ctx: UserCtx) { export async function unpublish(ctx: UserCtx<void, void>) {
const prodAppId = dbCore.getProdAppID(ctx.params.appId) const prodAppId = dbCore.getProdAppID(ctx.params.appId)
const dbExists = await dbCore.dbExists(prodAppId) const dbExists = await dbCore.dbExists(prodAppId)
@ -692,7 +710,7 @@ export async function unpublish(ctx: UserCtx) {
builderSocket?.emitAppUnpublish(ctx) builderSocket?.emitAppUnpublish(ctx)
} }
export async function sync(ctx: UserCtx) { export async function sync(ctx: UserCtx<void, SyncAppResponse>) {
const appId = ctx.params.appId const appId = ctx.params.appId
try { try {
ctx.body = await sdk.applications.syncApp(appId) ctx.body = await sdk.applications.syncApp(appId)
@ -701,10 +719,12 @@ export async function sync(ctx: UserCtx) {
} }
} }
export async function importToApp(ctx: UserCtx) { export async function importToApp(
ctx: UserCtx<ImportToUpdateAppRequest, ImportToUpdateAppResponse>
) {
const { appId } = ctx.params const { appId } = ctx.params
const appExport = ctx.request.files?.appExport const appExport = ctx.request.files?.appExport
const password = ctx.request.body.encryptionPassword as string const password = ctx.request.body.encryptionPassword
if (!appExport) { if (!appExport) {
ctx.throw(400, "Must supply app export to import") ctx.throw(400, "Must supply app export to import")
} }
@ -811,7 +831,7 @@ export async function updateAppPackage(
} }
export async function setRevertableVersion( export async function setRevertableVersion(
ctx: UserCtx<{ revertableVersion: string }, App> ctx: UserCtx<SetRevertableAppVersionRequest, void>
) { ) {
if (!env.isDev()) { if (!env.isDev()) {
ctx.status = 403 ctx.status = 403

View File

@ -7,7 +7,12 @@ import {
enableCronTrigger, enableCronTrigger,
} from "../../../automations/utils" } from "../../../automations/utils"
import { backups } from "@budibase/pro" import { backups } from "@budibase/pro"
import { App, AppBackupTrigger } from "@budibase/types" import {
App,
AppBackupTrigger,
PublishAppResponse,
UserCtx,
} from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { builderSocket } from "../../../websockets" import { builderSocket } from "../../../websockets"
@ -123,7 +128,9 @@ export async function deploymentProgress(ctx: any) {
} }
} }
export const publishApp = async function (ctx: any) { export const publishApp = async function (
ctx: UserCtx<void, PublishAppResponse>
) {
let deployment = new Deployment() let deployment = new Deployment()
console.log("Deployment object created") console.log("Deployment object created")
deployment.setStatus(DeploymentStatus.PENDING) deployment.setStatus(DeploymentStatus.PENDING)

View File

@ -113,7 +113,7 @@ export async function syncUsersToAllApps(userIds: string[]) {
export async function syncApp( export async function syncApp(
appId: string, appId: string,
opts?: { automationOnly?: boolean } opts?: { automationOnly?: boolean }
) { ): Promise<{ message: string }> {
if (env.DISABLE_AUTO_PROD_APP_SYNC) { if (env.DISABLE_AUTO_PROD_APP_SYNC) {
return { return {
message: message:

View File

@ -3,6 +3,10 @@ export enum PingSource {
APP = "app", APP = "app",
} }
export interface AnalyticsEnabledResponse {
enabled: boolean
}
export interface AnalyticsPingRequest { export interface AnalyticsPingRequest {
source: PingSource source: PingSource
timezone: string timezone: string

View File

@ -0,0 +1,10 @@
export type ApiKeyFetchResponse = Record<string, string>
export interface ApiKeyUpdateRequest {
value: string
}
export interface ApiKeyUpdateResponse {
_id: string
_rev: string
}

View File

@ -1,6 +1,10 @@
import type { PlanType } from "../../sdk" import type { PlanType } from "../../sdk"
import type { Layout, App, Screen } from "../../documents" import type { Layout, App, Screen } from "../../documents"
export interface SyncAppResponse {
message: string
}
export interface CreateAppRequest { export interface CreateAppRequest {
name: string name: string
url?: string url?: string
@ -12,6 +16,8 @@ export interface CreateAppRequest {
file?: { path: string } file?: { path: string }
} }
export interface CreateAppResponse extends App {}
export interface DuplicateAppRequest { export interface DuplicateAppRequest {
name: string name: string
url?: string url?: string
@ -37,6 +43,8 @@ export interface FetchAppPackageResponse {
hasLock: boolean hasLock: boolean
} }
export type FetchAppsResponse = App[]
export interface PublishResponse { export interface PublishResponse {
_id: string _id: string
status: string status: string
@ -45,3 +53,20 @@ export interface PublishResponse {
export interface UpdateAppRequest extends Partial<App> {} export interface UpdateAppRequest extends Partial<App> {}
export interface UpdateAppResponse extends App {} export interface UpdateAppResponse extends App {}
export interface UpdateAppClientResponse extends App {}
export interface RevertAppClientResponse extends App {}
export interface DestroyAppResponse {
ok: boolean
}
export interface ImportToUpdateAppRequest {
encryptionPassword?: string
}
export interface ImportToUpdateAppResponse {
message: string
}
export interface SetRevertableAppVersionRequest {
revertableVersion: string
}

View File

@ -0,0 +1,3 @@
import { DeploymentDoc } from "../../documents"
export interface PublishAppResponse extends DeploymentDoc {}

View File

@ -16,3 +16,5 @@ export * from "./layout"
export * from "./query" export * from "./query"
export * from "./role" export * from "./role"
export * from "./plugins" export * from "./plugins"
export * from "./apikeys"
export * from "./deployment"

View File

@ -0,0 +1,7 @@
export interface DeploymentDoc {
_id: string
verification: any
status?: string
err?: any
appUrl?: string
}

View File

@ -18,3 +18,4 @@ export * from "./sqlite"
export * from "./snippet" export * from "./snippet"
export * from "./rowAction" export * from "./rowAction"
export * from "./theme" export * from "./theme"
export * from "./deployment"

View File

@ -0,0 +1,5 @@
import { Document } from "../../"
export interface ApiKeyDoc extends Document {
apiKeys: Record<string, string>
}

View File

@ -7,3 +7,4 @@ export * from "./schedule"
export * from "./templates" export * from "./templates"
export * from "./environmentVariables" export * from "./environmentVariables"
export * from "./auditLogs" export * from "./auditLogs"
export * from "./apikeys"