UNPKG

@teerai/sdk

Version:

Official TypeScript SDK for Teer API - Track and manage AI model usage

360 lines (353 loc) 9.57 kB
/** * Base class for all Teer errors */ declare class TeerError extends Error { constructor(message: string); } /** * Error thrown when the API returns an error response */ declare class TeerAPIError extends TeerError { readonly status: number; readonly statusText: string; readonly data: any; readonly headers: Headers; readonly request: { method: string; path: string; baseUrl: string; headers: HeadersInit; }; constructor({ message, status, statusText, data, headers, request, }: { message: string; status: number; statusText: string; data: any; headers: Headers; request: { method: string; path: string; baseUrl: string; headers: HeadersInit; }; }); /** * Whether this error is a server error (5xx) */ get isServerError(): boolean; /** * Whether this error is a client error (4xx) */ get isClientError(): boolean; /** * Whether this error is a rate limit error (429) */ get isRateLimitError(): boolean; } /** * Error thrown when a request times out */ declare class TeerTimeoutError extends TeerError { readonly timeoutMs: number; readonly request: { method: string; path: string; baseUrl: string; }; constructor({ timeoutMs, request, }: { timeoutMs: number; request: { method: string; path: string; baseUrl: string; }; }); } /** * Error thrown when there's a network error */ declare class TeerNetworkError extends TeerError { readonly cause: Error; readonly request: { method: string; path: string; baseUrl: string; }; constructor({ cause, request, }: { cause: Error; request: { method: string; path: string; baseUrl: string; }; }); } /** * Options for making API requests */ interface RequestOptions { method: string; path: string; data?: any; } /** * Data structure for ingest requests */ type BillingProvider = 'stripe'; type BillingFieldsBase = { customer: string; email?: string; }; type SingleMeterFields = BillingFieldsBase & { meter: string; meters?: never; }; type MultiMeterFields = BillingFieldsBase & { meter?: never; meters: Record<string, string>; }; type BillingFields = SingleMeterFields | MultiMeterFields; interface TeerBillingConfig { provider: BillingProvider; fields: BillingFields; } interface IngestData { provider: 'anthropic' | 'openai' | 'google'; model: string; usage: { input: number; output: number; cache?: { anthropic?: { cache_creation_input_tokens?: number; cache_read_input_tokens?: number; }; openai?: { input_cached_tokens?: number; }; google?: { cached_content_token_count?: number; thoughts_token_count?: number; }; }; }; function_id?: string; response_id?: string; response_model?: string; operation_id?: string; platform?: { rate_card_id: string; }; billing?: TeerBillingConfig; metadata?: Record<string, unknown>; trace_id?: string; span_id?: string; parent_span_id?: string; batch?: boolean; } /** * Type definition for fetch function */ type FetchFunction = typeof fetch; /** * Options for configuring request behavior */ interface RequestConfigOptions { /** * Custom base URL for the Public API */ baseURL?: string; /** * Custom base URL for the Tracking API * Used for endpoints that handle tracking and data collection operations * like ingest and billing meter events */ trackURL?: string; /** * Request timeout in milliseconds */ timeoutMs?: number; /** * Maximum number of retry attempts */ maxRetries?: number; /** * Base delay between retries in milliseconds (will increase with exponential backoff) */ retryDelayMs?: number; /** * Custom fetch implementation */ customFetch?: FetchFunction; } interface TeerClient { makeRequest<T = any>(path: string, method: string, data?: any, resourceBaseUrl?: string, options?: RequestConfigOptions): Promise<T>; readonly baseURL: string; readonly trackURL: string; readonly defaultRequestConfig: RequestConfigOptions; } /** * Base Resource class that all resource classes will extend */ declare abstract class Resource { protected client: TeerClient; protected basePath: string; protected resourceBaseUrl?: string | undefined; constructor(client: TeerClient, basePath?: string, resourceBaseUrl?: string | undefined); /** * Get the full path for this resource */ protected getPath(subPath?: string): string; /** * Make a request to the API * * @param options Request options * @param requestOptions Additional options for the request (timeout, retries, etc.) */ protected request<T = any>(options: RequestOptions, requestOptions?: RequestConfigOptions): Promise<T>; } /** * Ingest Resource for handling ingest operations */ declare class IngestResource extends Resource { constructor(client: TeerClient); /** * Send usage data to the Teer API * * @param data The usage data to send * @param options Optional request configuration (timeout, retries, etc.) */ send(data: IngestData, options?: RequestConfigOptions): Promise<any>; } /** * Base interface for meter event fields */ interface MeterEventFieldsBase { /** * Optional identifier for the meter event */ identifier?: string; /** * Name of the event (required) */ event_name: string; /** * Optional timestamp for the event (ISO 8601 format) */ timestamp?: string; } /** * Stripe-specific meter event fields */ interface StripeMeterEventFields extends MeterEventFieldsBase { /** * Payload data for the Stripe event */ payload: { stripe_customer_id: string; value: string; }; } /** * Parameters for creating a meter event with Stripe provider */ interface StripeMeterEventCreateParams { /** * Provider type - Stripe */ provider: 'stripe'; /** * Stripe-specific fields */ fields: StripeMeterEventFields; } /** * Union type for all supported provider meter event params */ type MeterEventCreateParams = StripeMeterEventCreateParams; /** * Response type for meter events */ interface MeterEvent { id: string; event_name: string; timestamp: string; payload: Record<string, any>; identifier?: string; created_at: string; updated_at: string; } /** * Resource for meter events operations */ declare class MeterEventsResource extends Resource { constructor(client: TeerClient); /** * Create a meter event to record usage * * @param params Parameters for creating a meter event * @param options Optional request configuration * @returns The created meter event */ create(params: MeterEventCreateParams, options?: RequestConfigOptions): Promise<MeterEvent>; } /** * Billing Resource for handling billing operations */ declare class BillingResource extends Resource { /** * Meter events resource for recording usage */ readonly meterEvents: MeterEventsResource; constructor(client: TeerClient); } /** * Teer client for interacting with the Teer API */ declare class Teer { static readonly sdkVersion: string; static readonly namespace: string; static readonly baseURL: string; readonly ingest: IngestResource; readonly billing: BillingResource; private readonly _apiKey; private readonly _baseURL; private readonly _trackURL; private readonly _defaultRequestConfig; private readonly _fetchImpl; /** * Create a new Teer client * * @param apiKey Your Teer API key * @param options Optional configuration options including baseURL and request settings */ constructor(apiKey: string, options?: RequestConfigOptions); /** * Get the API key */ get apiKey(): string; /** * Get the base URL */ get baseURL(): string; /** * Get the tracking URL */ get trackURL(): string; /** * Get the default request configuration */ get defaultRequestConfig(): RequestConfigOptions; /** * Make a request to the Teer API * * @param path The path to the API endpoint * @param method The HTTP method to use * @param data The data to send with the request * @param resourceBaseUrl Optional resource-specific base URL (e.g., API URL or track URL) * @param options Additional request options * @returns The response from the API */ makeRequest<T = any>(path: string, method: string, data?: any, resourceBaseUrl?: string, options?: RequestConfigOptions): Promise<T>; } export { BillingResource, type FetchFunction, type IngestData, IngestResource, type MeterEvent, type MeterEventCreateParams, type MeterEventFieldsBase, MeterEventsResource, type RequestConfigOptions, type RequestOptions, Resource, type StripeMeterEventCreateParams, type StripeMeterEventFields, Teer, TeerAPIError, TeerError, TeerNetworkError, TeerTimeoutError };