UNPKG

baseflow-client

Version:

Official TypeScript/JavaScript client for BaseFlow - a powerful BaaS with OAuth authentication, RPC functions, database indexes, real-time features, and Supabase-compatible API

694 lines (693 loc) 15.4 kB
import { BaseFlowClientOptions, QueryBuilder, AuthEvent } from './types'; /** * The main client for interacting with a BaseFlow backend. * * @example * ```ts * const client = createClient({ * url: 'http://localhost:4000', * apiKey: 'your-api-key' * }); * * const { data, error } = await client.from('users').select('*'); * ``` */ export declare class BaseFlowClient { /** The URL of the BaseFlow backend. */ private url; /** The API key for the BaseFlow backend. */ private apiKey; /** Additional headers to send with each request. */ private headers; /** The underlying fetch implementation. */ private fetch; /** The authentication API. */ auth: AuthAPI; /** The storage API. */ storage: StorageAPI; /** The realtime API. */ realtime: RealtimeAPI; /** * Creates a new BaseFlowClient instance. * * @param options The options for the client. */ constructor(options: BaseFlowClientOptions); /** * Creates a query builder for a specific table. * * @param table The name of the table to query. * @returns A query builder for the specified table. */ from<T = any>(table: string): QueryBuilder<T>; /** * Makes a request to the BaseFlow backend. * * @param method The HTTP method to use. * @param path The path to request. * @param options Additional options for the request. * @returns The response from the backend. */ request(method: string, path: string, options?: { body?: any; headers?: Record<string, string>; params?: Record<string, any>; baseUrl?: string; }): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Get project information */ getProjectInfo(): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Set authentication token */ setAuth(token: string | null): void; /** * Execute a raw SQL query (if supported) */ sql(query: string, params?: any[]): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Call a remote procedure/function */ rpc(functionName: string, params?: any): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Create a new function */ createFunction(name: string, definition: string, options?: { language?: 'javascript' | 'sql'; returnType?: string; parameters?: Array<{ name: string; type: string; default?: any; }>; description?: string; }): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * List all functions */ listFunctions(): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Get function details */ getFunction(name: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Delete a function */ deleteFunction(name: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Create an index */ createIndex(table: string, columns: string[], options?: { name?: string; unique?: boolean; }): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * List indexes */ listIndexes(table?: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Drop an index */ dropIndex(name: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Analyze query performance */ analyzeQuery(query: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Define and create tables from schema (BaseFlow-style) */ defineSchema(schema: Record<string, any>): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Get current project schema */ getSchema(): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Create a single table from definition */ createTable(tableName: string, definition: any): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; } /** * Storage API for file operations */ declare class StorageAPI { private client; constructor(client: BaseFlowClient); upload(path: string, file: Buffer | Uint8Array | string, options?: { mimetype?: string; }): Promise<{ data: null; error: { message: any; code: any; details: any; }; } | { data: any; error: null; }>; getUrl(path: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; download(path: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; delete(path: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; list(path?: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; createFolder(path: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; } /** * Auth API for authentication operations */ declare class AuthAPI { private client; private authToken; private user; private listeners; constructor(client: BaseFlowClient); /** * Sign up a new user */ signUp(email: string, password: string, options?: { name?: string; data?: Record<string, any>; redirectTo?: string; }): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Sign in with email and password */ signInWithPassword(credentials: { email: string; password: string; }): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Sign in with OAuth provider (GitHub, Google, etc.) */ signInWithOAuth(provider: 'github' | 'google', options?: { redirectTo?: string; scopes?: string; }): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; } | { data: { url: any; }; error: null; }>; /** * Handle OAuth callback and exchange code for session */ handleOAuthCallback(code: string, provider: 'github' | 'google'): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Sign out the current user */ signOut(): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; /** * Get the current user */ getUser(): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; } | { user: null; error: null; }>; /** * Get the current session */ getSession(): { access_token: string | null; user: any; expires_at: null; refresh_token: null; }; /** * Set the auth token manually */ setSession(session: { access_token: string; user?: any; }): void; /** * Listen to auth state changes */ onAuthStateChange(callback: (event: AuthEvent) => void): () => void; /** * Notify all listeners of auth events */ private notifyListeners; signup(email: string, password: string, name?: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; login(email: string, password: string): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; logout(): Promise<{ data: null; error: { message: any; code: any; details: any; }; status: number; statusText: string; } | { data: any; error: null; status: number; statusText: string; }>; } /** * Realtime API for WebSocket subscriptions */ declare class RealtimeAPI { private client; private ws; private subscriptions; private authenticated; private reconnectAttempts; private maxReconnectAttempts; private reconnectDelay; constructor(client: BaseFlowClient); /** * Connect to realtime WebSocket */ connect(): Promise<void>; /** * Authenticate with the realtime server */ private authenticate; /** * Subscribe to table changes */ subscribe(table: string, callback: (payload: any) => void): () => void; /** * Handle incoming messages */ private handleMessage; /** * Attempt to reconnect */ private attemptReconnect; /** * Disconnect from realtime */ disconnect(): void; /** * Send ping to keep connection alive */ ping(): void; /** * Get connection status */ getStatus(): { connected: boolean; authenticated: boolean; subscriptions: number; }; } /** * Create a new BaseFlow client instance */ export declare function createClient(options: BaseFlowClientOptions): BaseFlowClient; export {};