import {z} from 'zod' export type GetApiMeOptions = {baseUrl: string, backendAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMeResponse = z.object({data: z.object({email: z.string().optional(), id: z.string(), is_light_user: z.boolean(), light_pfp_enabled: z.boolean(), light_pfp_profile_display_name: z.string().optional(), max_consumption_percentage: z.number().int().gte(1).lte(100).nullable().optional(), name: z.string(), paid_mtcsp_enabled: z.boolean(), resource: z.number(), role: z.enum(["admin", "user"]), tenant: z.object({case_studio_enabled: z.boolean(), claude_code_enabled: z.boolean(), codex_enabled: z.boolean(), disable_contact_form: z.boolean(), id: z.string(), is_zero_token: z.boolean(), light_pfp_enabled: z.boolean(), mtcsp_config: z.object({id: z.string(), max_paid_users: z.number().int(), notebook_units: z.number().int(), token_rate: z.number().int()}).optional(), show_upgrade_options: z.boolean(), token_rate: z.number().int()}), tenant_id: z.string()})}); 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 }; /** * Get logged in user information * * GET /me */ export const getApiMe = async (options: GetApiMeOptions) => { const {baseUrl, fetch: fetchFn = fetch, backendAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (backendAuth !== undefined) cookies.push("mtls-prd=" + encodeURIComponent(backendAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/me${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiMeResponse, await res.json()) };