@innv/nest-initializer 0.1.2 → 0.2.0
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.
- package/dist/cjs/core/app-initializer.js +28 -26
- package/dist/cjs/interceptors/response-pattern.interceptor.js +14 -0
- package/dist/esm/core/app-initializer.js +28 -26
- package/dist/esm/interceptors/response-pattern.interceptor.js +14 -0
- package/dist/types/core/app-initializer.d.ts +23 -0
- package/dist/types/interceptors/response-pattern.interceptor.d.ts +8 -0
- package/package.json +2 -2
|
@@ -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
|
|
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
|
-
|
|
397
|
-
.
|
|
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
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
|
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
|
-
|
|
397
|
-
.
|
|
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
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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(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.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@nestjs/mongoose": "^10.0.0 || ^11.0.0",
|
|
53
53
|
"@nestjs/platform-express": "^10.0.0 || ^11.0.0",
|
|
54
54
|
"@nestjs/platform-fastify": "^10.0.0 || ^11.0.0",
|
|
55
|
-
"@nestjs/swagger": "^7.0.0",
|
|
55
|
+
"@nestjs/swagger": "^7.0.0 || ^11.0.0",
|
|
56
56
|
"@nestjs/terminus": "^10.0.0 || ^11.0.0",
|
|
57
57
|
"@nestjs/typeorm": "^10.0.0 || ^11.0.0",
|
|
58
58
|
"axios": "^1.6.0",
|