import {z} from 'zod' export type UpdateApiV1InvoicesInvoiceIdProductsArgs = {invoice_id: string, products: string, 'products[0].id': string, 'products[0].quantity'?: number | undefined, 'products[0].sale_by_id'?: string | undefined}; export type UpdateApiV1InvoicesInvoiceIdProductsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV1InvoicesInvoiceIdProductsResponse = z.object({invoice_id: z.string().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 }; /** * Add a product to an invoice * * PUT /v1/invoices/{invoice_id}/products */ export const updateApiV1InvoicesInvoiceIdProducts = async (args: UpdateApiV1InvoicesInvoiceIdProductsArgs, options?: UpdateApiV1InvoicesInvoiceIdProductsOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.products != null) query.append("products", String(args.products)) if (args['products[0].id'] != null) query.append("products[0].id", String(args['products[0].id'])) if (args['products[0].quantity'] != null) query.append("products[0].quantity", String(args['products[0].quantity'])) if (args['products[0].sale_by_id'] != null) query.append("products[0].sale_by_id", String(args['products[0].sale_by_id'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/invoices/${encodeURIComponent(String(args.invoice_id))}/products${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiV1InvoicesInvoiceIdProductsResponse, await res.json()) };