import {z} from 'zod' export type CartItemForUpdate = {id: number, quantity: number}; export type BulkModificationOperation = 'ADD' | 'CHANGE_QUANTITY' | 'DELETE' | 'SUBSTITUTE' | 'CREDIT_ONLY'; export type BulkModificationItem = {productId: number, quantity: number}; export type BulkModificationInternalReason = 'VENDOR' | 'CUSTOMER' | 'BUSINESS_PROCESS' | 'PICK_PACK' | 'PURCHASING' | 'EXPECTED' | 'OTHER'; export type BulkModificationNotificationMode = 'IMMEDIATE' | 'BATCH' | 'NONE'; export type CreateApiBulkmodificationtoolCartUpdateArgs = {body: {addons: Array, operation: BulkModificationOperation, newItems?: Array | undefined, startDate?: string | undefined, endDate?: string | undefined, internalReason?: (BulkModificationInternalReason | null) | undefined, internalReasonNote?: (string | null) | undefined, notifyUsers: BulkModificationNotificationMode, nextOccurrenceOnly?: boolean | undefined}}; export type CreateApiBulkmodificationtoolCartUpdateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const namedUrl = z.object({name: z.string().min(1), url: z.string().min(1)}); export const bulkModificationUpdateResult = z.object({addonsAffected: z.number().int(), progressLink: namedUrl}); 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 }; /** * POST /bulkmodificationtool/cart/update */ export const createApiBulkmodificationtoolCartUpdate = async (args: CreateApiBulkmodificationtoolCartUpdateArgs, options: CreateApiBulkmodificationtoolCartUpdateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/bulkmodificationtool/cart/update${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(bulkModificationUpdateResult, await res.json()) };