Self API typing.
This commit is contained in:
parent
54c11e33c5
commit
cdabe324ad
|
@ -7,3 +7,4 @@ export * from "./license"
|
|||
export * from "./oldMigration"
|
||||
export * from "./email"
|
||||
export * from "./role"
|
||||
export * from "./self"
|
||||
|
|
|
@ -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>
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
import { Document } from "../document"
|
||||
|
||||
export interface DevInfo extends Document {
|
||||
userId: string
|
||||
apiKey?: string
|
||||
}
|
|
@ -8,3 +8,4 @@ export * from "./templates"
|
|||
export * from "./environmentVariables"
|
||||
export * from "./auditLogs"
|
||||
export * from "./apikeys"
|
||||
export * from "./devInfo"
|
||||
|
|
|
@ -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<GenerateAPIKeyRequest, GenerateAPIKeyResponse>
|
||||
) {
|
||||
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<any>(id)
|
||||
devInfo = await db.get<DevInfo>(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<void, FetchAPIKeyResponse>) {
|
||||
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<void, GetGlobalSelfResponse>) {
|
||||
if (!ctx.user) {
|
||||
ctx.throw(403, "User not logged in")
|
||||
}
|
||||
const userId = ctx.user._id
|
||||
const userId = ctx.user._id!
|
||||
ctx.params = {
|
||||
id: userId,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue