import {z} from 'zod' export type ImportResourceType = 'business-unit' | 'category' | 'customer' | 'discount-code' | 'inventory' | 'order' | 'order-patch' | 'price' | 'product' | 'product-draft' | 'product-selection' | 'product-tailoring' | 'product-type' | 'product-variant' | 'product-variant-patch' | 'standalone-price' | 'variant' | 'type'; export type ReturnShipmentState = 'Advised' | 'Returned' | 'BackInStock' | 'Unusable'; export type ReturnItemDraft = {quantity: number, lineItemId?: string | undefined, customLineItemId?: string | undefined, comment?: string | undefined, shipmentState: ReturnShipmentState}; export type ReturnInfo = {items: Array, returnTrackingId?: string | undefined, returnDate?: unknown | undefined}; export type ParcelMeasurements = {heightInMillimeter?: number | undefined, lengthInMillimeter?: number | undefined, widthInMillimeter?: number | undefined, weightInGram?: number | undefined}; export type TrackingData = {trackingId?: string | undefined, carrier?: string | undefined, provider?: string | undefined, providerTransaction?: string | undefined, isReturn?: boolean | undefined}; export type DeliveryItem = {id: string, quantity: number}; export type DeliveryParcel = {deliveryId: string, measurements?: ParcelMeasurements | undefined, trackingData?: TrackingData | undefined, items?: Array | undefined}; export type CountryCode = string; export type ReferenceType = 'associate-role' | 'business-unit' | 'cart' | 'cart-discount' | 'category' | 'channel' | 'customer' | 'customer-group' | 'discount-code' | 'key-value-document' | 'order' | 'payment' | 'price' | 'product' | 'product-discount' | 'product-type' | 'product-variant' | 'variant' | 'shipping-method' | 'state' | 'store' | 'tax-category' | 'type'; export type TypeKeyReference = {key: string, typeId: ReferenceType}; export type FieldContainer = {[key: string]: string}; export type Custom = {type: TypeKeyReference, fields?: FieldContainer | undefined}; export type Address = {id?: string | undefined, key?: string | undefined, title?: string | undefined, salutation?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, streetName?: string | undefined, streetNumber?: string | undefined, additionalStreetInfo?: string | undefined, postalCode?: string | undefined, city?: string | undefined, region?: string | undefined, state?: string | undefined, country: CountryCode, company?: string | undefined, department?: string | undefined, building?: string | undefined, apartment?: string | undefined, pOBox?: string | undefined, phone?: string | undefined, mobile?: string | undefined, email?: string | undefined, fax?: string | undefined, additionalAddressInfo?: string | undefined, externalId?: string | undefined, custom?: Custom | undefined}; export type DeliveryParcelDraft = {measurements?: ParcelMeasurements | undefined, trackingData?: TrackingData | undefined, items?: Array | undefined}; export type DeliveryDraft = {items: Array, address?: Address | undefined, parcels: Array}; export type RemoveDeliveryDraft = {id: string}; export type RemoveParcelFromDeliveryDraft = {parcelId: string}; export type DeliveryAddressDraft = {deliveryId: string, address?: Address | undefined}; export type ParcelMeasurementDraft = {parcelId: string, measurements?: ParcelMeasurements | undefined}; export type ParcelTrackingData = {parcelId: string, trackingData?: TrackingData | undefined}; export type ParcelItems = {parcelId: string, items?: Array | undefined}; export type OrderField = {addReturnInfo?: ReturnInfo | undefined, addParcelToDelivery?: DeliveryParcel | undefined, addDeliveries?: Array | undefined, removeDelivery?: RemoveDeliveryDraft | undefined, removeParcelFromDelivery?: RemoveParcelFromDeliveryDraft | undefined, setDeliveryAddress?: DeliveryAddressDraft | undefined, setParcelMeasurements?: ParcelMeasurementDraft | undefined, setParcelTrackingData?: ParcelTrackingData | undefined, setParcelItems?: Array | undefined}; export type OrderPatchImport = {orderNumber: string, fields: OrderField}; export type OrderPatchImportRequest = {type: ImportResourceType, patches: Array}; export type CreateApiProjectKeyOrderPatchesImportContainersImportContainerKeyArgs = {body: OrderPatchImportRequest}; export type CreateApiProjectKeyOrderPatchesImportContainersImportContainerKeyOptions = {baseUrl?: string | undefined, oauth20?: string | undefined, fetch?: typeof fetch | undefined}; export const importOperationState = z.enum(["processing", "validationFailed"]); export const errorObject = z.object({code: z.string(), message: z.string()}); export const importOperationStatus = z.object({operationId: z.string().optional(), state: importOperationState, errors: z.array(errorObject).optional()}); export const importResponse = z.object({operationStatus: z.array(importOperationStatus)}); 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 }; /** * Creates an Import Request for updating Orders. * * POST /{projectKey}/order-patches/import-containers/{importContainerKey} */ export const createApiProjectKeyOrderPatchesImportContainersImportContainerKey = async (args: CreateApiProjectKeyOrderPatchesImportContainersImportContainerKeyArgs, options?: CreateApiProjectKeyOrderPatchesImportContainersImportContainerKeyOptions) => { const {baseUrl = "https://import.europe-west1.gcp.commercetools.com", fetch: fetchFn = fetch, oauth20} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth20 !== undefined) headers.set('authorization', `Bearer ${oauth20}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.projectKey))}/order-patches/import-containers/${encodeURIComponent(String(args.importContainerKey))}${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(importResponse, await res.json()) };