-
Notifications
You must be signed in to change notification settings - Fork 397
Description
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 😀