@hemia/core 0.0.5 → 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.
@@ -1,6 +1,6 @@
1
1
  import 'reflect-metadata';
2
2
  import express, { Router } from 'express';
3
- import { METADATA_KEYS, ParamType, ApiResponse, isRedirectResponse } from '@hemia/common';
3
+ import { METADATA_KEYS, ParamType, ApiResponse, isRedirectResponse, ControllerRegistry } from '@hemia/common';
4
4
  import { TRACE_METADATA_KEY } from '@hemia/trace-manager';
5
5
  import { traceMiddleware } from '@hemia/app-context';
6
6
  import { plainToInstance } from 'class-transformer';
@@ -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, controllers, options = {}) {
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,9 +344,21 @@ 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
  }
357
+ const autoControllerList = ControllerRegistry.getAll();
358
+ const controllers = Array.from(new Set([
359
+ ...(options.controllers || []),
360
+ ...autoControllerList,
361
+ ]));
351
362
  controllers.forEach(controllerClass => {
352
363
  if (!container.isBound(controllerClass)) {
353
364
  container.bind(controllerClass).toSelf();
@@ -475,4 +486,4 @@ JWTGuard = __decorate([
475
486
  __metadata("design:paramtypes", [AuthService])
476
487
  ], JWTGuard);
477
488
 
478
- 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, controllers, options = {}) {
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,9 +346,21 @@ 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
  }
359
+ const autoControllerList = common.ControllerRegistry.getAll();
360
+ const controllers = Array.from(new Set([
361
+ ...(options.controllers || []),
362
+ ...autoControllerList,
363
+ ]));
353
364
  controllers.forEach(controllerClass => {
354
365
  if (!container.isBound(controllerClass)) {
355
366
  container.bind(controllerClass).toSelf();
@@ -477,8 +488,8 @@ exports.JWTGuard = __decorate([
477
488
  __metadata("design:paramtypes", [authSdk.AuthService])
478
489
  ], exports.JWTGuard);
479
490
 
491
+ exports.AppFactory = AppFactory;
480
492
  exports.GuardsConsumer = GuardsConsumer;
481
493
  exports.HemiaExecutionContext = HemiaExecutionContext;
482
- exports.HemiaFactory = HemiaFactory;
483
494
  exports.ResponseSerializer = ResponseSerializer;
484
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, controllers: Type<any>[], options?: HemiaFactoryOptions): 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.5",
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.7",
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",