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.

Support for custom param decorators

See original GitHub issue

I have seen that many people fork this repo to add custom decorators for exporting object from request object and inject it in the controllers methods params. It’s a bit pain to modify switch-case in core of routing-controllers every time we need e.g. user object, jwt token or sth else.

I like the way that class-validator let us define our custom validation decorators: https://github.com/pleerock/class-validator#custom-validation-decorators

It would be great if we could write custom decorators function to extract objects from request rather than forking and patching routing-controllers. It could be done the same way - the decorator function should call registerParamHandler with config object with required data (like in ParamMetadataArgs) and handler field - the function to call to extract:

export function CustomDecorator(customParam?: customType) {
    return function (object: Object, methodName: string, index: number) {
        let format = (Reflect as any).getMetadata("design:paramtypes", object, methodName)[index];
        registerParamHandler({
            reflectedType: format,
            isRequired: true,
            classTransformOptions: undefined,
            // other settings here
            handler: (req: Request) => {
                // do the extract operation here
                if (customParam == someValue) {
                    return req.property.more.arr[someValue];
                } else {
                    return req.property.more.value;
                }
            }
        });
    };
}

The handlers could be pushed to array of handlers and in core of routing-controllers, after matching standard param types it could traverse throught this array, call the handlers and do the transform magic 😀

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pleerockcommented, May 22, 2017

I just want to use the CurrentUser decorator without always have to define “required: true”.

What if you do it this way:

export default function CurrentUserAlwaysRequired()
{
    return CurrentUser({ required: true });
}
0reactions
github-actions[bot]commented, Dec 14, 2020

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom decorators | NestJS - A progressive Node.js framework
Nest provides a set of useful param decorators that you can use together with the HTTP route handlers. Below is a list of...
Read more >
NestJS — Creating custom parameter decorators for your APIs ...
Simply speaking, you should use createParamDecorator() only if you are creating parameter decorators, and your decorator is interacting with the req object. If ......
Read more >
Custom param decorator which transform param to database ...
You can implement a custom pipe that injects a TypeORM repository and returns the database entity when prompted with an ID, something like ......
Read more >
Custom decorators - TypeGraphQL
Custom decorators are a great way to reduce the boilerplate and reuse some ... TypeGraphQL supports two kinds of custom decorators - method...
Read more >
Documentation - Decorators - TypeScript
With the introduction of Classes in TypeScript and ES6, there now exist certain scenarios that require additional features to support annotating or ...
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