import {z} from 'zod' export type GetApiApiV1OrdersIdArgs = {id: string}; export type GetApiApiV1OrdersIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const orderFindResponse = z.object({status: z.object({code: z.string().optional(), label: z.string().optional(), color: z.string().nullable().optional(), nature: z.enum(["INITIAL", "PROCESSING", "FINAL", "ERROR"]).nullable().optional()}), channel: z.object({id: z.string().optional(), code: z.string().nullable().optional(), name: z.string().nullable().optional()}), customer: z.object({id: z.string().optional(), name: z.string().nullable().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), document: z.string().nullable().optional(), document_type: z.enum(["PF", "PJ"]).nullable().optional(), external_id: z.string().nullable().optional(), birth_date: z.string().nullable().optional()}).nullable(), state_machine: z.object({id: z.string().optional(), code: z.string().optional(), name: z.string().optional(), version: z.number().optional(), transitions: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).nullable(), metadata: z.record(z.string(), z.unknown()).nullable(), timeline: z.array(z.object({id: z.string().optional(), type: z.enum(["order", "fulfillment", "shipment"]).optional(), status: z.string().optional(), label: z.string().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), occurred_at: z.string().optional(), shipment: z.object({id: z.string().optional(), carrier: z.object({id: z.string().optional(), code: z.string().optional(), name: z.string().optional()}).nullable().optional(), tracking_code: z.string().nullable().optional(), source: z.string().nullable().optional(), event_id: z.string().nullable().optional()}).nullable().optional()})), fulfillments: z.array(z.object({id: z.string().optional(), status: z.string().optional(), store: z.object({id: z.string().optional(), code: z.string().optional(), name: z.string().optional()}).nullable().optional(), items: z.array(z.object({product_id: z.string().nullable().optional(), sku: z.string().optional(), name: z.string().optional(), quantity: z.number().optional()})).optional()})), id: z.string(), external_order_id: z.string(), requested_strategy_code: z.string().nullable(), source_store: z.string().nullable(), shipping_method: z.string().nullable(), currency: z.enum(["BRL", "USD"]), shipping_address: z.object({zip_code: z.string(), street: z.string(), number: z.string(), complement: z.string().nullable(), district: z.string(), city: z.string(), state: z.string(), country: z.string()}), billing_address: z.object({zip_code: z.string(), street: z.string(), number: z.string(), complement: z.string().nullable(), district: z.string(), city: z.string(), state: z.string(), country: z.string()}), items: z.array(z.object({sku: z.string(), name: z.string(), category: z.string().nullable(), quantity: z.number(), unit_price: z.number(), discount: z.number(), total_price: z.number(), metadata: z.object({}).nullable()})), payments: z.array(z.object({id: z.string(), method: z.string(), amount: z.number(), currency: z.enum(["BRL", "USD"]), installments: z.number(), status: z.enum(["PENDING", "PAID", "REJECTED", "REFUNDED"]), gateway: z.string().nullable(), gateway_payment_id: z.string().nullable(), authorization_code: z.string().nullable(), nsu: z.string().nullable(), acquirer: z.string().nullable(), card: z.object({brand: z.string().nullable().optional(), last4: z.string().nullable().optional(), funding: z.string().nullable().optional(), exp_month: z.number().nullable().optional(), exp_year: z.number().nullable().optional()}), pix: z.object({txid: z.string().nullable().optional(), e2eid: z.string().nullable().optional(), qr_code: z.string().nullable().optional(), expires_at: z.string().nullable().optional()}), boleto: z.object({digitable_line: z.string().nullable().optional(), url: z.string().nullable().optional(), due_date: z.string().nullable().optional()}), paid_at: z.string().nullable(), refunded_at: z.string().nullable(), created_at: z.string(), updated_at: z.string()})), totals: z.object({subtotal: z.number(), shipping: z.number(), discount: z.number(), total: z.number()}), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Get an order by id * * GET /api/v1/orders/{id} * * Also accepts auth: bearer */ export const getApiApiV1OrdersId = async (args: GetApiApiV1OrdersIdArgs, options?: GetApiApiV1OrdersIdOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/orders/${encodeURIComponent(String(args.id))}${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(orderFindResponse, await res.json()) };