import {z} from 'zod' export type GetApiAccountsIdCustomersArgs = {id: string}; export type GetApiAccountsIdCustomersOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const page = z.object({nextPageKey: z.string().optional(), totalElements: z.number().int().optional()}); export const personName = z.object({first: z.string().optional(), middle: z.string().optional(), last: z.string().optional(), suffix: z.string().optional(), prefix: z.string().optional(), company: z.string().optional()}); export const customerType = z.enum(["BUSINESS", "INDIVIDUAL"]); export const accountAddress = z.object({line1: z.string(), line2: z.string().optional(), line3: z.string().optional(), city: z.string(), region: z.string(), postalCode: z.string(), country: z.string()}); export const businessCustomer = z.object({name: z.string(), registeredId: z.string().optional()}); export const customer = z.object({customerId: z.string(), name: personName, taxId: z.string().optional(), type: customerType, addresses: z.array(accountAddress).optional(), businessCustomer: businessCustomer.optional()}); export const customerListResponse = z.object({page: page.optional(), customers: z.array(customer)}); 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 }; /** * List account customers * * GET /accounts/{id}/customers */ export const getApiAccountsIdCustomers = async (args: GetApiAccountsIdCustomersArgs, options?: GetApiAccountsIdCustomersOptions) => { const {baseUrl = "https://api.givechariot.com/fdx/v6", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.id))}/customers${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(customerListResponse, await res.json()) };