Skip to content

Commit 9b42abd

Browse files
committed
Merge branch 'epiphone-master' into next
2 parents 93cf01d + cd26ae1 commit 9b42abd

24 files changed

+403
-93
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].
4242
- [Throw HTTP errors](#throw-http-errors)
4343
- [Enable CORS](#enable-cors)
4444
- [Default settings](#default-settings)
45+
- [Selectively disabling request/response transform](#selectively-disable-requestresponse-transforming)
4546
* [Using middlewares](#using-middlewares)
4647
+ [Use exist middleware](#use-exist-middleware)
4748
+ [Creating your own express middleware](#creating-your-own-express-middleware)
@@ -831,6 +832,20 @@ const app = createExpressServer({
831832
app.listen(3000);
832833
```
833834

835+
#### Selectively disable request/response transform
836+
837+
To disable `class-transformer` on a per-controller or per-route basis, use the `transformRequest` and `transformResponse` options on your controller and route decorators:
838+
839+
```typescript
840+
@Controller("/users", {transformRequest: false, transformResponse: false})
841+
export class UserController {
842+
843+
@Get("/", {transformResponse: true}) {
844+
// route option overrides controller option
845+
}
846+
}
847+
```
848+
834849
## Using middlewares
835850

836851
You can use any existing express / koa middleware, or create your own.
@@ -1219,7 +1234,7 @@ If its a class - then instance of this class will be created.
12191234
This technique works with `@Body`, `@Param`, `@QueryParam`, `@BodyParam`, and other decorators.
12201235
Learn more about class-transformer and how to handle more complex object constructions [here][4].
12211236
This behaviour is enabled by default.
1222-
If you want to disable it simply pass `classTransformer: false` to createExpressServer method.
1237+
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).
12231238

12241239
## Auto validating action params
12251240

src/ActionParameterHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export class ActionParameterHandler<T extends BaseDriver> {
209209
*/
210210
protected transformValue(value: any, paramMetadata: ParamMetadata): any {
211211
if (this.driver.useClassTransformer &&
212+
paramMetadata.actionMetadata.options.transformRequest !== false &&
212213
paramMetadata.targetType &&
213214
paramMetadata.targetType !== Object &&
214215
!(value instanceof paramMetadata.targetType)) {

src/RoutingControllersOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface RoutingControllersOptions {
8585
* Special function used to get currently authorized user.
8686
*/
8787
currentUserChecker?: CurrentUserChecker;
88-
88+
8989
/**
9090
* Default settings
9191
*/
@@ -110,4 +110,4 @@ export interface RoutingControllersOptions {
110110
required?: boolean;
111111
};
112112
};
113-
}
113+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Extra options that apply to each controller action.
3+
*/
4+
export interface ControllerOptions {
5+
/**
6+
* If set to false, class-transformer won't be used to perform request serialization.
7+
*/
8+
transformRequest?: boolean;
9+
10+
/**
11+
* If set to false, class-transformer won't be used to perform response serialization.
12+
*/
13+
transformResponse?: boolean;
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Extra handler-specific options.
3+
*/
4+
export interface HandlerOptions {
5+
/**
6+
* If set to false, class-transformer won't be used to perform request serialization.
7+
*/
8+
transformRequest?: boolean;
9+
10+
/**
11+
* If set to false, class-transformer won't be used to perform response serialization.
12+
*/
13+
transformResponse?: boolean;
14+
}

src/decorator/Controller.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import {getMetadataArgsStorage} from "../index";
2+
import {ControllerOptions} from "../decorator-options/ControllerOptions";
23

34
/**
45
* Defines a class as a controller.
56
* Each decorated controller method is served as a controller action.
67
* Controller actions are executed when request come.
78
*
89
* @param baseRoute Extra path you can apply as a base route to all controller actions
10+
* @param options Extra options that apply to all controller actions
911
*/
10-
export function Controller(baseRoute?: string): Function {
12+
export function Controller(baseRoute?: string, options?: ControllerOptions): Function {
1113
return function (object: Function) {
1214
getMetadataArgsStorage().controllers.push({
1315
type: "default",
1416
target: object,
15-
route: baseRoute
17+
route: baseRoute,
18+
options
1619
});
1720
};
18-
}
21+
}

src/decorator/Delete.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import {HandlerOptions} from "../decorator-options/HandlerOptions";
12
import {getMetadataArgsStorage} from "../index";
23

34
/**
45
* Registers a controller method to be executed when DELETE request comes on a given route.
56
* Must be applied on a controller action.
67
*/
7-
export function Delete(route?: RegExp): Function;
8+
export function Delete(route?: RegExp, options?: HandlerOptions): Function;
89

910
/**
1011
* Registers a controller method to be executed when DELETE request comes on a given route.
1112
* Must be applied on a controller action.
1213
*/
13-
export function Delete(route?: string): Function;
14+
export function Delete(route?: string, options?: HandlerOptions): Function;
1415

1516
/**
1617
* Registers a controller method to be executed when DELETE request comes on a given route.
1718
* Must be applied on a controller action.
1819
*/
19-
export function Delete(route?: string|RegExp): Function {
20+
export function Delete(route?: string|RegExp, options?: HandlerOptions): Function {
2021
return function (object: Object, methodName: string) {
2122
getMetadataArgsStorage().actions.push({
2223
type: "delete",
2324
target: object.constructor,
2425
method: methodName,
25-
route: route
26+
route: route,
27+
options
2628
});
2729
};
2830
}

src/decorator/Get.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import {HandlerOptions} from "../decorator-options/HandlerOptions";
12
import {getMetadataArgsStorage} from "../index";
23

34
/**
45
* Registers an action to be executed when GET request comes on a given route.
56
* Must be applied on a controller action.
67
*/
7-
export function Get(route?: RegExp): Function;
8+
export function Get(route?: RegExp, options?: HandlerOptions): Function;
89

910
/**
1011
* Registers an action to be executed when GET request comes on a given route.
1112
* Must be applied on a controller action.
1213
*/
13-
export function Get(route?: string): Function;
14+
export function Get(route?: string, options?: HandlerOptions): Function;
1415

1516
/**
1617
* Registers an action to be executed when GET request comes on a given route.
1718
* Must be applied on a controller action.
1819
*/
19-
export function Get(route?: string|RegExp): Function {
20+
export function Get(route?: string|RegExp, options?: HandlerOptions): Function {
2021
return function (object: Object, methodName: string) {
2122
getMetadataArgsStorage().actions.push({
2223
type: "get",
2324
target: object.constructor,
2425
method: methodName,
25-
route: route
26+
options,
27+
route
2628
});
2729
};
28-
}
30+
}

src/decorator/Head.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import {HandlerOptions} from "../decorator-options/HandlerOptions";
12
import {getMetadataArgsStorage} from "../index";
23

34
/**
45
* Registers an action to be executed when HEAD request comes on a given route.
56
* Must be applied on a controller action.
67
*/
7-
export function Head(route?: RegExp): Function;
8+
export function Head(route?: RegExp, options?: HandlerOptions): Function;
89

910
/**
1011
* Registers an action to be executed when HEAD request comes on a given route.
1112
* Must be applied on a controller action.
1213
*/
13-
export function Head(route?: string): Function;
14+
export function Head(route?: string, options?: HandlerOptions): Function;
1415

1516
/**
1617
* Registers an action to be executed when HEAD request comes on a given route.
1718
* Must be applied on a controller action.
1819
*/
19-
export function Head(route?: string|RegExp): Function {
20+
export function Head(route?: string|RegExp, options?: HandlerOptions): Function {
2021
return function (object: Object, methodName: string) {
2122
getMetadataArgsStorage().actions.push({
2223
type: "head",
2324
target: object.constructor,
2425
method: methodName,
25-
route: route
26+
options,
27+
route
2628
});
2729
};
28-
}
30+
}

src/decorator/JsonController.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import {getMetadataArgsStorage} from "../index";
2+
import {ControllerOptions} from "../decorator-options/ControllerOptions";
23

34
/**
45
* Defines a class as a JSON controller. If JSON controller is used, then all controller actions will return
56
* a serialized json data, and its response content-type always will be application/json.
67
*
78
* @param baseRoute Extra path you can apply as a base route to all controller actions
9+
* @param options Extra options that apply to all controller actions
810
*/
9-
export function JsonController(baseRoute?: string) {
11+
export function JsonController(baseRoute?: string, options?: ControllerOptions) {
1012
return function (object: Function) {
1113
getMetadataArgsStorage().controllers.push({
1214
type: "json",
1315
target: object,
14-
route: baseRoute
16+
route: baseRoute,
17+
options
1518
});
1619
};
1720
}

0 commit comments

Comments
 (0)