--- title: Frontend API Reference sidebar_position: 1 hide_title: true --- # Frontend API Reference The `@apache-superset/core` package provides comprehensive APIs for frontend extension development. All APIs are organized into logical namespaces for easy discovery and use. ## Core API The core namespace provides fundamental extension functionality. ### registerView Registers a new view or panel in the specified contribution point. ```typescript core.registerView( id: string, component: React.ComponentType ): Disposable ``` **Example:** ```typescript const panel = context.core.registerView('my-extension.panel', () => ( )); ``` ### getActiveView Gets the currently active view in a contribution area. ```typescript core.getActiveView(area: string): View | undefined ``` ## Commands API Manages command registration and execution. ### registerCommand Registers a new command that can be triggered by menus, shortcuts, or programmatically. ```typescript commands.registerCommand( id: string, handler: CommandHandler ): Disposable interface CommandHandler { title: string; icon?: string; execute: (...args: any[]) => any; isEnabled?: (...args: any[]) => boolean; } ``` **Example:** ```typescript const cmd = context.commands.registerCommand('my-extension.analyze', { title: 'Analyze Query', icon: 'BarChartOutlined', execute: () => { const query = context.sqlLab.getCurrentQuery(); // Perform analysis }, isEnabled: () => { return context.sqlLab.hasActiveEditor(); } }); ``` ### executeCommand Executes a registered command by ID. ```typescript commands.executeCommand(id: string, ...args: any[]): Promise ``` ## SQL Lab API Provides access to SQL Lab functionality and events. ### Query Access ```typescript // Get current tab sqlLab.getCurrentTab(): Tab | undefined // Get all tabs sqlLab.getTabs(): Tab[] // Get current query sqlLab.getCurrentQuery(): string // Get selected text sqlLab.getSelectedText(): string | undefined ``` ### Database Access ```typescript // Get available databases sqlLab.getDatabases(): Database[] // Get database by ID sqlLab.getDatabase(id: number): Database | undefined // Get schemas for database sqlLab.getSchemas(databaseId: number): Promise // Get tables for schema sqlLab.getTables( databaseId: number, schema: string ): Promise ``` ### Events ```typescript // Query execution events sqlLab.onDidQueryRun: Event sqlLab.onDidQueryStop: Event sqlLab.onDidQueryFail: Event // Editor events sqlLab.onDidChangeEditorContent: Event sqlLab.onDidChangeSelection: Event // Tab events sqlLab.onDidChangeActiveTab: Event sqlLab.onDidCloseTab: Event sqlLab.onDidChangeTabTitle: Event<{tab: Tab, title: string}> // Panel events sqlLab.onDidOpenPanel: Event sqlLab.onDidClosePanel: Event sqlLab.onDidChangeActivePanel: Event ``` **Event Usage Example:** ```typescript const disposable = context.sqlLab.onDidQueryRun((result) => { console.log('Query executed:', result.query); console.log('Rows returned:', result.rowCount); console.log('Execution time:', result.executionTime); }); // Remember to dispose when done context.subscriptions.push(disposable); ``` ## Authentication API Handles authentication and security tokens. ### getCSRFToken Gets the current CSRF token for API requests. ```typescript authentication.getCSRFToken(): Promise ``` ### getCurrentUser Gets information about the current user. ```typescript authentication.getCurrentUser(): User interface User { id: number; username: string; email: string; roles: Role[]; permissions: Permission[]; } ``` ### hasPermission Checks if the current user has a specific permission. ```typescript authentication.hasPermission(permission: string): boolean ``` ## Extensions API Manages extension lifecycle and inter-extension communication. ### getExtension Gets information about an installed extension. ```typescript extensions.getExtension(id: string): Extension | undefined interface Extension { id: string; name: string; version: string; isActive: boolean; metadata: ExtensionMetadata; } ``` ### getActiveExtensions Gets all currently active extensions. ```typescript extensions.getActiveExtensions(): Extension[] ``` ### Events ```typescript // Extension lifecycle events extensions.onDidActivateExtension: Event extensions.onDidDeactivateExtension: Event ``` ## UI Components Import pre-built UI components from `@apache-superset/core`: ```typescript import { Button, Select, Input, Table, Modal, Alert, Tabs, Card, Dropdown, Menu, Tooltip, Icon, // ... many more } from '@apache-superset/core'; ``` ### Example Component Usage ```typescript import { Button, Alert } from '@apache-superset/core'; function MyExtensionPanel() { return (
); } ``` ## Storage API Provides persistent storage for extension data. ### Local Storage ```typescript // Store data storage.local.set(key: string, value: any): Promise // Retrieve data storage.local.get(key: string): Promise // Remove data storage.local.remove(key: string): Promise // Clear all extension data storage.local.clear(): Promise ``` ### Workspace Storage Workspace storage is shared across all users for collaborative features. ```typescript storage.workspace.set(key: string, value: any): Promise storage.workspace.get(key: string): Promise storage.workspace.remove(key: string): Promise ``` ## Network API Utilities for making API calls to Superset. ### fetch Enhanced fetch with CSRF token handling. ```typescript network.fetch(url: string, options?: RequestInit): Promise ``` ### API Client Type-safe API client for Superset endpoints. ```typescript // Get chart data network.api.charts.get(id: number): Promise // Query database network.api.sqlLab.execute( databaseId: number, query: string ): Promise // Get datasets network.api.datasets.list(): Promise ``` ## Utility Functions ### Formatting ```typescript // Format numbers utils.formatNumber(value: number, format?: string): string // Format dates utils.formatDate(date: Date, format?: string): string // Format SQL utils.formatSQL(sql: string): string ``` ### Validation ```typescript // Validate SQL syntax utils.validateSQL(sql: string): ValidationResult // Check if valid database ID utils.isValidDatabaseId(id: any): boolean ``` ## TypeScript Types Import common types for type safety: ```typescript import type { Database, Dataset, Chart, Dashboard, Query, QueryResult, Tab, Panel, User, Role, Permission, ExtensionContext, Disposable, Event, // ... more types } from '@apache-superset/core'; ``` ## Extension Context The context object passed to your extension's `activate` function: ```typescript interface ExtensionContext { // Subscription management subscriptions: Disposable[]; // Extension metadata extensionId: string; extensionPath: string; // API namespaces core: CoreAPI; commands: CommandsAPI; sqlLab: SqlLabAPI; authentication: AuthenticationAPI; extensions: ExtensionsAPI; storage: StorageAPI; network: NetworkAPI; utils: UtilsAPI; // Logging logger: Logger; } ``` ## Event Handling Events follow the VS Code pattern with subscribe/dispose: ```typescript // Subscribe to event const disposable = sqlLab.onDidQueryRun((result) => { // Handle event }); // Dispose when done disposable.dispose(); // Or add to context for automatic cleanup context.subscriptions.push(disposable); ``` ## Best Practices 1. **Always dispose subscriptions** to prevent memory leaks 2. **Use TypeScript** for better IDE support and type safety 3. **Handle errors gracefully** with try-catch blocks 4. **Check permissions** before sensitive operations 5. **Use provided UI components** for consistency 6. **Cache API responses** when appropriate 7. **Validate user input** before processing ## Version Compatibility The frontend API follows semantic versioning: - **Major version**: Breaking changes - **Minor version**: New features, backward compatible - **Patch version**: Bug fixes Check compatibility in your `extension.json`: ```json { "engines": { "@apache-superset/core": "^1.0.0" } } ```