User metadata API typing.

This commit is contained in:
mike12345567 2024-12-04 15:00:05 +00:00
parent 29d912e24e
commit a7a2a56044
2 changed files with 33 additions and 10 deletions

View File

@ -7,19 +7,24 @@ import {
FetchUserMetadataResponse, FetchUserMetadataResponse,
FindUserMetadataResponse, FindUserMetadataResponse,
Flags, Flags,
SetFlagRequest, SetUserFlagRequest,
UpdateSelfMetadataRequest,
UpdateSelfMetadataResponse,
UpdateUserMetadataResponse,
UpdateUserMetadataRequest,
UserCtx, UserCtx,
UserMetadata, DeleteUserMetadataResponse,
SetUserFlagResponse,
GetUserFlagsResponse,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../sdk" import sdk from "../../sdk"
import { DocumentInsertResponse } from "@budibase/nano"
export async function fetchMetadata(ctx: Ctx<void, FetchUserMetadataResponse>) { export async function fetchMetadata(ctx: Ctx<void, FetchUserMetadataResponse>) {
ctx.body = await sdk.users.fetchMetadata() ctx.body = await sdk.users.fetchMetadata()
} }
export async function updateSelfMetadata( export async function updateSelfMetadata(
ctx: UserCtx<UserMetadata, DocumentInsertResponse> ctx: UserCtx<UpdateSelfMetadataRequest, UpdateSelfMetadataResponse>
) { ) {
// overwrite the ID with current users // overwrite the ID with current users
ctx.request.body._id = ctx.user?._id ctx.request.body._id = ctx.user?._id
@ -31,7 +36,7 @@ export async function updateSelfMetadata(
} }
export async function updateMetadata( export async function updateMetadata(
ctx: UserCtx<UserMetadata, DocumentInsertResponse> ctx: UserCtx<UpdateUserMetadataRequest, UpdateUserMetadataResponse>
) { ) {
const db = context.getAppDB() const db = context.getAppDB()
const user = ctx.request.body const user = ctx.request.body
@ -44,7 +49,9 @@ export async function updateMetadata(
ctx.body = await db.put(metadata) ctx.body = await db.put(metadata)
} }
export async function destroyMetadata(ctx: UserCtx<void, { message: string }>) { export async function destroyMetadata(
ctx: UserCtx<void, DeleteUserMetadataResponse>
) {
const db = context.getAppDB() const db = context.getAppDB()
try { try {
const dbUser = await sdk.users.get(ctx.params.id) const dbUser = await sdk.users.get(ctx.params.id)
@ -64,7 +71,7 @@ export async function findMetadata(
} }
export async function setFlag( export async function setFlag(
ctx: UserCtx<SetFlagRequest, { message: string }> ctx: UserCtx<SetUserFlagRequest, SetUserFlagResponse>
) { ) {
const userId = ctx.user?._id const userId = ctx.user?._id
const { flag, value } = ctx.request.body const { flag, value } = ctx.request.body
@ -84,7 +91,7 @@ export async function setFlag(
ctx.body = { message: "Flag set successfully" } ctx.body = { message: "Flag set successfully" }
} }
export async function getFlags(ctx: UserCtx<void, Flags>) { export async function getFlags(ctx: UserCtx<void, GetUserFlagsResponse>) {
const userId = ctx.user?._id const userId = ctx.user?._id
const docId = generateUserFlagID(userId!) const docId = generateUserFlagID(userId!)
const db = context.getAppDB() const db = context.getAppDB()

View File

@ -1,11 +1,27 @@
import { ContextUserMetadata } from "../../../" import { ContextUserMetadata, Flags, UserMetadata } from "../../../"
import { DocumentInsertResponse } from "@budibase/nano"
export type FetchUserMetadataResponse = ContextUserMetadata[] export type FetchUserMetadataResponse = ContextUserMetadata[]
export type FindUserMetadataResponse = ContextUserMetadata export type FindUserMetadataResponse = ContextUserMetadata
export interface SetFlagRequest { export interface SetUserFlagRequest {
flag: string flag: string
value: any value: any
} }
export interface SetUserFlagResponse {
message: string
}
export interface GetUserFlagsResponse extends Flags {}
export type AppSelfResponse = ContextUserMetadata | {} export type AppSelfResponse = ContextUserMetadata | {}
export interface UpdateSelfMetadataRequest extends UserMetadata {}
export interface UpdateSelfMetadataResponse extends DocumentInsertResponse {}
export interface UpdateUserMetadataRequest extends UserMetadata {}
export interface UpdateUserMetadataResponse extends DocumentInsertResponse {}
export interface DeleteUserMetadataResponse {
message: string
}