import {z} from 'zod' export type GetApiInvoiceAccountNumberArgs = {account_id?: string | undefined, active?: boolean | undefined, tags?: Array | undefined, page_cursor?: string | undefined, page_size?: number | undefined, from_date?: string | undefined, to_date?: string | undefined}; export type GetApiInvoiceAccountNumberOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiInvoiceAccountNumberResponse = z.object({data: z.array(z.object({account_id: z.string().optional(), account_number: z.string().optional(), active: z.boolean().optional(), bank_id: z.string().optional(), id: z.string().optional(), org_id: z.string().optional(), tags: z.array(z.string()).optional(), userdata: z.object({}).optional()})).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 Account Numbers * * GET /invoice_account_number * * Also accepts auth: apiKey */ export const getApiInvoiceAccountNumber = async (args: GetApiInvoiceAccountNumberArgs, options?: GetApiInvoiceAccountNumberOptions) => { const {baseUrl = "https://api.treasuryprime.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.active != null) query.append("active", String(args.active)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoice_account_number${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(getApiInvoiceAccountNumberResponse, await res.json()) };