import {z} from 'zod' export type CarrierCode = 'ups' | 'dhl' | 'fedex' | 'usps' | 'other'; export type ShipmentStatus = 'pending' | 'processing' | 'ready_to_ship' | 'shipped' | 'in_transit' | 'out_for_delivery' | 'delivered' | 'failed_delivery' | 'returned' | 'cancelled'; export type InboundShipmentUpdate = {carrier?: (CarrierCode | null) | undefined, tracking_number?: (string | null) | undefined, tracking_url?: (string | null) | undefined, status?: (ShipmentStatus | null) | undefined, estimated_delivery?: (string | null) | undefined, actual_delivery?: (string | null) | undefined, last_known_location?: (string | null) | undefined}; export type PatchApiApiV1AdminInboundShipmentsInboundShipmentIdArgs = {inbound_shipment_id: string, body: InboundShipmentUpdate}; export type PatchApiApiV1AdminInboundShipmentsInboundShipmentIdOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const carrierCode = z.enum(["ups", "dhl", "fedex", "usps", "other"]); export const shipmentStatus = z.enum(["pending", "processing", "ready_to_ship", "shipped", "in_transit", "out_for_delivery", "delivered", "failed_delivery", "returned", "cancelled"]); export const carrierSyncStatus = z.enum(["never_synced", "syncing", "synced", "failed"]); export const inboundPackageResponse = z.object({id: z.string(), inbound_shipment_id: z.string(), package_index: z.number().int(), carrier_piece_id: z.string().nullable().optional(), tracking_number: z.string().nullable().optional(), weight: z.string().nullable().optional(), weight_unit: z.string().nullable().optional(), length: z.string().nullable().optional(), width: z.string().nullable().optional(), height: z.string().nullable().optional(), dimension_unit: z.string().nullable().optional(), description: z.string().nullable().optional()}); export const inboundShipmentEventResponse = z.object({id: z.string(), inbound_shipment_id: z.string(), carrier_event_id: z.string().nullable().optional(), status: shipmentStatus, description: z.string(), location: z.string().nullable().optional(), occurred_at: z.string(), created_at: z.string()}); export const inboundShipmentResponse = z.object({id: z.string(), project_id: z.string(), supplier_order_id: z.string(), supplier_contact_id: z.string(), carrier: carrierCode, tracking_number: z.string(), tracking_url: z.string().nullable().optional(), status: shipmentStatus, estimated_delivery: z.string().nullable().optional(), actual_delivery: z.string().nullable().optional(), last_known_location: z.string().nullable().optional(), origin_location: z.string().nullable().optional(), destination_location: z.string().nullable().optional(), service_name: z.string().nullable().optional(), last_synced_at: z.string().nullable().optional(), sync_status: carrierSyncStatus, sync_error: z.string().nullable().optional(), packages: z.array(inboundPackageResponse).optional(), events: z.array(inboundShipmentEventResponse).optional(), 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 }; /** * Update Inbound Shipment * * PATCH /api/v1/admin/inbound-shipments/{inbound_shipment_id} */ export const patchApiApiV1AdminInboundShipmentsInboundShipmentId = async (args: PatchApiApiV1AdminInboundShipmentsInboundShipmentIdArgs, options: PatchApiApiV1AdminInboundShipmentsInboundShipmentIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/admin/inbound-shipments/${encodeURIComponent(String(args.inbound_shipment_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(inboundShipmentResponse, await res.json()) };