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 LocalizedString = {[key: string]: string}; export type ProductSelectionMode = 'Individual' | 'IndividualExclusion'; 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 ProductKeyReference = {key: string, typeId: ReferenceType}; export type VariantSelectionType = 'includeOnly' | 'includeAllExcept'; export type VariantSelection = {type: VariantSelectionType, skus?: Array | undefined}; export type VariantExclusion = {skus?: Array | undefined}; export type ProductSelectionAssignment = {product: ProductKeyReference, variantSelection?: VariantSelection | undefined, variantExclusion?: VariantExclusion | undefined}; export type ProductSelectionImport = {key: string, name: LocalizedString, mode?: ProductSelectionMode | undefined, custom?: Custom | undefined, assignments?: Array | undefined}; export type ProductSelectionImportRequest = {type: ImportResourceType, resources: Array}; export type CreateApiProjectKeyProductSelectionsImportContainersImportContainerKeyArgs = {body: ProductSelectionImportRequest}; export type CreateApiProjectKeyProductSelectionsImportContainersImportContainerKeyOptions = {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 Product Selections. * * POST /{projectKey}/product-selections/import-containers/{importContainerKey} */ export const createApiProjectKeyProductSelectionsImportContainersImportContainerKey = async (args: CreateApiProjectKeyProductSelectionsImportContainersImportContainerKeyArgs, options?: CreateApiProjectKeyProductSelectionsImportContainersImportContainerKeyOptions) => { 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))}/product-selections/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()) };