Skip to content

Commit 9f472d1

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents a4e108b + e1eab48 commit 9f472d1

24 files changed

+3025
-5528
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules/
33
coverage/
44
npm-debug.log
55
.vscode/
6-
.idea/
6+
.idea/
7+
.nyc_output/

.mocharc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"require": "ts-node/register"
3+
}

.nycrc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"all": true,
3+
"check-coverage": true,
4+
"exclude": [
5+
"**/*.d.ts",
6+
"src/decorator-options/**",
7+
"src/metadata/args/**",
8+
"src/metadata/types/**"
9+
],
10+
"extension": [".ts"],
11+
"include": ["src/**"],
12+
"reporter": ["html", "lcov", "text-summary"]
13+
}

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: node_js
22
node_js:
3-
- stable
3+
- 12
4+
- 10
45
- 8
5-
- 6
66

77
after_success:
8-
- bash <(curl -s https://codecov.io/bash)
8+
- bash <(curl -s https://codecov.io/bash)

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Changelog and release notes
22

3-
### 0.7.8
3+
### 0.8.0 [BREAKING CHANGES]
4+
5+
- [class-transformer](https://github.com/typestack/class-transformer) and [class-validator](https://github.com/typestack/class-validator) are now peer dependencies.
46

57
#### Features
68

7-
- updated `class-transformer` and `class-validator` to latest.
9+
- extract generic `@Session()` deocorator into `@SessionParam()` and `@Session()` (ref [#335][#348][#407])
10+
- restore/introduce `@QueryParams()` and `@Params()` missing decorators options (ref [#289][#289])
11+
- normalize param object properties (for "queries", "headers", "params" and "cookies"), with this change you can easily validate query/path params using `class-validator` (ref [#289][#289])
12+
- improved params normalization, converting to primitive types is now more strict and can throw ParamNormalizationError (e.g. when number is expected but an invalid string (NaN) has been received) (ref [#289][#289])
813

914
### 0.7.7
1015

@@ -131,4 +136,4 @@
131136
[#276]: https://github.com/pleerock/routing-controllers/pull/276
132137
[#251]: https://github.com/pleerock/routing-controllers/pull/251
133138
[#240]: https://github.com/pleerock/routing-controllers/pull/240
134-
[#233]: https://github.com/pleerock/routing-controllers/pull/233
139+
[#233]: https://github.com/pleerock/routing-controllers/pull/233

README.md

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].
7373

7474
1. Install module:
7575

76-
`npm install routing-controllers class-validator --save`
76+
`npm install routing-controllers`
7777

7878
2. `reflect-metadata` shim is required:
7979

80-
`npm install reflect-metadata --save`
80+
`npm install reflect-metadata`
8181

8282
and make sure to import it before you use routing-controllers:
8383

@@ -89,21 +89,27 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].
8989

9090
**a. If you want to use routing-controllers with *express.js*, then install it and all required dependencies:**
9191

92-
`npm install express body-parser multer --save`
92+
`npm install express body-parser multer`
9393

9494
Optionally you can also install their typings:
9595

96-
`npm install @types/express @types/body-parser @types/multer --save`
96+
`npm install -D @types/express @types/body-parser @types/multer`
9797

9898
**b. If you want to use routing-controllers with *koa 2*, then install it and all required dependencies:**
9999

100-
`npm install koa koa-router koa-bodyparser koa-multer --save`
100+
`npm install koa koa-router koa-bodyparser koa-multer`
101101

102102
Optionally you can also install their typings:
103103

104-
`npm install @types/koa @types/koa-router @types/koa-bodyparser --save`
104+
`npm install -D @types/koa @types/koa-router @types/koa-bodyparser`
105105

106-
4. Its important to set these options in `tsconfig.json` file of your project:
106+
4. Install peer dependencies:
107+
108+
`npm install class-transformer class-validator`
109+
110+
In prior versions, these were direct dependencies, but now they are peer dependencies so you can choose when to upgrade and accept breaking changes.
111+
112+
5. Its important to set these options in `tsconfig.json` file of your project:
107113

108114
```json
109115
{
@@ -370,6 +376,37 @@ getUsers(@QueryParam("limit") limit: number) {
370376
```
371377

372378
If you want to inject all query parameters use `@QueryParams()` decorator.
379+
The bigest benefit of this approach is that you can perform validation of the params.
380+
381+
```typescript
382+
enum Roles {
383+
Admin = "admin",
384+
User = "user",
385+
Guest = "guest",
386+
}
387+
388+
class GetUsersQuery {
389+
390+
@IsPositive()
391+
limit: number;
392+
393+
@IsAlpha()
394+
city: string;
395+
396+
@IsEnum(Roles)
397+
role: Roles;
398+
399+
@IsBoolean()
400+
isActive: boolean;
401+
402+
}
403+
404+
@Get("/users")
405+
getUsers(@QueryParams() query: GetUsersQuery) {
406+
// here you can access query.role, query.limit
407+
// and others valid query parameters
408+
}
409+
```
373410

374411
#### Inject request body
375412

@@ -421,18 +458,20 @@ If you want to inject all header parameters use `@CookieParams()` decorator.
421458

422459
#### Inject session object
423460

424-
To inject a session value, use `@Session` decorator:
461+
To inject a session value, use `@SessionParam` decorator:
425462

426463
```typescript
427-
@Get("/login/")
428-
savePost(@Session("user") user: User, @Body() post: Post) {
429-
}
464+
@Get("/login")
465+
savePost(@SessionParam("user") user: User, @Body() post: Post) {}
430466
```
431467
If you want to inject the main session object, use `@Session()` without any parameters.
432-
468+
```typescript
469+
@Get("/login")
470+
savePost(@Session() session: any, @Body() post: Post) {}
471+
```
433472
The parameter marked with `@Session` decorator is required by default. If your action param is optional, you have to mark it as not required:
434473
```ts
435-
action(@Session("user", { required: false }) user: User)
474+
action(@Session("user", { required: false }) user: User) {}
436475
```
437476

438477
Express uses [express-session][5] / Koa uses [koa-session][6] or [koa-generic-session][7] to handle session, so firstly you have to install it manually to use `@Session` decorator.
@@ -1457,14 +1496,15 @@ export class QuestionController {
14571496
| `@Res()` | `getAll(@Res() response: Response)` | Injects a Response object. | `function (request, response)` |
14581497
| `@Ctx()` | `getAll(@Ctx() context: Context)` | Injects a Context object (koa-specific) | `function (ctx)` (koa-analogue) |
14591498
| `@Param(name: string, options?: ParamOptions)` | `get(@Param("id") id: number)` | Injects a router parameter. | `request.params.id` |
1460-
| `@Params()` | `get(@Params() params: any)` | Injects all request parameters. | `request.params` |
1499+
| `@Params()` | `get(@Params() params: any)` | Injects all router parameters. | `request.params` |
14611500
| `@QueryParam(name: string, options?: ParamOptions)` | `get(@QueryParam("id") id: number)` | Injects a query string parameter. | `request.query.id` |
14621501
| `@QueryParams()` | `get(@QueryParams() params: any)` | Injects all query parameters. | `request.query` |
14631502
| `@HeaderParam(name: string, options?: ParamOptions)` | `get(@HeaderParam("token") token: string)` | Injects a specific request headers. | `request.headers.token` |
14641503
| `@HeaderParams()` | `get(@HeaderParams() params: any)` | Injects all request headers. | `request.headers` |
14651504
| `@CookieParam(name: string, options?: ParamOptions)` | `get(@CookieParam("username") username: string)` | Injects a cookie parameter. | `request.cookie("username")` |
1466-
| `@CookieParams()` | `get(@CookieParams() params: any)` | Injects all cookies. | `request.cookies |
1467-
| `@Session(name?: string)` | `get(@Session("user") user: User)` | Injects an object from session (or the whole session). | `request.session.user` |
1505+
| `@CookieParams()` | `get(@CookieParams() params: any)` | Injects all cookies. | `request.cookies` |
1506+
| `@Session()` | `get(@Session() session: any)` | Injects the whole session object. | `request.session` |
1507+
| `@SessionParam(name: string)` | `get(@SessionParam("user") user: User)` | Injects an object from session property. | `request.session.user` |
14681508
| `@State(name?: string)` | `get(@State() session: StateType)` | Injects an object from the state (or the whole state). | `ctx.state` (koa-analogue) |
14691509
| `@Body(options?: BodyOptions)` | `post(@Body() body: any)` | Injects a body. In parameter options you can specify body parser middleware options. | `request.body` |
14701510
| `@BodyParam(name: string, options?: ParamOptions)` | `post(@BodyParam("name") name: string)` | Injects a body parameter. | `request.body.name` |

gulpfile.ts

Lines changed: 0 additions & 197 deletions
This file was deleted.

0 commit comments

Comments
 (0)