Self API typing.

This commit is contained in:
mike12345567 2024-12-05 16:07:16 +00:00
parent 54c11e33c5
commit cdabe324ad
5 changed files with 36 additions and 8 deletions

View File

@ -7,3 +7,4 @@ export * from "./license"
export * from "./oldMigration" export * from "./oldMigration"
export * from "./email" export * from "./email"
export * from "./role" export * from "./role"
export * from "./self"

View File

@ -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<string, string>
}

View File

@ -0,0 +1,6 @@
import { Document } from "../document"
export interface DevInfo extends Document {
userId: string
apiKey?: string
}

View File

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

View File

@ -10,6 +10,12 @@ import {
import env from "../../../environment" import env from "../../../environment"
import { groups } from "@budibase/pro" import { groups } from "@budibase/pro"
import { import {
Ctx,
DevInfo,
FetchAPIKeyResponse,
GenerateAPIKeyRequest,
GenerateAPIKeyResponse,
GetGlobalSelfResponse,
UpdateSelfRequest, UpdateSelfRequest,
UpdateSelfResponse, UpdateSelfResponse,
User, User,
@ -35,22 +41,24 @@ function cleanupDevInfo(info: any) {
return info return info
} }
export async function generateAPIKey(ctx: any) { export async function generateAPIKey(
ctx: UserCtx<GenerateAPIKeyRequest, GenerateAPIKeyResponse>
) {
let userId let userId
let apiKey let apiKey
if (env.isTest() && ctx.request.body.userId) { if (env.isTest() && ctx.request.body.userId) {
userId = ctx.request.body.userId userId = ctx.request.body.userId
apiKey = newTestApiKey() apiKey = newTestApiKey()
} else { } else {
userId = ctx.user._id userId = ctx.user._id!
apiKey = newApiKey() apiKey = newApiKey()
} }
const db = tenancy.getGlobalDB() const db = tenancy.getGlobalDB()
const id = dbCore.generateDevInfoID(userId) const id = dbCore.generateDevInfoID(userId)
let devInfo let devInfo: DevInfo
try { try {
devInfo = await db.get<any>(id) devInfo = await db.get<DevInfo>(id)
} catch (err) { } catch (err) {
devInfo = { _id: id, userId } devInfo = { _id: id, userId }
} }
@ -59,9 +67,9 @@ export async function generateAPIKey(ctx: any) {
ctx.body = cleanupDevInfo(devInfo) ctx.body = cleanupDevInfo(devInfo)
} }
export async function fetchAPIKey(ctx: any) { export async function fetchAPIKey(ctx: UserCtx<void, FetchAPIKeyResponse>) {
const db = tenancy.getGlobalDB() const db = tenancy.getGlobalDB()
const id = dbCore.generateDevInfoID(ctx.user._id) const id = dbCore.generateDevInfoID(ctx.user._id!)
let devInfo let devInfo
try { try {
devInfo = await db.get(id) devInfo = await db.get(id)
@ -87,11 +95,11 @@ const addSessionAttributesToUser = (ctx: any) => {
ctx.body.csrfToken = ctx.user.csrfToken ctx.body.csrfToken = ctx.user.csrfToken
} }
export async function getSelf(ctx: any) { export async function getSelf(ctx: UserCtx<void, GetGlobalSelfResponse>) {
if (!ctx.user) { if (!ctx.user) {
ctx.throw(403, "User not logged in") ctx.throw(403, "User not logged in")
} }
const userId = ctx.user._id const userId = ctx.user._id!
ctx.params = { ctx.params = {
id: userId, id: userId,
} }