import {z} from 'zod' export type GetApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsArgs = {fulfillmentOrderId: string, status?: string | undefined, page?: number | undefined, limit?: number | undefined, sortBy?: string | undefined, sortOrder?: string | undefined}; export type GetApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const fulfillmentOrderShipmentPackageResponse = 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 fulfillmentOrderShipmentListItemResponse = z.object({packages: z.array(fulfillmentOrderShipmentPackageResponse), metadata: z.record(z.string(), z.unknown()).nullable(), 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 paginationMetaResponse = z.object({total: z.number(), pages: z.number(), current_page: z.number(), next_page: z.number().nullable(), previous_page: z.number().nullable(), limit: z.number()}); export const fulfillmentOrderShipmentListResponse = z.object({data: z.array(fulfillmentOrderShipmentListItemResponse), meta: paginationMetaResponse}); 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 }; /** * List fulfillment order shipments * * GET /api/v1/fulfillment-orders/{fulfillmentOrderId}/shipments * * Also accepts auth: bearer */ export const getApiApiV1FulfillmentOrdersFulfillmentOrderIdShipments = async (args: GetApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsArgs, options?: GetApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsOptions) => { 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 (args.status != null) query.append("status", String(args.status)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/fulfillment-orders/${encodeURIComponent(String(args.fulfillmentOrderId))}/shipments${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(fulfillmentOrderShipmentListResponse, await res.json()) };