import {z} from 'zod' export type GetApiInvoicesInvoiceIdInvoiceItemsArgs = {invoice_id: unknown, page?: number | undefined, per_page?: number | undefined, sort?: Array | undefined, order?: ('asc' | 'desc') | undefined, 'q[]'?: Array | undefined}; export type GetApiInvoicesInvoiceIdInvoiceItemsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const invoiceItem = z.object({archived_at: z.string().nullable().optional(), billable_item: z.object({links: z.object({self: z.string().optional()}).optional()}).optional(), code: z.string().max(100).nullable().optional(), concession_type_name: z.string().nullable().optional(), created_at: z.string().optional(), deleted_at: z.string().nullable().optional(), discount_percentage: z.string().nullable().optional(), discounted_amount: z.string().nullable().optional(), id: z.string().optional(), invoice: z.object({links: z.object({self: z.string().optional()}).optional()}).optional(), is_monetary_discount: z.boolean().nullable().optional(), links: z.object({self: z.string().optional()}).optional(), name: z.string().nullable().optional(), net_price: z.string().nullable().optional(), product: z.object({links: z.object({self: z.string().optional()}).optional()}).optional(), quantity: z.string().optional(), tax_amount: z.string().nullable().optional(), tax_name: z.string().nullable().optional(), tax_rate: z.string().nullable().optional(), total_including_tax: z.number().nullable().optional(), unit_price: z.string().optional(), updated_at: z.string().optional()}); export const getApiInvoicesInvoiceIdInvoiceItemsResponse = z.object({invoice_items: z.array(invoiceItem).optional(), total_entries: z.number().int().optional(), links: z.object({self: z.string().optional(), previous: z.string().optional(), next: z.string().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 }; /** * List invoice items for invoice * * GET /invoices/{invoice_id}/invoice_items */ export const getApiInvoicesInvoiceIdInvoiceItems = async (args: GetApiInvoicesInvoiceIdInvoiceItemsArgs, options?: GetApiInvoicesInvoiceIdInvoiceItemsOptions) => { const {baseUrl = "https://api.au1.cliniko.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort != null) query.append("sort", args.sort.join(',')) if (args.order != null) query.append("order", String(args.order)) for (const item of args['q[]'] ?? []) query.append("q[]", String(item)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoices/${encodeURIComponent(String(args.invoice_id))}/invoice_items${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(getApiInvoicesInvoiceIdInvoiceItemsResponse, await res.json()) };