import {z} from 'zod' export type FundingRequestStatus = 'PENDING_ADDRESS' | 'ACTIVE' | 'FAILED' | 'EXPIRED' | 'REMOVED'; export type SortDirection = 'ASC' | 'DESC'; export type GetApiV1FundingRequestArgs = {after?: string | undefined, limit?: unknown | undefined, fundingRequestId?: string | undefined, customerAccountId?: string | undefined, beneficiaryAccountId?: string | undefined, beneficiaryCurrency?: ('AED' | 'USD') | undefined, fundingCurrency?: ('USDC' | 'USDT') | undefined, network?: string | undefined, status?: FundingRequestStatus | undefined, createdAtStart?: string | undefined, createdAtEnd?: string | undefined, expiresAtStart?: string | undefined, expiresAtEnd?: string | undefined, customerReference?: string | undefined, sortField?: ('status' | 'createdAt') | undefined, sortDirection?: SortDirection | undefined}; export type GetApiV1FundingRequestOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const feePaidBy = z.literal("MERCHANT"); export const fundingInstructions = z.object({address: z.string(), fundingCurrency: z.string(), network: z.string()}); export const fundingRequestStatus = z.enum(["PENDING_ADDRESS", "ACTIVE", "FAILED", "EXPIRED", "REMOVED"]); export const fundingRequest = z.object({customerReference: z.string().max(256).nullable().optional(), customerAccountId: z.string(), feePaidBy: feePaidBy, fundingRequestId: z.string(), fundingInstructions: fundingInstructions, status: fundingRequestStatus, conversionRate: z.string(), beneficiaryAccountId: z.string(), beneficiaryCurrency: z.string(), expiresAt: z.string().nullable().optional(), createdAt: z.string(), depositIds: z.array(z.string())}); export const getApiV1FundingRequestResponse = z.object({after: z.string().nullable().optional(), totalCount: z.string().optional(), items: z.array(fundingRequest).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 }; /** * List Funding Requests * * GET /v1/funding-request */ export const getApiV1FundingRequest = async (args: GetApiV1FundingRequestArgs, options?: GetApiV1FundingRequestOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.fundingRequestId != null) query.append("fundingRequestId", String(args.fundingRequestId)) if (args.customerAccountId != null) query.append("customerAccountId", String(args.customerAccountId)) if (args.beneficiaryAccountId != null) query.append("beneficiaryAccountId", String(args.beneficiaryAccountId)) if (args.beneficiaryCurrency != null) query.append("beneficiaryCurrency", String(args.beneficiaryCurrency)) if (args.fundingCurrency != null) query.append("fundingCurrency", String(args.fundingCurrency)) if (args.network != null) query.append("network", String(args.network)) if (args.status != null) query.append("status", String(args.status)) if (args.createdAtStart != null) query.append("createdAtStart", String(args.createdAtStart)) if (args.createdAtEnd != null) query.append("createdAtEnd", String(args.createdAtEnd)) if (args.expiresAtStart != null) query.append("expiresAtStart", String(args.expiresAtStart)) if (args.expiresAtEnd != null) query.append("expiresAtEnd", String(args.expiresAtEnd)) if (args.customerReference != null) query.append("customerReference", String(args.customerReference)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/funding-request${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(getApiV1FundingRequestResponse, await res.json()) };