Skip to content

Commit aee22a2

Browse files
author
kamil.mysliwiec
committed
Update to RC4
1 parent 491dc56 commit aee22a2

14 files changed

Lines changed: 94 additions & 32 deletions

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
## 1.0.0-RC4 (08.04.2017)
2+
3+
- Support for @Post, @Get, @Delete, @Put, @All decorators
4+
- Added ability to pass data to middleware metatypes
5+
16
## 1.0.0-BETA-1 (23.03.2017)
27

38
- @Inject -> @Dependencies
4-
- @Inject decorator for custom constructor parameters,
9+
- @Inject decorator for custom constructor parameters
510
- Custom providers support (useClass, useValue, useFactory)
611

712
## 1.0.0-ALPHA-23 (19.03.2017)

example/modules/users/auth.middleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { NestMiddleware } from '../../../src/core/middlewares/interfaces/nest-mi
55

66
@Middleware()
77
export class AuthMiddleware implements NestMiddleware {
8-
98
constructor(private usersService: UsersService) {}
109

1110
resolve() {

example/modules/users/users.controller.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { UsersService } from "./users.service";
22
import { RequestMethod, Controller, RequestMapping } from "./../../../src/";
33
import { ModuleRef } from '../../../src/core/injector/module-ref';
44
import { UsersModule } from './users.module';
5+
import { Get, Post } from '../../../src/common/utils/request-mapping.decorator';
56

67
@Controller({ path: 'users' })
78
export class UsersController {
@@ -10,19 +11,19 @@ export class UsersController {
1011
private usersService: UsersService,
1112
private moduleRef: ModuleRef) {}
1213

13-
@RequestMapping()
14+
@Get()
1415
async getAllUsers(req, res) {
1516
const users = await this.usersService.getAllUsers();
1617
res.status(200).json(users);
1718
}
1819

19-
@RequestMapping({ path: '/:id' })
20+
@Get('/:id')
2021
async getUser(req, res) {
2122
const user = await this.usersService.getUser(req.params.id);
2223
res.status(200).json(user);
2324
}
2425

25-
@RequestMapping({ method: RequestMethod.POST })
26+
@Post()
2627
async addUser(req, res) {
2728
const msg = await this.usersService.getUser(req.body.user);
2829
res.status(201).json(msg);

example/modules/users/users.module.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { UsersController } from './users.controller';
33
import { UsersService } from './users.service';
44
import { AuthMiddleware } from './auth.middleware';
55
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+
});
611

712
@Module({
813
controllers: [ UsersController ],
@@ -17,8 +22,12 @@ export class UsersModule {
1722

1823
configure(builder: MiddlewareBuilder) {
1924
builder.use({
20-
middlewares: [ AuthMiddleware ],
21-
forRoutes: [ UsersController ],
22-
})
25+
middlewares: [
26+
ProvideRoles(AuthMiddleware)
27+
],
28+
forRoutes: [ UsersController]
29+
});
2330
}
2431
}
32+
33+

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"author": "Kamil Mysliwiec",
3737
"license": "ISC",
3838
"dependencies": {
39-
"amqplib": "^0.5.1",
4039
"cli-color": "^1.1.0",
4140
"express": "^4.14.0",
4241
"json-socket": "^0.2.1",
@@ -51,7 +50,7 @@
5150
"@types/mocha": "^2.2.38",
5251
"@types/node": "^7.0.5",
5352
"@types/redis": "^0.12.36",
54-
"@types/sinon": "^1.16.34",
53+
"@types/sinon": "^1.16.36",
5554
"awesome-typescript-loader": "^3.0.0-beta.18",
5655
"chai": "^3.5.0",
5756
"concurrently": "^3.4.0",
@@ -63,7 +62,7 @@
6362
"mocha": "^3.2.0",
6463
"nodemon": "^1.11.0",
6564
"nyc": "^10.1.2",
66-
"sinon": "^2.0.0-pre.2",
65+
"sinon": "^2.1.0",
6766
"sinon-chai": "^2.8.0",
6867
"ts-node": "^2.0.0"
6968
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'reflect-metadata';
2+
import { expect } from 'chai';
3+
import { ProvideValues } from '../../utils/provide-values.util';
4+
5+
describe('ProvideValues', () => {
6+
let type, data = { test: [ 1, 2, 3 ] };
7+
class Test {}
8+
9+
beforeEach(() => {
10+
type = ProvideValues(data)(Test);
11+
});
12+
it('should enrich prototype with given values', () => {
13+
expect(type.prototype).to.contain(data);
14+
});
15+
it('should set name of metatype', () => {
16+
expect(type.name).to.eq(Test.name + JSON.stringify(data));
17+
});
18+
});

src/common/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './component.decorator';
44
export * from './module.decorator';
55
export * from './dependencies.decorator';
66
export * from './inject.decorator';
7-
export { Component as Middleware } from './component.decorator';
7+
export { Component as Middleware } from './component.decorator';
8+
export * from './provide-values.util';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'reflect-metadata';
2+
3+
interface Constructor<T> {
4+
new(...args: any[]): T
5+
}
6+
7+
export const ProvideValues = <T extends Constructor<{}>>(data) => {
8+
return (metatype: T) => {
9+
const type = class extends metatype {
10+
constructor(...args) {
11+
super(args);
12+
}
13+
};
14+
const token = metatype.name + JSON.stringify(data);
15+
Object.defineProperty(type, 'name', { value: token });
16+
Object.assign(type.prototype, data);
17+
return type;
18+
}
19+
};

src/common/utils/request-mapping.decorator.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { RequestMappingMetadata } from '../interfaces/request-mapping-metadata.i
33
import { RequestMethod } from '../enums/request-method.enum';
44
import { PATH_METADATA, METHOD_METADATA } from '../constants';
55

6-
const defaultMetadata = { [PATH_METADATA]: '/', [METHOD_METADATA]: RequestMethod.GET };
6+
const defaultMetadata = {
7+
[PATH_METADATA]: '/',
8+
[METHOD_METADATA]: RequestMethod.GET
9+
};
10+
711
export const RequestMapping = (metadata: RequestMappingMetadata = defaultMetadata): MethodDecorator => {
812
const path = metadata[PATH_METADATA] || '/';
913
const requestMethod = metadata[METHOD_METADATA] || RequestMethod.GET;
@@ -13,4 +17,18 @@ export const RequestMapping = (metadata: RequestMappingMetadata = defaultMetadat
1317
Reflect.defineMetadata(METHOD_METADATA, requestMethod, descriptor.value);
1418
return descriptor;
1519
}
16-
};
20+
};
21+
22+
const createMappingDecorator = (method: RequestMethod) => (path?: string): MethodDecorator => {
23+
return RequestMapping({
24+
[PATH_METADATA]: path,
25+
[METHOD_METADATA]: method
26+
});
27+
};
28+
29+
export const Post = createMappingDecorator(RequestMethod.POST);
30+
export const Get = createMappingDecorator(RequestMethod.GET);
31+
export const Delete = createMappingDecorator(RequestMethod.DELETE);
32+
export const Put = createMappingDecorator(RequestMethod.PUT);
33+
export const All = createMappingDecorator(RequestMethod.ALL);
34+

src/core/helpers/messages.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ export const getRouteMappedMessage =
77
(path: string, method) => `Mapped {${path}, ${RequestMethod[method]}} route`;
88

99
export const getControllerMappingMessage =
10-
(name: string) => `${name}:`;
11-
12-
export const getMiddlewareInitMessage =
13-
(middleware: string, module: string) => `${middleware} injected into ${module}`;
10+
(name: string) => `${name}:`;

0 commit comments

Comments
 (0)