@hemia/core 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -312,14 +312,13 @@ Reflector = __decorate([
312
312
  injectable()
313
313
  ], Reflector);
314
314
 
315
- class HemiaFactory {
315
+ class AppFactory {
316
316
  /**
317
317
  * Inicializa la aplicación Hemia conectando Express con Inversify.
318
318
  * @param container El contenedor de Inversify con tus servicios ya bindeados.
319
- * @param controllers Lista de Clases de Controladores a registrar.
320
319
  * @param options Opciones de configuración.
321
320
  */
322
- static async create(container, options = {}, controllers) {
321
+ static async create(container, options = {}, plugins = []) {
323
322
  const app = express();
324
323
  app.use(express.json(options.jsonOptions));
325
324
  app.use(express.urlencoded({ extended: true, ...options.urlencodedOptions }));
@@ -345,18 +344,27 @@ class HemiaFactory {
345
344
  }
346
345
  next();
347
346
  });
347
+ if (plugins && Array.isArray(plugins) && plugins.length > 0) {
348
+ for (const plugin of plugins) {
349
+ if (plugin && typeof plugin === 'function') {
350
+ await plugin(container);
351
+ }
352
+ }
353
+ }
348
354
  if (!container.isBound(Reflector)) {
349
355
  container.bind(Reflector).toSelf().inSingletonScope();
350
356
  }
351
- const controllerList = controllers && controllers.length > 0
352
- ? controllers
353
- : ControllerRegistry.getAll();
354
- controllerList.forEach(controllerClass => {
357
+ const autoControllerList = ControllerRegistry.getAll();
358
+ const controllers = Array.from(new Set([
359
+ ...(options.controllers || []),
360
+ ...autoControllerList,
361
+ ]));
362
+ controllers.forEach(controllerClass => {
355
363
  if (!container.isBound(controllerClass)) {
356
364
  container.bind(controllerClass).toSelf();
357
365
  }
358
366
  });
359
- await registerRoutes(app, container, controllerList, options.onTraceFinish || (() => { }), options.multerOptions);
367
+ await registerRoutes(app, container, controllers, options.onTraceFinish || (() => { }), options.multerOptions);
360
368
  app.use((err, req, res, next) => {
361
369
  console.error('[Hemia] Unhandled error:', err);
362
370
  res.status(err.status || 500).json({
@@ -365,7 +373,7 @@ class HemiaFactory {
365
373
  });
366
374
  });
367
375
  if (options.logger !== false) {
368
- console.log(`[Hemia] Application initialized with ${controllerList.length} controllers.`);
376
+ console.log(`[Hemia] Application initialized with ${controllers.length} controllers.`);
369
377
  }
370
378
  return app;
371
379
  }
@@ -478,4 +486,4 @@ JWTGuard = __decorate([
478
486
  __metadata("design:paramtypes", [AuthService])
479
487
  ], JWTGuard);
480
488
 
481
- export { ApiKeyGuard, AuthGuard, GuardsConsumer, HemiaExecutionContext, HemiaFactory, JWTGuard, Reflector, ResponseSerializer, registerRoutes };
489
+ export { ApiKeyGuard, AppFactory, AuthGuard, GuardsConsumer, HemiaExecutionContext, JWTGuard, Reflector, ResponseSerializer, registerRoutes };
@@ -314,14 +314,13 @@ exports.Reflector = __decorate([
314
314
  inversify.injectable()
315
315
  ], exports.Reflector);
316
316
 
317
- class HemiaFactory {
317
+ class AppFactory {
318
318
  /**
319
319
  * Inicializa la aplicación Hemia conectando Express con Inversify.
320
320
  * @param container El contenedor de Inversify con tus servicios ya bindeados.
321
- * @param controllers Lista de Clases de Controladores a registrar.
322
321
  * @param options Opciones de configuración.
323
322
  */
324
- static async create(container, options = {}, controllers) {
323
+ static async create(container, options = {}, plugins = []) {
325
324
  const app = express();
326
325
  app.use(express.json(options.jsonOptions));
327
326
  app.use(express.urlencoded({ extended: true, ...options.urlencodedOptions }));
@@ -347,18 +346,27 @@ class HemiaFactory {
347
346
  }
348
347
  next();
349
348
  });
349
+ if (plugins && Array.isArray(plugins) && plugins.length > 0) {
350
+ for (const plugin of plugins) {
351
+ if (plugin && typeof plugin === 'function') {
352
+ await plugin(container);
353
+ }
354
+ }
355
+ }
350
356
  if (!container.isBound(exports.Reflector)) {
351
357
  container.bind(exports.Reflector).toSelf().inSingletonScope();
352
358
  }
353
- const controllerList = controllers && controllers.length > 0
354
- ? controllers
355
- : common.ControllerRegistry.getAll();
356
- controllerList.forEach(controllerClass => {
359
+ const autoControllerList = common.ControllerRegistry.getAll();
360
+ const controllers = Array.from(new Set([
361
+ ...(options.controllers || []),
362
+ ...autoControllerList,
363
+ ]));
364
+ controllers.forEach(controllerClass => {
357
365
  if (!container.isBound(controllerClass)) {
358
366
  container.bind(controllerClass).toSelf();
359
367
  }
360
368
  });
361
- await registerRoutes(app, container, controllerList, options.onTraceFinish || (() => { }), options.multerOptions);
369
+ await registerRoutes(app, container, controllers, options.onTraceFinish || (() => { }), options.multerOptions);
362
370
  app.use((err, req, res, next) => {
363
371
  console.error('[Hemia] Unhandled error:', err);
364
372
  res.status(err.status || 500).json({
@@ -367,7 +375,7 @@ class HemiaFactory {
367
375
  });
368
376
  });
369
377
  if (options.logger !== false) {
370
- console.log(`[Hemia] Application initialized with ${controllerList.length} controllers.`);
378
+ console.log(`[Hemia] Application initialized with ${controllers.length} controllers.`);
371
379
  }
372
380
  return app;
373
381
  }
@@ -480,8 +488,8 @@ exports.JWTGuard = __decorate([
480
488
  __metadata("design:paramtypes", [authSdk.AuthService])
481
489
  ], exports.JWTGuard);
482
490
 
491
+ exports.AppFactory = AppFactory;
483
492
  exports.GuardsConsumer = GuardsConsumer;
484
493
  exports.HemiaExecutionContext = HemiaExecutionContext;
485
- exports.HemiaFactory = HemiaFactory;
486
494
  exports.ResponseSerializer = ResponseSerializer;
487
495
  exports.registerRoutes = registerRoutes;
@@ -1,8 +1,8 @@
1
1
  import { Container } from "inversify";
2
2
  import { TraceFinishCallback } from "./router";
3
- import { Type } from "@hemia/common";
3
+ import { Plugin, Type } from "@hemia/common";
4
4
  import { Express } from "express";
5
- export interface HemiaFactoryOptions {
5
+ export interface AppFactoryOptions {
6
6
  onTraceFinish?: TraceFinishCallback;
7
7
  logger?: boolean;
8
8
  corsHeaders?: Record<string, string>;
@@ -10,13 +10,13 @@ export interface HemiaFactoryOptions {
10
10
  multerOptions?: any;
11
11
  jsonOptions?: any;
12
12
  urlencodedOptions?: any;
13
+ controllers?: Type<any>[];
13
14
  }
14
- export declare class HemiaFactory {
15
+ export declare class AppFactory {
15
16
  /**
16
17
  * Inicializa la aplicación Hemia conectando Express con Inversify.
17
18
  * @param container El contenedor de Inversify con tus servicios ya bindeados.
18
- * @param controllers Lista de Clases de Controladores a registrar.
19
19
  * @param options Opciones de configuración.
20
20
  */
21
- static create(container: Container, options?: HemiaFactoryOptions, controllers?: Type<any>[]): Promise<Express>;
21
+ static create(container: Container, options?: AppFactoryOptions, plugins?: Plugin[]): Promise<Express>;
22
22
  }
@@ -1,7 +1,7 @@
1
1
  import 'reflect-metadata';
2
- export * from './hemia-factory';
2
+ export * from './app-factory';
3
3
  export * from "./services";
4
4
  export * from "./context";
5
5
  export * from "./router";
6
6
  export * from "./guards";
7
- export * from "./hemia-factory";
7
+ export * from "./app-factory";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/core",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Core utilities for Hemia projects",
5
5
  "main": "dist/hemia-core.js",
6
6
  "module": "dist/hemia-core.esm.js",
@@ -17,10 +17,10 @@
17
17
  "@rollup/plugin-commonjs": "^26.0.1",
18
18
  "@rollup/plugin-json": "^6.1.0",
19
19
  "@rollup/plugin-node-resolve": "^15.2.3",
20
- "@hemia/common": "^0.0.8",
20
+ "@hemia/common": "^0.0.9",
21
21
  "@hemia/app-context": "^0.0.6",
22
22
  "@hemia/trace-manager": "^0.0.9",
23
- "@hemia/auth-sdk": "^0.0.9",
23
+ "@hemia/auth-sdk": "^0.0.10",
24
24
  "@types/express": "^5.0.5",
25
25
  "express": "^5.1.0",
26
26
  "inversify": "^7.10.4",