import {z} from 'zod' export type GetApiApiV2ChannelPaymentArgs = {walletId: string, status?: ('DETECTED' | 'COMPLETE' | 'HELD' | 'CANCELLED') | undefined, fromDate?: string | undefined, toDate?: string | undefined, offset?: string | undefined, max?: string | undefined, q?: string | undefined}; export type GetApiApiV2ChannelPaymentOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangeRateDto = z.object({base: z.string().optional(), counter: z.string().optional(), rate: z.number().optional()}); export const networkFeeDto = z.object({paidCurrency: z.string().optional(), paidAmount: z.number().optional(), displayCurrency: z.string().optional(), displayAmount: z.number().optional()}); export const merchantChannelPaymentDto = z.object({channelId: z.string().optional(), merchantId: z.string().optional(), merchantDisplayName: z.string().optional(), reference: z.string().optional(), dateCreated: z.number().int().optional(), lastUpdated: z.number().int().optional(), status: z.enum(["DETECTED", "COMPLETE", "HELD", "CANCELLED"]).optional(), uuid: z.string().optional(), hash: z.string().optional(), address: z.string().optional(), tag: z.string().optional(), paidCurrency: z.string().optional(), displayCurrency: z.string().optional(), walletCurrency: z.string().optional(), feeCurrency: z.string().optional(), paidAmount: z.number().optional(), displayAmount: z.number().optional(), walletAmount: z.number().optional(), feeAmount: z.number().optional(), exchangeRate: exchangeRateDto.optional(), displayRate: exchangeRateDto.optional(), risk: z.object({level: z.string().optional(), resourceName: z.string().optional(), resourceCategory: z.string().optional(), alerts: z.array(z.unknown()).optional()}).optional(), sources: z.array(z.string()).optional(), networkFee: networkFeeDto.optional()}); export const getApiApiV2ChannelPaymentResponse = z.array(merchantChannelPaymentDto); 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 channel payments * * GET /api/v2/channel/payment */ export const getApiApiV2ChannelPayment = async (args: GetApiApiV2ChannelPaymentArgs, options?: GetApiApiV2ChannelPaymentOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.walletId != null) query.append("walletId", String(args.walletId)) if (args.status != null) query.append("status", String(args.status)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.max != null) query.append("max", String(args.max)) if (args.q != null) query.append("q", String(args.q)) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/channel/payment${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(getApiApiV2ChannelPaymentResponse, await res.json()) };