import {z} from 'zod' export type GetApiArticlePriceArgs = {page?: number | undefined, pageSize?: number | undefined, sort?: string | undefined}; export type GetApiArticlePriceOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const reductionAddition = z.object({id: z.string().optional(), version: z.string().optional(), createdDate: z.number().int().optional(), lastModifiedDate: z.number().int().optional(), name: z.string().optional(), type: z.enum(["ADDITIONAL_ABS", "ADDITION_PERCENT", "REDUCTION_ABS", "REDUCTION_PERCENT"]), value: z.string()}); export const standaloneSalesPrice = z.object({id: z.string().optional(), version: z.string().optional(), articleId: z.string(), createdDate: z.number().int().optional(), currencyId: z.string(), customerId: z.string().optional(), description: z.string().optional(), endDate: z.number().int().optional(), lastModifiedByUserId: z.string().optional(), lastModifiedDate: z.number().int().optional(), price: z.string(), priceScaleType: z.enum(["SCALE_FROM", "SCALE_TO"]), priceScaleValue: z.string(), reductionAdditions: z.array(reductionAddition).optional(), salesChannel: z.string(), startDate: z.number().int().optional()}); export const getApiArticlePriceResponse = z.object({result: z.array(standaloneSalesPrice).optional()}); 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 }; /** * query articlePrice * * GET /articlePrice */ export const getApiArticlePrice = async (args: GetApiArticlePriceArgs, options: GetApiArticlePriceOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.sort != null) query.append("sort", String(args.sort)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/articlePrice${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiArticlePriceResponse, await res.json()) };