import {z} from 'zod' export type FEDesiredDeliveryDateOptions = {exactDateDelivery?: boolean | undefined, guaranteedDateDelivery?: boolean | undefined, upgradeCostDeltaMax?: number | undefined, currencyCode?: string | undefined, fulfillmentPreference?: ('OnTime' | 'Consolidate' | 'Unknown') | undefined}; export type Address = {name?: string | undefined, company?: string | undefined, street1?: string | undefined, street2?: string | undefined, city?: string | undefined, region?: string | undefined, countryCode: string, postalCode: string, addressType?: ('commercial' | 'residential') | undefined}; export type FEOrderItem = {quantity: number, productId: string}; export type Criteria = {mustShipComplete?: boolean | undefined}; export type LinearDimensions = {linearUnit?: ('in' | 'cm') | undefined, length?: number | undefined, width?: number | undefined, height?: number | undefined}; export type Weight = {weightUnit?: ('g' | 'kg' | 'oz' | 'lb') | undefined, weight?: number | undefined}; export type FEPackagingType = {packagingTypeId: string, packagingMaterial: 'box' | 'envelope' | 'flat_pack' | 'unknown', linearDimensions: LinearDimensions, weight: Weight, maxContentWeight?: Weight | undefined, urn?: string | undefined, packagingSizeName?: string | undefined}; export type OrderParams = {partnerOrderId: string, orderedDateTime?: string | undefined, currencyCode?: ('chf' | 'usd' | 'not_set' | 'unknown') | undefined, desiredDeliveryDate?: string | undefined, desiredDeliveryDateOptions?: FEDesiredDeliveryDateOptions | undefined, businessDaysOfTransit?: number | undefined, shipOption?: string | undefined, shippedDateTime?: string | undefined, destinationAddress: Address, orderItemQuantities: Array, orderParams?: Criteria | undefined, partnerReferenceIdentifier?: string | undefined, partnerReferenceIdentifier2?: string | undefined, shipmentTags?: Array | undefined, packagingType?: FEPackagingType | undefined}; export type ProductInventoryStatus = {fulfillmentCenterId?: string | undefined, fulfillmentContextId?: string | undefined, fulfillmentContext?: string | undefined, quantityAvailable?: number | undefined, assumeQuantityAvailableInfinite?: boolean | undefined, fulfillmentCostCurrencyCode?: ('chf' | 'usd' | 'not_set' | 'unknown') | undefined, fulfillmentCostPerItem?: number | undefined}; export type FEHazmatInfo = {category: 'defined' | 'aerosols_flammable' | 'contains_lithium_ion' | 'packaged_lithium_ion' | 'lithium_ion_battery_only' | 'contains_lithium_metal' | 'packaged_lithium_metal' | 'lithium_metal_battery_only' | 'dry_ice', quantity: number, quantityType: 'gross' | 'net', quantityUnits: 'g' | 'kg' | 'lb' | 'oz' | 'ml' | 'l' | 'gal', containerType: 'fiberboard_box' | 'wooden_box' | 'plastic_jerrican' | 'metal_box' | 'steel_drum' | 'other' | 'plastic_box' | 'plastic_drum' | 'styrofoam_box' | 'cylinder' | 'envirotainer' | 'plywood_box' | 'aluminum_drum' | 'aluminum_cylinder' | 'plastic_pail' | 'plywood_drum' | 'fiber_drum' | 'steel_jerrican' | 'aluminum_jerrican' | 'steel_box' | 'carton' | 'aluminum_box', hazmatId?: string | undefined, properShippingName?: string | undefined, packingGroup?: ('I' | 'II' | 'III') | undefined, transportMode?: ('ground' | 'passenger_and_cargo_aircraft' | 'cargo_aircraft_only') | undefined, packingInstructionCode?: string | undefined, hazardClass?: ('class_1_explosive' | 'class_2_flammable_gas' | 'class_3_flammable_liquid' | 'class_4_flammable_solid' | 'class_5_organic_peroxide' | 'class_6_poisonous_material' | 'class_7_radioactive' | 'class_8_corrosive_material' | 'class_9_miscellaneous' | 'forbidden' | 'other') | undefined, subsidiaryClasses?: Array | undefined, numberOfInnerPackagings?: number | undefined, innerPackagingType?: string | undefined, outerPackagingType?: string | undefined, shipperDeclarationStatement?: ('FDG' | 'LQ_air' | 'EQ' | 'small_battery_air' | 'small_battery_ground' | 'cargo_aircraft_only') | undefined, wattHours?: number | undefined, lithiumContent?: number | undefined, numberOfCells?: number | undefined, numberOfBatteries?: number | undefined, batteryConfiguration?: ('contained_in_equipment' | 'packed_with_equipment' | 'standalone') | undefined}; export type ProductDetail = {productId: string, inventoryStatuses?: Array | undefined, assumeQuantityAvailableInfinite?: boolean | undefined, linearDimensions?: LinearDimensions | undefined, weight?: Weight | undefined, fulfillmentAttributes: Array, productDetails: Array, hazmat?: boolean | undefined, hazmatInfo?: FEHazmatInfo | undefined}; export type FulfillmentCost = {currencyCode?: ('chf' | 'usd' | 'not_set' | 'unknown') | undefined, perItem?: number | undefined, perPackage?: number | undefined}; export type SplitSettings = {allowSplits?: boolean | undefined, maxItemsPerPackage?: number | undefined, maxPackagesPerOrder?: number | undefined, splitTypes?: (Array<'fc_split' | 'hazmat' | 'inventory' | 'max_shipments' | 'package_planner' | 'sioc' | 'unit_count' | 'unknown'>) | undefined}; export type FulfillmentCenterRules = {splitSettings?: SplitSettings | undefined}; export type FulfillmentCenterData = {fulfillmentCenterId?: string | undefined, fulfillmentContextId?: string | undefined, fulfillmentContext?: string | undefined, packagingTypes?: Array | undefined, processingTime?: ({seconds?: number | undefined, zero?: boolean | undefined, nano?: number | undefined, negative?: boolean | undefined, units?: (Array<{durationEstimated?: boolean | undefined, duration?: ({seconds?: number | undefined, zero?: boolean | undefined, nano?: number | undefined, negative?: boolean | undefined}) | undefined, timeBased?: boolean | undefined, dateBased?: boolean | undefined}>) | undefined}) | undefined, fulfillmentCost?: FulfillmentCost | undefined, rules?: FulfillmentCenterRules | undefined}; export type PlannerParams = {partnerPlanId?: string | undefined, asOfDateTime?: string | undefined, orders: Array, productDetails?: Array | undefined, assumeQuantityAvailableInfinite?: boolean | undefined, packagingTypes?: Array | undefined, planDateTime?: string | undefined, fulfillmentCenters?: Array | undefined, name?: string | undefined, description?: string | undefined, tenantId?: string | undefined}; export type CreateApiApiV1FePlansArgs = {body: PlannerParams}; export type CreateApiApiV1FePlansOptions = {baseUrl?: string | undefined, oauth2Scheme?: string | undefined, fetch?: typeof fetch | undefined}; export const auditInfo = z.object({createdBy: z.string().optional(), creationTimeStamp: z.string().optional(), lastUpdatedBy: z.string().optional(), lastUpdatedTimeStamp: z.string().optional(), version: z.number().int().optional()}); export const performance = z.object({effectiveShipDateTime: z.string().optional(), estimatedDeliveryDate: z.string().optional(), estimatedDeliveryDateLocal: z.string().optional(), destinationTimezoneId: z.string().optional()}); export const costInfo = z.object({carrier: z.string().optional(), serviceMethod: z.string().optional(), cost: z.number().optional(), performance: performance.optional()}); export const address = z.object({name: z.string().optional(), company: z.string().optional(), street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), countryCode: z.string(), postalCode: z.string(), addressType: z.enum(["commercial", "residential"]).optional()}); export const linearDimensions = z.object({linearUnit: z.enum(["in", "cm"]).optional(), length: z.number().optional(), width: z.number().optional(), height: z.number().optional()}); export const weight = z.object({weightUnit: z.enum(["g", "kg", "oz", "lb"]).optional(), weight: z.number().optional()}); export const fEPackagingType = z.object({packagingTypeId: z.string(), packagingMaterial: z.enum(["box", "envelope", "flat_pack", "unknown"]), linearDimensions: linearDimensions, weight: weight, maxContentWeight: weight.optional(), urn: z.string().optional(), packagingSizeName: z.string().optional()}); export const shipmentItem = z.object({productId: z.string(), partnerOrderId: z.string(), linearDimensions: linearDimensions.optional(), weight: weight.optional()}); export const proposedShipment = z.object({proposedShipmentId: z.string(), fulfillmentCenterId: z.string().optional(), fulfillmentContextId: z.string(), fulfillmentContext: z.string().optional(), cost: costInfo.optional(), destinationAddress: address, packagingType: fEPackagingType, shipmentItems: z.array(shipmentItem), totalWeight: weight.optional()}); export const productInventoryStatus = z.object({fulfillmentCenterId: z.string().optional(), fulfillmentContextId: z.string().optional(), fulfillmentContext: z.string().optional(), quantityAvailable: z.number().int().optional(), assumeQuantityAvailableInfinite: z.boolean().optional(), fulfillmentCostCurrencyCode: z.enum(["chf", "usd", "not_set", "unknown"]).optional(), fulfillmentCostPerItem: z.number().optional()}); export const fEOrderItem = z.object({quantity: z.number().int(), productId: z.string()}); export const unfulfillableItem = z.object({partnerOrderId: z.string(), orderItem: fEOrderItem, reason: z.string(), countUnfulfilled: z.number().int()}); export const plan = z.object({partnerId: z.string().optional(), urn: z.string().optional(), name: z.string().optional(), description: z.string().optional(), active: z.boolean().optional(), archived: z.boolean().optional(), auditInfo: auditInfo.optional(), planId: z.string(), partnerPlanId: z.string().optional(), proposedShipments: z.array(proposedShipment), resultingInventory: z.record(z.string(), z.array(productInventoryStatus)), unfulfillableItems: z.array(unfulfillableItem), requestTenantId: z.string().optional(), shipiumTenantId: z.string().optional(), partnerTenantId: z.string().optional(), assumeQuantityAvailableInfinite: z.boolean().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 }; /** * Endpoint to take in order and item information from a partner, and generated the best shipment plan based on their configured fulfillment network. * * POST /api/v1/fe/plans */ export const createApiApiV1FePlans = async (args: CreateApiApiV1FePlansArgs, options?: CreateApiApiV1FePlansOptions) => { const {baseUrl = "https://api.shipium.com", fetch: fetchFn = fetch, oauth2Scheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2Scheme !== undefined) headers.set('authorization', `Bearer ${oauth2Scheme}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/fe/plans${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(plan, await res.json()) };