Filters

Filters feature lets you create a custom decorators that will be used on the methods parameters like or .

Example

This example show you, how you can implement a filter and decorator to use these, on a method Controller. 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 decorator and in option, implement the interface:

import {Filter, IFilter, ParseService} from "@tsed/common";

@Filter()
export class BodyParamsFilter implements IFilter {

    constructor(private parseService: ParseService) {
    }

    transform(expression: string, request, response) {
        return this.parseService.eval(expression, request["body"]);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

Then create the decorator. This decorator will be used on a controller method.

import {ParamRegistry} from "@tsed/common";
import {BodyParamsFilter} from "../filters"

export function BodyParams(expression?: string | any, useType?: any): Function {
    return ParamRegistry.decorate(BodyParamsFilter, {expression, useType});
}
1
2
3
4
5
6

TIP

To link the decorator with BodyParamsFilter, you must used the API.

Built-in filters