import {z} from 'zod' export type CreateItemRequest = {packageId?: (string | null) | undefined, containerId?: (string | null) | undefined, lineNumber?: number | undefined, sku?: string | undefined, partNumber?: string | undefined, description?: string | undefined, quantity?: number | undefined, unitOfMeasure?: string | undefined, unitPrice?: number | undefined, amount?: number | undefined, currency?: string | undefined, countryOfOrigin?: string | undefined, hsCode?: string | undefined, weight?: number | undefined, weightUnit?: string | undefined}; export type CreateApiApiV1ExternalShipmentsShipmentIdItemsArgs = {shipmentId: string, body: CreateItemRequest}; export type CreateApiApiV1ExternalShipmentsShipmentIdItemsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const shipmentItem = z.object({id: z.string().optional(), lineNumber: z.number().int().optional(), sku: z.string().optional(), partNumber: z.string().optional(), description: z.string().optional(), quantity: z.number().optional(), unitOfMeasure: z.string().optional(), unitPrice: z.number().optional(), amount: z.number().optional(), currency: z.string().optional(), countryOfOrigin: z.string().optional(), hsCode: z.string().optional(), weight: z.number().optional(), weightUnit: z.string().optional(), volume: z.number().optional(), volumeUnit: z.string().optional(), packageNumber: z.string().optional(), dimensions: z.object({length: z.number().optional(), width: z.number().optional(), height: z.number().optional(), unit: z.string().optional()}).optional()}); export const createApiApiV1ExternalShipmentsShipmentIdItemsResponse = z.object({success: z.boolean().optional(), data: shipmentItem.optional(), message: 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 }; /** * Create item * * POST /api/v1/external/shipments/{shipmentId}/items */ export const createApiApiV1ExternalShipmentsShipmentIdItems = async (args: CreateApiApiV1ExternalShipmentsShipmentIdItemsArgs, options?: CreateApiApiV1ExternalShipmentsShipmentIdItemsOptions) => { const {baseUrl = "https://api.wove.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/external/shipments/${encodeURIComponent(String(args.shipmentId))}/items${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(createApiApiV1ExternalShipmentsShipmentIdItemsResponse, await res.json()) };