import {z} from 'zod' export type GetApiV1PaymentAccountsArgs = {'search.payment_account_id'?: Array | undefined, 'search.card_expiration_date_start'?: string | undefined, 'search.card_expiration_date_end'?: string | undefined, 'search.bank_account_name'?: Array | undefined, 'search.bank_account_type'?: (Array<'CHECKING' | 'SAVINGS'>) | undefined, 'search.bank_name'?: Array | undefined, 'search.method'?: (Array<'CC' | 'ACH'>) | undefined, 'search.card_name_first'?: Array | undefined, 'search.card_name_last'?: Array | undefined, 'search.token'?: Array | undefined, 'search.merchant_profile_id'?: Array | undefined, 'search.last_used_start'?: string | undefined, 'search.last_used_end'?: string | undefined, 'search.last_process_result'?: (Array<'NONE' | 'DECLINED' | 'APPROVED' | 'ERROR'>) | undefined, 'search.created_date_start'?: string | undefined, 'search.created_date_end'?: string | undefined, 'search.created_by'?: Array | undefined, 'search.last_4'?: Array | undefined, 'search.customer_id'?: Array | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1PaymentAccountsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revAppModelsPaymentAccountsBankModel = z.object({account_name: z.string().optional(), account_type: z.enum(["CHECKING", "SAVINGS"]).optional(), bank_name: z.string().optional(), routing_number: z.string().optional(), institution_number: z.string().optional(), transit_number: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), links: z.array(revAppComponentsLink).optional()}); export const revAppModelsPaymentAccountsCardModel = z.object({expiration_date: z.string().optional(), name_first: z.string().optional(), name_last: z.string().optional(), brand: z.enum(["AMEX", "DISCOVER", "DINERS", "CC", "VISA", "MASTERCARD"]).optional()}); export const revAppModelsPaymentAccountsPaymentAccountModel = z.object({payment_account_id: z.number().int().optional(), address_id: z.number().int().optional(), method: z.enum(["CC", "ACH"]).optional(), last_used: z.string().optional(), last_process_result: z.enum(["NONE", "DECLINED", "APPROVED", "ERROR"]).optional(), created_date: z.string().optional(), created_by: z.number().int().optional(), last_4: z.string().optional(), token: z.string().optional(), merchant_profile_id: z.number().int().optional(), bank_account: revAppModelsPaymentAccountsBankModel.optional(), credit_card: revAppModelsPaymentAccountsCardModel.optional(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsPaymentAccountsPaymentAccountModel = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsPaymentAccountsPaymentAccountModel).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 }; /** * Search payment accounts * * GET /v1/PaymentAccounts */ export const getApiV1PaymentAccounts = async (args: GetApiV1PaymentAccountsArgs, options?: GetApiV1PaymentAccountsOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['search.payment_account_id'] ?? []) query.append("search.payment_account_id", String(item)) if (args['search.card_expiration_date_start'] != null) query.append("search.card_expiration_date_start", String(args['search.card_expiration_date_start'])) if (args['search.card_expiration_date_end'] != null) query.append("search.card_expiration_date_end", String(args['search.card_expiration_date_end'])) for (const item of args['search.bank_account_name'] ?? []) query.append("search.bank_account_name", String(item)) for (const item of args['search.bank_account_type'] ?? []) query.append("search.bank_account_type", String(item)) for (const item of args['search.bank_name'] ?? []) query.append("search.bank_name", String(item)) for (const item of args['search.method'] ?? []) query.append("search.method", String(item)) for (const item of args['search.card_name_first'] ?? []) query.append("search.card_name_first", String(item)) for (const item of args['search.card_name_last'] ?? []) query.append("search.card_name_last", String(item)) for (const item of args['search.token'] ?? []) query.append("search.token", String(item)) for (const item of args['search.merchant_profile_id'] ?? []) query.append("search.merchant_profile_id", String(item)) if (args['search.last_used_start'] != null) query.append("search.last_used_start", String(args['search.last_used_start'])) if (args['search.last_used_end'] != null) query.append("search.last_used_end", String(args['search.last_used_end'])) for (const item of args['search.last_process_result'] ?? []) query.append("search.last_process_result", String(item)) if (args['search.created_date_start'] != null) query.append("search.created_date_start", String(args['search.created_date_start'])) if (args['search.created_date_end'] != null) query.append("search.created_date_end", String(args['search.created_date_end'])) for (const item of args['search.created_by'] ?? []) query.append("search.created_by", String(item)) for (const item of args['search.last_4'] ?? []) query.append("search.last_4", String(item)) for (const item of args['search.customer_id'] ?? []) query.append("search.customer_id", String(item)) if (args['search.page_size'] != null) query.append("search.page_size", String(args['search.page_size'])) if (args['search.page'] != null) query.append("search.page", String(args['search.page'])) if (args['search.sort'] != null) query.append("search.sort", String(args['search.sort'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/PaymentAccounts${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsPaymentAccountsPaymentAccountModel, await res.json()) };