/** * Base Storage Plugin * @module @plyaz/storage/plugins * * Abstract base class for all storage plugins. * Provides default implementations and helper methods. */ import type { StoragePlugin, STORAGE_PLUGIN_TYPE, StoragePluginContext, StoragePluginFile, StoragePluginUploadResult, StoragePluginDeleteResult, StoragePluginAccessEvent, StoragePluginHealth, BeforeUploadResult, BeforeDeleteResult, FileMetadata, BasePluginConfig } from '@plyaz/types/storage'; import type { StorageLogger } from '../../core/logger'; import type { BaseStorageAdapter } from '../../adapters/base/BaseStorageAdapter'; import { createApiClient } from '@plyaz/api'; /** * Abstract Base Plugin * * Provides common functionality for all storage plugins: * - Default implementations for optional hooks * - Helper methods for context creation * - Health check support * - Lifecycle management * * @example * ```typescript * class VirusScanPlugin extends BasePlugin { * constructor(config: VirusScanConfig) { * super({ * name: 'virus-scan', * type: STORAGE_PLUGIN_TYPE.SECURITY, * priority: 10, // Run first * version: '1.0.0', * description: 'Scans files for viruses before upload', * }); * } * * async beforeUpload(file: StoragePluginFile, context: StoragePluginContext): Promise { * const result = await this.scanFile(file.buffer); * if (result.infected) { * return { allowed: false, reason: `Virus detected: ${result.virusName}` }; * } * return { allowed: true }; * } * } * ``` */ export declare abstract class BasePlugin implements StoragePlugin { readonly name: string; readonly type: STORAGE_PLUGIN_TYPE; readonly priority: number; readonly version?: string; readonly description?: string; enabled: boolean; protected readonly logger?: StorageLogger; protected initialized: boolean; protected storageAdapter?: BaseStorageAdapter; private apiClient; private clientInitPromise; private readonly apiClientConfig?; constructor(config: BasePluginConfig); /** * Initialize the plugin * Override to perform setup (connections, validation, etc.) */ initialize(): Promise; /** * Cleanup the plugin * Override to release resources (close connections, etc.) */ destroy(): Promise; /** * Set the storage adapter for file operations * Called by PluginRegistry when adapter is registered or updated * * @param adapter - Storage adapter instance */ setStorageAdapter(adapter: BaseStorageAdapter): void; /** * Get the current storage adapter * Prefer using context.storageAdapter in hooks as it may be operation-specific * * @returns Current storage adapter or undefined if not set */ protected getStorageAdapter(): BaseStorageAdapter | undefined; /** * Initialize API client for REST API-based plugins * SDK-based plugins do NOT need this */ private initializeApiClient; /** * Ensure API client is initialized (for REST API plugins) * Call this before making API requests */ protected ensureApiClientInitialized(): Promise; /** * Get API client for making HTTP requests * Returns null if API client not configured * Subclasses can use this for REST API calls */ protected getApiClient(): Awaited> | null; /** * Check plugin health * Override to provide custom health checks */ healthCheck(): Promise; /** * PRE-UPLOAD HOOK (BLOCKING) * Override to implement custom logic * Default: Allow all uploads */ beforeUpload?(file: StoragePluginFile, context: StoragePluginContext): Promise; /** * POST-UPLOAD HOOK (ASYNC) * Override to implement custom logic * Default: No action */ afterUpload?(result: StoragePluginUploadResult, context: StoragePluginContext): Promise; /** * PRE-DELETE HOOK (BLOCKING) * Override to implement custom logic * Default: Allow all deletions */ beforeDelete?(fileId: string, metadata: FileMetadata | undefined, context: StoragePluginContext): Promise; /** * POST-DELETE HOOK (ASYNC) * Override to implement custom logic * Default: No action */ afterDelete?(result: StoragePluginDeleteResult, context: StoragePluginContext): Promise; /** * ON-ACCESS HOOK (ASYNC) * Override to implement custom logic * Default: No action */ onAccess?(event: StoragePluginAccessEvent, context: StoragePluginContext): Promise; /** * Helper: Create plugin context */ protected createContext(operation: 'upload' | 'delete' | 'access' | 'update', storageAdapter: BaseStorageAdapter, options?: { userId?: string; correlationId?: string; metadata?: Record; }): StoragePluginContext; /** * Helper: Check if plugin should execute */ protected shouldExecute(): boolean; /** * Helper: Log with plugin context */ protected log(level: 'debug' | 'info' | 'warn' | 'error', message: string, meta?: Record): void; }