@nestjs/core 9.2.1 → 9.3.0-beta.2

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.
Files changed (58) hide show
  1. package/LICENSE +1 -1
  2. package/Readme.md +6 -3
  3. package/constants.d.ts +2 -0
  4. package/constants.js +7 -1
  5. package/injector/container.d.ts +9 -5
  6. package/injector/container.js +14 -9
  7. package/injector/injector.d.ts +4 -0
  8. package/injector/injector.js +7 -1
  9. package/injector/instance-loader.d.ts +6 -4
  10. package/injector/instance-loader.js +17 -7
  11. package/injector/instance-wrapper.d.ts +3 -0
  12. package/injector/instance-wrapper.js +10 -1
  13. package/injector/internal-core-module/internal-core-module-factory.d.ts +2 -1
  14. package/injector/internal-core-module/internal-core-module-factory.js +16 -8
  15. package/injector/module.d.ts +18 -7
  16. package/injector/module.js +50 -13
  17. package/injector/modules-container.d.ts +1 -0
  18. package/injector/modules-container.js +3 -0
  19. package/inspector/deterministic-uuid-registry.d.ts +5 -0
  20. package/inspector/deterministic-uuid-registry.js +20 -0
  21. package/inspector/graph-inspector.d.ts +25 -0
  22. package/inspector/graph-inspector.js +136 -0
  23. package/inspector/interfaces/edge.interface.d.ts +28 -0
  24. package/inspector/interfaces/edge.interface.js +2 -0
  25. package/inspector/interfaces/enhancer-metadata-cache-entry.interface.d.ts +12 -0
  26. package/inspector/interfaces/enhancer-metadata-cache-entry.interface.js +2 -0
  27. package/inspector/interfaces/entrypoint.interface.d.ts +23 -0
  28. package/inspector/interfaces/entrypoint.interface.js +2 -0
  29. package/inspector/interfaces/extras.interface.d.ts +18 -0
  30. package/inspector/interfaces/extras.interface.js +2 -0
  31. package/inspector/interfaces/node.interface.d.ts +47 -0
  32. package/inspector/interfaces/node.interface.js +2 -0
  33. package/inspector/noop-graph-inspector.d.ts +2 -0
  34. package/inspector/noop-graph-inspector.js +8 -0
  35. package/inspector/serialized-graph.d.ts +56 -0
  36. package/inspector/serialized-graph.js +104 -0
  37. package/middleware/container.js +2 -1
  38. package/middleware/middleware-module.d.ts +3 -1
  39. package/middleware/middleware-module.js +18 -2
  40. package/middleware/resolver.d.ts +3 -2
  41. package/middleware/resolver.js +7 -8
  42. package/middleware/routes-mapper.d.ts +1 -1
  43. package/middleware/routes-mapper.js +3 -3
  44. package/nest-application-context.d.ts +10 -5
  45. package/nest-application-context.js +39 -4
  46. package/nest-application.d.ts +6 -4
  47. package/nest-application.js +28 -8
  48. package/nest-factory.d.ts +5 -4
  49. package/nest-factory.js +27 -14
  50. package/package.json +2 -2
  51. package/router/paths-explorer.d.ts +18 -0
  52. package/router/paths-explorer.js +37 -0
  53. package/router/router-explorer.d.ts +8 -9
  54. package/router/router-explorer.js +22 -31
  55. package/router/routes-resolver.d.ts +3 -2
  56. package/router/routes-resolver.js +2 -2
  57. package/scanner.d.ts +14 -8
  58. package/scanner.js +67 -36
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SerializedGraph = void 0;
4
+ const application_config_1 = require("../application-config");
5
+ const external_context_creator_1 = require("../helpers/external-context-creator");
6
+ const http_adapter_host_1 = require("../helpers/http-adapter-host");
7
+ const inquirer_constants_1 = require("../injector/inquirer/inquirer-constants");
8
+ const lazy_module_loader_1 = require("../injector/lazy-module-loader/lazy-module-loader");
9
+ const module_ref_1 = require("../injector/module-ref");
10
+ const modules_container_1 = require("../injector/modules-container");
11
+ const request_constants_1 = require("../router/request/request-constants");
12
+ const reflector_service_1 = require("../services/reflector.service");
13
+ const deterministic_uuid_registry_1 = require("./deterministic-uuid-registry");
14
+ class SerializedGraph {
15
+ constructor() {
16
+ this.nodes = new Map();
17
+ this.edges = new Map();
18
+ this.entrypoints = new Map();
19
+ this.extras = {
20
+ orphanedEnhancers: [],
21
+ attachedEnhancers: [],
22
+ };
23
+ }
24
+ insertNode(nodeDefinition) {
25
+ if (nodeDefinition.metadata.type === 'provider' &&
26
+ SerializedGraph.INTERNAL_PROVIDERS.includes(nodeDefinition.metadata.token)) {
27
+ nodeDefinition.metadata = Object.assign(Object.assign({}, nodeDefinition.metadata), { internal: true });
28
+ }
29
+ if (this.nodes.has(nodeDefinition.id)) {
30
+ return this.nodes.get(nodeDefinition.id);
31
+ }
32
+ this.nodes.set(nodeDefinition.id, nodeDefinition);
33
+ return nodeDefinition;
34
+ }
35
+ insertEdge(edgeDefinition) {
36
+ var _a;
37
+ if (edgeDefinition.metadata.type === 'class-to-class' &&
38
+ (SerializedGraph.INTERNAL_PROVIDERS.includes(edgeDefinition.metadata.sourceClassToken) ||
39
+ SerializedGraph.INTERNAL_PROVIDERS.includes(edgeDefinition.metadata.targetClassToken))) {
40
+ edgeDefinition.metadata = Object.assign(Object.assign({}, edgeDefinition.metadata), { internal: true });
41
+ }
42
+ const id = (_a = edgeDefinition.id) !== null && _a !== void 0 ? _a : this.generateUuidByEdgeDefinition(edgeDefinition);
43
+ const edge = Object.assign(Object.assign({}, edgeDefinition), { id });
44
+ this.edges.set(id, edge);
45
+ return edge;
46
+ }
47
+ insertEntrypoint(definition, parentId) {
48
+ if (this.entrypoints.has(parentId)) {
49
+ const existingCollection = this.entrypoints.get(parentId);
50
+ existingCollection.push(definition);
51
+ }
52
+ else {
53
+ this.entrypoints.set(parentId, [definition]);
54
+ }
55
+ }
56
+ insertOrphanedEnhancer(entry) {
57
+ this.extras.orphanedEnhancers.push(entry);
58
+ }
59
+ insertAttachedEnhancer(nodeId) {
60
+ this.extras.attachedEnhancers.push({
61
+ nodeId,
62
+ });
63
+ }
64
+ getNodeById(id) {
65
+ return this.nodes.get(id);
66
+ }
67
+ toJSON() {
68
+ return {
69
+ nodes: Object.fromEntries(this.nodes),
70
+ edges: Object.fromEntries(this.edges),
71
+ entrypoints: Object.fromEntries(this.entrypoints),
72
+ extras: this.extras,
73
+ };
74
+ }
75
+ toString() {
76
+ const replacer = (key, value) => {
77
+ var _a;
78
+ if (typeof value === 'symbol') {
79
+ return value.toString();
80
+ }
81
+ return typeof value === 'function' ? (_a = value.name) !== null && _a !== void 0 ? _a : 'Function' : value;
82
+ };
83
+ return JSON.stringify(this.toJSON(), replacer, 2);
84
+ }
85
+ generateUuidByEdgeDefinition(edgeDefinition) {
86
+ const UUID_NAMESPACE = '7a6d19ab-ec88-419f-a833-062532c468a7';
87
+ return deterministic_uuid_registry_1.DeterministicUuidRegistry.get(JSON.stringify(edgeDefinition), UUID_NAMESPACE);
88
+ }
89
+ }
90
+ exports.SerializedGraph = SerializedGraph;
91
+ SerializedGraph.INTERNAL_PROVIDERS = [
92
+ application_config_1.ApplicationConfig,
93
+ module_ref_1.ModuleRef,
94
+ http_adapter_host_1.HttpAdapterHost,
95
+ lazy_module_loader_1.LazyModuleLoader,
96
+ external_context_creator_1.ExternalContextCreator,
97
+ modules_container_1.ModulesContainer,
98
+ reflector_service_1.Reflector,
99
+ SerializedGraph,
100
+ http_adapter_host_1.HttpAdapterHost.name,
101
+ reflector_service_1.Reflector.name,
102
+ request_constants_1.REQUEST,
103
+ inquirer_constants_1.INQUIRER,
104
+ ];
@@ -25,11 +25,12 @@ class MiddlewareContainer {
25
25
  const targetConfig = this.getTargetConfig(moduleKey);
26
26
  const configurations = configList || [];
27
27
  const insertMiddleware = (metatype) => {
28
+ var _a;
28
29
  const token = metatype;
29
30
  middleware.set(token, new instance_wrapper_1.InstanceWrapper({
30
31
  scope: (0, get_class_scope_1.getClassScope)(metatype),
31
32
  durable: (0, is_durable_1.isDurable)(metatype),
32
- name: token,
33
+ name: (_a = token === null || token === void 0 ? void 0 : token.name) !== null && _a !== void 0 ? _a : token,
33
34
  metatype,
34
35
  token,
35
36
  }));
@@ -4,6 +4,7 @@ import { ApplicationConfig } from '../application-config';
4
4
  import { NestContainer } from '../injector/container';
5
5
  import { Injector } from '../injector/injector';
6
6
  import { Module } from '../injector/module';
7
+ import { GraphInspector } from '../inspector/graph-inspector';
7
8
  import { RoutePathFactory } from '../router/route-path-factory';
8
9
  import { MiddlewareContainer } from './container';
9
10
  export declare class MiddlewareModule {
@@ -17,8 +18,9 @@ export declare class MiddlewareModule {
17
18
  private config;
18
19
  private container;
19
20
  private httpAdapter;
21
+ private graphInspector;
20
22
  constructor(routePathFactory: RoutePathFactory);
21
- register(middlewareContainer: MiddlewareContainer, container: NestContainer, config: ApplicationConfig, injector: Injector, httpAdapter: HttpServer): Promise<void>;
23
+ register(middlewareContainer: MiddlewareContainer, container: NestContainer, config: ApplicationConfig, injector: Injector, httpAdapter: HttpServer, graphInspector: GraphInspector): Promise<void>;
22
24
  resolveMiddleware(middlewareContainer: MiddlewareContainer, modules: Map<string, Module>): Promise<void>;
23
25
  loadConfiguration(middlewareContainer: MiddlewareContainer, moduleRef: Module, moduleKey: string): Promise<void>;
24
26
  registerMiddleware(middlewareContainer: MiddlewareContainer, applicationRef: any): Promise<void>;
@@ -22,15 +22,16 @@ class MiddlewareModule {
22
22
  this.routerProxy = new router_proxy_1.RouterProxy();
23
23
  this.exceptionFiltersCache = new WeakMap();
24
24
  }
25
- async register(middlewareContainer, container, config, injector, httpAdapter) {
25
+ async register(middlewareContainer, container, config, injector, httpAdapter, graphInspector) {
26
26
  const appRef = container.getHttpAdapterRef();
27
27
  this.routerExceptionFilter = new router_exception_filters_1.RouterExceptionFilters(container, config, appRef);
28
28
  this.routesMapper = new routes_mapper_1.RoutesMapper(container);
29
- this.resolver = new resolver_1.MiddlewareResolver(middlewareContainer);
29
+ this.resolver = new resolver_1.MiddlewareResolver(middlewareContainer, injector);
30
30
  this.config = config;
31
31
  this.injector = injector;
32
32
  this.container = container;
33
33
  this.httpAdapter = httpAdapter;
34
+ this.graphInspector = graphInspector;
34
35
  const modules = container.getModules();
35
36
  await this.resolveMiddleware(middlewareContainer, modules);
36
37
  }
@@ -77,6 +78,7 @@ class MiddlewareModule {
77
78
  }
78
79
  }
79
80
  async registerRouteMiddleware(middlewareContainer, routeInfo, config, moduleKey, applicationRef) {
81
+ var _a;
80
82
  const middlewareCollection = [].concat(config.middleware);
81
83
  const moduleRef = this.container.getModuleByKey(moduleKey);
82
84
  for (const metatype of middlewareCollection) {
@@ -88,6 +90,20 @@ class MiddlewareModule {
88
90
  if (instanceWrapper.isTransient) {
89
91
  return;
90
92
  }
93
+ this.graphInspector.insertClassNode(moduleRef, instanceWrapper, 'middleware');
94
+ const middlewareDefinition = {
95
+ type: 'middleware',
96
+ methodName: 'use',
97
+ className: instanceWrapper.name,
98
+ classNodeId: instanceWrapper.id,
99
+ metadata: {
100
+ key: routeInfo.path,
101
+ path: routeInfo.path,
102
+ requestMethod: (_a = request_method_enum_1.RequestMethod[routeInfo.method]) !== null && _a !== void 0 ? _a : 'ALL',
103
+ version: routeInfo.version,
104
+ },
105
+ };
106
+ this.graphInspector.insertEntrypointDefinition(middlewareDefinition, instanceWrapper.id);
91
107
  await this.bindHandler(instanceWrapper, applicationRef, routeInfo, moduleRef, collection);
92
108
  }
93
109
  }
@@ -1,9 +1,10 @@
1
+ import { Injector } from '../injector/injector';
1
2
  import { Module } from '../injector/module';
2
3
  import { MiddlewareContainer } from './container';
3
4
  export declare class MiddlewareResolver {
4
5
  private readonly middlewareContainer;
5
- private readonly instanceLoader;
6
- constructor(middlewareContainer: MiddlewareContainer);
6
+ private readonly injector;
7
+ constructor(middlewareContainer: MiddlewareContainer, injector: Injector);
7
8
  resolveInstances(moduleRef: Module, moduleName: string): Promise<void>;
8
9
  private resolveMiddlewareInstance;
9
10
  }
@@ -1,19 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MiddlewareResolver = void 0;
4
- const injector_1 = require("../injector/injector");
5
4
  class MiddlewareResolver {
6
- constructor(middlewareContainer) {
5
+ constructor(middlewareContainer, injector) {
7
6
  this.middlewareContainer = middlewareContainer;
8
- this.instanceLoader = new injector_1.Injector();
7
+ this.injector = injector;
9
8
  }
10
9
  async resolveInstances(moduleRef, moduleName) {
11
- const middleware = this.middlewareContainer.getMiddlewareCollection(moduleName);
12
- const resolveInstance = async (wrapper) => this.resolveMiddlewareInstance(wrapper, middleware, moduleRef);
13
- await Promise.all([...middleware.values()].map(resolveInstance));
10
+ const middlewareMap = this.middlewareContainer.getMiddlewareCollection(moduleName);
11
+ const resolveInstance = async (wrapper) => this.resolveMiddlewareInstance(wrapper, middlewareMap, moduleRef);
12
+ await Promise.all([...middlewareMap.values()].map(resolveInstance));
14
13
  }
15
- async resolveMiddlewareInstance(wrapper, middleware, moduleRef) {
16
- await this.instanceLoader.loadMiddleware(wrapper, middleware, moduleRef);
14
+ async resolveMiddlewareInstance(wrapper, middlewareMap, moduleRef) {
15
+ await this.injector.loadMiddleware(wrapper, middlewareMap, moduleRef);
17
16
  }
18
17
  }
19
18
  exports.MiddlewareResolver = MiddlewareResolver;
@@ -2,7 +2,7 @@ import { RouteInfo, Type } from '@nestjs/common/interfaces';
2
2
  import { NestContainer } from '../injector/container';
3
3
  export declare class RoutesMapper {
4
4
  private readonly container;
5
- private readonly routerExplorer;
5
+ private readonly pathsExplorer;
6
6
  constructor(container: NestContainer);
7
7
  mapRouteToRouteInfo(route: Type<any> | RouteInfo | string): RouteInfo[];
8
8
  private getRouteInfoFromPath;
@@ -4,12 +4,12 @@ exports.RoutesMapper = void 0;
4
4
  const constants_1 = require("@nestjs/common/constants");
5
5
  const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6
6
  const metadata_scanner_1 = require("../metadata-scanner");
7
- const router_explorer_1 = require("../router/router-explorer");
7
+ const paths_explorer_1 = require("../router/paths-explorer");
8
8
  const router_module_1 = require("../router/router-module");
9
9
  class RoutesMapper {
10
10
  constructor(container) {
11
11
  this.container = container;
12
- this.routerExplorer = new router_explorer_1.RouterExplorer(new metadata_scanner_1.MetadataScanner(), container);
12
+ this.pathsExplorer = new paths_explorer_1.PathsExplorer(new metadata_scanner_1.MetadataScanner());
13
13
  }
14
14
  mapRouteToRouteInfo(route) {
15
15
  if ((0, shared_utils_1.isString)(route)) {
@@ -41,7 +41,7 @@ class RoutesMapper {
41
41
  return [routeInfo];
42
42
  }
43
43
  getRouteInfoFromController(controller, routePath) {
44
- const controllerPaths = this.routerExplorer.scanForPaths(Object.create(controller), controller.prototype);
44
+ const controllerPaths = this.pathsExplorer.scanForPaths(Object.create(controller), controller.prototype);
45
45
  const moduleRef = this.getHostModuleOfController(controller);
46
46
  const modulePath = this.getModulePath(moduleRef === null || moduleRef === void 0 ? void 0 : moduleRef.metatype);
47
47
  const concatPaths = (acc, currentValue) => acc.concat(currentValue);
@@ -1,5 +1,6 @@
1
- import { INestApplicationContext, LoggerService, LogLevel, ShutdownSignal } from '@nestjs/common';
1
+ import { INestApplicationContext, Logger, LoggerService, LogLevel, ShutdownSignal } from '@nestjs/common';
2
2
  import { DynamicModule, Type } from '@nestjs/common/interfaces';
3
+ import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
3
4
  import { AbstractInstanceResolver } from './injector/abstract-instance-resolver';
4
5
  import { NestContainer } from './injector/container';
5
6
  import { Injector } from './injector/injector';
@@ -9,12 +10,14 @@ import { Module } from './injector/module';
9
10
  /**
10
11
  * @publicApi
11
12
  */
12
- export declare class NestApplicationContext extends AbstractInstanceResolver implements INestApplicationContext {
13
+ export declare class NestApplicationContext<TOptions extends NestApplicationContextOptions = NestApplicationContextOptions> extends AbstractInstanceResolver implements INestApplicationContext {
13
14
  protected readonly container: NestContainer;
14
- private readonly scope;
15
+ protected readonly appOptions: TOptions;
15
16
  private contextModule;
17
+ private readonly scope;
16
18
  protected isInitialized: boolean;
17
- protected readonly injector: Injector;
19
+ protected injector: Injector;
20
+ protected readonly logger: Logger;
18
21
  private shouldFlushLogsOnOverride;
19
22
  private readonly activeShutdownSignals;
20
23
  private readonly moduleCompiler;
@@ -22,7 +25,7 @@ export declare class NestApplicationContext extends AbstractInstanceResolver imp
22
25
  private _instanceLinksHost;
23
26
  private _moduleRefsByDistance?;
24
27
  protected get instanceLinksHost(): InstanceLinksHost;
25
- constructor(container: NestContainer, scope?: Type<any>[], contextModule?: Module);
28
+ constructor(container: NestContainer, appOptions?: TOptions, contextModule?: Module, scope?: Type<any>[]);
26
29
  selectContextModule(): void;
27
30
  /**
28
31
  * Allows navigating through the modules tree, for example, to pull out a specific instance from the selected module.
@@ -161,5 +164,7 @@ export declare class NestApplicationContext extends AbstractInstanceResolver imp
161
164
  * modules and children.
162
165
  */
163
166
  protected callBeforeShutdownHook(signal?: string): Promise<void>;
167
+ protected assertNotInPreviewMode(methodName: string): void;
164
168
  private getModulesSortedByDistance;
169
+ private printInPreviewModeWarning;
165
170
  }
@@ -16,16 +16,23 @@ const instance_links_host_1 = require("./injector/instance-links-host");
16
16
  * @publicApi
17
17
  */
18
18
  class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstanceResolver {
19
- constructor(container, scope = new Array(), contextModule = null) {
19
+ constructor(container, appOptions = {}, contextModule = null, scope = new Array()) {
20
20
  super();
21
21
  this.container = container;
22
- this.scope = scope;
22
+ this.appOptions = appOptions;
23
23
  this.contextModule = contextModule;
24
+ this.scope = scope;
24
25
  this.isInitialized = false;
25
- this.injector = new injector_1.Injector();
26
+ this.logger = new common_1.Logger(NestApplicationContext.name, {
27
+ timestamp: true,
28
+ });
26
29
  this.shouldFlushLogsOnOverride = false;
27
30
  this.activeShutdownSignals = new Array();
28
31
  this.moduleCompiler = new compiler_1.ModuleCompiler();
32
+ this.injector = new injector_1.Injector();
33
+ if (this.appOptions.preview) {
34
+ this.printInPreviewModeWarning();
35
+ }
29
36
  }
30
37
  get instanceLinksHost() {
31
38
  if (!this._instanceLinksHost) {
@@ -52,7 +59,7 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
52
59
  if (!selectedModule) {
53
60
  throw new exceptions_1.UnknownModuleException();
54
61
  }
55
- return new NestApplicationContext(this.container, scope, selectedModule);
62
+ return new NestApplicationContext(this.container, this.appOptions, selectedModule, scope);
56
63
  }
57
64
  /**
58
65
  * Retrieves an instance (or a list of instances) of either injectable or controller, otherwise, throws exception.
@@ -205,6 +212,9 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
205
212
  * modules and its children.
206
213
  */
207
214
  async callInitHook() {
215
+ if (this.appOptions.preview) {
216
+ return;
217
+ }
208
218
  const modulesSortedByDistance = this.getModulesSortedByDistance();
209
219
  for (const module of modulesSortedByDistance) {
210
220
  await (0, hooks_1.callModuleInitHook)(module);
@@ -215,6 +225,9 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
215
225
  * modules and its children.
216
226
  */
217
227
  async callDestroyHook() {
228
+ if (this.appOptions.preview) {
229
+ return;
230
+ }
218
231
  const modulesSortedByDistance = this.getModulesSortedByDistance();
219
232
  for (const module of modulesSortedByDistance) {
220
233
  await (0, hooks_1.callModuleDestroyHook)(module);
@@ -225,6 +238,9 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
225
238
  * modules and its children.
226
239
  */
227
240
  async callBootstrapHook() {
241
+ if (this.appOptions.preview) {
242
+ return;
243
+ }
228
244
  const modulesSortedByDistance = this.getModulesSortedByDistance();
229
245
  for (const module of modulesSortedByDistance) {
230
246
  await (0, hooks_1.callModuleBootstrapHook)(module);
@@ -235,6 +251,9 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
235
251
  * modules and children.
236
252
  */
237
253
  async callShutdownHook(signal) {
254
+ if (this.appOptions.preview) {
255
+ return;
256
+ }
238
257
  const modulesSortedByDistance = this.getModulesSortedByDistance();
239
258
  for (const module of modulesSortedByDistance) {
240
259
  await (0, hooks_1.callAppShutdownHook)(module, signal);
@@ -245,11 +264,21 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
245
264
  * modules and children.
246
265
  */
247
266
  async callBeforeShutdownHook(signal) {
267
+ if (this.appOptions.preview) {
268
+ return;
269
+ }
248
270
  const modulesSortedByDistance = this.getModulesSortedByDistance();
249
271
  for (const module of modulesSortedByDistance) {
250
272
  await (0, hooks_1.callBeforeAppShutdownHook)(module, signal);
251
273
  }
252
274
  }
275
+ assertNotInPreviewMode(methodName) {
276
+ if (this.appOptions.preview) {
277
+ const error = `Calling the "${methodName}" in the preview mode is not supported.`;
278
+ this.logger.error(error);
279
+ throw new Error(error);
280
+ }
281
+ }
253
282
  getModulesSortedByDistance() {
254
283
  if (this._moduleRefsByDistance) {
255
284
  return this._moduleRefsByDistance;
@@ -259,5 +288,11 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
259
288
  this._moduleRefsByDistance = Array.from(modulesContainer.values()).sort(compareFn);
260
289
  return this._moduleRefsByDistance;
261
290
  }
291
+ printInPreviewModeWarning() {
292
+ this.logger.warn('------------------------------------------------');
293
+ this.logger.warn('Application is running in the PREVIEW mode!');
294
+ this.logger.warn('Providers/controllers will not be instantiated.');
295
+ this.logger.warn('------------------------------------------------');
296
+ }
262
297
  }
263
298
  exports.NestApplicationContext = NestApplicationContext;
@@ -1,18 +1,20 @@
1
1
  import { CanActivate, ExceptionFilter, HttpServer, INestApplication, INestMicroservice, NestHybridApplicationOptions, NestInterceptor, PipeTransform, VersioningOptions, WebSocketAdapter } from '@nestjs/common';
2
2
  import { GlobalPrefixOptions, NestApplicationOptions } from '@nestjs/common/interfaces';
3
3
  import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface';
4
+ import { Logger } from '@nestjs/common/services/logger.service';
4
5
  import { AbstractHttpAdapter } from './adapters';
5
6
  import { ApplicationConfig } from './application-config';
6
7
  import { NestContainer } from './injector/container';
8
+ import { GraphInspector } from './inspector/graph-inspector';
7
9
  import { NestApplicationContext } from './nest-application-context';
8
10
  /**
9
11
  * @publicApi
10
12
  */
11
- export declare class NestApplication extends NestApplicationContext implements INestApplication {
13
+ export declare class NestApplication extends NestApplicationContext<NestApplicationOptions> implements INestApplication {
12
14
  private readonly httpAdapter;
13
15
  private readonly config;
14
- private readonly appOptions;
15
- private readonly logger;
16
+ private readonly graphInspector;
17
+ protected readonly logger: Logger;
16
18
  private readonly middlewareModule;
17
19
  private readonly middlewareContainer;
18
20
  private readonly microservicesModule;
@@ -21,7 +23,7 @@ export declare class NestApplication extends NestApplicationContext implements I
21
23
  private readonly microservices;
22
24
  private httpServer;
23
25
  private isListening;
24
- constructor(container: NestContainer, httpAdapter: HttpServer, config: ApplicationConfig, appOptions?: NestApplicationOptions);
26
+ constructor(container: NestContainer, httpAdapter: HttpServer, config: ApplicationConfig, graphInspector: GraphInspector, appOptions?: NestApplicationOptions);
25
27
  protected dispose(): Promise<void>;
26
28
  getHttpAdapter(): AbstractHttpAdapter;
27
29
  registerHttpServer(): void;
@@ -11,6 +11,7 @@ const pathToRegexp = require("path-to-regexp");
11
11
  const application_config_1 = require("./application-config");
12
12
  const constants_1 = require("./constants");
13
13
  const optional_require_1 = require("./helpers/optional-require");
14
+ const injector_1 = require("./injector/injector");
14
15
  const container_1 = require("./middleware/container");
15
16
  const middleware_module_1 = require("./middleware/middleware-module");
16
17
  const nest_application_context_1 = require("./nest-application-context");
@@ -22,11 +23,11 @@ const { MicroservicesModule } = (0, optional_require_1.optionalRequire)('@nestjs
22
23
  * @publicApi
23
24
  */
24
25
  class NestApplication extends nest_application_context_1.NestApplicationContext {
25
- constructor(container, httpAdapter, config, appOptions = {}) {
26
- super(container);
26
+ constructor(container, httpAdapter, config, graphInspector, appOptions = {}) {
27
+ super(container, appOptions);
27
28
  this.httpAdapter = httpAdapter;
28
29
  this.config = config;
29
- this.appOptions = appOptions;
30
+ this.graphInspector = graphInspector;
30
31
  this.logger = new logger_service_1.Logger(NestApplication.name, {
31
32
  timestamp: true,
32
33
  });
@@ -37,8 +38,9 @@ class NestApplication extends nest_application_context_1.NestApplicationContext
37
38
  this.isListening = false;
38
39
  this.selectContextModule();
39
40
  this.registerHttpServer();
41
+ this.injector = new injector_1.Injector({ preview: this.appOptions.preview });
40
42
  this.middlewareModule = new middleware_module_1.MiddlewareModule(new route_path_factory_1.RoutePathFactory(config));
41
- this.routesResolver = new routes_resolver_1.RoutesResolver(this.container, this.config, this.injector);
43
+ this.routesResolver = new routes_resolver_1.RoutesResolver(this.container, this.config, this.injector, this.graphInspector);
42
44
  }
43
45
  async dispose() {
44
46
  this.socketModule && (await this.socketModule.close());
@@ -75,16 +77,16 @@ class NestApplication extends nest_application_context_1.NestApplicationContext
75
77
  async registerModules() {
76
78
  this.registerWsModule();
77
79
  if (this.microservicesModule) {
78
- this.microservicesModule.register(this.container, this.config);
80
+ this.microservicesModule.register(this.container, this.graphInspector, this.config, this.appOptions);
79
81
  this.microservicesModule.setupClients(this.container);
80
82
  }
81
- await this.middlewareModule.register(this.middlewareContainer, this.container, this.config, this.injector, this.httpAdapter);
83
+ await this.middlewareModule.register(this.middlewareContainer, this.container, this.config, this.injector, this.httpAdapter, this.graphInspector);
82
84
  }
83
85
  registerWsModule() {
84
86
  if (!this.socketModule) {
85
87
  return;
86
88
  }
87
- this.socketModule.register(this.container, this.config, this.httpServer);
89
+ this.socketModule.register(this.container, this.config, this.graphInspector, this.appOptions, this.httpServer);
88
90
  }
89
91
  async init() {
90
92
  var _a;
@@ -123,7 +125,7 @@ class NestApplication extends nest_application_context_1.NestApplicationContext
123
125
  const applicationConfig = inheritAppConfig
124
126
  ? this.config
125
127
  : new application_config_1.ApplicationConfig();
126
- const instance = new NestMicroservice(this.container, microserviceOptions, applicationConfig);
128
+ const instance = new NestMicroservice(this.container, microserviceOptions, this.graphInspector, applicationConfig);
127
129
  instance.registerListeners();
128
130
  instance.setIsInitialized(true);
129
131
  instance.setIsInitHookCalled(true);
@@ -137,6 +139,7 @@ class NestApplication extends nest_application_context_1.NestApplicationContext
137
139
  return this.httpServer;
138
140
  }
139
141
  async startAllMicroservices() {
142
+ this.assertNotInPreviewMode('startAllMicroservices');
140
143
  await Promise.all(this.microservices.map(msvc => msvc.listen()));
141
144
  return this;
142
145
  }
@@ -152,6 +155,7 @@ class NestApplication extends nest_application_context_1.NestApplicationContext
152
155
  return this;
153
156
  }
154
157
  async listen(port, ...args) {
158
+ this.assertNotInPreviewMode('listen');
155
159
  !this.isInitialized && (await this.init());
156
160
  return new Promise((resolve, reject) => {
157
161
  const errorHandler = (e) => {
@@ -242,18 +246,34 @@ class NestApplication extends nest_application_context_1.NestApplicationContext
242
246
  }
243
247
  useGlobalFilters(...filters) {
244
248
  this.config.useGlobalFilters(...filters);
249
+ filters.forEach(item => this.graphInspector.insertOrphanedEnhancer({
250
+ subtype: 'filter',
251
+ ref: item,
252
+ }));
245
253
  return this;
246
254
  }
247
255
  useGlobalPipes(...pipes) {
248
256
  this.config.useGlobalPipes(...pipes);
257
+ pipes.forEach(item => this.graphInspector.insertOrphanedEnhancer({
258
+ subtype: 'pipe',
259
+ ref: item,
260
+ }));
249
261
  return this;
250
262
  }
251
263
  useGlobalInterceptors(...interceptors) {
252
264
  this.config.useGlobalInterceptors(...interceptors);
265
+ interceptors.forEach(item => this.graphInspector.insertOrphanedEnhancer({
266
+ subtype: 'interceptor',
267
+ ref: item,
268
+ }));
253
269
  return this;
254
270
  }
255
271
  useGlobalGuards(...guards) {
256
272
  this.config.useGlobalGuards(...guards);
273
+ guards.forEach(item => this.graphInspector.insertOrphanedEnhancer({
274
+ subtype: 'guard',
275
+ ref: item,
276
+ }));
257
277
  return this;
258
278
  }
259
279
  useStaticAssets(pathOrOptions, options) {
package/nest-factory.d.ts CHANGED
@@ -35,23 +35,23 @@ export declare class NestFactoryStatic {
35
35
  /**
36
36
  * Creates an instance of NestMicroservice.
37
37
  *
38
- * @param module Entry (root) application module class
38
+ * @param moduleCls Entry (root) application module class
39
39
  * @param options Optional microservice configuration
40
40
  *
41
41
  * @returns A promise that, when resolved,
42
42
  * contains a reference to the NestMicroservice instance.
43
43
  */
44
- createMicroservice<T extends object>(module: any, options?: NestMicroserviceOptions & T): Promise<INestMicroservice>;
44
+ createMicroservice<T extends object>(moduleCls: any, options?: NestMicroserviceOptions & T): Promise<INestMicroservice>;
45
45
  /**
46
46
  * Creates an instance of NestApplicationContext.
47
47
  *
48
- * @param module Entry (root) application module class
48
+ * @param moduleCls Entry (root) application module class
49
49
  * @param options Optional Nest application configuration
50
50
  *
51
51
  * @returns A promise that, when resolved,
52
52
  * contains a reference to the NestApplicationContext instance.
53
53
  */
54
- createApplicationContext(module: any, options?: NestApplicationContextOptions): Promise<INestApplicationContext>;
54
+ createApplicationContext(moduleCls: any, options?: NestApplicationContextOptions): Promise<INestApplicationContext>;
55
55
  private createNestInstance;
56
56
  private initialize;
57
57
  private handleInitializationError;
@@ -63,6 +63,7 @@ export declare class NestFactoryStatic {
63
63
  private isHttpServer;
64
64
  private setAbortOnError;
65
65
  private createAdapterProxy;
66
+ private createGraphInspector;
66
67
  }
67
68
  /**
68
69
  * Use NestFactory to create an application instance.