import {z} from 'zod' export type GetApiInvoicesArgs = {nextToken?: string | undefined, status?: string | undefined, owner_id?: string | undefined, customer_id?: string | undefined, pageSize?: number | undefined, start_time?: number | undefined, end_time?: number | undefined}; export type GetApiInvoicesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoicesStatus = z.enum(["DRAFT", "DUE", "PAID", "VOID", "UN_COLLECTIBLE", "REFUND_INITIATED", "REFUND_COMPLETED", "MERGED", "PARTIALLY_PAID"]); export const invoicesClass = z.enum(["INVOICE", "ORDER", "BILLABLE"]); export const invoicesType = z.enum(["STANDARD", "AD_HOC", "CUSTOM", "COMPOSITE", "STANDARD_ADVANCED", "ADHOC_ADVANCED"]); export const address = z.object({phoneNumber: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), postalCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional()}); export const invoiceDetails = z.object({customer: z.object({name: z.string(), primaryEmail: z.string(), billingAddress: z.string().optional(), address: address.optional()}).optional(), account: z.object({name: z.string(), invoiceCurrency: z.string(), address: address, primaryEmail: z.string()}).optional(), pricePlanName: z.string().optional(), invoiceGroup: z.object({id: z.string(), name: z.string(), email: z.string().optional(), dailyInvoiceConsolidation: z.boolean(), invoiceCurrency: z.string(), address: address}).optional(), organization: z.object({businessName: z.string(), address: address, primaryEmail: z.string(), metadata: z.record(z.string(), z.string()).optional()}).optional(), logoUrl: z.string().optional()}); export const invoiceSummary = z.object({id: z.string().max(50), customerId: z.string().optional(), ownerId: z.string().optional(), pricePlanId: z.string().optional(), status: invoicesStatus, finalizingStatus: z.enum(["FINALIZING", "FINALIZED"]).optional(), invoiceClass: invoicesClass, invoiceType: invoicesType, startDate: z.string().optional(), endDate: z.string().optional(), endDateInclusive: z.string().optional(), invoiceDate: z.string(), dueDate: z.string().optional(), generatedAt: z.string().optional(), updatedAt: z.string(), sequenceId: z.string().optional(), pdfUrl: z.string().optional(), totalAmount: z.number(), paidAmount: z.number(), invoiceDetails: invoiceDetails.optional(), netTermDays: z.number().int()}); export const listInvoicesResponse = z.object({data: z.array(invoiceSummary), nextToken: z.string().max(500).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 invoices * * GET /invoices */ export const getApiInvoices = async (args: GetApiInvoicesArgs, options?: GetApiInvoicesOptions) => { const {baseUrl = "https://api.togai.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.nextToken != null) query.append("nextToken", String(args.nextToken)) if (args.status != null) query.append("status", String(args.status)) if (args.owner_id != null) query.append("owner_id", String(args.owner_id)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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()) };