import {z} from 'zod' export type GetApiV3CollectionAccountArgs = {ending_before?: string | undefined, limit?: number | undefined, starting_after?: string | undefined, payment_method_type?: string | undefined, country?: string | undefined, currencies?: Array | undefined, on_behalf_of?: string | undefined, status?: ('disabled' | 'enabled') | undefined, type?: ('virtual_account' | 'wallet') | undefined, created?: ({gt?: number | undefined, gte?: number | undefined, lt?: number | undefined, lte?: number | undefined}) | undefined}; export type GetApiV3CollectionAccountOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const collectionAccountRequest = z.object({id: z.string().optional(), object: z.string().optional(), collection_account_id: z.string().optional(), type: z.enum(["enablement", "disablement", "reenablement"]).optional(), status: z.enum(["processing", "approval_hold", "requires_action", "succeeded", "failed", "cancelled"]).optional(), requested_currencies: z.array(z.string()).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), status_description: z.string().optional()}); export const collectionAccount = z.object({id: z.string().optional(), object: z.string().optional(), type: z.enum(["virtual_account", "wallet"]).optional(), description: z.string().optional(), on_behalf_of: z.string().optional(), country: z.string().optional(), currencies: z.array(z.string()).optional(), payment_method_type: z.string().optional(), virtual_account: z.object({account_holder_name: z.string().optional(), account_number: z.string().optional(), iban: z.string().optional(), bank_name: z.string().optional(), bank_branch: z.string().optional(), bank_address: z.object({address_line_1: z.string().optional(), address_line_2: z.string().optional(), city: z.string().optional(), country: z.string().optional()}).optional(), bank_codes: z.record(z.string(), z.string()).optional(), account_reenablement_supported: z.boolean().optional()}).optional(), wallet: z.object({type: z.string().optional(), deposit_address: z.string().optional()}).optional(), status: z.enum(["disabled", "enabled"]).optional(), requests: z.array(collectionAccountRequest).optional(), status_description: z.string().optional(), alias: z.string().optional(), balance_transaction: z.string().optional(), metadata: z.object({}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const getApiV3CollectionAccountResponse = z.object({object: z.string().optional(), has_more: z.boolean().optional(), data: z.array(collectionAccount).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 Collection Accounts * * GET /v3/collection_account */ export const getApiV3CollectionAccount = async (args: GetApiV3CollectionAccountArgs, options?: GetApiV3CollectionAccountOptions) => { const {baseUrl = "https://service-sandbox.tazapay.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.payment_method_type != null) query.append("payment_method_type", String(args.payment_method_type)) if (args.country != null) query.append("country", String(args.country)) for (const item of args.currencies ?? []) query.append("currencies", String(item)) if (args.on_behalf_of != null) query.append("on_behalf_of", String(args.on_behalf_of)) if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (args.created != null) query.append("created", JSON.stringify(args.created)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/collection_account${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(getApiV3CollectionAccountResponse, await res.json()) };