Tsoa: Middleware decorators

Created on 27 Jan 2017  路  8Comments  路  Source: lukeautry/tsoa

Hello,

it would be great to have some kind of Middleware decorators, where you can give middlewares to run before (or even after) a controller method. Middlewares could be on a separate layer or even special controller methods.

e.g.

@Route('Users')
export class UsersController {

    @Middleware
     public validate(request: express.Request, response: express.Request, next: function) {
        // TODO: implement a middleware
    }

    @Middleware
     public render(request: express.Request, response: express.Request, next: function) {
        // TODO: implement a rendering middleware
    }

    @MiddlewareBefore(UserController.validate)
    @MiddlewareAfter(UserController.render)
    @Get('{id}')
    public async getUser(id: number, @Request() request: express.Request): Promise<User> {
        // TODO: implement some code that uses request as well
    }
}

With this feature, existing Node.js projects would be much easeier to migrate into a tsoa project and also middleware pattern is essential part of Node.js applications.

It's could be an issue how to handle middleware chains, so e.g. MiddlewareBefore could contain an array of middlewares, or multiple decorators could be attached.

If we discuss the details I can help with implementation.
Thanks!

Most helpful comment

@lukeautry While custom route templates will work since they are highly customizable, I think the routes-template is a little tedious to maintain (IMHO).

I'm currently using inversify-express-utils in my projects and they solved the issue using decorators, which I think is a little nicer and is discussed here: https://github.com/inversify/InversifyJS/issues/487.
TLDR;
Option 1

@Controller("/")
@Middleware(Constants.AuthorizeMiddleware)
class MyController {

Option 2

@Controller("/", Constants.AuthorizeMiddleware)
class MyController {

Thoughts?

All 8 comments

@Paldom I really like that idea. I'll get back to you with some more in-depth thoughts.

@Paldom Custom route templates have been introduced recently; I think that offers enough to allow you to provide these customized middlewares.

@lukeautry While custom route templates will work since they are highly customizable, I think the routes-template is a little tedious to maintain (IMHO).

I'm currently using inversify-express-utils in my projects and they solved the issue using decorators, which I think is a little nicer and is discussed here: https://github.com/inversify/InversifyJS/issues/487.
TLDR;
Option 1

@Controller("/")
@Middleware(Constants.AuthorizeMiddleware)
class MyController {

Option 2

@Controller("/", Constants.AuthorizeMiddleware)
class MyController {

Thoughts?

@ezra-quemuel I really like your proposal, have you solved it in some way?

may be it's possible to add this middleware decorator in easy way ?

@lukeautry Has this functionality ever been implemented? As far as I know (which isn't much) there's no way to add additional custom metadata to controllers/methods making it really awkward and maintenance-heavy to implement this using custom templates.

I'm looking into migrating out of inversifyjs into this library and typescript-ioc.
I'm currently using passport for authentication and I'm looking for a way to integrate it using a middleware for a specific route (login only). I couldn't find how to do it using the documentation here, can someone share a demo project?
I also favor the decoration for middleware inversify is using, I hope this can be done here as well...

Can we reopen this issue? I'm also having the same issue right now, even though I'm aware of the custom route templates, It would be way cleaner if I just coudl add "Middleware" like propsed earlier. I could work on this feature if this is a "valid" feature @lukeautry ?

62 is the currently open issue that may encompass your issue aswell @simllll.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ancizararenas picture ancizararenas  路  3Comments

fahrradflucht picture fahrradflucht  路  4Comments

strongpauly picture strongpauly  路  3Comments

amcdnl picture amcdnl  路  6Comments

jakubzloczewski picture jakubzloczewski  路  6Comments