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 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 CustomerKeyReference = {key: string, typeId: ReferenceType}; export type ProductKeyReference = {key: string, typeId: ReferenceType}; export type LocalizedString = {[key: string]: string}; export type ProductVariantKeyReference = {key: string, typeId: ReferenceType}; export type MoneyType = 'centPrecision' | 'highPrecision'; export type CurrencyCode = string; export type TypedMoney = {type: MoneyType, fractionDigits?: number | undefined, centAmount: number, currencyCode: CurrencyCode}; export type CountryCode = string; export type CustomerGroupKeyReference = {key: string, typeId: ReferenceType}; export type ChannelKeyReference = {key: string, typeId: ReferenceType}; export type ProductDiscountKeyReference = {key: string, typeId: ReferenceType}; export type DiscountedPrice = {value: TypedMoney, discount: ProductDiscountKeyReference}; export type PriceTier = {minimumQuantity: number, value: TypedMoney}; export type TypeKeyReference = {key: string, typeId: ReferenceType}; export type FieldContainer = {[key: string]: string}; export type Custom = {type: TypeKeyReference, fields?: FieldContainer | undefined}; export type LineItemPrice = {value: TypedMoney, country?: CountryCode | undefined, validFrom?: unknown | undefined, validUntil?: unknown | undefined, customerGroup?: CustomerGroupKeyReference | undefined, channel?: ChannelKeyReference | undefined, discounted?: DiscountedPrice | undefined, tiers?: Array | undefined, custom?: Custom | undefined}; export type Attribute = {name?: string | undefined, type: string}; export type AssetDimensions = {w: number, h: number}; export type Image = {url: string, dimensions: AssetDimensions, label?: string | undefined}; export type LineItemProductVariantImportDraft = {productVariant?: ProductVariantKeyReference | undefined, sku?: string | undefined, prices?: Array | undefined, attributes?: Array | undefined, images?: Array | undefined}; export type StateKeyReference = {key: string, typeId: ReferenceType}; export type ItemState = {quantity: number, state: StateKeyReference}; export type SubRate = {name: string, amount: number}; export type TaxRate = {id?: string | undefined, name: string, amount: number, includedInPrice: boolean, country: CountryCode, state?: string | undefined, subRates?: Array | undefined}; export type ItemShippingTarget = {addressKey: string, quantity: number}; export type ItemShippingDetailsDraft = {targets: Array}; export type LineItemImportDraft = {product?: ProductKeyReference | undefined, name: LocalizedString, variant: LineItemProductVariantImportDraft, price: LineItemPrice, quantity: number, state?: Array | undefined, supplyChannel?: ChannelKeyReference | undefined, distributionChannel?: ChannelKeyReference | undefined, taxRate?: TaxRate | undefined, shippingDetails?: ItemShippingDetailsDraft | undefined, custom?: Custom | undefined}; export type CustomLineItemTaxedPrice = {totalNet: TypedMoney, totalGross: TypedMoney}; export type TaxCategoryKeyReference = {key: string, typeId: ReferenceType}; export type ExternalTaxRateDraft = {name: string, amount?: number | undefined, country: CountryCode, state?: string | undefined, subRates?: Array | undefined, includedInPrice?: boolean | undefined}; export type Money = {type: MoneyType, fractionDigits?: number | undefined, centAmount: number, currencyCode: CurrencyCode}; export type CartDiscountKeyReference = {key: string, typeId: ReferenceType}; export type DiscountedLineItemPortion = {discount: CartDiscountKeyReference, discountedAmount: Money}; export type DiscountedLineItemPriceDraft = {value: Money, includedDiscounts: Array}; export type CustomLineItemDraft = {name: LocalizedString, money: TypedMoney, taxedPrice?: CustomLineItemTaxedPrice | undefined, totalPrice: TypedMoney, slug: string, quantity: number, state?: Array | undefined, taxCategory?: TaxCategoryKeyReference | undefined, taxRate?: TaxRate | undefined, externalTaxRate?: ExternalTaxRateDraft | undefined, discountedPricePerQuantity?: Array | undefined, shippingDetails?: ItemShippingDetailsDraft | undefined}; export type TaxPortion = {name?: string | undefined, rate: number, amount: TypedMoney}; export type TaxedPrice = {totalNet: Money, totalGross: Money, taxPortions: Array}; 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 OrderState = 'Open' | 'Confirmed' | 'Complete' | 'Cancelled'; export type ShipmentState = 'Shipped' | 'Ready' | 'Pending' | 'Delayed' | 'Partial' | 'Backorder'; export type PaymentState = 'BalanceDue' | 'Failed' | 'Pending' | 'CreditOwed' | 'Paid'; export type ShippingRateTierType = 'CartValue' | 'CartClassification' | 'CartScore'; export type ShippingRatePriceTier = {type: ShippingRateTierType}; export type ShippingRateDraft = {price: Money, freeAbove?: Money | undefined, tiers?: Array | undefined}; export type ShippingMethodKeyReference = {key: string, typeId: ReferenceType}; export type DeliveryItem = {id: string, quantity: number}; 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 Parcel = {id: string, createdAt: unknown, measurements?: ParcelMeasurements | undefined, trackingData?: TrackingData | undefined, items?: Array | undefined, custom?: Custom | undefined}; export type Delivery = {id: string, createdAt: unknown, items: Array, parcels: Array, address?: Address | undefined}; export type ShippingMethodState = 'DoesNotMatchCart' | 'MatchesCart'; export type ShippingInfoImportDraft = {shippingMethodName: string, price: TypedMoney, shippingRate: ShippingRateDraft, taxRate?: TaxRate | undefined, taxCategory?: TaxCategoryKeyReference | undefined, shippingMethod?: ShippingMethodKeyReference | undefined, deliveries?: Array | undefined, discountedPrice?: DiscountedLineItemPriceDraft | undefined, shippingMethodState?: ShippingMethodState | undefined}; export type InventoryMode = 'TrackOnly' | 'ReserveOnOrder'; export type RoundingMode = 'HalfEven' | 'HalfUp' | 'HalfDown'; export type TaxCalculationMode = 'LineItemLevel' | 'UnitPriceLevel'; export type CartOrigin = 'Customer' | 'Merchant'; export type StoreKeyReference = {key: string, typeId: ReferenceType}; export type OrderImport = {orderNumber: string, customer?: CustomerKeyReference | undefined, customerEmail?: string | undefined, lineItems?: Array | undefined, customLineItems?: Array | undefined, totalPrice: TypedMoney, taxedPrice?: TaxedPrice | undefined, shippingAddress?: Address | undefined, billingAddress?: Address | undefined, customerGroup?: CustomerGroupKeyReference | undefined, country?: string | undefined, orderState?: OrderState | undefined, shipmentState?: ShipmentState | undefined, paymentState?: PaymentState | undefined, shippingInfo?: ShippingInfoImportDraft | undefined, completedAt?: unknown | undefined, custom?: Custom | undefined, inventoryMode?: InventoryMode | undefined, taxRoundingMode?: RoundingMode | undefined, taxCalculationMode?: TaxCalculationMode | undefined, origin?: CartOrigin | undefined, itemShippingAddresses?: Array
| undefined, store?: StoreKeyReference | undefined, state?: StateKeyReference | undefined}; export type OrderImportRequest = {type: ImportResourceType, resources: Array}; export type CreateApiProjectKeyOrdersImportContainersImportContainerKeyArgs = {body: OrderImportRequest}; export type CreateApiProjectKeyOrdersImportContainersImportContainerKeyOptions = {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 creating Orders. * * POST /{projectKey}/orders/import-containers/{importContainerKey} */ export const createApiProjectKeyOrdersImportContainersImportContainerKey = async (args: CreateApiProjectKeyOrdersImportContainersImportContainerKeyArgs, options?: CreateApiProjectKeyOrdersImportContainersImportContainerKeyOptions) => { 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))}/orders/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()) };