import {z} from 'zod' export type PriceType = 0 | 1; export type PriceRowStatus = 0 | 1 | 2; export type GetApiV1AccountsAccountIdPricesArgs = {accountId: number, ItemId?: number | undefined, Type?: PriceType | undefined, AsOf: string, status?: Array | undefined, Page?: number | undefined, PageSize?: number | undefined, SortColumn?: string | undefined, SortOrder?: string | undefined}; export type GetApiV1AccountsAccountIdPricesOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const pricelistRelation = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const priceType = z.union([z.literal(0), z.literal(1)]); export const itemPricelistResponse = z.object({id: z.number().int().optional(), itemId: z.number().int().optional(), itemCode: z.string().nullable().optional(), itemName: z.string().nullable().optional(), relation: pricelistRelation.optional(), relationId: z.number().int().optional(), relationName: z.string().nullable().optional(), relationCode: z.string().nullable().optional(), type: priceType.optional(), price: z.number().optional(), unitId: z.string().nullable().optional(), quantity: z.number().int().optional(), fromDate: z.string().optional(), endDate: z.string().nullable().optional(), modifiedOn: z.string().nullable().optional(), rowVersion: z.string().nullable().optional()}); export const itemPricelistResponsePagedResult = z.object({pageSize: z.number().int().optional(), page: z.number().int().optional(), totalCount: z.number().int().optional(), filteredCount: z.number().int().optional(), data: z.array(itemPricelistResponse).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 }; /** * List one company's price rows across items, paged. Purchase rows are hidden per non-owned item. * `asOf` is required and `status` selects which lifecycles to return; see * IO.Catalog.Contracts.DTO.Pricelists.AccountPricesListRequest for every parameter. * * GET /v1/Accounts/{accountId}/prices */ export const getApiV1AccountsAccountIdPrices = async (args: GetApiV1AccountsAccountIdPricesArgs, options: GetApiV1AccountsAccountIdPricesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ItemId != null) query.append("ItemId", String(args.ItemId)) if (args.Type != null) query.append("Type", String(args.Type)) if (args.AsOf != null) query.append("AsOf", String(args.AsOf)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.SortColumn != null) query.append("SortColumn", String(args.SortColumn)) if (args.SortOrder != null) query.append("SortOrder", String(args.SortOrder)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Accounts/${encodeURIComponent(String(args.accountId))}/prices${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(itemPricelistResponsePagedResult, await res.json()) };