import {z} from 'zod' export type GetApiChargesArgs = {code?: string | undefined, status?: string | undefined, payment_method?: string | undefined, customer_id?: string | undefined, order_id?: string | undefined, created_since?: string | undefined, created_until?: string | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiChargesOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiChargesResponse = z.object({data: z.array(z.object({id: z.string().optional(), code: z.string().optional(), amount: z.number().int().optional(), paid_amount: z.number().int().optional(), status: z.string().optional(), currency: z.string().optional(), payment_method: z.string().optional(), paid_at: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), customer: z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), delinquent: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), phones: z.object({}).optional()}).optional(), last_transaction: z.object({id: z.string().optional(), transaction_type: z.string().optional(), funding_source: z.string().optional(), gateway_id: z.string().optional(), amount: z.number().int().optional(), status: z.string().optional(), success: z.boolean().optional(), installments: z.number().int().optional(), statement_descriptor: z.string().optional(), acquirer_tid: z.string().optional(), acquirer_nsu: z.string().optional(), acquirer_auth_code: z.string().optional(), acquirer_message: z.string().optional(), acquirer_return_code: z.string().optional(), operation_type: z.string().optional(), card: z.object({id: z.string().optional(), first_six_digits: z.string().optional(), last_four_digits: z.string().optional(), brand: z.string().optional(), holder_name: z.string().optional(), exp_month: z.number().int().optional(), exp_year: z.number().int().optional(), status: z.string().optional(), type: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), billing_address: z.object({zip_code: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), line_1: z.string().optional()}).optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), gateway_response: z.object({code: z.string().optional(), errors: z.array(z.object({})).optional()}).optional()}).optional(), metadata: z.object({code: z.string().optional()}).optional()})).optional(), paging: z.object({total: z.number().int().optional(), next: z.string().optional()}).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 }; /** * Listar cobranças * * GET /charges/ */ export const getApiCharges = async (args: GetApiChargesArgs, options?: GetApiChargesOptions) => { const {baseUrl = "https://api.mundipagg.com/core/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.code != null) query.append("code", String(args.code)) if (args.status != null) query.append("status", String(args.status)) if (args.payment_method != null) query.append("payment_method", String(args.payment_method)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.order_id != null) query.append("order_id", String(args.order_id)) if (args.created_since != null) query.append("created_since", String(args.created_since)) if (args.created_until != null) query.append("created_until", String(args.created_until)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) 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(getApiChargesResponse, await res.json()) };