import {z} from 'zod' export type GetApiOrganizationsOrganizationIDBillingInvoicesArgs = {cursor?: string | undefined, limit?: number | undefined}; export type GetApiOrganizationsOrganizationIDBillingInvoicesOptions = {baseUrl?: string | undefined, xata?: string | undefined, fetch?: typeof fetch | undefined}; export const billingInvoice = z.object({id: z.string(), invoice_number: z.string(), amount_due: z.number(), currency: z.string(), invoice_date: z.string(), status: z.enum(["draft", "issued", "paid", "void", "synced"]), invoice_pdf: z.string().nullable()}); export const paginationMetadata = z.object({has_more: z.boolean(), next_cursor: z.string().nullable()}); export const billingInvoicesResponse = z.object({data: z.array(billingInvoice), pagination_metadata: paginationMetadata}); 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 billing invoices * * GET /organizations/{organizationID}/billing/invoices */ export const getApiOrganizationsOrganizationIDBillingInvoices = async (args: GetApiOrganizationsOrganizationIDBillingInvoicesArgs, options?: GetApiOrganizationsOrganizationIDBillingInvoicesOptions) => { const {baseUrl = "https://api.xata.tech", fetch: fetchFn = fetch, xata} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (xata !== undefined) headers.set('authorization', `Bearer ${xata}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.organizationID))}/billing/invoices${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(billingInvoicesResponse, await res.json()) };