import {z} from 'zod' export type FulfillmentOrderShipmentPackageDimensionsCreate = {length_cm: number, width_cm: number, height_cm: number}; export type FulfillmentOrderShipmentPackageItemCreate = {sku: string, quantity: number}; export type FulfillmentOrderShipmentPackageCreate = {weight_kg?: (number | null) | undefined, dimensions?: (FulfillmentOrderShipmentPackageDimensionsCreate | null) | undefined, items: Array, label_url?: (string | null) | undefined}; export type FulfillmentOrderShipmentCreate = {carrier_id: string, tracking_code?: (string | null) | undefined, label?: (string | null) | undefined, packages: Array, metadata?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsArgs = {fulfillmentOrderId: string, body: FulfillmentOrderShipmentCreate}; export type CreateApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const fulfillmentOrderShipmentPackageCreateResponse = 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 fulfillmentOrderShipmentCreateResponse = z.object({packages: z.array(fulfillmentOrderShipmentPackageCreateResponse), metadata: z.record(z.string(), z.unknown()).nullable(), shipment_id: z.string(), fo_status: 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 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 }; /** * Create a shipment on a fulfillment order (synchronous) * * POST /api/v1/fulfillment-orders/{fulfillmentOrderId}/shipments * * Also accepts auth: bearer */ export const createApiApiV1FulfillmentOrdersFulfillmentOrderIdShipments = async (args: CreateApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsArgs, options?: CreateApiApiV1FulfillmentOrdersFulfillmentOrderIdShipmentsOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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.fulfillmentOrderId))}/shipments${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(fulfillmentOrderShipmentCreateResponse, await res.json()) };