import {z} from 'zod' export type DeliveryBehaviorOverrides = {saturdayDeliveryBehavior?: ('ALLOW' | 'NON_DELIVERY_DAY' | 'NON_TRANSIT_NON_DELIVERY_DAY') | undefined, sundayDeliveryBehavior?: ('ALLOW' | 'NON_DELIVERY_DAY' | 'NON_TRANSIT_NON_DELIVERY_DAY') | undefined, holidayDeliveryBehavior?: ('ALLOW' | 'NON_DELIVERY_DAY' | 'NON_TRANSIT_NON_DELIVERY_DAY') | undefined}; export type ShippingOptions = {shipOption?: ('NotProvided' | 'Standard' | 'StandardPlus' | 'SameDay' | 'NextDay' | 'Overnight' | 'PriorityOvernight' | 'TwoDay' | 'TwoDayMorning' | 'ThreeDay' | 'International24Hour' | 'InternationalOneToThreeDayEarly' | 'InternationalOneToThreeDay' | 'InternationalTwoToFiveDay' | 'InternationalTwoToTenDay' | 'InternationalOneToTwoWeek' | 'InternationalOneToFourWeek' | 'DesiredDeliveryDate' | 'BusinessDaysOfTransit' | 'Unknown') | undefined, fromCountryCode?: string | undefined, fromPostalCode?: string | undefined, originId?: string | undefined, estimateSource?: ('PartnerProvided' | 'ShipiumCalculated' | 'CarrierProvided') | undefined, deliveryBehaviorOverrides?: DeliveryBehaviorOverrides | undefined, timeInTransitSetting?: string | undefined}; export type CartShippingOptions = {shippingLocationOptions: Array}; export type ReferenceIdentifier = {name?: string | undefined, value?: string | undefined}; export type CartDeliveryEstimateRequest = {partnerCartId?: string | undefined, customerCountryCode: string, customerPostalCode: string, requestDateOverride?: string | undefined, shipDateOverride?: string | undefined, productViewDeliveryEstimateIds?: Array | undefined, cartShippingOptions: CartShippingOptions, referenceIdentifiers?: Array | undefined, partnerReferenceIdentifier?: string | undefined, partnerReferenceIdentifier2?: string | undefined, shipmentTags?: Array | undefined}; export type CreateApiApiV1DeliveryexperienceCartDeliveryEstimateArgs = {body: CartDeliveryEstimateRequest}; export type CreateApiApiV1DeliveryexperienceCartDeliveryEstimateOptions = {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 deliveryBehaviorOverrides = z.object({saturdayDeliveryBehavior: z.enum(["ALLOW", "NON_DELIVERY_DAY", "NON_TRANSIT_NON_DELIVERY_DAY"]).optional(), sundayDeliveryBehavior: z.enum(["ALLOW", "NON_DELIVERY_DAY", "NON_TRANSIT_NON_DELIVERY_DAY"]).optional(), holidayDeliveryBehavior: z.enum(["ALLOW", "NON_DELIVERY_DAY", "NON_TRANSIT_NON_DELIVERY_DAY"]).optional()}); export const cartOriginShippingOptions = z.object({shipOption: z.enum(["NotProvided", "Standard", "StandardPlus", "SameDay", "NextDay", "Overnight", "PriorityOvernight", "TwoDay", "TwoDayMorning", "ThreeDay", "International24Hour", "InternationalOneToThreeDayEarly", "InternationalOneToThreeDay", "InternationalTwoToFiveDay", "InternationalTwoToTenDay", "InternationalOneToTwoWeek", "InternationalOneToFourWeek", "DesiredDeliveryDate", "BusinessDaysOfTransit", "Unknown"]).optional(), fromCountryCode: z.string().optional(), fromPostalCode: z.string().optional(), originId: z.string().optional(), estimateSource: z.enum(["PartnerProvided", "ShipiumCalculated", "CarrierProvided", "Unknown"]).optional(), deliveryBehaviorOverrides: deliveryBehaviorOverrides.optional(), timeInTransitSetting: z.string().optional()}); export const cartOriginDeliveryEstimate = z.object({deliveryEstimateId: z.string(), shipEstimateDate: z.string(), shipEstimateDateUTC: z.string(), deliveryEstimateDate: z.string(), deliveryEstimateDateUTC: z.string(), customerOrderCutoff: z.string().optional(), customerOrderCutoffUTC: z.string().optional(), estimateSource: z.enum(["PartnerProvided", "ShipiumCalculated", "CarrierProvided", "Unknown"]), estimatedTimeInTransitDays: z.number().int(), shippingOrigin: shippingOrigin, shippingOptions: cartOriginShippingOptions}); export const referenceIdentifier = z.object({name: z.string().optional(), value: z.string().optional()}); export const cartDeliveryEstimateResponse = z.object({shipiumCartId: z.string(), partnerCartId: z.string(), customerCountryCode: z.string(), customerPostalCode: z.string(), requestDateOverride: z.string().optional(), shipDateOverride: z.string().optional(), productViewDeliveryEstimateIds: z.array(z.string()).optional(), originDeliveryEstimates: z.array(cartOriginDeliveryEstimate), referenceIdentifiers: z.array(referenceIdentifier).optional(), partnerReferenceIdentifier: z.string().optional(), partnerReferenceIdentifier2: z.string().optional(), shipmentTags: z.array(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 }; /** * Cart Delivery Date Estimate * * POST /api/v1/deliveryexperience/cartDeliveryEstimate */ export const createApiApiV1DeliveryexperienceCartDeliveryEstimate = async (args: CreateApiApiV1DeliveryexperienceCartDeliveryEstimateArgs, options?: CreateApiApiV1DeliveryexperienceCartDeliveryEstimateOptions) => { 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/cartDeliveryEstimate${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(cartDeliveryEstimateResponse, await res.json()) };