import {z} from 'zod' export type GetApiV2TransactionsCashIdArgs = {id: string, cursor?: (string | null) | undefined, limit?: (number | null) | undefined, posted_at_start?: (string | null) | undefined}; export type GetApiV2TransactionsCashIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const money = z.object({amount: z.number().int(), currency: z.string().nullable().optional()}); export const cashTransactionType = z.enum(["PAYMENT", "DIVIDEND", "FEE", "ADJUSTMENT", "INTEREST", "CARD_COLLECTION", "REWARDS_REDEMPTION", "RECEIVABLES_OFFERS_ADVANCE", "FBO_TRANSFER", "RECEIVABLES_OFFERS_REPAYMENT", "RECEIVABLES_OFFERS_COLLECTION", "BREX_OPERATIONAL_TRANSFER", "INTRA_CUSTOMER_ACCOUNT_BOOK_TRANSFER", "BOOK_TRANSFER", "CRYPTO_BRIDGE", "STABLECOIN", "TRANSACTION_FEES_COLLECTION", "PAYBACK"]); export const cashTransaction = z.object({id: z.string(), description: z.string(), amount: money.nullable().optional(), initiated_at_date: z.string(), posted_at_date: z.string(), type: cashTransactionType.nullable().optional(), transfer_id: z.string().nullable().optional()}); export const pageCashTransaction = z.object({next_cursor: z.string().nullable().optional(), items: z.array(cashTransaction)}); 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 transactions for the selected cash account. * * * GET /v2/transactions/cash/{id} */ export const getApiV2TransactionsCashId = async (args: GetApiV2TransactionsCashIdArgs, options?: GetApiV2TransactionsCashIdOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.posted_at_start != null) query.append("posted_at_start", String(args.posted_at_start)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/transactions/cash/${encodeURIComponent(String(args.id))}${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(pageCashTransaction, await res.json()) };