Skip to content

Commit

Permalink
Merge pull request #6573 from MyAeroCode/lutz-feat/route-injection
Browse files Browse the repository at this point in the history
feat(core): support route injection
  • Loading branch information
kamilmysliwiec authored Mar 12, 2021
2 parents 2cda45b + 3f154cf commit 166ef67
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
9 changes: 5 additions & 4 deletions packages/core/router/router-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,23 @@ 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)]
: routePath.map(p => addLeadingSlash(p));
return {
path,
requestMethod,
targetCallback,
targetCallback: instanceCallback,
methodName,
};
}
Expand Down
86 changes: 82 additions & 4 deletions packages/core/test/router/router-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,55 +109,133 @@ 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', () => {
const instance = new TestRouteAlias();
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', () => {
const instance = new TestRoute();
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', () => {
const instance = new TestRouteAlias();
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);
});
});
});

Expand Down

0 comments on commit 166ef67

Please sign in to comment.