@nestjs/core 8.0.2 → 8.0.6

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
@@ -108,6 +108,7 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
108
108
  <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
109
  <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
110
  <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>
111
+ <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
112
  </tr></table>
112
113
 
113
114
  ## Backers
@@ -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) {
@@ -259,8 +259,8 @@ class Module {
259
259
  return addExportedUnit(provider);
260
260
  }
261
261
  else if (this.isDynamicModule(provider)) {
262
- const { module } = provider;
263
- return addExportedUnit(module.name);
262
+ const { module: moduleClassRef } = provider;
263
+ return addExportedUnit(moduleClassRef);
264
264
  }
265
265
  addExportedUnit(provider);
266
266
  }
@@ -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.2",
3
+ "version": "8.0.6",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@core)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -36,7 +36,7 @@
36
36
  "uuid": "8.3.2"
37
37
  },
38
38
  "devDependencies": {
39
- "@nestjs/common": "8.0.2"
39
+ "@nestjs/common": "8.0.6"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@nestjs/common": "^8.0.0",
@@ -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;