import {z} from 'zod' export type UpdateAccountRequest = {country?: string | undefined, company_name?: string | undefined, address?: string | undefined, website?: string | undefined, phone_number?: string | undefined, onboarding_progress?: Record | undefined, region?: string | undefined, provider?: string | undefined, cloud?: boolean | undefined, hub?: boolean | undefined}; export type PatchApiAccountsArgs = {body: UpdateAccountRequest}; export type PatchApiAccountsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountPublicInfo = z.object({name: z.string().optional(), id: z.string().optional(), display_name: z.string().optional(), root_email: z.string().optional(), subscription_status: z.enum(["active", "pending", "cancelled"]).optional(), subscription_plan: z.string().optional(), emergency_credit: z.number().optional(), country: z.string().optional(), company_name: z.string().optional(), address: z.string().optional(), website: z.string().optional(), phone_number: z.string().optional(), scheduled_for_deletion: z.boolean().optional(), deletion_date: z.string().optional(), region: z.string().optional(), provider: z.string().optional(), bstier: z.number().int().optional(), cloud: z.boolean().optional(), hub: z.boolean().optional()}); export const patchApiAccountsResponse = z.object({data: accountPublicInfo}); 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 account * * PATCH /accounts * * Also accepts auth: apiKeyAuth */ export const patchApiAccounts = async (args: PatchApiAccountsArgs, options?: PatchApiAccountsOptions) => { const {baseUrl = "https://api.quiva.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(/\/+$/, '')}/accounts${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(patchApiAccountsResponse, await res.json()) };