import {z} from 'zod' export type GetApiV2TransactionsCardPrimaryArgs = {cursor?: (string | null) | undefined, limit?: (number | null) | undefined, user_ids?: (Array | null) | undefined, posted_at_start?: (string | null) | undefined, 'expand[]'?: (Array | null) | undefined}; export type GetApiV2TransactionsCardPrimaryOptions = {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 cardTransactionType = z.enum(["PURCHASE", "REFUND", "CHARGEBACK", "REWARDS_CREDIT", "COLLECTION", "BNPL_FEE"]); export const merchant = z.object({raw_descriptor: z.string(), mcc: z.string(), country: z.string()}); export const cardTransaction = z.object({id: z.string(), card_id: z.string().nullable().optional(), description: z.string(), amount: money, initiated_at_date: z.string(), posted_at_date: z.string(), type: cardTransactionType.nullable().optional(), merchant: merchant.nullable().optional(), card_metadata: z.record(z.string(), z.string()).nullable().optional(), expense_id: z.string().nullable().optional(), card_transaction_operation_reference_id: z.string().nullable().optional()}); export const pageCardTransaction = z.object({next_cursor: z.string().nullable().optional(), items: z.array(cardTransaction)}); 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 all card accounts. * * * GET /v2/transactions/card/primary */ export const getApiV2TransactionsCardPrimary = async (args: GetApiV2TransactionsCardPrimaryArgs, options?: GetApiV2TransactionsCardPrimaryOptions) => { 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)) for (const item of args.user_ids ?? []) query.append("user_ids", String(item)) if (args.posted_at_start != null) query.append("posted_at_start", String(args.posted_at_start)) for (const item of args['expand[]'] ?? []) query.append("expand[]", String(item)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/transactions/card/primary${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(pageCardTransaction, await res.json()) };