import {z} from 'zod' export type GetApiTransactionsArgs = {customer_id?: string | undefined, account_id?: string | undefined, payment_type?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, page?: number | undefined, count?: number | undefined, order_by?: string | undefined, transaction_state?: string | undefined}; export type GetApiTransactionsOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const aCH = z.object({card_id: z.string(), external_account_id: z.string(), class_code: z.string(), direction: z.string(), network: z.string(), description: z.string(), failure_reason: z.string().optional(), return_code: z.string().optional()}); export const card = z.object({card_id: z.string(), mcc: z.string().optional(), mcc_description: z.string(), currency: z.string().optional(), exchange_rate: z.number().optional(), merchant_id: z.string().optional(), merchant_name: z.string().optional(), merchant_city: z.string().optional(), merchant_state: z.string().optional(), merchant_country: z.string().optional(), merchant_postal_code: z.string().optional(), cardholder_presence: z.boolean().optional(), statement_descriptor: z.string(), arn: z.string().optional(), fraud_rule_triggered: z.string().optional(), merchant_currency: z.string().optional(), merchant_amount: z.string().optional()}); export const transactionSchema = z.object({transaction_id: z.string(), bond_brand_id: z.string(), customer_id: z.string(), account_id: z.string(), payment_type: z.string(), transaction_type: z.string().optional(), previous_transaction_id: z.string().optional(), state: z.string().optional(), amount: z.string(), currency: z.string().optional(), created_time: z.string(), updated_time: z.string(), details: z.union([aCH, card]).optional()}); export const paginatedTransactions = z.object({page: z.number().int().optional(), pages: z.number().int().optional(), count: z.number().int().optional(), next_page: z.number().int().optional(), transactions: z.array(transactionSchema)}); 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 }; /** * Get transactions * * GET /transactions */ export const getApiTransactions = async (args: GetApiTransactionsArgs, options?: GetApiTransactionsOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.payment_type != null) query.append("payment_type", String(args.payment_type)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.page != null) query.append("page", String(args.page)) if (args.count != null) query.append("count", String(args.count)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.transaction_state != null) query.append("transaction_state", String(args.transaction_state)) if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/transactions${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(paginatedTransactions, await res.json()) };