Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware decorators #47

Closed
Paldom opened this issue Jan 27, 2017 · 9 comments · Fixed by #1123
Closed

Middleware decorators #47

Paldom opened this issue Jan 27, 2017 · 9 comments · Fixed by #1123

Comments

@Paldom
Copy link

Paldom commented Jan 27, 2017

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!

@lukeautry
Copy link
Owner

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

@lukeautry
Copy link
Owner

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

@ezra-quemuel
Copy link

ezra-quemuel commented May 3, 2017

@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: inversify/InversifyJS#487.
TLDR;
Option 1

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

Option 2

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

Thoughts?

@myflowpl
Copy link

@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 ?

@HoldYourWaffle
Copy link
Contributor

@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.

@HarelM
Copy link

HarelM commented Jul 7, 2019

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...

@simllll
Copy link
Contributor

simllll commented Sep 30, 2019

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 ?

@WoH
Copy link
Collaborator

WoH commented Sep 30, 2019

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

@kempsteven
Copy link

id like to do the MiddlewareAfter is there anyway to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants