import {z} from 'zod' export type GetApiApiV1ExternalShipmentsShipmentIdItemsArgs = {shipmentId: string, packageId?: string | undefined}; export type GetApiApiV1ExternalShipmentsShipmentIdItemsOptions = {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 getApiApiV1ExternalShipmentsShipmentIdItemsResponse = z.object({success: z.boolean().optional(), data: z.object({items: z.array(shipmentItem).optional()}).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 }; /** * List items * * GET /api/v1/external/shipments/{shipmentId}/items */ export const getApiApiV1ExternalShipmentsShipmentIdItems = async (args: GetApiApiV1ExternalShipmentsShipmentIdItemsArgs, options?: GetApiApiV1ExternalShipmentsShipmentIdItemsOptions) => { const {baseUrl = "https://api.wove.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.packageId != null) query.append("packageId", String(args.packageId)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1ExternalShipmentsShipmentIdItemsResponse, await res.json()) };