import {z} from 'zod' export type GetApiApiV1FePlansPlanIdArgs = {planId: string, asOfDateTime?: string | undefined}; export type GetApiApiV1FePlansPlanIdOptions = {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 retrieve an existing plan. * * GET /api/v1/fe/plans/{planId} */ export const getApiApiV1FePlansPlanId = async (args: GetApiApiV1FePlansPlanIdArgs, options?: GetApiApiV1FePlansPlanIdOptions) => { const {baseUrl = "https://api.shipium.com", fetch: fetchFn = fetch, oauth2Scheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.asOfDateTime != null) query.append("asOfDateTime", String(args.asOfDateTime)) if (oauth2Scheme !== undefined) headers.set('authorization', `Bearer ${oauth2Scheme}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/fe/plans/${encodeURIComponent(String(args.planId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };