MCPcopy
hub / github.com/nestjs/nest / bindHandler

Method bindHandler

packages/core/middleware/middleware-module.ts:245–301  ·  view source on GitHub ↗
(
    wrapper: InstanceWrapper<NestMiddleware>,
    applicationRef: HttpServer,
    routeInfo: RouteInfo,
    moduleRef: Module,
    collection: Map<InjectionToken, InstanceWrapper>,
  )

Source from the content-addressed store, hash-verified

243 }
244
245 private async bindHandler(
246 wrapper: InstanceWrapper<NestMiddleware>,
247 applicationRef: HttpServer,
248 routeInfo: RouteInfo,
249 moduleRef: Module,
250 collection: Map<InjectionToken, InstanceWrapper>,
251 ) {
252 const { instance, metatype } = wrapper;
253
254 if (isUndefined(instance?.use)) {
255 throw new InvalidMiddlewareException(metatype!.name);
256 }
257 const isStatic = wrapper.isDependencyTreeStatic();
258 if (isStatic) {
259 const proxy = await this.createProxy(instance);
260 return this.registerHandler(applicationRef, routeInfo, proxy);
261 }
262
263 const isTreeDurable = wrapper.isDependencyTreeDurable();
264
265 await this.registerHandler(
266 applicationRef,
267 routeInfo,
268 async <TRequest, TResponse>(
269 req: TRequest,
270 res: TResponse,
271 next: () => void,
272 ) => {
273 try {
274 const contextId = this.getContextId(req, isTreeDurable);
275 const contextInstance = await this.injector.loadPerContext(
276 instance,
277 moduleRef,
278 collection,
279 contextId,
280 );
281 const proxy = await this.createProxy<TRequest, TResponse>(
282 contextInstance,
283 contextId,
284 );
285 return proxy(req, res, next);
286 } catch (err) {
287 let exceptionsHandler = this.exceptionFiltersCache.get(instance.use);
288 if (!exceptionsHandler) {
289 exceptionsHandler = this.routerExceptionFilter.create(
290 instance,
291 instance.use,
292 undefined,
293 );
294 this.exceptionFiltersCache.set(instance.use, exceptionsHandler);
295 }
296 const host = new ExecutionContextHost([req, res, next]);
297 exceptionsHandler.next(err, host);
298 }
299 },
300 );
301 }
302

Callers 1

Calls 11

createProxyMethod · 0.95
registerHandlerMethod · 0.95
getContextIdMethod · 0.95
isUndefinedFunction · 0.90
loadPerContextMethod · 0.80
getMethod · 0.65
createMethod · 0.65
setMethod · 0.65
nextMethod · 0.45

Tested by

no test coverage detected