@nestjs/core 8.0.3 → 8.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.
package/Readme.md CHANGED
@@ -67,8 +67,11 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
67
67
 
68
68
  <table style="text-align:center;"><tr><td>
69
69
  <a href="https://careers.labster.com/departments/platform" target="_blank"><img src="https://nestjs.com/img/labster-logo.png" width="170" valign="middle" /></a></td><td>
70
- <a href="https://weld.so/" target="_blank"><img src="https://nestjs.com/img/weld-logo.svg" width="150" valign="middle" /></a></td><td>
71
- <a href="https://valor-software.com/" target="_blank"><img src="https://docs.nestjs.com/assets/sponsors/valor-software.png" width="170" valign="middle" /></a></td></tr></table>
70
+ <a href="https://weld.app/" target="_blank"><img src="https://nestjs.com/img/weld-logo.svg" width="150" valign="middle" /></a></td>
71
+ <td>
72
+ <a href="https://valor-software.com/" target="_blank"><img src="https://docs.nestjs.com/assets/sponsors/valor-software.png" width="170" valign="middle" /></a></td>
73
+ <td>
74
+ <a href="https://intrinsic.ventures/" target="_blank"><img src="https://nestjs.com/img/intrinisic-logo.png" width="210" valign="middle" /></a></td></</tr></table>
72
75
 
73
76
  #### Silver Sponsors
74
77
 
@@ -76,7 +79,7 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
76
79
  <a href="https://neoteric.eu/" target="_blank"><img src="https://nestjs.com/img/neoteric-cut.png" width="120" valign="middle" /></a> </td><td>
77
80
  <a href="http://gojob.com" target="_blank"><img src="http://nestjs.com/img/gojob-logo.png" valign="middle" width="100" /></a> </td><td>
78
81
  <a href="http://www.leogistics.com" target="_blank"><img src="https://nestjs.com/img/leogistics-logo.jpeg" width="150" valign="middle" /></td><td>
79
- <a href="http://www.meetdandy.com" target="_blank"><img src="https://nestjs.com/img/dandy-wide-logo.png" width="150" valign="middle" /></td><td>
82
+ <a href="http://www.meetdandy.com" target="_blank"><img src="https://nestjs.com/img/dandy-roles-logo.svg" width="150" valign="middle" /></td><td>
80
83
  <a href="https://www.castlecraft.in" target="_blank"><img src="https://nestjs.com/img/castlecraft-logo.png" width="150" valign="middle" /></td></tr></table>
81
84
 
82
85
  #### Sponsors
@@ -108,6 +111,7 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
108
111
  <td align="center" valign="middle"><a href="https://www.bystored.com/" target="_blank"><img src="https://nestjs.com/img/stored-logo.svg" width="110" valign="middle" /></a></td>
109
112
  <td align="center" valign="middle"><a href="https://studyclerk.com/pay-for-research-paper" target="_blank"><img src="https://nestjs.com/img/studyclerk-logo.png" width="125" valign="middle" /></a></td><td align="center" valign="middle"><a href="https://xyndata.com" target="_blank"><img src="https://nestjs.com/img/xyndata-logo.png" width="125" valign="middle" /></a></td></tr><tr>
110
113
  <td align="center" valign="middle"><a href="https://www.anonymistic.com/" target="_blank"><img src="https://nestjs.com/img/anonymistic-logo.png" width="125" valign="middle" /></a></td>
114
+ <td align="center" valign="middle"><a href="https://www.naologic.com/" target="_blank"><img src="https://nestjs.com/img/naologic-logo.svg" width="125" valign="middle" /></a></td>
111
115
  </tr></table>
112
116
 
113
117
  ## Backers
@@ -6,9 +6,9 @@ import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-applicati
6
6
  * @publicApi
7
7
  */
