question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add decorator after @Get

See original GitHub issue

Its posible add my own decorator after @Get decorator? Because when I do this res seems to be not linked with express response

  @Get()
  @Decorator()
  public async getAll( @Res() res: Response) {
    res.status(HttpStatus.OK).send();

Update: Watching this I think it can be posible

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
FranciZcommented, Jul 10, 2017

@cojack Thanks, but I’m aware of this. I’d still rather have the option to apply middleware in the controller since it’ll be much more transparent.

I see usefulness in defining middleware in the module if it’s for all the routes of a controller or something more general. But for things like role specific access control and authentication it’s much more transparent to add it as a decorator in controller imo.

Having to define paths and methods affected in two different places is also something I’d rather avoid.

2reactions
FranciZcommented, Jul 9, 2017

@silvelo @ThomRick I’m facing a similar dilemma. I want to have transparent route handling and plug authentication and role checking per route in the controller in most cases. Not sure what the advantage is of having this logic separated in another file (Module) and paths redefined in two places.

Something like the UseMiddlewares decorator as below would be great.

  @Delete('v1/user/:userId')
  @UseMiddlewares(new AuthMiddleware(['ADMIN']))
  public async deleteUser(@Req() req: IRequest, @Res() res: Response, @Param() reqParams: VDeleteUser): Promise<any> {
    const userId = reqParams.userId;
    const removedUser = await this.userService.remove(userId);
    if (removedUser === null) return this.responseService.error({
      res,
      code: HttpStatus.NOT_FOUND,
      data: { _id: userId }
    });
    this.responseService.success({ res, data: { _id: userId } });
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can one attach a decorator to a function "after the fact" in ...
You imported sqrt into your module, just apply the decorator there in your own global namespace: sqrt = print_args_decor(sqrt).
Read more >
Primer on Python Decorators
In this introductory tutorial, we'll look at what Python decorators are and how to create and use them.
Read more >
How to Create and Use Decorators in Python With Examples
We use a decorator by placing the name of the decorator directly above the function we want to use it on. You prefix...
Read more >
using decorator functions in Python - ZetCode
Decorators are functions which decorate (or wrap) other functions and execute code before and after the wrapped function runs.
Read more >
Decorators - Python 3 Patterns, Recipes and Idioms
Function Decorators¶ · aFunction() is called after it has been decorated, we get completely different behavior; the · my_decorator.__call__() method is called ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found