admin管理员组文章数量:1202805
I'm trying to create some dynamic methods to a controller in NestJS, without much success. Basically, for every "command" declaration that I have, I want to create the same "implementation", but in a different "endpoint".
This is my generic controller:
@Controller()
export class DynamicCommandController {
handleFunction(@Req() req: FastifyRequest, @Res() response: FastifyReply) {
console.log(`Handle function...`);
console.log({ req, response });
response
.status(200)
.header('Content-Type', 'application/json')
.send({ hello: 'world' });
}
}
My generic implementation is as it follows:
const decorators = {
GET: Get,
PUT: Put,
POST: Post,
PATCH: Patch,
DELETE: Delete,
};
dynamicCommands.map(({ command }) => {
const endpointDefintion = command.prototype.endpointDefinition();
if (!endpointDefintion) {
return;
}
if (this.registeredRoutes.has(endpointDefintion.path)) {
return;
}
const currentDecorator = decorators[endpointDefintion.method];
if (!currentDecorator) {
return;
}
currentDecorator(endpointDefintion.path)(
DynamicCommandController.prototype,
'handleFunction',
Object.getOwnPropertyDescriptor(
DynamicCommandController.prototype,
'handleFunction',
),
);
this.registeredRoutes.add(endpointDefintion.path);
});
I can decorate my methods, but I can't create two @Get for the same function, they get overwriten.
If I try to "copy" the handleFunction
method for every "command", then the implementation looks like the following:
dynamicCommands.map(({ command }) => {
const endpointDefintion = command.prototype.endpointDefinition();
if (!endpointDefintion) {
return;
}
if (this.registeredRoutes.has(endpointDefintion.path)) {
return;
}
const currentDecorator = decorators[endpointDefintion.method];
if (!currentDecorator) {
return;
}
const methodName = `handleFunction_${command.prototypemandName}${command.prototype.version}`;
// Dynamically create a new method for the endpoint
const dynamicMethod = async function (
request: FastifyRequest,
response: FastifyReply,
) {
console.log(`Handle function...`);
console.log({ request, response });
response
.status(200)
.header('Content-Type', 'application/json')
.send({ hello: 'world' });
};
// Copy the method
Object.defineProperty(DynamicCommandController.prototype, methodName, {
value: dynamicMethod,
writable: true,
configurable: true,
});
applyDecorators(Req)(
DynamicCommandController.prototype,
methodName,
Object.getOwnPropertyDescriptor(
DynamicCommandController.prototype,
methodName,
),
);
currentDecorator(endpointDefintion.path)(
DynamicCommandController.prototype,
methodName,
Object.getOwnPropertyDescriptor(
DynamicCommandController.prototype,
methodName,
),
);
this.registeredRoutes.add(endpointDefintion.path);
});
The methods are created, but somehow request
and response
are undefined
.
How can I generate these methods, properly decorated with @Req
and @Res
?
本文标签: reflectionDynamic NestJS controller methodsStack Overflow
版权声明:本文标题:reflection - Dynamic NestJS controller methods - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738609683a2102560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论