import {z} from 'zod' export type GetApiChargesArgs = {limit?: number | undefined, page?: string | undefined, search?: string | undefined, order?: string | undefined, starting_at?: string | undefined, ending_at?: string | undefined}; export type GetApiChargesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const charge = z.object({id: z.string().optional(), amount: z.number().optional(), customer_id: z.string().optional(), amount_refunded: z.number().optional(), canceled: z.boolean().optional(), canceled_at: z.string().optional(), captured: z.boolean().optional(), captured_at: z.string().optional(), created_at: z.string().optional(), invoice_url: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional(), number: z.string().optional(), terms: z.enum(["due_upon_receipt", "net7", "net10", "net10th", "net15", "net20", "net30", "net45", "net60", "net75", "net90", "net120", "net180"]).optional(), updated_at: z.string().optional(), merchant_invoice_id: z.string().optional(), order_number: z.string().optional(), po_number: z.string().optional(), fee: z.number().optional(), fee_refunded: z.number().optional()}); export const chargeListResponse = z.object({count: z.number().int().optional(), limit: z.number().int().optional(), page: z.number().int().optional(), results: z.array(charge).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 charges * * GET /charges * * Also accepts auth: basicAuth */ export const getApiCharges = async (args: GetApiChargesArgs, options?: GetApiChargesOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.search != null) query.append("search", String(args.search)) if (args.order != null) query.append("order", String(args.order)) if (args.starting_at != null) query.append("starting_at", String(args.starting_at)) if (args.ending_at != null) query.append("ending_at", String(args.ending_at)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/charges${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(chargeListResponse, await res.json()) };