import {z} from 'zod' export type GetApiV1BillingOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionSettings = z.object({max_tokens: z.number().int().gt(0), max_apps: z.number().int(), llm_routing: z.boolean().optional(), prompts: z.boolean(), prompt_models: z.boolean(), personalized_llm_routing: z.boolean(), fine_tuning: z.boolean(), support_level: z.enum(["community", "all"]), customer_success: z.boolean(), max_tokens_readable: z.string(), max_apps_readable: z.string(), support_level_readable: z.string(), is_trial: z.boolean().optional(), is_none: z.boolean().optional(), tier: z.enum(["_NONE_", "_TRIAL_", "_INTERNAL_", "STARTUP", "GROWTH", "SCALE", "ENTERPRISE"]), name: z.string(), monthly_price_usd: z.number().nullable(), yearly_price_usd: z.number().nullable(), monthly_discounted_usd: z.number().nullable().optional(), yearly_discounted_usd: z.number().nullable().optional(), monthly_price_id: z.string().nullable(), yearly_price_id: z.string().nullable(), monthly_price_label: z.string().nullable().optional(), yearly_price_label: z.string().nullable().optional(), monthly_contact_sales: z.boolean().optional(), yearly_contact_sales: z.boolean().optional(), monthly_button_label: z.string().nullable().optional(), yearly_button_label: z.string().nullable().optional()}); export const subscriptionPausedInvoiceData = z.object({invoice_id: z.string(), invoice_number: z.string(), amount: z.number(), timestamp: z.number().int(), pause_reason: z.string(), invoice_name: z.string(), payment_link: z.string().nullable(), severity: z.union([z.literal(1), z.literal(2)])}); export const subscriptionPaused = z.object({invoices: z.array(subscriptionPausedInvoiceData)}); export const billingAndUsageInformation = z.object({spending_limit_soft: z.number().optional(), spending_limit_hard: z.number().optional(), currency: z.literal("usd").optional(), balance: z.number().optional(), last_invoice_date: z.number().int(), billing_zip: z.string().nullable(), subscription: subscriptionSettings, subscription_paused: subscriptionPaused.nullable(), trial_end: z.string().nullable().optional(), trial_ending_soon: z.boolean().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 }; /** * Get Org Balance And Subscription Details * * GET /v1/billing */ export const getApiV1Billing = async (options: GetApiV1BillingOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing${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(billingAndUsageInformation, await res.json()) };