import {z} from 'zod' export type ApiWrapperOrderBy = 'createdAt' | 'updatedAt'; export type GetApiPaymentsArgs = {status?: string | undefined, walletId?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, amountGreaterThan?: string | undefined, amountLessThan?: string | undefined, senderBusinessId?: string | undefined, recipientBusinessId?: string | undefined, businessId?: string | undefined, description?: string | undefined, orderBy?: ApiWrapperOrderBy | undefined, order?: ('asc' | 'desc') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiPaymentsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentStatusEnum = z.enum(["queued", "requested", "initiated", "lockbox", "released", "cancelled", "declined", "completed", "failed", "burned", "minted", "transferred", "confirmed", "reverted"]); export const payment = z.object({id: z.string(), sender: z.string().optional(), senderBusinessId: z.string().optional(), senderBusinessName: z.string().optional(), senderWalletAddress: z.string().optional(), recipient: z.string().optional(), recipientBusinessId: z.string().optional(), recipientBusinessName: z.string().optional(), recipientWalletAddress: z.string().optional(), amount: z.union([z.string(), z.number()]), status: paymentStatusEnum.nullable(), description: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), totalCount: z.number().optional(), rowNum: z.number().optional()}); export const errorResponse = z.object({error: z.string()}); export const getApiPaymentsResponse = z.union([z.array(payment), errorResponse]); 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 Payments * * GET /payments */ export const getApiPayments = async (args: GetApiPaymentsArgs, options: GetApiPaymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.walletId != null) query.append("walletId", String(args.walletId)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.amountGreaterThan != null) query.append("amountGreaterThan", String(args.amountGreaterThan)) if (args.amountLessThan != null) query.append("amountLessThan", String(args.amountLessThan)) if (args.senderBusinessId != null) query.append("senderBusinessId", String(args.senderBusinessId)) if (args.recipientBusinessId != null) query.append("recipientBusinessId", String(args.recipientBusinessId)) if (args.businessId != null) query.append("businessId", String(args.businessId)) if (args.description != null) query.append("description", String(args.description)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments${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(getApiPaymentsResponse, await res.json()) };