8
8
  export declare abstract class AbstractHttpAdapter<TServer = any, TRequest = any, TResponse = any> implements HttpServer<TRequest, TResponse> {
9
- protected readonly instance: any;
9
+ protected instance?: any;
10
10
  protected httpServer: TServer;
11
- constructor(instance: any);
11
+ constructor(instance?: any);
12
12
  init(): Promise<void>;
13
13
  use(...args: any[]): any;
14
14
  get(handler: RequestHandler): any;
@@ -31,6 +31,7 @@ export declare abstract class AbstractHttpAdapter<TServer = any, TRequest = any,
31
31
  listen(port: string | number, hostname: string, callback?: () => void): any;
32
32
  getHttpServer(): TServer;
33
33
  setHttpServer(httpServer: TServer): void;
34
+ setInstance<T = any>(instance: T): void;
34
35
  getInstance<T = any>(): T;
35
36
  abstract close(): any;
36
37
  abstract initHttpServer(options: NestApplicationOptions): any;
@@ -46,6 +46,9 @@ class AbstractHttpAdapter {
46
46
  setHttpServer(httpServer) {
47
47
  this.httpServer = httpServer;
48
48
  }
49
+ setInstance(instance) {
50
+ this.instance = instance;
51
+ }
49
52
  getInstance() {
50
53
  return this.instance;
51
54
  }
@@ -32,7 +32,12 @@ function callOperator(instances, signal) {
32
32
  async function callBeforeAppShutdownHook(module, signal) {
33
33
  const providers = module.getNonAliasProviders();
34
34
  const [_, moduleClassHost] = providers.shift();
35
- const instances = [...module.controllers, ...providers];
35
+ const instances = [
36
+ ...module.controllers,
37
+ ...providers,
38
+ ...module.injectables,
39
+ ...module.middlewares,
40
+ ];
36
41
  const nonTransientInstances = transient_instances_1.getNonTransientInstances(instances);
37
42
  await Promise.all(callOperator(nonTransientInstances, signal));
38
43
  const transientInstances = transient_instances_1.getTransientInstances(instances);
@@ -78,7 +78,7 @@ class Injector {
78
78
  return done;
79
79
  }
80
80
  async resolveConstructorParams(wrapper, moduleRef, inject, callback, contextId = constants_2.STATIC_CONTEXT, inquirer, parentInquirer) {
81
- const inquirerId = this.getInquirerId(inquirer);
81
+ let inquirerId = this.getInquirerId(inquirer);
82
82
  const metadata = wrapper.getCtorMetadata();
83
83
  if (metadata && contextId !== constants_2.STATIC_CONTEXT) {
84
84
  const deps = await this.loadCtorMetadata(metadata, contextId, inquirer, parentInquirer);
@@ -96,6 +96,10 @@ class Injector {
96
96
  if (this.isInquirer(param, parentInquirer)) {
97
97
  return parentInquirer && parentInquirer.instance;
98
98
  }
99
+ if ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.isTransient) && parentInquirer) {
100
+ inquirer = parentInquirer;
101
+ inquirerId = this.getInquirerId(parentInquirer);
102
+ }
99
103
  const paramWrapper = await this.resolveSingleParam(wrapper, param, { index, dependencies }, moduleRef, contextId, inquirer, index);
100
104
  const instanceHost = paramWrapper.getInstanceByContextId(contextId, inquirerId);
101
105
  if (!instanceHost.isResolved && !paramWrapper.forwardRef) {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MiddlewareModule = void 0;
4
+ const request_method_enum_1 = require("@nestjs/common/enums/request-method.enum");
4
5
  const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
5
6
  const invalid_middleware_exception_1 = require("../errors/exceptions/invalid-middleware.exception");
6
7
  const runtime_exception_1 = require("../errors/exceptions/runtime.exception");
@@ -13,6 +14,7 @@ const router_proxy_1 = require("../router/router-proxy");
13
14
  const builder_1 = require("./builder");
14
15
  const resolver_1 = require("./resolver");
15
16
  const routes_mapper_1 = require("./routes-mapper");
17
+ const utils_1 = require("./utils");
16
18
  class MiddlewareModule {
17
19
  constructor() {
18
20
  this.routerProxy = new router_proxy_1.RouterProxy();
@@ -92,13 +94,12 @@ class MiddlewareModule {
92
94
  if (shared_utils_1.isUndefined(instance === null || instance === void 0 ? void 0 : instance.use)) {
93
95
  throw new invalid_middleware_exception_1.InvalidMiddlewareException(metatype.name);
94
96
  }
95
- const router = await applicationRef.createMiddlewareFactory(method);
96
97
  const isStatic = wrapper.isDependencyTreeStatic();
97
98
  if (isStatic) {
98
99
  const proxy = await this.createProxy(instance);
99
- return this.registerHandler(router, path, proxy);
100
+ return this.registerHandler(applicationRef, method, path, proxy);
100
101
  }
101
- this.registerHandler(router, path, async (req, res, next) => {
102
+ await this.registerHandler(applicationRef, method, path, async (req, res, next) => {
102
103
  try {
103
104
  const contextId = context_id_factory_1.ContextIdFactory.getByRequest(req);
104
105
  if (!req[request_constants_1.REQUEST_CONTEXT_ID]) {
@@ -130,15 +131,25 @@ class MiddlewareModule {
130
131
  const middleware = instance.use.bind(instance);
131
132
  return this.routerProxy.createProxy(middleware, exceptionsHandler);
132
133
  }
133
- registerHandler(router, path, proxy) {
134
+ async registerHandler(applicationRef, method, path, proxy) {
134
135
  const prefix = this.config.getGlobalPrefix();
135
136
  const basePath = shared_utils_1.addLeadingSlash(prefix);
136
- if (basePath && path === '/*') {
137
+ if ((basePath === null || basePath === void 0 ? void 0 : basePath.endsWith('/')) && (path === null || path === void 0 ? void 0 : path.startsWith('/'))) {
137
138
  // strip slash when a wildcard is being used
138
139
  // and global prefix has been set
139
- path = '*';
140
+ path = path === null || path === void 0 ? void 0 : path.slice(1);
140
141
  }
141
- router(basePath + path, proxy);
142
+ const isMethodAll = utils_1.isRequestMethodAll(method);
143
+ const requestMethod = request_method_enum_1.RequestMethod[method];
144
+ const router = await applicationRef.createMiddlewareFactory(method);
145
+ router(basePath + path, isMethodAll
146
+ ? proxy
147
+ : (req, res, next) => {
148
+ if (applicationRef.getRequestMethod(req) === requestMethod) {
149
+ return proxy(req, res, next);
150
+ }
151
+ return next();
152
+ });
142
153
  }
143
154
  }
144
155
  exports.MiddlewareModule = MiddlewareModule;
@@ -1,7 +1,9 @@
1
+ import { RequestMethod } from '@nestjs/common';
1
2
  import { HttpServer, RouteInfo, Type } from '@nestjs/common/interfaces';
2
3
  declare type RouteInfoRegex = RouteInfo & {
3
4
  regex: RegExp;
4
5
  };
6
+ export declare const isRequestMethodAll: (method: RequestMethod) => boolean;
5
7
  export declare const filterMiddleware: <T extends Function | Type<any> = any>(middleware: T[], excludedRoutes: RouteInfo[], httpAdapter: HttpServer) => Type<any>[];
6
8
  export declare const mapToClass: <T extends Function | Type<any>>(middleware: T, excludedRoutes: RouteInfoRegex[], httpAdapter: HttpServer) => Type<any>;
7
9
  export declare function isMiddlewareClass(middleware: any): middleware is Type<any>;
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isRouteExcluded = exports.assignToken = exports.isMiddlewareClass = exports.mapToClass = exports.filterMiddleware = void 0;
4
- /* eslint-disable @typescript-eslint/no-use-before-define */
3
+ exports.isRouteExcluded = exports.assignToken = exports.isMiddlewareClass = exports.mapToClass = exports.filterMiddleware = exports.isRequestMethodAll = void 0;
5
4
  const common_1 = require("@nestjs/common");
6
5
  const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
7
6
  const iterare_1 = require("iterare");
8
7
  const pathToRegexp = require("path-to-regexp");
9
8
  const uuid_1 = require("uuid");
9
+ const isRequestMethodAll = (method) => {
10
+ return common_1.RequestMethod.ALL === method || method === -1;
11
+ };
12
+ exports.isRequestMethodAll = isRequestMethodAll;
10
13
  const filterMiddleware = (middleware, excludedRoutes, httpAdapter) => {
11
14
  const excluded = excludedRoutes.map(route => (Object.assign(Object.assign({}, route), { regex: pathToRegexp(route.path) })));
12
15
  return iterare_1.iterate([])
@@ -75,9 +78,7 @@ function isRouteExcluded(req, excludedRoutes, httpAdapter) {
75
78
  ? originalUrl.slice(0, queryParamsIndex)
76
79
  : originalUrl;
77
80
  const isExcluded = excludedRoutes.some(({ method, regex }) => {
78
- if (common_1.RequestMethod.ALL === method ||
79
- common_1.RequestMethod[method] === reqMethod ||
80
- method === -1) {
81
+ if (exports.isRequestMethodAll(method) || common_1.RequestMethod[method] === reqMethod) {
81
82
  return regex.exec(pathname);
82
83
  }
83
84
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/core",
3
- "version": "8.0.3",
3
+ "version": "8.0.7",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@core)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -32,11 +32,11 @@
32
32
  "iterare": "1.2.1",
33
33
  "object-hash": "2.2.0",
34
34
  "path-to-regexp": "3.2.0",
35
- "tslib": "2.3.0",
35
+ "tslib": "2.3.1",
36
36
  "uuid": "8.3.2"
37
37
  },
38
38
  "devDependencies": {
39
- "@nestjs/common": "8.0.3"
39
+ "@nestjs/common": "8.0.7"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@nestjs/common": "^8.0.0",
@@ -112,7 +112,7 @@ class RouterExplorer {
112
112
  if (isVersioned &&
113
113
  routePathMetadata.versioningOptions.type !== version_type_enum_1.VersioningType.URI) {
114
114
  // All versioning (except for URI Versioning) is done via the "Version Filter"
115
- routeHandler = this.applyVersionFilter(routePathMetadata, routeHandler);
115
+ routeHandler = this.applyVersionFilter(router, routePathMetadata, routeHandler);
116
116
  }
117
117
  routePathMetadata.methodPath = path;
118
118
  const pathsToRegister = this.routePathFactory.create(routePathMetadata, requestMethod);
@@ -159,9 +159,16 @@ class RouterExplorer {
159
159
  return next();
160
160
  };
161
161
  }
162
- applyVersionFilter(routePathMetadata, handler) {
162
+ applyVersionFilter(router, routePathMetadata, handler) {
163
163
  const { versioningOptions } = routePathMetadata;
164
164
  const version = this.routePathFactory.getVersion(routePathMetadata);
165
+ if (router === null || router === void 0 ? void 0 : router.applyVersionFilter) {
166
+ return router.applyVersionFilter(handler, version, versioningOptions);
167
+ }
168
+ /**
169
+ * This can be removed in the next major release.
170
+ * Left for backward-compatibility.
171
+ */
165
172
  return (req, res, next) => {
166
173
  var _a, _b, _c, _d;
167
174
  if (version === version_options_interface_1.VERSION_NEUTRAL) {
@@ -12,6 +12,7 @@ export declare class RouterModule {
12
12
  private readonly routes;
13
13
  constructor(modulesContainer: ModulesContainer, routes: Routes);
14
14
  static register(routes: Routes): DynamicModule;
15
+ private deepCloneRoutes;
15
16
  private initialize;
16
17
  private registerModulePathMetadata;
17
18
  private updateTargetModulesCache;
@@ -17,6 +17,7 @@ let RouterModule = RouterModule_1 = class RouterModule {
17
17
  constructor(modulesContainer, routes) {
18
18
  this.modulesContainer = modulesContainer;
19
19
  this.routes = routes;
20
+ this.routes = this.deepCloneRoutes(routes);
20
21
  this.initialize();
21
22
  }
22
23
  static register(routes) {
@@ -30,6 +31,17 @@ let RouterModule = RouterModule_1 = class RouterModule {
30
31
  ],
31
32
  };
32
33
  }
34
+ deepCloneRoutes(routes) {
35
+ return routes.map((routeOrType) => {
36
+ if (typeof routeOrType === 'function') {
37
+ return routeOrType;
38
+ }
39
+ if (routeOrType.children) {
40
+ return Object.assign(Object.assign({}, routeOrType), { children: this.deepCloneRoutes(routeOrType.children) });
41
+ }
42
+ return Object.assign({}, routeOrType);
43
+ });
44
+ }
33
45
  initialize() {
34
46
  const flattenedRoutes = utils_1.flattenRoutePaths(this.routes);
35
47
  flattenedRoutes.forEach(route => {
package/scanner.js CHANGED
@@ -159,15 +159,17 @@ class DependenciesScanner {
159
159
  // Skip "InternalCoreModule" from calculating distance
160
160
  modulesGenerator.next();
161
161
  const modulesStack = [];
162
- const calculateDistance = (moduleRef, distance = 0) => {
162
+ const calculateDistance = (moduleRef, distance = 1) => {
163
163
  if (modulesStack.includes(moduleRef)) {
164
164
  return;
165
165
  }
166
166
  modulesStack.push(moduleRef);
167
167
  const moduleImports = moduleRef.imports;
168
168
  moduleImports.forEach(importedModuleRef => {
169
- importedModuleRef.distance = distance;
170
- calculateDistance(importedModuleRef, distance + 1);
169
+ if (importedModuleRef) {
170
+ importedModuleRef.distance = distance;
171
+ calculateDistance(importedModuleRef, distance + 1);
172
+ }
171
173
  });
172
174
  };
173
175
  const rootModule = modulesGenerator.next().value;