From 3f154cf0ddec67fe1407f384ccfb8a021fd06481 Mon Sep 17 00:00:00 2001 From: Lutz Date: Fri, 5 Mar 2021 12:14:53 +0900 Subject: [PATCH] feat(core): support route injection --- packages/core/router/router-explorer.ts | 9 +- .../core/test/router/router-explorer.spec.ts | 86 ++++++++++++++++++- 2 files changed, 87 insertions(+), 8 deletions(-) diff --git a/packages/core/router/router-explorer.ts b/packages/core/router/router-explorer.ts index 3dd77072e5d..1d1e8d956d9 100644 --- a/packages/core/router/router-explorer.ts +++ b/packages/core/router/router-explorer.ts @@ -124,14 +124,15 @@ export class RouterExplorer { prototype: object, methodName: string, ): RoutePathProperties { - const targetCallback = prototype[methodName]; - const routePath = Reflect.getMetadata(PATH_METADATA, targetCallback); + const instanceCallback = instance[methodName]; + const prototypeCallback = prototype[methodName]; + const routePath = Reflect.getMetadata(PATH_METADATA, prototypeCallback); if (isUndefined(routePath)) { return null; } const requestMethod: RequestMethod = Reflect.getMetadata( METHOD_METADATA, - targetCallback, + prototypeCallback, ); const path = isString(routePath) ? [addLeadingSlash(routePath)] @@ -139,7 +140,7 @@ export class RouterExplorer { return { path, requestMethod, - targetCallback, + targetCallback: instanceCallback, methodName, }; } diff --git a/packages/core/test/router/router-explorer.spec.ts b/packages/core/test/router/router-explorer.spec.ts index 0d247136000..5025c647e0b 100644 --- a/packages/core/test/router/router-explorer.spec.ts +++ b/packages/core/test/router/router-explorer.spec.ts @@ -109,13 +109,14 @@ describe('RouterExplorer', () => { const instanceProto = Object.getPrototypeOf(instance); const route = routerBuilder.exploreMethodMetadata( - new TestRoute(), + instance, instanceProto, 'getTest', ); expect(route.path).to.eql(['/test']); expect(route.requestMethod).to.eql(RequestMethod.GET); + expect(route.targetCallback).to.eq(instance.getTest); }); it('should method return expected object which represent single route with alias', () => { @@ -123,13 +124,14 @@ describe('RouterExplorer', () => { const instanceProto = Object.getPrototypeOf(instance); const route = routerBuilder.exploreMethodMetadata( - new TestRouteAlias(), + instance, instanceProto, 'getTest', ); expect(route.path).to.eql(['/test']); expect(route.requestMethod).to.eql(RequestMethod.GET); + expect(route.targetCallback).to.eq(instance.getTest); }); it('should method return expected object which represent multiple routes', () => { @@ -137,13 +139,14 @@ describe('RouterExplorer', () => { const instanceProto = Object.getPrototypeOf(instance); const route = routerBuilder.exploreMethodMetadata( - new TestRoute(), + instance, instanceProto, 'getTestUsingArray', ); expect(route.path).to.eql(['/foo', '/bar']); expect(route.requestMethod).to.eql(RequestMethod.GET); + expect(route.targetCallback).to.eq(instance.getTestUsingArray); }); it('should method return expected object which represent multiple routes with alias', () => { @@ -151,13 +154,88 @@ describe('RouterExplorer', () => { const instanceProto = Object.getPrototypeOf(instance); const route = routerBuilder.exploreMethodMetadata( - new TestRouteAlias(), + instance, instanceProto, 'getTestUsingArray', ); expect(route.path).to.eql(['/foo', '/bar']); expect(route.requestMethod).to.eql(RequestMethod.GET); + expect(route.targetCallback).to.eq(instance.getTestUsingArray); + }); + + describe('when new implementation is injected into router', () => { + it('should method return changed impl of single route', () => { + const instance = new TestRoute(); + const instanceProto = Object.getPrototypeOf(instance); + + const newImpl = function () {}; + instance.getTest = newImpl; + + const route = routerBuilder.exploreMethodMetadata( + instance, + instanceProto, + 'getTest', + ); + + expect(route.targetCallback).to.eq(newImpl); + expect(route.path).to.eql(['/test']); + expect(route.requestMethod).to.eql(RequestMethod.GET); + }); + + it('should method return changed impl of single route which alias applied', () => { + const instance = new TestRouteAlias(); + const instanceProto = Object.getPrototypeOf(instance); + + const newImpl = function () {}; + instance.getTest = newImpl; + + const route = routerBuilder.exploreMethodMetadata( + instance, + instanceProto, + 'getTest', + ); + + expect(route.targetCallback).to.eq(newImpl); + expect(route.path).to.eql(['/test']); + expect(route.requestMethod).to.eql(RequestMethod.GET); + }); + + it('should method return changed impl of multiple routes', () => { + const instance = new TestRoute(); + const instanceProto = Object.getPrototypeOf(instance); + + const newImpl = function () {}; + instance.getTestUsingArray = newImpl; + + const route = routerBuilder.exploreMethodMetadata( + instance, + instanceProto, + 'getTestUsingArray', + ); + + expect(route.targetCallback).to.eq(newImpl); + expect(route.path).to.eql(['/foo', '/bar']); + expect(route.requestMethod).to.eql(RequestMethod.GET); + }); + + it('should method return changed impl of multiple routes which alias applied', () => { + const instance = new TestRouteAlias(); + const instanceProto = Object.getPrototypeOf(instance); + + const newImpl = function () {}; + instance.getTestUsingArray = newImpl; + + const route = routerBuilder.exploreMethodMetadata( + instance, + instanceProto, + 'getTestUsingArray', + ); + + expect(route.targetCallback).to.eq(newImpl); + expect(route.path).to.eql(['/foo', '/bar']); + expect(route.requestMethod).to.eql(RequestMethod.GET); + }); }); });