# Custom endpoint decorator

Custom endpoint decorator could be interesting when you want to handle a request and perform actions before or after the endpoint method.

Unlike Pipes that operate on the parameters of a method, the endpoint decorator will operate on the method itself.

It's based on the middleware and use one of these decorators to work:

  • : use the given middleware before the method,
  • : use the given middleware after UseBefore but before the method,
  • : use the given middleware after the method.

# Built-in

Many other decorators are implemented and can be taken as an example to build your own endpoint decorator. Just follow the links here and click on source link to see source code on Github:

# Build your own decorator

One of the usecases already implemented by Ts.ED is the :

import {NotAcceptable} from "@tsed/exceptions";
import {Middleware, Req} from "../../mvc/decorators";
import {IMiddleware} from "../../mvc/interfaces";

/**
 * @middleware
 */
@Middleware()
export class AcceptMimesMiddleware implements IMiddleware {
  public use(@Req() request: Req): void {
    const mimes = request.ctx.endpoint.get(AcceptMimesMiddleware) || [];
    const find = mimes.find((mime: string) => request.accepts(mime));

    if (!find) {
      throw new NotAcceptable(mimes.join(", "));
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

You can see in this example the usage of endpoint.get from . This method contains all options which can be passed to the decorator associated to AcceptMimesMiddleware.

import {Controller, Get} from "@tsed/common";
import {AcceptMime} from "@tsed/common/src/mvc/decorators/method/acceptMime";

@Controller("/mypath")
export class MyCtrl {
  @Get("/")
  @AcceptMime("application/json")
  public getResource() {
  }
}
1
2
3
4
5
6
7
8
9
10

TIP

This example uses decorator with one option, the application/json. This option will be set to endpoint.get seen before with AcceptMimesMiddleware example and can be retrieved by using endpoint.get(AcceptMimesMiddleware).

Ts.ED provides API to create your own decorator like which registers the options and at least one middleware with theses decorators and utils:

  • , , , or for middleware registration,
  • if you want to combine different decorators,
  • or to register options.

For example, we can take the decorator as an example and see how it works. Here is its code:

import {AcceptMimesMiddleware, UseBefore} from "@tsed/common";
import {applyDecorators, StoreSet} from "@tsed/core";

export function AcceptMime(...mimes: string[]): Function {
  return applyDecorators(
    StoreSet(AcceptMimesMiddleware, mimes),
    UseBefore(AcceptMimesMiddleware)
  );
}
1
2
3
4
5
6
7
8
9

TIP

It is also possible to create a custom endpoint decorator without Ts.ED middleware. If your action is really small, you can register a pure express middleware to reach better performance.

import {UseAfter} from "@tsed/common";
import {applyDecorators} from "@tsed/core";

export function CustomStatus(code: number) {
  return applyDecorators(
    UseAfter((req: any, res: any, next: any) => {
      res.status(code);
      next();
    })
  );
}
1
2
3
4
5
6
7
8
9
10
11