UNPKG

arvox-backend

Version:

Un framework backend moderne et modulaire basé sur Hono, TypeScript et l'architecture hexagonale avec authentification Better Auth + Drizzle intégrée

60 lines 1.94 kB
/** * Base class for all use cases following the command pattern * Provides common functionality like logging, error handling, and validation */ export class BaseUseCase { /** * Validate input parameters before execution * Override this method to add custom validation logic * @param params - Parameters to validate * @throws Error if validation fails */ async validate(params) { // Default validation - override in child classes if (params === null || params === undefined) { throw new Error('Parameters cannot be null or undefined'); } } /** * Handle errors in a consistent way across all use cases * @param error - The error that occurred * @param context - Additional context about the error * @returns Formatted error response */ handleError(error, context) { const errorMessage = error?.message || 'An unexpected error occurred'; const fullMessage = context ? `${context}: ${errorMessage}` : errorMessage; // Log the error for debugging console.error(`UseCase Error [${this.constructor.name}]:`, fullMessage, error); return { success: false, error: fullMessage }; } /** * Create a successful response * @param data - The data to return * @returns Formatted success response */ createSuccessResponse(data) { return { success: true, data }; } /** * Execute the use case with automatic validation and error handling * @param params - Input parameters * @returns Promise containing the response */ async run(params) { try { await this.validate(params); return await this.execute(params); } catch (error) { return this.handleError(error); } } } //# sourceMappingURL=base-use-case.js.map