import {z} from 'zod' export type ReferenceIdentifier = {name?: string | undefined, value?: string | undefined}; export type ProductViewParameters = {customerCountryCode: string, customerPostalCode: string, requestDateOverride?: string | undefined, shipDateOverride?: string | undefined, referenceIdentifiers?: Array | undefined, partnerReferenceIdentifier?: string | undefined, partnerReferenceIdentifier2?: string | undefined, shipmentTags?: Array | undefined}; export type CreateApiApiV1DeliveryexperienceProductviewArgs = {body: ProductViewParameters}; export type CreateApiApiV1DeliveryexperienceProductviewOptions = {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 referenceIdentifier = z.object({name: z.string().optional(), value: z.string().optional()}); export const deliveryEstimate = z.object({deliveryEstimateId: z.string(), requestDate: 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(), customerCountryCode: z.string(), customerPostalCode: z.string(), shippingOrigin: shippingOrigin, requestDateOverride: z.string().optional(), shipDateOverride: z.string().optional(), 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 }; /** * Product View Delivery Estimate * * POST /api/v1/deliveryexperience/productview */ export const createApiApiV1DeliveryexperienceProductview = async (args: CreateApiApiV1DeliveryexperienceProductviewArgs, options?: CreateApiApiV1DeliveryexperienceProductviewOptions) => { 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/productview${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(deliveryEstimate, await res.json()) };