import {z} from 'zod' export type BeezUPMarketplaceTechnicalCode = string; export type BeezUPMarketplaceAccountId = number; export type BeezUPOrderId = string; export type OrderIdentifier = {marketplaceTechnicalCode: BeezUPMarketplaceTechnicalCode, accountId: BeezUPMarketplaceAccountId, beezUPOrderId: BeezUPOrderId}; export type ClearMerchantOrderInfoListRequest = {orders: Array}; export type CreateApiOrdersV3BatchesClearMerchantOrderInfosArgs = {testMode?: boolean | undefined, body: ClearMerchantOrderInfoListRequest}; export type CreateApiOrdersV3BatchesClearMerchantOrderInfosOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const beezUPMarketplaceTechnicalCode = z.string(); export const beezUPMarketplaceAccountId = z.number().int(); export const beezUPOrderId = z.string(); export const orderIdentifier = z.object({marketplaceTechnicalCode: beezUPMarketplaceTechnicalCode, accountId: beezUPMarketplaceAccountId, beezUPOrderId: beezUPOrderId}); export const docUrl = z.string(); export const errorCode = z.string(); export const cultureName = z.string(); export const userErrorMessageArguments = z.record(z.string(), z.object({})); export const userErrorMessage = z.object({docUrl: docUrl.optional(), code: errorCode, message: z.string(), cultureName: cultureName.optional(), arguments: userErrorMessageArguments.optional()}); export const executionUUID = z.string(); export const orderOperationResponse = z.object({order: orderIdentifier, success: z.boolean(), status: z.number().int(), errors: z.array(userErrorMessage).optional(), executionUUID: executionUUID.optional()}); export const batchOrderOperationResponse = z.object({operations: z.array(orderOperationResponse)}); 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 }; /** * Send a batch of operations to clear an Order's merchant information (max 100 items per call) * * POST /orders/v3//batches/clearMerchantOrderInfos */ export const createApiOrdersV3BatchesClearMerchantOrderInfos = async (args: CreateApiOrdersV3BatchesClearMerchantOrderInfosArgs, options?: CreateApiOrdersV3BatchesClearMerchantOrderInfosOptions) => { const {baseUrl = "https://api.beezup.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.testMode != null) query.append("testMode", String(args.testMode)) const url = `${baseUrl.replace(/\/+$/, '')}/orders/v3//batches/clearMerchantOrderInfos${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(batchOrderOperationResponse, await res.json()) };