import {z} from 'zod' export type GetApiApiV1BillingPaymentsArgs = {page?: number | undefined, limit?: number | undefined}; export type GetApiApiV1BillingPaymentsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const payment = z.object({id: z.string(), status: z.enum(["draft", "pending", "paid", "refunded", "partially_refunded", "void"]), paid: z.boolean(), created_at: z.string(), subtotal_amount: z.number().int(), discount_amount: z.number().int(), tax_amount: z.number().int(), total_amount: z.number().int(), refunded_amount: z.number().int(), currency: z.string(), billing_reason: z.string(), description: z.string().nullable(), invoice_number: z.string().nullable(), invoice_available: z.boolean(), product_name: z.string().nullable(), plan: z.enum(["free", "developer", "pro", "enterprise"]).nullable()}); export const paymentsResponse = z.object({payments: z.array(payment), pagination: z.object({page: z.number().int(), limit: z.number().int(), total_count: z.number().int(), max_page: z.number().int()})}); 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 org payment history * * GET /api/v1/billing/payments */ export const getApiApiV1BillingPayments = async (args: GetApiApiV1BillingPaymentsArgs, options: GetApiApiV1BillingPaymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = 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.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/billing/payments${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(paymentsResponse, await res.json()) };