@innv/nest-initializer 0.1.3 → 0.2.1

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.
@@ -22,6 +22,7 @@ const config_validator_helper_1 = require("./config-validator.helper");
22
22
  const features_1 = require("../features");
23
23
  const plugins_1 = require("../plugins");
24
24
  const starters_1 = require("../starters");
25
+ const response_pattern_interceptor_1 = require("../interceptors/response-pattern.interceptor");
25
26
  /**
26
27
  * Uma classe fluente (Builder) para inicializar uma aplicação NestJS de forma declarativa.
27
28
  */
@@ -41,6 +42,7 @@ class AppInitializer {
41
42
  advancedSwaggerUiOptions;
42
43
  autoDiscoveredComponents;
43
44
  globalProviders = [];
45
+ globalInterceptors = [];
44
46
  factoryGeneratedControllers = [];
45
47
  constructor(module, adapter) {
46
48
  this.module = module;
@@ -351,6 +353,22 @@ class AppInitializer {
351
353
  this.autoDiscoveredComponents = (0, auto_discovery_helper_1.discoverComponents)(options.basePath, reflector);
352
354
  return this;
353
355
  }
356
+ /**
357
+ * Adiciona um Interceptor global diretamente (instância).
358
+ * @param interceptor
359
+ */
360
+ addGlobalInterceptor(interceptor) {
361
+ this.globalInterceptors.push(interceptor);
362
+ return this;
363
+ }
364
+ /**
365
+ * Adiciona um ResponseMapper global para padronizar respostas.
366
+ * @param mapper
367
+ */
368
+ withResponseMapper(mapper) {
369
+ this.globalInterceptors.push(new response_pattern_interceptor_1.ResponsePatternInterceptor(mapper));
370
+ return this;
371
+ }
354
372
  async listen() {
355
373
  this.logger.log('Criando a instância da aplicação NestJS...');
356
374
  const rootImports = [this.module, ...this.featureModules];
@@ -388,36 +406,20 @@ class AppInitializer {
388
406
  this.logger.log(`Aplicando o plugin: ${plugin.constructor.name}`);
389
407
  await plugin.apply(this.app);
390
408
  }
409
+ for (const interceptor of this.globalInterceptors) {
410
+ this.app.useGlobalInterceptors(interceptor);
411
+ }
391
412
  if (this.swaggerOptions) {
392
- const config = new swagger_1.DocumentBuilder()
413
+ const documentBuilder = new swagger_1.DocumentBuilder()
393
414
  .setTitle(this.swaggerOptions.title)
394
415
  .setDescription(this.swaggerOptions.description)
395
- .setVersion(this.swaggerOptions.version)
396
- .addBearerAuth()
397
- .build();
398
- const document = swagger_1.SwaggerModule.createDocument(this.app, config);
399
- const swaggerSetupOptions = {};
400
- if (this.advancedSwaggerUiOptions?.customCss) {
401
- swaggerSetupOptions.customCssUrl =
402
- this.advancedSwaggerUiOptions.customCss;
403
- }
404
- if (this.advancedSwaggerUiOptions?.customJs) {
405
- swaggerSetupOptions.customJsUrl =
406
- this.advancedSwaggerUiOptions.customJs;
416
+ .setVersion(this.swaggerOptions.version);
417
+ for (const tag of this.swaggerOptions.tags ?? []) {
418
+ documentBuilder.addTag(tag.name, tag.description);
407
419
  }
408
- swaggerSetupOptions.swaggerOptions = {
409
- docExpansion: 'none',
410
- filter: true,
411
- showRequestDuration: true,
412
- deepLinking: true,
413
- displayRequestDuration: true,
414
- tryItOutEnabled: true,
415
- persistAuthorization: true,
416
- defaultModelsExpandDepth: -1,
417
- operationsSorter: 'alpha',
418
- tagsSorter: 'alpha',
419
- };
420
- swagger_1.SwaggerModule.setup(this.swaggerOptions.path ?? 'docs', this.app, document, swaggerSetupOptions);
420
+ const config = documentBuilder.build();
421
+ const document = swagger_1.SwaggerModule.createDocument(this.app, config, this.swaggerOptions.documentOptions);
422
+ swagger_1.SwaggerModule.setup(this.swaggerOptions.path ?? 'docs', this.app, document, this.swaggerOptions.customOptions);
421
423
  }
422
424
  await this.app.listen(this.port);
423
425
  const appUrl = await this.app.getUrl();
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResponsePatternInterceptor = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ class ResponsePatternInterceptor {
6
+ mapper;
7
+ constructor(mapper) {
8
+ this.mapper = mapper;
9
+ }
10
+ intercept(context, next) {
11
+ return next.handle().pipe((0, rxjs_1.map)((data) => this.mapper(data, context)));
12
+ }
13
+ }
14
+ exports.ResponsePatternInterceptor = ResponsePatternInterceptor;
@@ -22,6 +22,7 @@ const config_validator_helper_1 = require("./config-validator.helper");
22
22
  const features_1 = require("../features");
23
23
  const plugins_1 = require("../plugins");
24
24
  const starters_1 = require("../starters");
25
+ const response_pattern_interceptor_1 = require("../interceptors/response-pattern.interceptor");
25
26
  /**
26
27
  * Uma classe fluente (Builder) para inicializar uma aplicação NestJS de forma declarativa.
27
28
  */
@@ -41,6 +42,7 @@ class AppInitializer {
41
42
  advancedSwaggerUiOptions;
42
43
  autoDiscoveredComponents;
43
44
  globalProviders = [];
45
+ globalInterceptors = [];
44
46
  factoryGeneratedControllers = [];
45
47
  constructor(module, adapter) {
46
48
  this.module = module;
@@ -351,6 +353,22 @@ class AppInitializer {
351
353
  this.autoDiscoveredComponents = (0, auto_discovery_helper_1.discoverComponents)(options.basePath, reflector);
352
354
  return this;
353
355
  }
356
+ /**
357
+ * Adiciona um Interceptor global diretamente (instância).
358
+ * @param interceptor
359
+ */
360
+ addGlobalInterceptor(interceptor) {
361
+ this.globalInterceptors.push(interceptor);
362
+ return this;
363
+ }
364
+ /**
365
+ * Adiciona um ResponseMapper global para padronizar respostas.
366
+ * @param mapper
367
+ */
368
+ withResponseMapper(mapper) {
369
+ this.globalInterceptors.push(new response_pattern_interceptor_1.ResponsePatternInterceptor(mapper));
370
+ return this;
371
+ }
354
372
  async listen() {
355
373
  this.logger.log('Criando a instância da aplicação NestJS...');
356
374
  const rootImports = [this.module, ...this.featureModules];
@@ -388,36 +406,20 @@ class AppInitializer {
388
406
  this.logger.log(`Aplicando o plugin: ${plugin.constructor.name}`);
389
407
  await plugin.apply(this.app);
390
408
  }
409
+ for (const interceptor of this.globalInterceptors) {
410
+ this.app.useGlobalInterceptors(interceptor);
411
+ }
391
412
  if (this.swaggerOptions) {
392
- const config = new swagger_1.DocumentBuilder()
413
+ const documentBuilder = new swagger_1.DocumentBuilder()
393
414
  .setTitle(this.swaggerOptions.title)
394
415
  .setDescription(this.swaggerOptions.description)
395
- .setVersion(this.swaggerOptions.version)
396
- .addBearerAuth()
397
- .build();
398
- const document = swagger_1.SwaggerModule.createDocument(this.app, config);
399
- const swaggerSetupOptions = {};
400
- if (this.advancedSwaggerUiOptions?.customCss) {
401
- swaggerSetupOptions.customCssUrl =
402
- this.advancedSwaggerUiOptions.customCss;
403
- }
404
- if (this.advancedSwaggerUiOptions?.customJs) {
405
- swaggerSetupOptions.customJsUrl =
406
- this.advancedSwaggerUiOptions.customJs;
416
+ .setVersion(this.swaggerOptions.version);
417
+ for (const tag of this.swaggerOptions.tags ?? []) {
418
+ documentBuilder.addTag(tag.name, tag.description);
407
419
  }
408
- swaggerSetupOptions.swaggerOptions = {
409
- docExpansion: 'none',
410
- filter: true,
411
- showRequestDuration: true,
412
- deepLinking: true,
413
- displayRequestDuration: true,
414
- tryItOutEnabled: true,
415
- persistAuthorization: true,
416
- defaultModelsExpandDepth: -1,
417
- operationsSorter: 'alpha',
418
- tagsSorter: 'alpha',
419
- };
420
- swagger_1.SwaggerModule.setup(this.swaggerOptions.path ?? 'docs', this.app, document, swaggerSetupOptions);
420
+ const config = documentBuilder.build();
421
+ const document = swagger_1.SwaggerModule.createDocument(this.app, config, this.swaggerOptions.documentOptions);
422
+ swagger_1.SwaggerModule.setup(this.swaggerOptions.path ?? 'docs', this.app, document, this.swaggerOptions.customOptions);
421
423
  }
422
424
  await this.app.listen(this.port);
423
425
  const appUrl = await this.app.getUrl();
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResponsePatternInterceptor = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ class ResponsePatternInterceptor {
6
+ mapper;
7
+ constructor(mapper) {
8
+ this.mapper = mapper;
9
+ }
10
+ intercept(context, next) {
11
+ return next.handle().pipe((0, rxjs_1.map)((data) => this.mapper(data, context)));
12
+ }
13
+ }
14
+ exports.ResponsePatternInterceptor = ResponsePatternInterceptor;
@@ -1,8 +1,10 @@
1
1
  import { CanActivate, ExceptionFilter, INestApplication, NestInterceptor, PipeTransform, Type, ValidationPipeOptions, VersioningOptions } from '@nestjs/common';
2
2
  import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
3
3
  import { AbstractHttpAdapter } from '@nestjs/core';
4
+ import { SwaggerCustomOptions, SwaggerDocumentOptions } from '@nestjs/swagger';
4
5
  import { TerminusHealthCheckOptions } from '../features';
5
6
  import { CachingStarterOptions, MongooseStarterOptions, TypeOrmStarterOptions } from '../starters';
7
+ import { ResponseMapper } from '../interceptors/response-pattern.interceptor';
6
8
  /**
7
9
  * Interface que todos os plugins do AppInitializer devem implementar.
8
10
  */
@@ -14,6 +16,13 @@ export interface AppInitializerPlugin {
14
16
  */
15
17
  apply(app: INestApplication): Promise<void> | void;
16
18
  }
19
+ /**
20
+ * Representa uma tag na documentação Swagger (OpenAPI).
21
+ */
22
+ export type SwaggerDocumentTags = {
23
+ name: string;
24
+ description?: string;
25
+ };
17
26
  /**
18
27
  * Opções para a configuração do Swagger (OpenAPI).
19
28
  */
@@ -21,7 +30,10 @@ export type SwaggerOptions = {
21
30
  title: string;
22
31
  description: string;
23
32
  version: string;
33
+ tags?: SwaggerDocumentTags[];
24
34
  path?: string;
35
+ documentOptions?: SwaggerDocumentOptions;
36
+ customOptions?: SwaggerCustomOptions;
25
37
  };
26
38
  /**
27
39
  * Assinatura da função de callback usada para configurar o inicializador.
@@ -47,6 +59,7 @@ export declare class AppInitializer<T extends INestApplication = INestApplicatio
47
59
  private advancedSwaggerUiOptions?;
48
60
  private autoDiscoveredComponents?;
49
61
  private readonly globalProviders;
62
+ private readonly globalInterceptors;
50
63
  private readonly factoryGeneratedControllers;
51
64
  private constructor();
52
65
  /**
@@ -201,6 +214,16 @@ export declare class AppInitializer<T extends INestApplication = INestApplicatio
201
214
  withAutoDiscovery(options: {
202
215
  basePath: string;
203
216
  }): this;
217
+ /**
218
+ * Adiciona um Interceptor global diretamente (instância).
219
+ * @param interceptor
220
+ */
221
+ addGlobalInterceptor(interceptor: NestInterceptor): this;
222
+ /**
223
+ * Adiciona um ResponseMapper global para padronizar respostas.
224
+ * @param mapper
225
+ */
226
+ withResponseMapper<T>(mapper: ResponseMapper<T>): this;
204
227
  private listen;
205
228
  }
206
229
  export {};
@@ -0,0 +1,8 @@
1
+ import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
2
+ import { Observable } from 'rxjs';
3
+ export type ResponseMapper<T> = (data: any, context: ExecutionContext) => T;
4
+ export declare class ResponsePatternInterceptor implements NestInterceptor {
5
+ private readonly mapper;
6
+ constructor(mapper: ResponseMapper<any>);
7
+ intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innv/nest-initializer",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "description": "Uma plataforma fluente e opinativa para inicializar e configurar aplicações NestJS com as melhores práticas.",
5
5
  "author": "Innovare",
6
6
  "license": "MIT",