import {z} from 'zod' export type GetApiV1ShipmentsShipmentIdArgs = {shipmentId: string}; export type GetApiV1ShipmentsShipmentIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const trackingKeyShipmentsV1 = z.object({carrier: z.enum(["DHL", "DHL_EXPRESS", "DPD", "GLS", "HERMES", "UPS", "ABIKS", "AJ", "AJE", "BTW", "BURSPED", "CARGOLINE", "CARGOBOARD", "CTL", "DACHSER", "DB_SCHENKER", "DHL_FREIGHT", "DHL_HOME_DELIVERY", "DHL_2_MANN_HANDLING", "DSV", "ECE", "EMONS", "EPH_DIREKT", "FEDEX", "FROEDE", "GEIS", "GEL", "GRASSL", "GUETTLER", "HASENAUER", "HEPPNER", "HELLMANN", "HES", "HEUEL", "IDS", "ISG", "KIESSLING", "KOCH", "KUEHNE_NAGEL", "LOGWIN", "LOOKWAY", "MEYER_JUMBO", "MIRJAN", "ORCARGO", "PALLETWAYS", "PEISKER", "RABEN_LOGISTIK", "REIMER", "REITHMEIER", "RHENUS", "RIECK", "SCHMIDT_GEVELSBERG", "SCHOCKEMOEHLE", "SIEVERS", "SIMPLE_LOGISTIK", "SPEEDTRANS", "SPT", "VERHOEK", "WEISS", "ZUFALL", "OTHER_FORWARDER"]), trackingNumber: z.string()}); export const addressShipmentsV1 = z.object({city: z.string().min(1).max(60), countryCode: z.string(), zipCode: z.string().min(1).max(10)}); export const returnTrackingKeyShipmentsV1 = z.object({carrier: z.enum(["DHL", "GLS", "HERMES"]), trackingNumber: z.string()}); export const positionItemShipmentsV1 = z.object({positionItemId: z.string(), salesOrderId: z.string(), returnTrackingKey: returnTrackingKeyShipmentsV1, returnAddressCarrierId: z.string().optional()}); export const shipmentStateShipmentsV1 = z.object({state: z.enum(["SENT", "ON_THE_WAY", "DELIVERED", "DELIVERED_FOR_PICKUP", "DELIVERY_ATTEMPT_FAILED", "ISSUE_OCCURRED"]), occurredOn: z.string()}); export const shipmentShipmentsV1 = z.object({shipmentId: z.string().optional(), creationDate: z.string().optional(), trackingKey: trackingKeyShipmentsV1.optional(), shipDate: z.string().optional(), shipFromAddress: addressShipmentsV1.optional(), positionItems: z.array(positionItemShipmentsV1).optional(), states: z.array(shipmentStateShipmentsV1).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 }; /** * Retrieve shipment by shipment ID. * * GET /v1/shipments/{shipmentId} */ export const getApiV1ShipmentsShipmentId = async (args: GetApiV1ShipmentsShipmentIdArgs, options?: GetApiV1ShipmentsShipmentIdOptions) => { const {baseUrl = "https://api.otto.market", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/shipments/${encodeURIComponent(String(args.shipmentId))}${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(shipmentShipmentsV1, await res.json()) };