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 { AnalyticsPingRequest, App, PingSource } from "@budibase/types"
import {
AnalyticsPingRequest,
App,
PingSource,
Ctx,
AnalyticsEnabledResponse,
} from "@budibase/types"
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()
ctx.body = {
enabled,
}
}
export const ping = async (ctx: any) => {
const body = ctx.request.body as AnalyticsPingRequest
export const ping = async (ctx: Ctx<AnalyticsPingRequest, void>) => {
const body = ctx.request.body
switch (body.source) {
case PingSource.APP: {

View File

@ -1,18 +1,25 @@
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
async function getBuilderMainDoc() {
const db = tenancy.getGlobalDB()
try {
return await db.get<any>(KEYS_DOC)
} catch (err) {
// doesn't exist yet, nothing to get
const doc = await db.tryGet<ApiKeyDoc>(KEYS_DOC)
if (!doc) {
return {
_id: KEYS_DOC,
apiKeys: {},
}
}
return doc
}
async function setBuilderMainDoc(doc: Document) {
@ -22,7 +29,7 @@ async function setBuilderMainDoc(doc: Document) {
return db.put(doc)
}
export async function fetch(ctx: BBContext) {
export async function fetch(ctx: UserCtx<void, ApiKeyFetchResponse>) {
try {
const mainDoc = await getBuilderMainDoc()
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 value = ctx.request.body.value

View File

@ -59,6 +59,16 @@ import {
BBReferenceFieldSubType,
Row,
BBRequest,
SyncAppResponse,
CreateAppResponse,
FetchAppsResponse,
UpdateAppClientResponse,
RevertAppClientResponse,
DestroyAppResponse,
ImportToUpdateAppRequest,
ImportToUpdateAppResponse,
SetRevertableAppVersionRequest,
SetRevertableAppVersionResponse,
} from "@budibase/types"
import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts"
import sdk from "../../sdk"
@ -166,7 +176,7 @@ async function createInstance(appId: string, template: AppTemplate) {
return { _id: appId }
}
export const addSampleData = async (ctx: UserCtx) => {
export const addSampleData = async (ctx: UserCtx<void, void>) => {
const db = context.getAppDB()
try {
@ -182,7 +192,7 @@ export const addSampleData = async (ctx: UserCtx) => {
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.query.status as AppStatus,
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 { body } = ctx.request
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))
await appPostCreate(ctx, newApplication)
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
const application = await sdk.applications.metadata.get()
const currentVersion = application.version
@ -581,7 +597,9 @@ export async function updateClient(ctx: UserCtx) {
ctx.body = app
}
export async function revertClient(ctx: UserCtx) {
export async function revertClient(
ctx: UserCtx<void, RevertAppClientResponse>
) {
// Check app can be reverted
const application = await sdk.applications.metadata.get()
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)
const result = await destroyApp(ctx)
await postDestroyApp(ctx)
@ -676,7 +694,7 @@ export async function destroy(ctx: UserCtx) {
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 dbExists = await dbCore.dbExists(prodAppId)
@ -692,7 +710,7 @@ export async function unpublish(ctx: UserCtx) {
builderSocket?.emitAppUnpublish(ctx)
}
export async function sync(ctx: UserCtx) {
export async function sync(ctx: UserCtx<void, SyncAppResponse>) {
const appId = ctx.params.appId
try {
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 appExport = ctx.request.files?.appExport
const password = ctx.request.body.encryptionPassword as string
const password = ctx.request.body.encryptionPassword
if (!appExport) {
ctx.throw(400, "Must supply app export to import")
}
@ -811,7 +831,7 @@ export async function updateAppPackage(
}
export async function setRevertableVersion(
ctx: UserCtx<{ revertableVersion: string }, App>
ctx: UserCtx<SetRevertableAppVersionRequest, void>
) {
if (!env.isDev()) {
ctx.status = 403

View File

@ -7,7 +7,12 @@ import {
enableCronTrigger,
} from "../../../automations/utils"
import { backups } from "@budibase/pro"
import { App, AppBackupTrigger } from "@budibase/types"
import {
App,
AppBackupTrigger,
PublishAppResponse,
UserCtx,
} from "@budibase/types"
import sdk from "../../../sdk"
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()
console.log("Deployment object created")
deployment.setStatus(DeploymentStatus.PENDING)

View File

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

View File

@ -3,6 +3,10 @@ export enum PingSource {
APP = "app",
}
export interface AnalyticsEnabledResponse {
enabled: boolean
}
export interface AnalyticsPingRequest {
source: PingSource
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 { Layout, App, Screen } from "../../documents"
export interface SyncAppResponse {
message: string
}
export interface CreateAppRequest {
name: string
url?: string
@ -12,6 +16,8 @@ export interface CreateAppRequest {
file?: { path: string }
}
export interface CreateAppResponse extends App {}
export interface DuplicateAppRequest {
name: string
url?: string
@ -37,6 +43,8 @@ export interface FetchAppPackageResponse {
hasLock: boolean
}
export type FetchAppsResponse = App[]
export interface PublishResponse {
_id: string
status: string
@ -45,3 +53,20 @@ export interface PublishResponse {
export interface UpdateAppRequest extends Partial<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 "./role"
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 "./rowAction"
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 "./environmentVariables"
export * from "./auditLogs"
export * from "./apikeys"