import {z} from 'zod' export type GetApiProfileArgs = {team_id?: string | undefined}; export type GetApiProfileOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const tierAddOnProfilePayload = z.object({name: z.string(), type: z.string(), period: z.string(), quantity: z.number().int().optional(), extra_information: z.object({})}); export const permissionResponse = z.object({name: z.string(), label: z.string().optional(), value: z.boolean().optional(), category: z.string().optional(), help_text: z.string().optional()}); export const userProfileResponse = z.object({id: z.string(), profile_picture: z.string().optional(), full_name: z.string(), email: z.string(), tier_id: z.string(), tier_name: z.string(), monthly_messages_used: z.number().int(), monthly_message_limit: z.number().int(), tier_note: z.string().optional(), date_created: z.string().optional(), tier_price_as_float: z.number().optional(), tier_type: z.string().optional(), tier_on_trial: z.boolean().optional(), trial_used: z.boolean().optional(), user_openai_api_key: z.string().optional(), add_ons: z.array(tierAddOnProfilePayload).optional(), has_created_chatbot: z.boolean().optional(), show_fifty_percent_discount: z.boolean().optional(), selected_team_id: z.string().optional(), permissions: z.array(permissionResponse).optional(), requires_2FA: z.boolean().optional(), stripe_customer_id: z.string().optional(), enabled_realtime_voice: z.boolean().optional(), applied_one_dollar_bf_starter: z.boolean().optional(), applied_25_percent_bf_discount: z.boolean().optional(), can_access_new_design: z.boolean().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 }; /** * Get Profile * * GET /profile * * Also accepts auth: OAuth2PasswordBearer */ export const getApiProfile = async (args: GetApiProfileArgs, options: GetApiProfileOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.team_id != null) query.append("team_id", String(args.team_id)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/profile${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(userProfileResponse, await res.json()) };