Skip to content

Commit

Permalink
Merge 5e9e385 into 1812567
Browse files Browse the repository at this point in the history
  • Loading branch information
lizardruss committed Feb 2, 2018
2 parents 1812567 + 5e9e385 commit 2742fbd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/core/middlewares/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { Metatype } from '@nestjs/common/interfaces';

export const filterMiddlewares = middlewares => {
export function filterMiddlewares(middlewares) {
return []
.concat(middlewares)
.filter(isFunction)
.map(middleware => mapToClass(middleware));
};

export const mapToClass = middleware => {
if (this.isClass(middleware)) {
export function mapToClass(middleware) {
if (isClass(middleware)) {
return middleware;
}
return assignToken(
Expand All @@ -20,12 +20,16 @@ export const mapToClass = middleware => {
);
};

export const isClass = middleware => {
export function isClass(middleware) {
return middleware.toString().substring(0, 5) === 'class';
};

export const assignToken = (metatype): Metatype<any> => {
this.id = this.id || 1;
Object.defineProperty(metatype, 'name', { value: ++this.id });
return metatype;
};
function assignTokenFactory() {
let id = 1;
return (metatype): Metatype<any> => {
Object.defineProperty(metatype, 'name', { value: ++id });
return metatype;
}
}

export const assignToken = assignTokenFactory();
7 changes: 7 additions & 0 deletions src/core/test/middlewares/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,34 @@ describe('middleware utils', () => {
});
it('should returns filtered middlewares', () => {
expect(filterMiddlewares(middlewares)).to.have.length(2);
expect(middlewares[0].name).to.eq('Test');
expect(middlewares[1].name).to.eq('fnMiddleware');
});
});
describe('mapToClass', () => {
describe('when middleware is a class', () => {
it('should returns identity', () => {
const type = mapToClass(Test);
expect(type).to.eql(Test);
expect(type.name).to.eq('Test');
});
});
describe('when middleware is a function', () => {
it('should returns metatype', () => {
const metatype = mapToClass(fnMiddleware);
expect(metatype).to.not.eql(fnMiddleware);
expect(metatype.name).to.eq(4);
});
it('should define `resolve` method', () => {
const metatype = mapToClass(fnMiddleware);
expect(new metatype().resolve).to.exist;
expect(metatype.name).to.eq(5);
});
it('should encapsulate function', () => {
const spy = sinon.spy();
const metatype = mapToClass(spy);
new metatype().resolve()();
expect(metatype.name).to.eq(6);
expect(spy.called).to.be.true;
});
});
Expand All @@ -61,6 +67,7 @@ describe('middleware utils', () => {
const anonymousType = class {};
assignToken(anonymousType);
expect(anonymousType.name).to.exist;
expect(anonymousType.name).to.eq(2);
});
});
});

0 comments on commit 2742fbd

Please sign in to comment.