import {z} from 'zod' export type GetApiV1ProductsArgs = {PageIndex?: number | undefined, PageSize?: number | undefined, OrderBy?: string | undefined, query?: string | undefined, familyId?: string | undefined}; export type GetApiV1ProductsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const productFamily = z.object({id: z.number().int().optional(), label: z.string().nullable().optional(), number: z.string().nullable().optional()}); export const productType = z.enum(["Product", "Service", "Text"]); export const unitType = z.enum(["Time", "Weight", "Area", "Volume", "Dimension", "Other"]); export const unit = z.object({code: z.string().nullable().optional(), conversion: z.number().optional(), decimals: z.number().nullable().optional(), id: z.number().int().nullable().optional(), name: z.string().nullable().optional(), type: unitType.optional()}); export const vatRegion = z.enum(["FR", "EU", "IE", "DOM", "Corse", "Monaco"]); export const vat = z.object({default: z.boolean().optional(), id: z.number().int().nullable().optional(), label: z.string().nullable().optional(), rate: z.number().nullable().optional(), region: vatRegion.optional()}); export const product = z.object({active: z.boolean().optional(), defaultQuantity: z.number().optional(), description: z.string().nullable().optional(), family: productFamily.optional(), id: z.number().int().optional(), imageUrl: z.string().nullable().optional(), internalId: z.string().nullable().optional(), internalNotes: z.string().nullable().optional(), isUnitPriceTaxIncluded: z.boolean().optional(), margin: z.number().optional(), marginRate: z.number().optional(), markupRate: z.number().optional(), metadata: z.record(z.string(), z.string().nullable()).nullable().optional(), name: z.string().nullable().optional(), number: z.string().nullable().optional(), purchasePrice: z.number().optional(), taxExcludedPrice: z.number().optional(), taxIncludedPrice: z.number().optional(), type: productType.optional(), unit: unit.optional(), unitPrice: z.number().optional(), vat: vat.optional()}); export const getApiV1ProductsResponse = z.array(product); 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 products * * GET /v1/products */ export const getApiV1Products = async (args: GetApiV1ProductsArgs, options?: GetApiV1ProductsOptions) => { const {baseUrl = "https://api.altoviz.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.PageIndex != null) query.append("PageIndex", String(args.PageIndex)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.OrderBy != null) query.append("OrderBy", String(args.OrderBy)) if (args.query != null) query.append("query", String(args.query)) if (args.familyId != null) query.append("familyId", String(args.familyId)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/products${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(getApiV1ProductsResponse, await res.json()) };