import {z} from 'zod' export type LinearDimensionSchema = {linearUnit: 'in' | 'cm', length: number, height: number, width: number}; export type WeightSchema = {weight: number, weightUnit: 'lb' | 'kg' | 'g' | 'oz'}; export type ProductSchema = {productLinearDimensions: LinearDimensionSchema, productWeight: WeightSchema, quantity: number, productId: string, productReferenceIdentifier?: string | undefined, productDetails: Array | null, productCompressionFactor?: ({compressionFactor?: number | undefined, compressHeight?: boolean | undefined, compressWidth?: boolean | undefined, compressDepth?: boolean | undefined} | null) | undefined}; export type PartnerPackagingSchema = {linearDimensions: LinearDimensionSchema, maxContentWeight: WeightSchema, packagingWeight: WeightSchema, packagingMaterial: 'box' | 'envelope' | 'flat_pack', packagingSizeName: string, packagingTypeId?: string | undefined, unsupportedProductDetails?: (Array | null) | undefined}; export type PackingStrategySchema = {packingAlgorithm?: ('standard' | 'volumetric') | undefined, packingAlgorithmOptions?: ({volumetricPackingEfficiency?: number | undefined, pureVolumetricPacking?: (boolean | null) | undefined} | null) | undefined} | null; export type CreateApiApiV1PackagingPlannerArgs = {body: {includeDetails?: (boolean | null) | undefined, items: Array, packagingIds?: (Array | null) | undefined, packagingSetId?: (string | null) | undefined, packaging: Array | null, packingStrategyOptions?: PackingStrategySchema | undefined, maxItemsPerPackage?: (number | null) | undefined, referenceIdentifier?: string | undefined, referenceIdentifier2?: string | undefined, referenceIdentifier3?: string | undefined, referenceIdentifier4?: string | undefined, referenceIdentifier5?: string | undefined} | {includeDetails?: (boolean | null) | undefined, items: Array, packagingIds?: (Array | null) | undefined, packagingSetId: string | null, packaging?: (Array | null) | undefined, packingStrategyOptions?: PackingStrategySchema | undefined, maxItemsPerPackage?: (number | null) | undefined, referenceIdentifier?: string | undefined, referenceIdentifier2?: string | undefined, referenceIdentifier3?: string | undefined, referenceIdentifier4?: string | undefined, referenceIdentifier5?: string | undefined} | {includeDetails?: (boolean | null) | undefined, items: Array, packagingIds: Array | null, packagingSetId?: (string | null) | undefined, packaging?: (Array | null) | undefined, packingStrategyOptions?: PackingStrategySchema | undefined, maxItemsPerPackage?: (number | null) | undefined, referenceIdentifier?: string | undefined, referenceIdentifier2?: string | undefined, referenceIdentifier3?: string | undefined, referenceIdentifier4?: string | undefined, referenceIdentifier5?: string | undefined}}; export type CreateApiApiV1PackagingPlannerOptions = {baseUrl?: string | undefined, oauth2Scheme?: string | undefined, fetch?: typeof fetch | undefined}; export const linearDimensionSchema = z.object({linearUnit: z.enum(["in", "cm"]), length: z.number().gte(0), height: z.number().gte(0), width: z.number().gte(0)}); export const weightSchema = z.object({weight: z.number(), weightUnit: z.enum(["lb", "kg", "g", "oz"])}); export const productResponseSchema = z.object({productLinearDimensions: linearDimensionSchema.optional(), productWeight: weightSchema.optional(), quantity: z.number(), productId: z.string(), productReferenceIdentifier: z.string().optional(), productDetails: z.array(z.string()).optional(), productCompressionFactor: z.object({compressionFactor: z.number().optional(), compressHeight: z.boolean().optional(), compressWidth: z.boolean().optional(), compressDepth: z.boolean().optional()}).nullable().optional()}); export const binPackingPackageSchema = z.object({linearDimensions: linearDimensionSchema.optional(), maxContentWeight: weightSchema.optional(), packagingWeight: weightSchema.optional(), packagingMaterial: z.enum(["box", "envelope", "flat_pack"]).optional(), packagePlanPackageId: z.string().optional(), packagingTypeId: z.string().optional(), packagingSizeName: z.string().optional(), packageEstimatedWeight: weightSchema.optional(), volumetricPackEfficiency: z.number().optional(), maxItemsPerPackage: z.number().optional(), packageContents: z.array(productResponseSchema).nullable().optional()}); export const packingStrategySchema = z.object({packingAlgorithm: z.enum(["standard", "volumetric"]).optional(), packingAlgorithmOptions: z.object({volumetricPackingEfficiency: z.number().optional(), pureVolumetricPacking: z.boolean().nullable().optional()}).nullable().optional()}).nullable(); export const createApiApiV1PackagingPlannerResponse = z.object({packageCount: z.number().optional(), packagePlanId: z.string().optional(), unpackableItems: z.array(productResponseSchema).nullable().optional(), packages: z.array(binPackingPackageSchema).optional(), volumetricPackEfficiency: z.number().optional(), packingStrategyOptions: packingStrategySchema.optional(), maxItemsPerPackage: z.number().nullable().optional(), referenceIdentifier: z.string().optional(), referenceIdentifier2: z.string().optional(), referenceIdentifier3: z.string().optional(), referenceIdentifier4: z.string().optional(), referenceIdentifier5: 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 }; /** * POST /api/v1/packaging/planner */ export const createApiApiV1PackagingPlanner = async (args: CreateApiApiV1PackagingPlannerArgs, options?: CreateApiApiV1PackagingPlannerOptions) => { 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/packaging/planner${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(createApiApiV1PackagingPlannerResponse, await res.json()) };