import {z} from 'zod' export type ProductFamily = {id?: number | undefined, label?: (string | null) | undefined, number?: (string | null) | undefined}; export type ProductType = 'Product' | 'Service' | 'Text'; export type UnitType = 'Time' | 'Weight' | 'Area' | 'Volume' | 'Dimension' | 'Other'; export type Unit = {code?: (string | null) | undefined, conversion?: number | undefined, decimals?: (number | null) | undefined, id?: (number | null) | undefined, name?: (string | null) | undefined, type?: UnitType | undefined}; export type VatRegion = 'FR' | 'EU' | 'IE' | 'DOM' | 'Corse' | 'Monaco'; export type Vat = {default?: boolean | undefined, id?: (number | null) | undefined, label?: (string | null) | undefined, rate?: (number | null) | undefined, region?: VatRegion | undefined}; export type Product = {active?: boolean | undefined, defaultQuantity?: number | undefined, description?: (string | null) | undefined, family?: ProductFamily | undefined, id?: number | undefined, imageUrl?: (string | null) | undefined, internalId?: (string | null) | undefined, internalNotes?: (string | null) | undefined, isUnitPriceTaxIncluded?: boolean | undefined, margin?: number | undefined, marginRate?: number | undefined, markupRate?: number | undefined, metadata?: ({[key: string]: string | null} | null) | undefined, name?: (string | null) | undefined, number?: (string | null) | undefined, purchasePrice?: number | undefined, taxExcludedPrice?: number | undefined, taxIncludedPrice?: number | undefined, type?: ProductType | undefined, unit?: Unit | undefined, unitPrice?: number | undefined, vat?: Vat | undefined}; export type CreateApiV1ProductsArgs = {body: Product}; export type CreateApiV1ProductsOptions = {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 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 }; /** * Create a new product * * POST /v1/products */ export const createApiV1Products = async (args: CreateApiV1ProductsArgs, options?: CreateApiV1ProductsOptions) => { const {baseUrl = "https://api.altoviz.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/products${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(product, await res.json()) };