import {z} from 'zod' export type PriceType = 0 | 1; export type GetApiV1ItemsItemIdEffectivePriceArgs = {itemId: number, priceAccountId?: number | undefined, accountId?: number | undefined, type: PriceType, asOf: string, quantity?: number | undefined}; export type GetApiV1ItemsItemIdEffectivePriceOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const priceSource = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const effectiveItemPriceResponse = z.object({price: z.number().optional(), unitId: z.string().nullable().optional(), quantity: z.number().int().optional(), source: priceSource.optional(), sourceId: z.number().int().nullable().optional(), validUntil: z.string().nullable().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 }; /** * Resolve the effective price for an item + account (company tier → base price) on a given date. * *
`asOf` is required, and is a System.DateOnly. It used to be an optional * System.DateTime, which made this the one place in the feature where the answer depended on * where the question was asked from — twice over. Omitted, the server substituted its own UTC date, so * a caller in UTC+3 asking at 01:00 local was answered for the previous day. Supplied with an offset, * query-string binding converted it to universal time, so `2026-08-01T00:00:00+03:00` resolved * prices as of `2026-07-31T21:00Z` — again the wrong calendar day, and again only east of UTC. * A calendar date has no instant to convert, and requiring it leaves the caller as the only party who * decides what "today" means. * * GET /v1/Items/{itemId}/effective-price */ export const getApiV1ItemsItemIdEffectivePrice = async (args: GetApiV1ItemsItemIdEffectivePriceArgs, options: GetApiV1ItemsItemIdEffectivePriceOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.priceAccountId != null) query.append("priceAccountId", String(args.priceAccountId)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.type != null) query.append("type", String(args.type)) if (args.asOf != null) query.append("asOf", String(args.asOf)) if (args.quantity != null) query.append("quantity", String(args.quantity)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Items/${encodeURIComponent(String(args.itemId))}/effective-price${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(effectiveItemPriceResponse, await res.json()) };