import {z} from 'zod' export type CreateApiV1InvoicesProductsArgs = {center_id: string, guest_id: string, notes?: string | undefined, products: string, 'products[0].id'?: string | undefined, 'products[0].quantity'?: number | undefined, 'products[0].sale_by_id'?: string | undefined, application_name: string, application_version: string, created_by_id: string, body: {RAW_BODY?: unknown | undefined}}; export type CreateApiV1InvoicesProductsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1InvoicesProductsResponse = 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 }; /** * Create an invoice for products * * POST /v1/invoices/products */ export const createApiV1InvoicesProducts = async (args: CreateApiV1InvoicesProductsArgs, options?: CreateApiV1InvoicesProductsOptions) => { const {baseUrl = "https://api.zenoti.com", 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.center_id != null) query.append("center_id", String(args.center_id)) if (args.guest_id != null) query.append("guest_id", String(args.guest_id)) if (args.notes != null) query.append("notes", String(args.notes)) 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'])) if (args.application_name != null) headers.set("application_name", String(args.application_name)) if (args.application_version != null) headers.set("application_version", String(args.application_version)) if (args.created_by_id != null) query.append("created_by_id", String(args.created_by_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/invoices/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(createApiV1InvoicesProductsResponse, await res.json()) };