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
132 lines (131 loc) • 3.18 kB
TypeScript
export interface RealtimeSubscription {
id: string;
table: string;
filter?: Record<string, any>;
event: string;
callback: (payload: RealtimePayload) => void;
}
export interface RealtimePayload {
subscription_id: string;
table: string;
event: 'INSERT' | 'UPDATE' | 'DELETE';
old?: Record<string, any>;
new?: Record<string, any>;
timestamp: string;
}
export interface RealtimeMessage {
type: string;
event: string;
payload: any;
}
export declare class RealtimeClient {
private ws;
private url;
private apiKey;
private projectId;
private subscriptions;
private reconnectAttempts;
private maxReconnectAttempts;
private reconnectDelay;
private heartbeatInterval;
private connected;
private authenticated;
constructor(url: string, apiKey: string);
/**
* Connect to WebSocket server
*/
connect(): Promise<void>;
/**
* Authenticate with the server
*/
private authenticate;
/**
* Subscribe to table changes
*/
subscribe(table: string, callback: (payload: RealtimePayload) => void, options?: {
event?: 'INSERT' | 'UPDATE' | 'DELETE' | '*';
filter?: Record<string, any>;
}): RealtimeSubscription;
/**
* Unsubscribe from changes
*/
unsubscribe(subscription: RealtimeSubscription): void;
/**
* Create a subscription builder (Supabase-like API)
*/
from(table: string): RealtimeSubscriptionBuilder;
/**
* Send subscription request to server
*/
private sendSubscriptionRequest;
/**
* Handle incoming WebSocket message
*/
private handleMessage;
/**
* Handle broadcast message
*/
private handleBroadcast;
/**
* Check if record matches filter
*/
private matchesFilter;
/**
* Start heartbeat
*/
private startHeartbeat;
/**
* Stop heartbeat
*/
private stopHeartbeat;
/**
* Attempt to reconnect
*/
private attemptReconnect;
/**
* Extract project ID from URL
*/
private extractProjectId;
/**
* Generate unique subscription ID
*/
private generateSubscriptionId;
/**
* Disconnect from server
*/
disconnect(): void;
/**
* Get connection status
*/
getStatus(): {
connected: boolean;
authenticated: boolean;
subscriptions: number;
};
}
/**
* Subscription builder for fluent API
*/
export declare class RealtimeSubscriptionBuilder {
private client;
private table;
private events;
private filter;
constructor(client: RealtimeClient, table: string);
/**
* Listen for INSERT events
*/
on(event: 'INSERT' | 'UPDATE' | 'DELETE' | '*', callback: (payload: RealtimePayload) => void): RealtimeSubscriptionBuilder;
/**
* Add filter conditions
*/
eq(column: string, value: any): RealtimeSubscriptionBuilder;
/**
* Subscribe to all events
*/
subscribe(): RealtimeSubscription;
}
/**
* Create realtime client
*/
export declare function createRealtimeClient(url: string, apiKey: string): RealtimeClient;