import {z} from 'zod' export type GetApiAccountCollateralTopUpsArgs = {page?: number | undefined, limit?: number | undefined, status?: (Array<'created' | 'paid' | 'completed' | 'failed' | 'pending_review'>) | undefined, repaymentMethod?: (Array<'manual' | 'bank_transfer' | 'direct_debit' | 'crypto' | 'van_transfer' | 'crypto_top_up' | 'crypto_triple_a' | 'fiat_collateral'>) | undefined, fromDate?: string | undefined, toDate?: string | undefined, 'Accept-Version': string}; export type GetApiAccountCollateralTopUpsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAccountCollateralTopUpsResponse = z.object({items: z.array(z.object({id: z.string(), budgetId: z.string(), status: z.enum(["created", "paid", "completed", "failed", "pending_review"]), repaymentMethod: z.enum(["manual", "bank_transfer", "direct_debit", "crypto", "van_transfer", "crypto_top_up", "crypto_triple_a", "fiat_collateral"]), referenceId: z.string(), creditedAmount: z.string(), creditedCurrency: z.string(), grossPaymentAmount: z.string().nullable(), fees: z.object({processingFee: z.string().nullable(), processingFeePercentage: z.string().nullable()}).nullable(), assetType: z.string().nullable(), assets: z.array(z.object({asset: z.string(), amount: z.string(), exchangeRate: z.string().nullable()})), transactionHash: z.string().nullable(), instructionIndex: z.number().int().nullable(), fiat: z.object({currency: z.string().nullable(), amount: z.string().nullable(), senderName: z.string().nullable(), receivedAt: z.string().nullable()}).nullable(), providerReferenceId: z.string().nullable(), failedReason: z.string().nullable(), proofId: z.string().nullable(), createdAt: z.string(), paidDate: z.string().nullable(), verifiedDate: z.string().nullable()})), meta: z.object({itemCount: z.number().int(), totalItems: z.number().int(), itemsPerPage: z.number().int(), totalPages: z.number().int(), currentPage: z.number().int()})}); 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 collateral top-ups * * GET /account/collateral-top-ups */ export const getApiAccountCollateralTopUps = async (args: GetApiAccountCollateralTopUpsArgs, options?: GetApiAccountCollateralTopUpsOptions) => { 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)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.repaymentMethod ?? []) query.append("repaymentMethod", String(item)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) 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(/\/+$/, '')}/account/collateral-top-ups${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(getApiAccountCollateralTopUpsResponse, await res.json()) };