import {z} from 'zod' export type CreateApiOrdersArgs = {body: {items: Array, customer_id?: string | undefined, customer?: Record | undefined, shipping?: Record | undefined, payments: Array, metadata?: string | undefined}}; export type CreateApiOrdersOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiOrdersResponse = 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(), phones: z.object({}).optional()}).optional(), status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), checkouts: z.array(z.object({id: z.string().optional(), amount: z.number().int().optional(), status: z.string().optional(), success_url: z.string().optional(), payment_url: z.string().optional(), customer_editable: z.boolean().optional(), billing_address_editable: z.boolean().optional(), skip_checkout_success_page: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), expires_at: z.string().optional(), accepted_payment_methods: z.array(z.string()).optional(), accepted_multi_payment_methods: z.array(z.array(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(), credit_card: z.object({capture: z.boolean().optional(), installments: z.array(z.object({number: z.number().int().optional(), total: z.number().int().optional()})).optional()}).optional(), boleto: z.object({due_at: z.string().optional(), instructions: z.string().optional()}).optional(), billing_address: z.object({}).optional(), shippable: z.boolean().optional(), currency: z.string().optional(), metadata: z.object({}).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 }; /** * Criar pedido com meio de pagamento Checkout multimeios * * POST /orders */ export const createApiOrders = async (args: CreateApiOrdersArgs, options?: CreateApiOrdersOptions) => { const {baseUrl = "https://api.mundipagg.com/core/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiOrdersResponse, await res.json()) };