import {z} from 'zod' export type PatchApiV1AccountArgs = {body: {email: string}}; export type PatchApiV1AccountOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiV1AccountResponse = z.object({id: z.string(), email: z.string(), email_verified: z.boolean(), plan: z.enum(["free", "paid"]), credit_balance: z.number(), api_keys: z.array(z.object({id: z.string(), label: z.string(), prefix: z.string(), rate_limit: z.number(), is_active: z.boolean(), created_at: z.string()})), created_at: 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 }; /** * Update your account email address. Sets email_verified to false and sends a verification email to the new address. You must re-verify before using credit-consuming endpoints. * * PATCH /v1/account/ * * Also accepts auth: bearerAuth */ export const patchApiV1Account = async (args: PatchApiV1AccountArgs, options: PatchApiV1AccountOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/account/${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(patchApiV1AccountResponse, await res.json()) };