import {z} from 'zod' export type GetApiAppOrderOrderIdArgs = {order_id: string}; export type GetApiAppOrderOrderIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAppOrderOrderIdResponse = z.object({client_identifier: z.string().optional(), client_email: z.string().optional(), client_name: z.string().optional(), client_document: z.string().nullable().optional(), client_phone: z.string().nullable().optional(), client_discord: z.string().nullable().optional(), price: z.number().optional(), net_received: z.number().optional(), internal_id: z.string().optional(), external_id: z.string().optional(), promo_code: z.string().nullable().optional(), promo_type: z.enum(["PERCENTAGE", "PRICE"]).nullable().optional(), promo_value: z.string().nullable().optional(), gateway: z.string().optional(), status: z.enum(["APPROVED", "REJECTED", "CANCELED", "REFUNDED", "CHARGEDBACK"]).optional(), currency: z.enum(["BRL", "USD", "EUR"]).optional(), store_id: z.number().optional(), packages: z.array(z.object({id: z.number().optional(), store_id: z.number().optional(), package_id: z.number().optional(), name: z.string().optional(), price: z.number().optional(), quantity: z.number().optional(), currency: z.string().optional(), meta: z.object({}).optional(), options: z.array(z.unknown()).optional()})).optional(), variables: z.array(z.object({name: z.string().optional(), value: z.string().optional()})).optional(), queued_commands: z.array(z.unknown()).optional(), deliveries: z.array(z.unknown()).optional(), notes: z.array(z.unknown()).optional(), affiliate: z.object({}).nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), paid_at: z.string().nullable().optional(), price_display: z.string().optional(), status_display: z.string().optional(), gateway_display: z.string().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 }; /** * Order Details * * GET /app/order/{order_id} */ export const getApiAppOrderOrderId = async (args: GetApiAppOrderOrderIdArgs, options?: GetApiAppOrderOrderIdOptions) => { const {baseUrl = "https://api.centralcart.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/app/order/${encodeURIComponent(String(args.order_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(getApiAppOrderOrderIdResponse, await res.json()) };