diff --git a/packages/types/src/api/web/global/index.ts b/packages/types/src/api/web/global/index.ts index e0c585a932..8359d8cd1a 100644 --- a/packages/types/src/api/web/global/index.ts +++ b/packages/types/src/api/web/global/index.ts @@ -7,3 +7,4 @@ export * from "./license" export * from "./oldMigration" export * from "./email" export * from "./role" +export * from "./self" diff --git a/packages/types/src/api/web/global/self.ts b/packages/types/src/api/web/global/self.ts new file mode 100644 index 0000000000..4ba51d2cd5 --- /dev/null +++ b/packages/types/src/api/web/global/self.ts @@ -0,0 +1,12 @@ +import { DevInfo, User } from "../../../documents" + +export interface GenerateAPIKeyRequest { + userId: string +} +export interface GenerateAPIKeyResponse extends DevInfo {} + +export interface FetchAPIKeyResponse extends DevInfo {} + +export interface GetGlobalSelfResponse extends User { + flags?: Record +} diff --git a/packages/types/src/documents/global/devInfo.ts b/packages/types/src/documents/global/devInfo.ts new file mode 100644 index 0000000000..f993fc5df6 --- /dev/null +++ b/packages/types/src/documents/global/devInfo.ts @@ -0,0 +1,6 @@ +import { Document } from "../document" + +export interface DevInfo extends Document { + userId: string + apiKey?: string +} diff --git a/packages/types/src/documents/global/index.ts b/packages/types/src/documents/global/index.ts index 7d2f5a767c..9bf24f078d 100644 --- a/packages/types/src/documents/global/index.ts +++ b/packages/types/src/documents/global/index.ts @@ -8,3 +8,4 @@ export * from "./templates" export * from "./environmentVariables" export * from "./auditLogs" export * from "./apikeys" +export * from "./devInfo" diff --git a/packages/worker/src/api/controllers/global/self.ts b/packages/worker/src/api/controllers/global/self.ts index 14a2ce8c73..7f2a139106 100644 --- a/packages/worker/src/api/controllers/global/self.ts +++ b/packages/worker/src/api/controllers/global/self.ts @@ -10,6 +10,12 @@ import { import env from "../../../environment" import { groups } from "@budibase/pro" import { + Ctx, + DevInfo, + FetchAPIKeyResponse, + GenerateAPIKeyRequest, + GenerateAPIKeyResponse, + GetGlobalSelfResponse, UpdateSelfRequest, UpdateSelfResponse, User, @@ -35,22 +41,24 @@ function cleanupDevInfo(info: any) { return info } -export async function generateAPIKey(ctx: any) { +export async function generateAPIKey( + ctx: UserCtx +) { let userId let apiKey if (env.isTest() && ctx.request.body.userId) { userId = ctx.request.body.userId apiKey = newTestApiKey() } else { - userId = ctx.user._id + userId = ctx.user._id! apiKey = newApiKey() } const db = tenancy.getGlobalDB() const id = dbCore.generateDevInfoID(userId) - let devInfo + let devInfo: DevInfo try { - devInfo = await db.get(id) + devInfo = await db.get(id) } catch (err) { devInfo = { _id: id, userId } } @@ -59,9 +67,9 @@ export async function generateAPIKey(ctx: any) { ctx.body = cleanupDevInfo(devInfo) } -export async function fetchAPIKey(ctx: any) { +export async function fetchAPIKey(ctx: UserCtx) { const db = tenancy.getGlobalDB() - const id = dbCore.generateDevInfoID(ctx.user._id) + const id = dbCore.generateDevInfoID(ctx.user._id!) let devInfo try { devInfo = await db.get(id) @@ -87,11 +95,11 @@ const addSessionAttributesToUser = (ctx: any) => { ctx.body.csrfToken = ctx.user.csrfToken } -export async function getSelf(ctx: any) { +export async function getSelf(ctx: UserCtx) { if (!ctx.user) { ctx.throw(403, "User not logged in") } - const userId = ctx.user._id + const userId = ctx.user._id! ctx.params = { id: userId, }