import {z} from 'zod' export type GetApiV1InvoicesArgs = {id: string, pageSize?: number | undefined, startingAfter?: string | undefined, status?: string | undefined}; export type GetApiV1InvoicesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoice = z.object({amountDue: z.string().optional(), amountPaid: z.string().optional(), amountRemaining: z.string().optional(), autoAdvance: z.boolean().optional(), billingReason: z.string().optional(), created: z.string().optional(), currency: z.string().optional(), description: z.string().optional(), dueDate: z.string().optional(), hostedInvoiceUrl: z.string().optional(), id: z.string().optional(), number: z.string().optional(), paidDate: z.string().optional(), periodEnd: z.string().optional(), periodStart: z.string().optional(), status: z.string().optional(), subtotal: z.string().optional(), subtotalExcludingTax: z.string().optional(), tax: z.string().optional(), total: z.string().optional(), totalExcludingTax: z.string().optional()}); export const listInvoicesResponse = z.object({hasMore: z.boolean(), invoices: z.array(invoice)}); 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 invoices * * GET /v1/invoices */ export const getApiV1Invoices = async (args: GetApiV1InvoicesArgs, options?: GetApiV1InvoicesOptions) => { const {baseUrl = "https://rest.compute.cudo.org", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.startingAfter != null) query.append("startingAfter", String(args.startingAfter)) if (args.status != null) query.append("status", String(args.status)) if (bearerAuth !== undefined) headers.set("Authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(listInvoicesResponse, await res.json()) };