import {z} from 'zod' export type ProductService = {productStructureType?: (string | null) | undefined, type: number, code?: string | undefined, name: string, sellDescription?: string | undefined, sellPrice?: number | undefined, sellVatType?: number | undefined, unitName?: string | undefined, categoryName?: string | undefined, barcode?: string | undefined}; export type ProductNonInventory = {productStructureType?: (string | null) | undefined, type: number, code?: string | undefined, name: string, sellDescription?: string | undefined, sellPrice?: number | undefined, sellVatType?: number | undefined, unitName?: string | undefined, categoryName?: string | undefined, barcode?: string | undefined, buyDescription?: string | undefined, buyPrice?: number | undefined, buyVatType?: number | undefined}; export type ProductInventory = {productStructureType?: (string | null) | undefined, type: number, code?: string | undefined, name: string, sellDescription?: string | undefined, sellPrice?: number | undefined, sellVatType?: number | undefined, unitName: string, categoryName?: string | undefined, barcode?: string | undefined, buyDescription?: string | undefined, buyPrice?: number | undefined, buyVatType?: number | undefined}; export type ProductInventoryBalance = {productStructureType?: (string | null) | undefined, type: number, code?: string | undefined, name: string, sellDescription?: string | undefined, sellPrice?: number | undefined, sellVatType?: number | undefined, unitName: string, categoryName?: string | undefined, barcode?: string | undefined, buyDescription?: string | undefined, buyPrice?: number | undefined, buyVatType?: number | undefined, inventoryPublishedOn: string, inventoryQuantity: number, inventoryPrice: number}; export type ProductType = ProductService | ProductNonInventory | ProductInventory | ProductInventoryBalance; export type UpdateApiProductsIdArgs = {Authorization: string, id: string, body: ProductType}; export type UpdateApiProductsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const product = z.object({id: z.string().optional(), type: z.number().int().optional(), code: z.string().optional(), name: z.string().optional(), sellDescription: z.string().optional(), sellPrice: z.number().optional(), sellPriceWithVat: z.number().optional(), sellVatType: z.number().int().optional(), unitName: z.string().optional(), categoryId: z.number().int().optional(), categoryName: z.string().optional(), barcode: z.string().optional(), buyDescription: z.string().optional(), buyPrice: z.number().optional(), buyPriceWithVat: z.number().optional(), buyVatType: z.number().int().optional(), inventoryPublishedOn: z.string().optional(), inventoryQuantity: z.number().optional(), inventoryPrice: z.number().optional(), inventoryTotal: z.number().optional(), averageBuyPrice: z.number().optional(), averageSellPrice: z.number().optional(), remainingStock: z.number().optional(), totalValueInHand: z.number().optional(), isMainProduct: z.boolean().optional()}); export const productResponse = z.object({status: z.boolean().optional(), message: z.string().optional(), code: z.number().int().optional(), data: z.object({total: z.number().int().optional(), currentPage: z.number().int().optional(), list: z.array(product).optional(), isDB: z.boolean().optional()}).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 }; /** * Update products. * * PUT /products/{id} */ export const updateApiProductsId = async (args: UpdateApiProductsIdArgs, options?: UpdateApiProductsIdOptions) => { const {baseUrl = "https://openapi.flowaccount.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/products/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(productResponse, await res.json()) };