import {z} from 'zod' export type GetApiApiV2PurchasePricesArgs = {filter?: (Array<{key?: ('id' | 'product' | 'supplier' | 'supplierItemNumber' | 'currency' | 'fromQuantity' | 'isValidAt') | undefined, op?: ('equals' | 'notEquals' | 'in' | 'notIn' | 'contains' | 'notContains' | 'startsWith' | 'endsWith' | 'lessThan' | 'lessThanOrEquals' | 'greaterThan' | 'greaterThanOrEquals' | 'is') | undefined, value?: (string | Array) | undefined}>) | undefined, page?: {number: string, size: string} | undefined, order?: (Array<{field?: 'id' | undefined, dir?: ('asc' | 'desc') | undefined}>) | undefined}; export type GetApiApiV2PurchasePricesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV2PurchasePricesResponse = z.object({data: z.array(z.object({id: z.string().optional(), product: z.object({id: z.string()}).optional(), supplier: z.object({id: z.string()}).optional(), isStandardSupplier: z.boolean().optional(), supplierDesignation: z.string().optional(), supplierItemNumber: z.string().optional(), fromQuantity: z.number().optional(), packageAmount: z.number().nullable().optional(), price: z.object({amount: z.string().optional(), currency: z.enum(["EUR", "USD", "JPY", "BGN", "CZK", "DKK", "GBP", "HUF", "PLN", "RON", "SEK", "CHF", "ISK", "NOK", "HRK", "RUB", "TRY", "AUD", "BRL", "CAD", "CNY", "HKD", "IDR", "ILS", "INR", "KRW", "MXN", "MYR", "NZD", "PHP", "SGD", "THB", "ZAR", "ARS", "CLP", "SAR", "AED", "QAR"]).optional()}).optional(), internalComment: z.string().optional(), validFrom: z.string().nullable().optional(), expiresAt: z.string().nullable().optional()})).optional(), extra: z.object({totalCount: z.number().int(), page: z.object({number: z.number().int(), size: z.number().int()})}).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 }; /** * List purchase prices V2 * * GET /api/v2/purchasePrices */ export const getApiApiV2PurchasePrices = async (args: GetApiApiV2PurchasePricesArgs, options?: GetApiApiV2PurchasePricesOptions) => { const {baseUrl = "https://xentral.xentral.biz", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", args.filter.join(',')) if (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.order != null) query.append("order", args.order.join(',')) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/purchasePrices${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(getApiApiV2PurchasePricesResponse, await res.json()) };