import {z} from 'zod' export type GetApiPayablesPayerAccountsArgs = {Authorization: string, after_cursor?: string | undefined, before_cursor?: string | undefined, limit?: number | undefined, status?: ('pending' | 'active' | 'disabled' | 'archived') | undefined, business?: string | undefined, name?: string | undefined, payer_id?: string | undefined}; export type GetApiPayablesPayerAccountsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPayablesPayerAccountsResponse = z.object({id: z.unknown(), type: z.string(), data: z.array(z.object({id: z.string().optional(), type: z.string().optional(), business_id: z.string().optional(), name: z.string().optional(), mode: z.enum(["test", "live"]).optional(), status: z.enum(["pending", "active", "disabled", "archived"]).optional(), provisioned_at: z.string().nullable().optional(), funding_bank_account_id: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()})), page_info: z.object({start_cursor: z.string().optional(), end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: 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 }; /** * List Payer Accounts * * GET /payables/payer_accounts */ export const getApiPayablesPayerAccounts = async (args: GetApiPayablesPayerAccountsArgs, options?: GetApiPayablesPayerAccountsOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.after_cursor != null) query.append("after_cursor", String(args.after_cursor)) if (args.before_cursor != null) query.append("before_cursor", String(args.before_cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.status != null) query.append("status", String(args.status)) if (args.business != null) query.append("business", String(args.business)) if (args.name != null) query.append("name", String(args.name)) if (args.payer_id != null) query.append("payer_id", String(args.payer_id)) const url = `${baseUrl.replace(/\/+$/, '')}/payables/payer_accounts${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(getApiPayablesPayerAccountsResponse, await res.json()) };