forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kamil.mysliwiec
committed
Apr 11, 2017
1 parent
e161627
commit ad10000
Showing
11 changed files
with
175 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'mocha'; | ||
import 'reflect-metadata'; | ||
import { expect } from 'chai'; | ||
import { BindResolveMiddlewareValues } from '../../utils/bind-resolve-values.util'; | ||
import { NestMiddleware } from '../../../core/middlewares/interfaces/nest-middleware.interface'; | ||
|
||
describe('BindResolveMiddlewareValues', () => { | ||
let type, | ||
arg1 = 3, | ||
arg2 = 4; | ||
|
||
class Test implements NestMiddleware { | ||
resolve(a, b) { | ||
return () => [a, b]; | ||
} | ||
} | ||
|
||
beforeEach(() => { | ||
type = BindResolveMiddlewareValues([ arg1, arg2 ])(Test); | ||
}); | ||
it('should pass values to resolve() method', () => { | ||
const obj = new type(); | ||
const hof = obj.resolve(); | ||
expect(hof()).to.deep.equal([arg1, arg2]); | ||
}); | ||
it('should set name of metatype', () => { | ||
expect(type.name).to.eq((<any>Test).name + JSON.stringify([ arg1, arg2 ])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...on/test/utils/provide-values.util.spec.ts → ...test/utils/merge-with-values.util.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Constructor } from './merge-with-values.util'; | ||
import { NestMiddleware } from '../../core/middlewares/interfaces/nest-middleware.interface'; | ||
|
||
export const BindResolveMiddlewareValues = <T extends Constructor<NestMiddleware>>(data: Array<any>) => { | ||
return (metatype: T): any => { | ||
const type = class extends metatype { | ||
resolve() { | ||
return super.resolve(...data); | ||
} | ||
}; | ||
const token = metatype.name + JSON.stringify(data); | ||
Object.defineProperty(type, 'name', { value: token }); | ||
return type; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
src/common/utils/provide-values.util.ts → src/common/utils/merge-with-values.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import 'reflect-metadata'; | ||
|
||
interface Constructor<T> { | ||
export interface Constructor<T> { | ||
new(...args: any[]): T | ||
} | ||
|
||
export const ProvideValues = <T extends Constructor<{}>>(data) => { | ||
return (metatype: T) => { | ||
export const MergeWithValues = <T extends Constructor<{}>>(data: { [param: string]: any }) => { | ||
return (metatype: T): any => { | ||
const type = class extends metatype { | ||
constructor(...args) { | ||
super(args); | ||
super(...args); | ||
} | ||
}; | ||
const token = metatype.name + JSON.stringify(data); | ||
Object.defineProperty(type, 'name', { value: token }); | ||
Object.assign(type.prototype, data); | ||
return type; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,66 @@ | ||
import { MiddlewareConfiguration } from './interfaces/middleware-configuration.interface'; | ||
import { InvalidMiddlewareConfigurationException } from '../../errors/exceptions/invalid-middleware-configuration.exception'; | ||
import { isUndefined } from '../../common/utils/shared.utils'; | ||
import { isUndefined, isNil } from '../../common/utils/shared.utils'; | ||
import { BindResolveMiddlewareValues } from '../../common/utils/bind-resolve-values.util'; | ||
import { Logger } from '../../common/services/logger.service'; | ||
import { Metatype } from '../../common/interfaces/metatype.interface'; | ||
|
||
export class MiddlewareBuilder { | ||
private storedConfiguration = new Set<MiddlewareConfiguration>(); | ||
private middlewaresCollection = new Set<MiddlewareConfiguration>(); | ||
private logger = new Logger(MiddlewareBuilder.name); | ||
|
||
apply(metatypes: Metatype<any> | Array<Metatype<any>>): MiddlewareConfigProxy { | ||
let resolveParams = null; | ||
const configProxy = { | ||
with: (...data) => { | ||
resolveParams = data; | ||
return configProxy as MiddlewareConfigProxy; | ||
}, | ||
forRoutes: (routes) => { | ||
if (isUndefined(routes)) { | ||
throw new InvalidMiddlewareConfigurationException(); | ||
} | ||
const configuration = { | ||
middlewares: this.bindValuesToResolve(metatypes, resolveParams), | ||
forRoutes: [].concat(routes) | ||
}; | ||
this.middlewaresCollection.add(<MiddlewareConfiguration>configuration); | ||
return this; | ||
} | ||
}; | ||
return configProxy; | ||
} | ||
|
||
/** | ||
* @deprecated | ||
* Since version RC.6 this method is deprecated. Use apply() instead. | ||
*/ | ||
use(configuration: MiddlewareConfiguration) { | ||
this.logger.warn('DEPRECATED! Since version RC.6 `use()` method is deprecated. Use `apply()` instead.'); | ||
|
||
const { middlewares, forRoutes } = configuration; | ||
if (isUndefined(middlewares) || isUndefined(forRoutes)) { | ||
throw new InvalidMiddlewareConfigurationException(); | ||
} | ||
|
||
this.storedConfiguration.add(configuration); | ||
this.middlewaresCollection.add(configuration); | ||
return this; | ||
} | ||
|
||
build() { | ||
return [ ...this.storedConfiguration ]; | ||
return [ ...this.middlewaresCollection ]; | ||
} | ||
|
||
private bindValuesToResolve(middlewares: Metatype<any> | Array<Metatype<any>>, resolveParams: Array<any>) { | ||
if (isNil(resolveParams)) { | ||
return middlewares; | ||
} | ||
const bindArgs = BindResolveMiddlewareValues(resolveParams); | ||
return [].concat(middlewares).map(bindArgs); | ||
} | ||
} | ||
|
||
export interface MiddlewareConfigProxy { | ||
with: (...data) => MiddlewareConfigProxy; | ||
forRoutes: (routes) => MiddlewareBuilder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
|
||
export interface NestMiddleware { | ||
resolve: () => (req?: Request, res?: Response, next?: NextFunction) => void; | ||
resolve(...args): (req?: Request, res?: Response, next?: NextFunction) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters