Skip to content

Commit 7cfca52

Browse files
Merge pull request nestjs#9 from kamilmysliwiec/update-rc8
Update to RC.8
2 parents e161627 + d516d90 commit 7cfca52

11 files changed

Lines changed: 170 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.0.0-RC7 (08.04.2017)
2+
3+
- MiddlewareBuilder: `use()` deprecated, use `apply()` instead
4+
- MiddlewareBuilder: new `apply()` method
5+
16
## 1.0.0-RC4 (08.04.2017)
27

38
- Support for @Post, @Get, @Delete, @Put, @All decorators

example/modules/users/users.module.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { Module } from './../../../src/';
22
import { UsersController } from './users.controller';
33
import { UsersService } from './users.service';
4-
import { AuthMiddleware } from './auth.middleware';
54
import { MiddlewareBuilder } from '../../../src/core/middlewares/builder';
6-
import { ProvideValues } from '../../../src/common/utils/provide-values.util';
7-
8-
const ProvideRoles = ProvideValues({
9-
role: ['admin', 'user']
10-
});
5+
import { AuthMiddleware } from './auth.middleware';
116

127
@Module({
138
controllers: [ UsersController ],
@@ -21,12 +16,9 @@ export class UsersModule {
2116
}
2217

2318
configure(builder: MiddlewareBuilder) {
24-
builder.use({
25-
middlewares: [
26-
ProvideRoles(AuthMiddleware)
27-
],
28-
forRoutes: [ UsersController]
29-
});
19+
builder.apply(AuthMiddleware)
20+
.with('admin')
21+
.forRoutes(UsersController);
3022
}
3123
}
3224

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'mocha';
2+
import 'reflect-metadata';
3+
import { expect } from 'chai';
4+
import { BindResolveMiddlewareValues } from '../../utils/bind-resolve-values.util';
5+
import { NestMiddleware } from '../../../core/middlewares/interfaces/nest-middleware.interface';
6+
7+
describe('BindResolveMiddlewareValues', () => {
8+
let type,
9+
arg1 = 3,
10+
arg2 = 4;
11+
12+
class Test implements NestMiddleware {
13+
resolve(a, b) {
14+
return () => [a, b];
15+
}
16+
}
17+
18+
beforeEach(() => {
19+
type = BindResolveMiddlewareValues([ arg1, arg2 ])(Test);
20+
});
21+
it('should pass values to resolve() method', () => {
22+
const obj = new type();
23+
const hof = obj.resolve();
24+
expect(hof()).to.deep.equal([arg1, arg2]);
25+
});
26+
it('should set name of metatype', () => {
27+
expect(type.name).to.eq((<any>Test).name + JSON.stringify([ arg1, arg2 ]));
28+
});
29+
});

src/common/test/utils/component.decorator.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'reflect-metadata';
2-
import 'mocha';
32
import { expect } from 'chai';
43
import { Component } from '../../utils/component.decorator';
54

src/common/test/utils/provide-values.util.spec.ts renamed to src/common/test/utils/merge-with-values.util.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import 'reflect-metadata';
22
import { expect } from 'chai';
3-
import { ProvideValues } from '../../utils/provide-values.util';
3+
import { MergeWithValues } from '../../utils/merge-with-values.util';
44

5-
describe('ProvideValues', () => {
5+
describe('MergeWithValues', () => {
66
let type, data = { test: [ 1, 2, 3 ] };
77
class Test {}
88

99
beforeEach(() => {
10-
type = ProvideValues(data)(Test);
10+
type = MergeWithValues(data)(Test);
1111
});
1212
it('should enrich prototype with given values', () => {
1313
expect(type.prototype).to.contain(data);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Constructor } from './merge-with-values.util';
2+
import { NestMiddleware } from '../../core/middlewares/interfaces/nest-middleware.interface';
3+
4+
export const BindResolveMiddlewareValues = <T extends Constructor<NestMiddleware>>(data: Array<any>) => {
5+
return (metatype: T): any => {
6+
const type = class extends metatype {
7+
resolve() {
8+
return super.resolve(...data);
9+
}
10+
};
11+
const token = metatype.name + JSON.stringify(data);
12+
Object.defineProperty(type, 'name', { value: token });
13+
return type;
14+
}
15+
};

src/common/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export * from './module.decorator';
55
export * from './dependencies.decorator';
66
export * from './inject.decorator';
77
export { Component as Middleware } from './component.decorator';
8-
export * from './provide-values.util';
8+
export * from './merge-with-values.util';
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import 'reflect-metadata';
22

3-
interface Constructor<T> {
3+
export interface Constructor<T> {
44
new(...args: any[]): T
55
}
66

7-
export const ProvideValues = <T extends Constructor<{}>>(data) => {
8-
return (metatype: T) => {
7+
export const MergeWithValues = <T extends Constructor<{}>>(data: { [param: string]: any }) => {
8+
return (metatype: T): any => {
99
const type = class extends metatype {
1010
constructor(...args) {
11-
super(args);
11+
super(...args);
1212
}
1313
};
1414
const token = metatype.name + JSON.stringify(data);
1515
Object.defineProperty(type, 'name', { value: token });
1616
Object.assign(type.prototype, data);
1717
return type;
1818
}
19-
};
19+
};

src/core/middlewares/builder.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,66 @@
11
import { MiddlewareConfiguration } from './interfaces/middleware-configuration.interface';
22
import { InvalidMiddlewareConfigurationException } from '../../errors/exceptions/invalid-middleware-configuration.exception';
3-
import { isUndefined } from '../../common/utils/shared.utils';
3+
import { isUndefined, isNil } from '../../common/utils/shared.utils';
4+
import { BindResolveMiddlewareValues } from '../../common/utils/bind-resolve-values.util';
5+
import { Logger } from '../../common/services/logger.service';
6+
import { Metatype } from '../../common/interfaces/metatype.interface';
47

58
export class MiddlewareBuilder {
6-
private storedConfiguration = new Set<MiddlewareConfiguration>();
9+
private middlewaresCollection = new Set<MiddlewareConfiguration>();
10+
private logger = new Logger(MiddlewareBuilder.name);
711

12+
apply(metatypes: Metatype<any> | Array<Metatype<any>>): MiddlewareConfigProxy {
13+
let resolveParams = null;
14+
const configProxy = {
15+
with: (...data) => {
16+
resolveParams = data;
17+
return configProxy as MiddlewareConfigProxy;
18+
},
19+
forRoutes: (routes) => {
20+
if (isUndefined(routes)) {
21+
throw new InvalidMiddlewareConfigurationException();
22+
}
23+
const configuration = {
24+
middlewares: this.bindValuesToResolve(metatypes, resolveParams),
25+
forRoutes: [].concat(routes)
26+
};
27+
this.middlewaresCollection.add(<MiddlewareConfiguration>configuration);
28+
return this;
29+
}
30+
};
31+
return configProxy;
32+
}
33+
34+
/**
35+
* @deprecated
36+
* Since version RC.6 this method is deprecated. Use apply() instead.
37+
*/
838
use(configuration: MiddlewareConfiguration) {
39+
this.logger.warn('DEPRECATED! Since version RC.6 `use()` method is deprecated. Use `apply()` instead.');
40+
941
const { middlewares, forRoutes } = configuration;
1042
if (isUndefined(middlewares) || isUndefined(forRoutes)) {
1143
throw new InvalidMiddlewareConfigurationException();
1244
}
1345

14-
this.storedConfiguration.add(configuration);
46+
this.middlewaresCollection.add(configuration);
1547
return this;
1648
}
1749

1850
build() {
19-
return [ ...this.storedConfiguration ];
51+
return [ ...this.middlewaresCollection ];
52+
}
53+
54+
private bindValuesToResolve(middlewares: Metatype<any> | Array<Metatype<any>>, resolveParams: Array<any>) {
55+
if (isNil(resolveParams)) {
56+
return middlewares;
57+
}
58+
const bindArgs = BindResolveMiddlewareValues(resolveParams);
59+
return [].concat(middlewares).map(bindArgs);
2060
}
61+
}
2162

63+
export interface MiddlewareConfigProxy {
64+
with: (...data) => MiddlewareConfigProxy;
65+
forRoutes: (routes) => MiddlewareBuilder;
2266
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response, NextFunction } from 'express';
22

33
export interface NestMiddleware {
4-
resolve: () => (req?: Request, res?: Response, next?: NextFunction) => void;
4+
resolve(...args): (req?: Request, res?: Response, next?: NextFunction) => void;
55
}

0 commit comments

Comments
 (0)