Skip to content

Commit 844a69e

Browse files
fix(core) request scoped middleware fix nestjs#2748
1 parent b4c190b commit 844a69e

4 files changed

Lines changed: 41 additions & 15 deletions

File tree

packages/core/middleware/middleware-module.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ import { RuntimeException } from '../errors/exceptions/runtime.exception';
1414
import { createContextId } from '../helpers/context-id-factory';
1515
import { ExecutionContextHost } from '../helpers/execution-context-host';
1616
import { NestContainer } from '../injector/container';
17-
import { InstanceWrapper } from '../injector/instance-wrapper';
17+
import { ContextId, InstanceWrapper } from '../injector/instance-wrapper';
1818
import { Module } from '../injector/module';
19+
import {
20+
REQUEST,
21+
REQUEST_CONTEXT_ID,
22+
} from '../router/request/request-constants';
1923
import { RouterExceptionFilters } from '../router/router-exception-filters';
2024
import { RouterProxy } from '../router/router-proxy';
2125
import { STATIC_CONTEXT } from './../injector/constants';
@@ -124,17 +128,18 @@ export class MiddlewareModule {
124128
applicationRef: any,
125129
) {
126130
const { forRoutes } = config;
127-
await Promise.all(
128-
forRoutes.map(async (routeInfo: Type<any> | string | RouteInfo) => {
129-
await this.registerRouteMiddleware(
130-
middlewareContainer,
131-
routeInfo as RouteInfo,
132-
config,
133-
module,
134-
applicationRef,
135-
);
136-
}),
137-
);
131+
const registerRouteMiddleware = async (
132+
routeInfo: Type<any> | string | RouteInfo,
133+
) => {
134+
await this.registerRouteMiddleware(
135+
middlewareContainer,
136+
routeInfo as RouteInfo,
137+
config,
138+
module,
139+
applicationRef,
140+
);
141+
};
142+
await Promise.all(forRoutes.map(registerRouteMiddleware));
138143
}
139144

140145
public async registerRouteMiddleware(
@@ -195,7 +200,16 @@ export class MiddlewareModule {
195200
next: () => void,
196201
) => {
197202
try {
198-
const contextId = createContextId();
203+
const contextId = req[REQUEST_CONTEXT_ID] || createContextId();
204+
if (!req[REQUEST_CONTEXT_ID]) {
205+
Object.defineProperty(req, REQUEST_CONTEXT_ID, {
206+
value: contextId,
207+
enumerable: false,
208+
writable: false,
209+
configurable: false,
210+
});
211+
this.registerRequestProvider(req, contextId);
212+
}
199213
const contextInstance = await this.injector.loadPerContext(
200214
instance,
201215
module,
@@ -256,4 +270,14 @@ export class MiddlewareModule {
256270
}
257271
router(basePath + path, proxy);
258272
}
273+
274+
private registerRequestProvider<T = any>(request: T, contextId: ContextId) {
275+
const coreModuleRef = this.container.getInternalCoreModuleRef();
276+
const wrapper = coreModuleRef.getProviderByKey(REQUEST);
277+
278+
wrapper.setInstanceByContextId(contextId, {
279+
instance: request,
280+
isResolved: true,
281+
});
282+
}
259283
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './request-constants';
1+
export { REQUEST } from './request-constants';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const REQUEST = Symbol('REQUEST');
2+
export const REQUEST_CONTEXT_ID = Symbol('REQUEST_CONTEXT_ID');

packages/core/router/router-explorer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { PipesConsumer } from '../pipes/pipes-consumer';
2929
import { PipesContextCreator } from '../pipes/pipes-context-creator';
3030
import { ExceptionsFilter } from './interfaces/exceptions-filter.interface';
3131
import { REQUEST } from './request';
32+
import { REQUEST_CONTEXT_ID } from './request/request-constants';
3233
import { RouteParamsFactory } from './route-params-factory';
3334
import { RouterExecutionContext } from './router-execution-context';
3435
import { RouterProxy, RouterProxyCallback } from './router-proxy';
@@ -256,7 +257,7 @@ export class RouterExplorer {
256257
next: () => void,
257258
) => {
258259
try {
259-
const contextId = createContextId();
260+
const contextId = req[REQUEST_CONTEXT_ID] || createContextId();
260261
this.registerRequestProvider(req, contextId);
261262

262263
const contextInstance = await this.injector.loadPerContext(

0 commit comments

Comments
 (0)