Skip to content

Commit 260c689

Browse files
authored
Merge pull request #596 from typestack/next
Merge next branch in preparation for stable release
2 parents a2142ff + 8034568 commit 260c689

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2667
-2086
lines changed

README.md

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].
4747
- [Throw HTTP errors](#throw-http-errors)
4848
- [Enable CORS](#enable-cors)
4949
- [Default settings](#default-settings)
50+
- [Selectively disabling request/response transform](#selectively-disable-requestresponse-transforming)
5051
* [Using middlewares](#using-middlewares)
5152
+ [Use exist middleware](#use-exist-middleware)
5253
+ [Creating your own express middleware](#creating-your-own-express-middleware)
@@ -59,6 +60,7 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].
5960
+ [Interceptor classes](#interceptor-classes)
6061
+ [Global interceptors](#global-interceptors)
6162
* [Creating instances of classes from action params](#creating-instances-of-classes-from-action-params)
63+
* [Controller inheritance](#controller-inheritance)
6264
* [Auto validating action params](#auto-validating-action-params)
6365
* [Using authorization features](#using-authorization-features)
6466
- [@Authorized decorator](#authorized-decorator)
@@ -267,10 +269,19 @@ import { Controller, Req, Res, Get } from "routing-controllers";
267269
export class UserController {
268270
269271
@Get("/users")
270-
getAll(@Req() request: any, @Res() response: any) {
272+
getAllUsers(@Req() request: any, @Res() response: any) {
271273
return response.send("Hello response!");
272274
}
273275
276+
@Get("/posts")
277+
getAllPosts(@Req() request: any, @Res() response: any) {
278+
// some response functions don't return the response object,
279+
// so it needs to be returned explicitly
280+
response.redirect("/users");
281+
282+
return response;
283+
}
284+
274285
}
275286
```
276287

@@ -559,7 +570,7 @@ You can specify a custom ContentType header:
559570

560571
```typescript
561572
@Get("/users")
562-
@ContentType("text/cvs")
573+
@ContentType("text/csv")
563574
getUsers() {
564575
// ...
565576
}
@@ -606,7 +617,7 @@ You can use template to generate the Redirect header:
606617
@Redirect("http://github.com/:owner/:repo")
607618
getUsers() {
608619
return {
609-
owner: "pleerock",
620+
owner: "typestack",
610621
repo: "routing-controllers"
611622
};
612623
}
@@ -739,7 +750,7 @@ There are set of prepared errors you can use:
739750
* UnauthorizedError
740751

741752

742-
You can also create and use your own errors by extending `HttpError` class.
753+
You can also create and use your own errors by extending `HttpError` class.
743754
To define the data returned to the client, you could define a toJSON method in your error.
744755

745756
```typescript
@@ -761,7 +772,7 @@ class DbError extends HttpError {
761772
}
762773
}
763774
}
764-
```
775+
```
765776

766777
#### Enable CORS
767778

@@ -802,7 +813,7 @@ app.listen(3000);
802813

803814
#### Default settings
804815

805-
You can override default status code in routing-controllers options.
816+
You can override default status code in routing-controllers options.
806817

807818
```typescript
808819
import "reflect-metadata";
@@ -815,9 +826,9 @@ const app = createExpressServer({
815826
//with this option, null will return 404 by default
816827
nullResultCode: 404,
817828

818-
//with this option, void or Promise<void> will return 204 by default
829+
//with this option, void or Promise<void> will return 204 by default
819830
undefinedResultCode: 204,
820-
831+
821832
paramOptions: {
822833
//with this option, argument will be required by default
823834
required: true
@@ -828,6 +839,20 @@ const app = createExpressServer({
828839
app.listen(3000);
829840
```
830841

842+
#### Selectively disable request/response transform
843+
844+
To disable `class-transformer` on a per-controller or per-route basis, use the `transformRequest` and `transformResponse` options on your controller and route decorators:
845+
846+
```typescript
847+
@Controller("/users", {transformRequest: false, transformResponse: false})
848+
export class UserController {
849+
850+
@Get("/", {transformResponse: true}) {
851+
// route option overrides controller option
852+
}
853+
}
854+
```
855+
831856
## Using middlewares
832857

833858
You can use any existing express / koa middleware, or create your own.
@@ -1216,7 +1241,34 @@ If its a class - then instance of this class will be created.
12161241
This technique works with `@Body`, `@Param`, `@QueryParam`, `@BodyParam`, and other decorators.
12171242
Learn more about class-transformer and how to handle more complex object constructions [here][4].
12181243
This behaviour is enabled by default.
1219-
If you want to disable it simply pass `classTransformer: false` to createExpressServer method.
1244+
If you want to disable it simply pass `classTransformer: false` to createExpressServer method. Alternatively you can disable transforming for [individual controllers or routes](#selectively-disable-requestresponse-transforming).
1245+
1246+
## Controller Inheritance
1247+
Often your application may need to have an option to inherit controller from another to reuse code and void duplication.
1248+
A good example of the use is the CRUD operations which can be hidden inside `AbstractBaseController` with the possibility to add new and overload methods, the template method pattern.
1249+
1250+
```typescript
1251+
@Controller(`/product`)
1252+
class ProductController extends AbstractControllerTemplate {}
1253+
@Controller(`/category`)
1254+
class CategoryController extends AbstractControllerTemplate {}
1255+
abstract class AbstractControllerTemplate {
1256+
@Post()
1257+
public create() {}
1258+
1259+
@Read()
1260+
public read() {}
1261+
1262+
@Put()
1263+
public update() {}
1264+
1265+
@Delete()
1266+
public delete() {}
1267+
}
1268+
1269+
```
1270+
https://en.wikipedia.org/wiki/Template_method_pattern
1271+
12201272

12211273
## Auto validating action params
12221274

@@ -1270,7 +1322,7 @@ export class UserController {
12701322
}
12711323
```
12721324
If the param doesn't satisfy the requirements defined by class-validator decorators,
1273-
an error will be thrown and captured by routing-controllers, so the client will receive 400 Bad Request and JSON with nice detailed [Validation errors](https://github.com/pleerock/class-validator#validation-errors) array.
1325+
an error will be thrown and captured by routing-controller, so the client will receive 400 Bad Request and JSON with nice detailed [Validation errors](https://github.com/typestack/class-validator#validation-errors) array.
12741326

12751327
If you need special options for validation (groups, skipping missing properties, etc.) or transforming (groups, excluding prefixes, versions, etc.), you can pass them as global config as `validation ` in createExpressServer method or as a local `validate` setting for method parameter - `@Body({ validate: localOptions })`.
12761328

@@ -1371,7 +1423,7 @@ If you mark `@CurrentUser` as `required` and currentUserChecker logic will retur
13711423

13721424
`routing-controllers` supports a DI container out of the box. You can inject your services into your controllers,
13731425
middlewares and error handlers. Container must be setup during application bootstrap.
1374-
Here is example how to integrate routing-controllers with [typedi](https://github.com/pleerock/typedi):
1426+
Here is example how to integrate routing-controllers with [typedi](https://github.com/typestack/typedi):
13751427

13761428
```typescript
13771429
import "reflect-metadata";
@@ -1492,7 +1544,8 @@ export class QuestionController {
14921544
| `@Patch(route: string\|RegExp)` | `@Patch("/users/:id") patch()` | Methods marked with this decorator will register a request made with PATCH HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.patch("/users/:id", patch)` |
14931545
| `@Delete(route: string\|RegExp)` | `@Delete("/users/:id") delete()` | Methods marked with this decorator will register a request made with DELETE HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.delete("/users/:id", delete)` |
14941546
| `@Head(route: string\|RegExp)` | `@Head("/users/:id") head()` | Methods marked with this decorator will register a request made with HEAD HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.head("/users/:id", head)` |
1495-
| `@Method(methodName: string, route: string\|RegExp)` | `@Method("move", "/users/:id") move()` | Methods marked with this decorator will register a request made with given `methodName` HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.move("/users/:id", move)` |
1547+
| `@All(route: string\|RegExp)` | `@All("/users/me") rewrite()` | Methods marked with this decorator will register a request made with any HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.all("/users/me", rewrite)` |
1548+
| `@Method(methodName: string, route: string\|RegExp)` | `@Method("move", "/users/:id") move()` | Methods marked with this decorator will register a request made with given `methodName` HTTP Method to a given route. In action options you can specify if action should response json or regular text response. | `app.move("/users/:id", move)` |
14961549

14971550
#### Method Parameter Decorators
14981551

@@ -1531,7 +1584,7 @@ export class QuestionController {
15311584

15321585
| Signature | Example | Description |
15331586
|--------------------------------------------------------------------|-----------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
1534-
| `@Authorized(roles?: string\|string[])` | `@Authorized("SUPER_ADMIN")` get() | Checks if user is authorized and has given roles on a given route. `currentUserChecker` should be defined in routing-controllers options. | |
1587+
| `@Authorized(roles?: string\|string[])` | `@Authorized("SUPER_ADMIN")` get() | Checks if user is authorized and has given roles on a given route. `authorizationChecker` should be defined in routing-controllers options. | |
15351588
| `@CurrentUser(options?: { required?: boolean })` | get(@CurrentUser({ required: true }) user: User) | Injects currently authorized user. `currentUserChecker` should be defined in routing-controllers options. |
15361589
| `@Header(headerName: string, headerValue: string)` | `@Header("Cache-Control", "private")` get() | Allows to explicitly set any HTTP header returned in the response. |
15371590
| `@ContentType(contentType: string)` | `@ContentType("text/csv")` get() | Allows to explicitly set HTTP Content-Type returned in the response. |
@@ -1545,11 +1598,11 @@ export class QuestionController {
15451598

15461599
## Samples
15471600

1548-
* Take a look on [routing-controllers with express](https://github.com/pleerock/routing-controllers-express-demo) which is using routing-controllers.
1549-
* Take a look on [routing-controllers with koa](https://github.com/pleerock/routing-controllers-koa-demo) which is using routing-controllers.
1550-
* Take a look on [routing-controllers with angular 2](https://github.com/pleerock/routing-controllers-angular2-demo) which is using routing-controllers.
1601+
* Take a look on [routing-controllers with express](https://github.com/typestack/routing-controllers-express-demo) which is using routing-controllers.
1602+
* Take a look on [routing-controllers with koa](https://github.com/typestack/routing-controllers-koa-demo) which is using routing-controllers.
1603+
* Take a look on [routing-controllers with angular 2](https://github.com/typestack/routing-controllers-angular2-demo) which is using routing-controllers.
15511604
* Take a look on [node-microservice-demo](https://github.com/swimlane/node-microservice-demo) which is using routing-controllers.
1552-
* Take a look on samples in [./sample](https://github.com/pleerock/routing-controllers/tree/master/sample) for more examples
1605+
* Take a look on samples in [./sample](https://github.com/typestack/routing-controllers/tree/master/sample) for more examples
15531606
of usage.
15541607

15551608
## Release notes
@@ -1559,9 +1612,9 @@ See information about breaking changes and release notes [here](CHANGELOG.md).
15591612
[1]: http://expressjs.com/
15601613
[2]: http://koajs.com/
15611614
[3]: https://github.com/expressjs/multer
1562-
[4]: https://github.com/pleerock/class-transformer
1615+
[4]: https://github.com/typestack/class-transformer
15631616
[5]: https://www.npmjs.com/package/express-session
15641617
[6]: https://www.npmjs.com/package/koa-session
15651618
[7]: https://www.npmjs.com/package/koa-generic-session
15661619
[8]: http://koajs.com/#ctx-state
1567-
[9]: https://github.com/pleerock/class-validator
1620+
[9]: https://github.com/typestack/class-validator

lang/chinese/READEME.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,51 @@
1919
* [安装](#安装)
2020
* [快速使用](#快速使用)
2121
* [更多用法](#更多用法)
22-
- [使用 JSON](#使用JSON)
23-
- [返回 Promise](#返回Promise)
24-
- [使用 Request 和 Response 对象](#请求对象与响应对象)
25-
- [预配置 express / Koa](#预配置express/koa)
22+
- [使用 JSON](#使用-JSON)
23+
- [返回 Promise](#返回-Promise)
24+
- [使用 Request 和 Response 对象](#使用-Request-和-Response-对象)
25+
- [预配置 express / Koa](#预配置-express-/-Koa)
2626
- [从目录加载控制器](#从目录加载控制器)
2727
- [全局路由前缀](#全局路由前缀)
2828
- [指定控制器路由前缀](#指定控制器路由前缀)
29-
- [注入 param 参数](#注入param参数)
30-
- [注入 query 参数](#注入query参数)
31-
- [注入请求 Body](#注入请求body)
32-
- [注入请求 Body 参数](#注入请求body参数)
33-
- [注入请求 Header 参数](#注入请求header参数)
34-
- [注入 Cookie 参数](#注入cookie参数)
35-
- [注入 Session 对象](#注入session对象)
36-
- [注入 state 对象](#注入state对象)
29+
- [注入 param 参数](#注入-param-参数)
30+
- [注入 query 参数](#注入-query-参数)
31+
- [注入请求 Body](#注入请求-Body)
32+
- [注入请求 Body 参数](#注入请求-Body-参数)
33+
- [注入请求 Header 参数](#注入请求-Header-参数)
34+
- [注入 Cookie 参数](#注入-Cookie-参数)
35+
- [注入 Session 对象](#注入-Session-对象)
36+
- [注入 state 对象](#注入-State-对象)
3737
- [注入上传文件](#注入上传文件)
3838
- [限制必填参数](#限制必填参数)
3939
- [参数转为对象](#参数转为对象)
40-
- [设置 ContentType](#设置ContentType)
41-
- [设置 Location](#设置Location)
40+
- [设置 ContentType](#设置-ContentType)
41+
- [设置 Location](#设置-Location)
4242
- [设置重定向](#设置重定向)
43-
- [设置 HTTP 响应代码](#设置HTTP响应代码)
43+
- [设置 HTTP 响应代码](#设置-HTTP-响应代码)
4444
- [管理空响应](#管理空响应)
45-
- [自定义 Header](#自定义header)
45+
- [自定义 Header](#自定义-Header)
4646
- [模板渲染](#模板渲染)
47-
- [抛出 HTTP 错误](#抛出HTTP错误)
47+
- [抛出 HTTP 错误](#抛出-HTTP-错误)
4848
- [允许跨域](#允许跨域)
4949
- [默认设置](#默认设置)
5050
* [使用中间件](#使用中间件)
5151
+ [使用已有中间件](#使用已有中间件)
52-
+ [自行实现 express 中间件](#自行实现express中间件)
53-
+ [自行实现 Koa 中间件](#自行实现Koa中间件)
52+
+ [自行实现 express 中间件](#自行实现-express-中间件)
53+
+ [自行实现 Koa 中间件](#自行实现-Koa-中间件)
5454
+ [全局中间件](#全局中间件)
5555
+ [错误处理程序](#错误处理程序)
5656
+ [从目录加载中间件,拦截器和控制器](#从目录加载中间件,拦截器和控制器)
5757
* [使用拦截器](#拦截器)
5858
+ [函数式拦截器](#函数式拦截器)
59-
+ [class 式拦截器](#class式拦截器)
59+
+ [class 式拦截器](#class-式拦截器)
6060
+ [全局拦截器](#全局拦截器)
6161
* [实例化参数](#实例化参数)
6262
* [参数自动校验](#参数自动校验)
6363
* [使用权限管理](#使用权限管理)
64-
- [@Authorized 装饰器](#@Authorized装饰器)
65-
- [@CurrentUser 装饰器](#@CurrentUser装饰器)
66-
* [使用 DI 容器](#使用DI容器)
64+
- [@Authorized 装饰器](#@Authorized-装饰器)
65+
- [@CurrentUser 装饰器](#@CurrentUser-装饰器)
66+
* [使用 DI 容器](#使用-DI-容器)
6767
* [自定义参数装饰器](#自定义参数装饰器)
6868
* [装饰器参考](#装饰器参考)
6969
- [控制器装饰器](#控制器装饰器)

0 commit comments

Comments
 (0)