import {z} from 'zod' export type PatchApiV2UserArgs = {body: {firstName?: string | undefined, lastName?: string | undefined, meta?: {[key: string]: unknown} | undefined, photo?: string | undefined, language?: string | undefined}}; export type PatchApiV2UserOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const authData = z.object({id: z.string(), firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().optional(), language: z.unknown()}).and(z.record(z.string(), z.unknown())); export const supportedMFA = z.enum(["totp", "none", "*"]); export const user = z.object({email: z.string().optional(), status: z.enum(["pending", "validated", "deactivated"]).optional(), language: z.string().optional(), authData: z.object({anonymous: z.object({}).optional(), prismeai: z.object({}).optional(), azure: authData.optional()}).and(z.record(z.string(), z.unknown())).optional(), mfa: supportedMFA.optional(), meta: z.object({}).optional(), orgSlugs: z.array(z.string()).optional(), platformRole: z.string().optional(), groupAcls: z.record(z.string(), z.object({role: z.string().optional()})).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), updatedBy: z.string().optional(), firstName: z.string(), lastName: z.string().optional(), photo: z.string().optional(), groups: z.array(z.string()).optional(), membership: z.object({roleSlug: z.string().optional(), status: z.enum(["active", "invited", "suspended"]).optional(), joinedAt: z.string().optional(), joinedVia: z.enum(["direct", "invite-code", "invite-email", "auto-join", "join-rule"]).optional(), invitedAt: z.string().optional(), invitedBy: z.string().optional()}).optional(), id: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Patch My User * * PATCH /v2/user * * Also accepts auth: OrgApiKeyAuth, BearerAuth + OrgApiKeyAuth */ export const patchApiV2User = async (args: PatchApiV2UserArgs, options?: PatchApiV2UserOptions) => { const {baseUrl = "https://api.studio.prisme.ai", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/user${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(user, await res.json()) };