import {z} from 'zod' export type PayoutStatus = 'PENDING_AUTHORIZATION' | 'PROCESSING' | 'COMPLETED' | 'FAILED'; export type GetApiV1PayoutsArgs = {after?: string | undefined, limit?: unknown | undefined, payoutId?: string | undefined, beneficiaryId?: string | undefined, beneficiaryCurrency?: string | undefined, sourceCurrency?: string | undefined, status?: PayoutStatus | undefined, requestedAt?: string | undefined}; export type GetApiV1PayoutsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const feePaidBy = z.literal("MERCHANT"); export const payoutsAuthorize = z.object({payoutId: z.string(), beneficiaryAddressId: z.string(), beneficiaryId: z.string().optional(), sourceAccountId: z.string(), sourceQuantity: z.string(), beneficiaryQuantity: z.string(), payoutFeeQuantity: z.string(), netBeneficiaryQuantity: z.string(), sourceCurrency: z.string(), feeCurrency: z.string(), beneficiaryCurrency: z.string(), memo: z.string().nullable(), feePaidBy: feePaidBy, status: z.enum(["PENDING_AUTHORIZATION", "PROCESSING", "COMPLETED", "FAILED"]), payoutFeeRate: z.string(), clientReference: z.string().nullable().optional(), conversionRate: z.string(), paymentReason: z.string(), requestedAt: z.string(), expiresAt: z.string(), authorizedAt: z.string().optional()}); export const getApiV1PayoutsResponse = z.object({after: z.string().nullable().optional(), totalCount: z.string().optional(), items: z.array(payoutsAuthorize).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 }; /** * Payouts * * GET /v1/payouts */ export const getApiV1Payouts = async (args: GetApiV1PayoutsArgs, options?: GetApiV1PayoutsOptions) => { 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.payoutId != null) query.append("payoutId", String(args.payoutId)) if (args.beneficiaryId != null) query.append("beneficiaryId", String(args.beneficiaryId)) if (args.beneficiaryCurrency != null) query.append("beneficiaryCurrency", String(args.beneficiaryCurrency)) if (args.sourceCurrency != null) query.append("sourceCurrency", String(args.sourceCurrency)) if (args.status != null) query.append("status", String(args.status)) if (args.requestedAt != null) query.append("requestedAt", String(args.requestedAt)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/payouts${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(getApiV1PayoutsResponse, await res.json()) };