import {z} from 'zod' export type UpdateProfileRequest = {display_name?: (string | null) | undefined, avatar_url?: (string | null) | undefined}; export type UpdateApiUsersMeArgs = {body: UpdateProfileRequest}; export type UpdateApiUsersMeOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const currentOrg = z.object({id: z.string(), name: z.string(), claimed_domain: z.string().nullable().optional(), external_sharing_enabled: z.boolean(), viewer_role: z.enum(["owner", "admin", "member"]), is_personal: z.boolean()}); export const userProfileResponse = z.object({user_id: z.string(), display_name: z.string(), avatar_url: z.string().nullable(), email: z.string().nullable(), needs_onboarding: z.boolean().optional(), current_org: currentOrg.nullable().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 }; /** * Update My Profile * * PUT /users/me */ export const updateApiUsersMe = async (args: UpdateApiUsersMeArgs, options: UpdateApiUsersMeOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/me${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(userProfileResponse, await res.json()) };