import {z} from 'zod' export type DesiredDeliveryDateOptions = {exactDateDelivery?: boolean | undefined}; export type SPAddress = {street1?: string | undefined, street2?: string | undefined, city?: string | undefined, state?: string | undefined, countryCode: string, postalCode: string, addressType?: ('commercial' | 'residential') | undefined}; export type SPShipmentParameters = {shipFromAddress?: SPAddress | undefined, destinationAddress: SPAddress, originId?: string | undefined, deliverySignatureOption?: string | undefined, fulfillmentContext?: string | undefined, fulfillmentType?: string | undefined, shipOption?: string | undefined, testMode?: boolean | undefined, labelToDockDoorHours?: number | undefined}; export type ReferenceIdentifier = {name?: string | undefined, value?: string | undefined}; export type ShipmentPlanningParameters = {requestDateOverride?: string | undefined, desiredDeliveryDate?: string | undefined, businessDaysOfTransit?: number | undefined, mustShipByDate?: string | undefined, mustShipByBusinessDays?: number | undefined, mustShipByCalendarDays?: number | undefined, deliveryWindowDays?: number | undefined, deliveryWindowDaysType?: ('BUSINESS' | 'CALENDAR') | undefined, desiredDeliveryDateOptions?: DesiredDeliveryDateOptions | undefined, carrierServiceMethodAllowList?: Array | undefined, shipmentParameters: SPShipmentParameters, referenceIdentifier?: string | undefined, partnerReferenceIdentifier?: string | undefined, partnerReferenceIdentifier2?: string | undefined, referenceIdentifiers?: Array | undefined, shipmentTags?: Array | undefined}; export type CreateApiApiV1DeliveryexperienceShipmentPlanningArgs = {body: ShipmentPlanningParameters}; export type CreateApiApiV1DeliveryexperienceShipmentPlanningOptions = {baseUrl?: string | undefined, oauth2Scheme?: string | undefined, fetch?: typeof fetch | undefined}; export const shippingOrigin = z.object({countryCode: z.string().optional(), postalCode: z.string().optional(), shipiumOriginId: z.string().optional(), originId: z.string().optional(), partnerOriginId: z.string().optional(), originProcessingDays: z.number().optional()}); export const desiredDeliveryDateOptions = z.object({exactDateDelivery: z.boolean().optional()}); export const sPAddress = z.object({street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), countryCode: z.string().min(2).max(2), postalCode: z.string(), addressType: z.enum(["commercial", "residential"]).optional()}); export const sPShipmentParameters = z.object({shipFromAddress: sPAddress.optional(), destinationAddress: sPAddress, originId: z.string().optional(), deliverySignatureOption: z.string().optional(), fulfillmentContext: z.string().optional(), fulfillmentType: z.string().optional(), shipOption: z.string().optional(), testMode: z.boolean().optional(), labelToDockDoorHours: z.number().optional()}); export const referenceIdentifier = z.object({name: z.string().optional(), value: z.string().optional()}); export const shipmentDateExceptionInfo = z.object({exceptionType: z.enum(["ExactDeliveryDateNotSupported", "LateDeliveryDate", "ShipDateInPast", "ShipDateAfterMustShipBy", "FcDropByDateInPast", "EarlyDeliveryDate"]).optional(), exceptionDescription: z.string().optional()}); export const shipmentDateException = z.object({exceptions: z.array(shipmentDateExceptionInfo).optional(), effectiveFcDropByDate: z.string().optional(), effectiveShipByDate: z.string().optional(), effectiveDeliveryDate: z.string().optional()}); export const shipmentPlanning = z.object({shipmentPlanningId: z.string(), shippingOrigin: shippingOrigin, requestDateOverride: z.string().optional(), desiredDeliveryDate: z.string().optional(), businessDaysOfTransit: z.number().int().optional(), mustShipByDate: z.string().optional(), mustShipByBusinessDays: z.number().int().optional(), mustShipByCalendarDays: z.number().int().optional(), desiredDeliveryDateOptions: desiredDeliveryDateOptions.optional(), carrierServiceMethodAllowList: z.array(z.string()).optional(), shipmentParameters: sPShipmentParameters.optional(), shipByDate: z.string(), fcDropByDate: z.string(), estimateSource: z.enum(["PartnerProvided", "ShipiumCalculated", "CarrierProvided", "Unknown"]), estimatedTransitDays: z.number().int(), referenceIdentifier: z.string().optional(), partnerReferenceIdentifier: z.string().optional(), partnerReferenceIdentifier2: z.string().optional(), referenceIdentifiers: z.array(referenceIdentifier).optional(), shipmentTags: z.array(z.string()).optional(), shipDateException: z.boolean().optional(), shipDateExceptionDetails: shipmentDateException.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 }; /** * Shipment Planning API * * POST /api/v1/deliveryexperience/shipmentPlanning */ export const createApiApiV1DeliveryexperienceShipmentPlanning = async (args: CreateApiApiV1DeliveryexperienceShipmentPlanningArgs, options?: CreateApiApiV1DeliveryexperienceShipmentPlanningOptions) => { 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/deliveryexperience/shipmentPlanning${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(shipmentPlanning, await res.json()) };