import {z} from 'zod' export type GetApiShipmentsBookedShipmentIdArgs = {bookedShipmentId: number}; export type GetApiShipmentsBookedShipmentIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const shipmentDeliveryAddress = z.object({name: z.string().optional(), company: z.string().optional(), phone: z.string().optional(), email: z.string().optional(), zip: z.string().optional(), city: z.string(), state: z.string().optional(), countryIso2: z.string().optional(), street: z.string(), street2: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional()}); export const currency = z.object({currencyCode: z.string().optional(), defaultFractionDigits: z.number().int().optional(), numericCode: z.number().int().optional(), symbol: z.string().optional(), displayName: z.string().optional(), numericCodeAsString: z.string().optional()}); export const exportPositionGet = z.object({currency: currency.optional(), quantity: z.number().int().optional(), origin: z.enum(["AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "CI", "HR", "CU", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "XK", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SK", "SI", "SB", "SO", "ZA", "GS", "ES", "LK", "SD", "SR", "SJ", "CW", "BQ", "SX", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW"]).optional(), tariff: z.string().optional(), warehouseSku: z.string().optional(), contentDescription: z.string().optional(), unNumber: z.string().optional(), description: z.string().optional(), pieceNetCustomsValue: z.number().optional(), pieceNetWeightKg: z.number().optional()}); export const shipmentParcelGet = z.object({weight: z.number().gt(0), length: z.number().optional(), width: z.number().optional(), height: z.number().optional(), reference: z.string().optional()}); export const shippingOrderGet = z.object({shipTo: shipmentDeliveryAddress.optional(), shipFrom: shipmentDeliveryAddress.optional(), reference: z.string().optional(), grossWeightKg: z.number().optional(), lengthM: z.number().optional(), widthM: z.number().optional(), heightM: z.number().optional(), totalWeight: z.number().optional(), totalVolume: z.number().optional(), shipmentDate: z.string().optional(), invoiceNumber: z.string().optional(), invoiceDate: z.string().optional(), includeReturnLabel: z.boolean().optional(), trackingEnabled: z.boolean().optional(), userIdentifier: z.string().optional(), cashOnDeliveryAmount: z.number().optional(), cashOnDeliveryCurrency: currency.optional(), customsPositions: z.array(exportPositionGet).optional(), transportInsuranceAmount: z.number().optional(), termsOfTrade: z.enum(["DAP", "DDP"]).optional(), additionalParcels: z.array(shipmentParcelGet).optional(), orderReference: z.string().optional()}); export const shipmentDocumentGet = z.object({shipmentDocumentId: z.number().int().optional(), type: z.enum(["LABEL", "EXPORT_DOCUMENT", "NON_PRINT_EXPORT_DOCUMENT", "MANIFEST_DOCUMENT", "RETURN_LABEL"]).optional()}); export const trackingDetailGet = z.object({status: z.string().optional(), created: z.string().optional(), message: z.string().optional(), rawTag: z.string().optional()}); export const additionalTrackingNumberGet = z.object({trackingNumber: z.string().optional(), reference: z.string().optional()}); export const bookedShipmentGet = z.object({sellerId: z.number().int().optional(), shippingServiceId: z.number().int().optional(), request: shippingOrderGet.optional(), documents: z.array(shipmentDocumentGet).optional(), trackingDetails: z.array(trackingDetailGet).optional(), id: z.number().int().optional(), created: z.string().optional(), requestId: z.string().optional(), trackingNumber: z.string().optional(), additionalTrackingNumbers: z.array(additionalTrackingNumberGet).optional(), returnTrackingNumber: z.string().optional(), additionalReturnTrackingNumbers: z.array(additionalTrackingNumberGet).optional(), shipmentIdentifier: z.string().optional(), cancelledAt: z.string().optional(), carrierCancelledAt: z.string().optional(), shipTo: shipmentDeliveryAddress.optional(), shipFrom: shipmentDeliveryAddress.optional(), parcelWeight: z.number().optional(), parcelReference: z.string().optional(), contentDescription: z.string().optional(), trackingLink: z.string().optional(), returnTrackingLink: z.string().optional(), carrierSlug: z.string().optional(), carrierName: z.string().optional(), direction: 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 }; /** * Get details of a booked shipment * * GET /shipments/{bookedShipmentId} */ export const getApiShipmentsBookedShipmentId = async (args: GetApiShipmentsBookedShipmentIdArgs, options?: GetApiShipmentsBookedShipmentIdOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/shipments/${encodeURIComponent(String(args.bookedShipmentId))}${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(bookedShipmentGet, await res.json()) };