From 90c616ca4128dca7894a89e651b9a0960b9e5135 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Wed, 10 Nov 2021 13:14:40 +0100 Subject: [PATCH] fix(core): update --- ts/typedrequest.classes.typedrouter.ts | 40 +++++++++++--------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/ts/typedrequest.classes.typedrouter.ts b/ts/typedrequest.classes.typedrouter.ts index d9ce8d0..ef1681f 100644 --- a/ts/typedrequest.classes.typedrouter.ts +++ b/ts/typedrequest.classes.typedrouter.ts @@ -9,10 +9,7 @@ import { TypedRequest } from './typedrequest.classes.typedrequest'; * This is thought for reusing the same url endpoint for different methods */ export class TypedRouter { - public upstreamTypedRouter: TypedRouter; - public routerMap = new plugins.lik.ObjectMap(); - public handlerMap = new plugins.lik.ObjectMap< TypedHandler >(); @@ -45,12 +42,11 @@ export class TypedRouter { * @param typedRequest */ public addTypedRouter(typedRouterArg: TypedRouter) { - this.routerMap.add(typedRouterArg); - typedRouterArg.setUpstreamTypedRouter(this); - } - - public setUpstreamTypedRouter(typedRouterArg: TypedRouter) { - this.upstreamTypedRouter = typedRouterArg; + const routerExists = this.routerMap.findSync(routerArg => routerArg === typedRouterArg) + if (!routerExists) { + this.routerMap.add(typedRouterArg); + typedRouterArg.addTypedRouter(this); + } } public checkForTypedHandler(methodArg: string): boolean { @@ -64,24 +60,22 @@ export class TypedRouter { */ public getTypedHandlerForMethod( methodArg: string, - checkUpstreamRouter = true + checkedRouters: TypedRouter[] = [] ): TypedHandler { + checkedRouters.push(this); + let typedHandler: TypedHandler; - if (this.upstreamTypedRouter && checkUpstreamRouter) { - typedHandler = this.upstreamTypedRouter.getTypedHandlerForMethod(methodArg); - } else { - typedHandler = this.handlerMap.findSync((handler) => { - return handler.method === methodArg; + typedHandler = this.handlerMap.findSync((handler) => { + return handler.method === methodArg; + }); + + if (!typedHandler) { + this.routerMap.getArray().forEach((typedRouterArg) => { + if (!typedHandler && !checkedRouters.includes(typedRouterArg)) { + typedHandler = typedRouterArg.getTypedHandlerForMethod(methodArg, checkedRouters); + } }); - - if (!typedHandler) { - this.routerMap.getArray().forEach((typedRouter) => { - if (!typedHandler) { - typedHandler = typedRouter.getTypedHandlerForMethod(methodArg, false); - } - }); - } } return typedHandler;