import {z} from 'zod' export type StoreId = string; export type GetApiInventoriesV1ShipmentsArgs = {limit?: string | undefined, token?: string | undefined, 'X-Store-Id': StoreId}; export type GetApiInventoriesV1ShipmentsOptions = {baseUrl?: string | undefined, OAuth20?: string | undefined, fetch?: typeof fetch | undefined}; export const deliveryWindow = z.object({start: z.string(), end: z.string()}); export const shipmentDeliveryInfo = z.discriminatedUnion("deliveryType", [z.object({deliveryWindow: deliveryWindow, deliveryType: z.string(), courierName: z.string(), trackingId: z.string().optional()}).and(z.record(z.string(), z.unknown())), z.object({deliveryWindow: deliveryWindow, deliveryType: z.string(), carrierName: z.enum(["FED_EX", "UPS", "ON_TRAC", "DHL", "CORREOS", "ESTAFETA", "BR_POST", "TNT", "OTHER"]), trackingNumber: z.string().optional()}).and(z.record(z.string(), z.unknown())), z.object({deliveryWindow: deliveryWindow, deliveryType: z.string(), delivererName: z.string(), delivererEmailAddress: z.string().optional()}).and(z.record(z.string(), z.unknown()))]); export const shipmentLineItem = z.object({id: z.string(), manifestQuantity: z.number().int(), receivedSellableQuantity: z.number().int().optional(), receivedUnsellableQuantity: z.number().int().optional()}); export const shipmentState = z.enum(["SCHEDULED", "ARRIVED", "STOCKING", "CLOSED", "CANCELED"]); export const shipmentStateChange = z.object({state: shipmentState, timestamp: z.string()}); export const shipment = z.object({id: z.string(), deliveryInfo: shipmentDeliveryInfo, lineItems: z.array(shipmentLineItem), stateChanges: z.array(shipmentStateChange)}); export const listShipmentsResponse = z.object({shipments: z.array(shipment).optional(), nextToken: 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 }; /** * List shipments * * GET /inventories/v1/shipments */ export const getApiInventoriesV1Shipments = async (args: GetApiInventoriesV1ShipmentsArgs, options?: GetApiInventoriesV1ShipmentsOptions) => { const {baseUrl = "https://partners.tryotter.com", fetch: fetchFn = fetch, OAuth20} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.token != null) query.append("token", String(args.token)) if (args['X-Store-Id'] != null) headers.set("X-Store-Id", String(args['X-Store-Id'])) if (OAuth20 !== undefined) headers.set('authorization', `Bearer ${OAuth20}`) const url = `${baseUrl.replace(/\/+$/, '')}/inventories/v1/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(listShipmentsResponse, await res.json()) };