Basically I am trying to build a class on base a some parameters that are given on the function args, but returning it as anonymous class, but the error 'Decorators are not valid here.ts(1206)'.
const ControllerTemplateBuilder = (newRoute:string)=>{
return class {
@route(newRoute)
async getAll(req: Request, res: Response) {
try {
return res.status(200).json({ mesg: "helo" })
} catch (error) {
const { message } = error as Error;
throw new Error(message);
}
}
}
}
Decorators are not allowed in class expressions. You can easily work around this by defining the class, then returning it:
const ControllerTemplateBuilder = (newRoute:string)=>{
class _ {
@route(newRoute)
async getAll(req: Request, res: Response) {
try {
return res.status(200).json({ mesg: "helo" })
} catch (error) {
const { message } = error as Error;
throw new Error(message);
}
}
};
return _;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With