import {z} from 'zod' export type GetApiApiV1FulfillmentOrdersIdArgs = {id: string}; export type GetApiApiV1FulfillmentOrdersIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const fulfillmentOrderFindItemResponse = z.object({metadata: z.record(z.string(), z.unknown()).nullable(), sku: z.string(), name: z.string(), category: z.string().nullable(), quantity: z.number(), unit_price: z.number(), discount: z.number(), total_price: z.number()}); export const fulfillmentOrderTimelineFindResponse = z.object({metadata: z.record(z.string(), z.unknown()).nullable(), id: z.string(), previous_status: z.string().nullable(), new_status: z.string(), user_id: z.string().nullable(), service_account_id: z.string().nullable(), occurred_at: z.string()}); export const fulfillmentOrderShipmentPackageFindResponse = z.object({dimensions: z.object({length_cm: z.number().optional(), width_cm: z.number().optional(), height_cm: z.number().optional()}).nullable(), items: z.array(z.object({sku: z.string().optional(), quantity: z.number().optional()})), weight_kg: z.number().nullable(), label_url: z.string().nullable()}); export const fulfillmentOrderShipmentFindResponse = z.object({packages: z.array(fulfillmentOrderShipmentPackageFindResponse), metadata: z.record(z.string(), z.unknown()).nullable(), timeline: z.array(z.object({id: z.string().optional(), status: z.string().optional(), occurred_at: z.string().optional(), source: z.string().nullable().optional(), event_id: z.string().nullable().optional()})), id: z.string(), fulfillment_order_id: z.string(), order_id: z.string(), carrier_id: z.string(), status: z.enum(["SHIPMENT_CREATED", "DISPATCHED", "IN_TRANSIT", "OUT_FOR_DELIVERY", "DELIVERED", "DELIVERY_FAILED", "RETURNED", "LOST", "LABEL_CREATED", "AWAITING_PICKUP", "IN_TRANSFER"]), tracking_code: z.string().nullable(), label: z.string().nullable(), dispatched_at: z.string().nullable(), delivered_at: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const fulfillmentOrderFindResponse = z.object({items: z.array(fulfillmentOrderFindItemResponse), timeline: z.array(fulfillmentOrderTimelineFindResponse), shipments: z.array(fulfillmentOrderShipmentFindResponse), metadata: z.record(z.string(), z.unknown()).nullable(), id: z.string(), order_id: z.string(), strategy_id: z.string().nullable(), status: z.string(), 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 a fulfillment order by id * * GET /api/v1/fulfillment-orders/{id} * * Also accepts auth: bearer */ export const getApiApiV1FulfillmentOrdersId = async (args: GetApiApiV1FulfillmentOrdersIdArgs, options?: GetApiApiV1FulfillmentOrdersIdOptions) => { 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/fulfillment-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(fulfillmentOrderFindResponse, await res.json()) };