import {z} from 'zod' export type GetApiOrdersArgs = {code?: string | undefined, status?: string | undefined, customer_id?: string | undefined, created_since?: string | undefined, created_until?: string | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiOrdersOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiOrdersResponse = z.object({data: z.array(z.object({id: z.string().optional(), code: z.string().optional(), amount: z.number().int().optional(), currency: z.string().optional(), closed: z.boolean().optional(), items: z.array(z.object({id: z.string().optional(), description: z.string().optional(), amount: z.number().int().optional(), quantity: z.number().int().optional(), status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()})).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()}).optional(), status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), closed_at: z.string().optional(), charges: z.array(z.object({id: z.string().optional(), code: z.string().optional(), gateway_id: z.string().optional(), 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()}).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 pedidos * * GET /orders */ export const getApiOrders = async (args: GetApiOrdersArgs, options?: GetApiOrdersOptions) => { 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.customer_id != null) query.append("customer_id", String(args.customer_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(/\/+$/, '')}/orders${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(getApiOrdersResponse, await res.json()) };