import {z} from 'zod' export type GetApiCardsCardIdBalanceHistoryArgs = {cardId: string, page?: string | undefined, limit?: string | undefined, toDate?: string | undefined, fromDate?: string | undefined, type?: ('DEPOSIT' | 'WITHDRAWAL') | undefined, txnHash?: string | undefined, 'Accept-Version': string}; export type GetApiCardsCardIdBalanceHistoryOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCardsCardIdBalanceHistoryResponse = z.object({items: z.array(z.object({id: z.string(), date: z.string(), type: z.enum(["DEPOSIT", "WITHDRAWAL"]), source: z.enum(["EXTERNAL_CRYPTO_WALLET", "MASTER_ACCOUNT_BALANCE", "CARD_BALANCE"]), destination: z.enum(["EXTERNAL_CRYPTO_WALLET", "MASTER_ACCOUNT_BALANCE", "CARD_BALANCE"]), status: z.string(), amount: z.number(), currency: z.string(), cryptoMeta: z.object({currency: z.string(), amount: z.number(), network: z.string(), wallet: z.string(), service_fee: z.string(), service_fee_percentage: z.string()}).nullable().optional(), txnHash: z.string().nullable().optional(), exchangeRate: z.string().nullable().optional()})).optional(), meta: z.object({itemCount: z.number(), totalItems: z.number(), itemsPerPage: z.number(), totalPages: z.number(), currentPage: z.number()}).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 }; /** * Card Balance History * * GET /cards/{cardId}/balance-history */ export const getApiCardsCardIdBalanceHistory = async (args: GetApiCardsCardIdBalanceHistoryArgs, options?: GetApiCardsCardIdBalanceHistoryOptions) => { const {baseUrl = "https://sandbox.api.caas.reap.global", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.type != null) query.append("type", String(args.type)) if (args.txnHash != null) query.append("txnHash", String(args.txnHash)) if (args['Accept-Version'] != null) headers.set("Accept-Version", String(args['Accept-Version'])) if (ApiKeyAuth !== undefined) headers.set("x-reap-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/cards/${encodeURIComponent(String(args.cardId))}/balance-history${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(getApiCardsCardIdBalanceHistoryResponse, await res.json()) };