Skip to content

Commit

Permalink
docs: Update filter documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Aug 19, 2019
1 parent 9e4c880 commit e8de875
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
60 changes: 15 additions & 45 deletions docs/docs/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,23 @@ In this case, we need to retrieve the body content from an Express.Request.
So to do that, you must create a class and annotate it with the @@Filter@@
decorator and in option, implement the @@IFilter@@ interface:

```typescript
import {Filter, IFilter, ParseService} from "@tsed/common";
<<< @/docs/docs/snippets/filters/basic-filter.ts

@Filter()
export class BodyParamsFilter implements IFilter {
Then create the decorator. This decorator will be used on a controller method.

constructor(private parseService: ParseService) {
}
<<< @/docs/docs/snippets/filters/filter-decorator.ts

transform(expression: string, request, response) {
return this.parseService.eval(expression, request["body"]);
}
}
```
And finally you can use your custom filter on your controller/middleware:

Then create the decorator. This decorator will be used on a controller method.
<<< @/docs/docs/snippets/filters/filter-usage.ts

### UseFilter Options

@@UseFilter@@ allow you to register your custom decorator with few options as following:

```typescript
import {Type} from "@tsed/core";
import {BodyParamsFilter} from "../components/BodyParamsFilter";
import {IParamOptions} from "../interfaces/IParamOptions";
import {ParamTypes} from "../interfaces/ParamTypes";
import {UseFilter} from "./useFilter";
import {mapParamsOptions} from "./utils/mapParamsOptions";

export function BodyParams(expression: string): ParameterDecorator;
export function BodyParams(useType: Type<any> | Function): ParameterDecorator;
export function BodyParams(options: IParamOptions<any>): ParameterDecorator;
export function BodyParams(): ParameterDecorator;
export function BodyParams(...args: any[]): ParameterDecorator {
const {expression, useType, useConverter = true, useValidation = true} = mapParamsOptions(args);

return UseFilter(BodyParamsFilter, {
expression,
useType,
useConverter,
useValidation,
paramType: ParamTypes.BODY
});
}

```

::: tip
To link the decorator with BodyParamsFilter, you must used the @@ParamRegistry@@ API.
:::

## Built-in filters

<ApiList query="symbolName.endsWith('Filter') && symbolType === 'class'" />
- `paramType` (ParamTypes): Parameter type like BODY, QUERY, PARAMS, etc...,
- `required` (boolean, optional): Throw an error when if the value is undefined,
- `expression` (string, optional): An expression to parse,
- `useConverter` (boolean): Enable converterService to deserialize value,
- `useValidation` (boolean): Enable ValidationService,
- `useType` (boolean): Set explicitly the class/model used by the parameters.
13 changes: 13 additions & 0 deletions docs/docs/snippets/filters/basic-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Filter, IFilter, ParseService} from "@tsed/common";
import * as Express from "express";

@Filter()
export class CustomBodyParamFilter implements IFilter {

constructor(private parseService: ParseService) {
}

transform(expression: string, request: Express.Request, response: Express.Response) {
return this.parseService.eval(expression, request["body"]);
}
}
14 changes: 14 additions & 0 deletions docs/docs/snippets/filters/filter-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {IParamOptions, ParamTypes, UseFilter} from "@tsed/common";
import {CustomBodyParamFilter} from "./CustomBodyParamFilter";

export function CustomBodyParams(options: IParamOptions<any> = {}): ParameterDecorator {
const {expression, useType, useConverter = true, useValidation = true} = options;

return UseFilter(CustomBodyParamFilter, {
expression,
useType,
useConverter,
useValidation,
paramType: ParamTypes.BODY // for swagger
});
}
12 changes: 12 additions & 0 deletions docs/docs/snippets/filters/filter-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Controller, Post} from "../../../../packages/common/src/mvc/decorators";
import {CustomBodyParams} from "./filter-decorator";


@Controller("/")
export class MyController {

@Post("/")
save(@CustomBodyParams({}) payload: any) {
console.log(payload);
}
}

0 comments on commit e8de875

Please sign in to comment.