import {z} from 'zod' export type CreateApiBusinessesIdArgs = {id: string, body: {name?: string | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, external_id?: (string | null) | undefined, website?: (string | null) | undefined, disabled_steps?: (Array<'email' | 'sms'> | null) | undefined}}; export type CreateApiBusinessesIdOptions = {baseUrl?: string | undefined, HttpBasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const business = z.object({id: z.string(), external_id: z.string().nullable(), name: z.string(), metadata: z.record(z.string(), z.unknown()), created_at: z.string(), website: z.string().nullable(), disabled_steps: z.array(z.enum(["email", "sms"])).nullable()}); 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 a business * * POST /businesses/{id} */ export const createApiBusinessesId = async (args: CreateApiBusinessesIdArgs, options?: CreateApiBusinessesIdOptions) => { const {baseUrl = "https://api.pocketflows.com", fetch: fetchFn = fetch, HttpBasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HttpBasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${HttpBasicAuth.username}:${HttpBasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/businesses/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(business, await res.json()) };