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 BusinessUnitType = 'Company' | 'Division'; export type BusinessUnitStatus = 'Active' | 'Inactive'; 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 AssociateRoleKeyReference = {key: string, typeId: ReferenceType}; export type AssociateRoleInheritanceMode = 'Enabled' | 'Disabled'; export type AssociateRoleAssignmentDraft = {associateRole: AssociateRoleKeyReference, inheritance: AssociateRoleInheritanceMode}; export type AssociateDraft = {customer: CustomerKeyReference, associateRoleAssignments: Array}; export type CountryCode = string; 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 StoreKeyReference = {key: string, typeId: ReferenceType}; export type BusinessUnitImport = {unitType: BusinessUnitType, key: string, name: string, status?: BusinessUnitStatus | undefined, contactEmail?: string | undefined, associates?: Array | undefined, addresses?: Array
| undefined, shippingAddresses?: Array | undefined, defaultShippingAddress?: number | undefined, billingAddresses?: Array | undefined, defaultBillingAddress?: number | undefined, stores?: Array | undefined, custom?: Custom | undefined}; export type BusinessUnitImportRequest = {type: ImportResourceType, resources: Array}; export type CreateApiProjectKeyBusinessUnitsImportContainersImportContainerKeyArgs = {body: BusinessUnitImportRequest}; export type CreateApiProjectKeyBusinessUnitsImportContainersImportContainerKeyOptions = {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 Business Units. * * POST /{projectKey}/business-units/import-containers/{importContainerKey} */ export const createApiProjectKeyBusinessUnitsImportContainersImportContainerKey = async (args: CreateApiProjectKeyBusinessUnitsImportContainersImportContainerKeyArgs, options?: CreateApiProjectKeyBusinessUnitsImportContainersImportContainerKeyOptions) => { 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))}/business-units/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()) };