[ ] Regression
[ ] Bug report
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
We are trying to implement an internal API Router Proxy. The objective is to call other REST queries by path from inside NestJS: we want to query other REST endpoints procedurally to then post process the data all together (our aim is to export data as CSV from multiple REST endpoints).
We have been trying to use reflection, but we realized that Nest already handles it internally in RoutesMapper. Is there any option to access this class or somehow instantiate it from inside the business logic of an endpoint? Mostly we would pass some URLs and they would be resolved to functions that we should be able to call. We could create a huge internal switch inside our Business logic to handle so, but this means registering the URLs twice (in NestJS REST and in our BI).
I know we could do HTTP requests, but as the user is already autentified and HTTP produces overhead, we were thinking if this can be achived internally in NestJS with some work.
Also, we would be willing to implement it if we get some pointers on how this could be achieved or if RoutesMapper is the proper class to check on for this features.
Wouldn't it be easier to just move the logic from controllers to services and use services from this "proxy"?
Yes, in fact we have all this logic inside of services already.
However, this would mean creating a new internal routing service that would map a request to its function internally, which in fact NestJS is already doing.
To explain a bit better what we are aiming to do: we want to send a POST with a list of queries (in JSON format) to an endpoint. This endpoint's logic would be to batch process this queries (JSON), doing the request to the proper function in the proper service and then return all together to the client after some transformation. So, virtually, this would trigger multiple queries, and return them all together after applying some transformation.
Of course, this request the proper function in the proper service means that we need to map each function to an URL, that, well, if you think it through, NestJS already has already previously done to expose its endpoints to REST. So mostly, I was wondering if there is any way to access the metadata that NestJS already uses under the hood in order to be able to map a URL to a function.
Otherwise, we will end replicating exactly the routing system that NodeJS uses (which, well, it's a bit pointless I think)
What this feature would provide is a way to batch process multiple queries with a single REST call.
That we could do multiple calls and fetch everything in different requests? Yes, but we want to add some extra logic to merge all the services' answers into a CSV to export API data, or any other transformation afterwards.
plus 1 as we are trying to do something similar
With @nestjs/platform-express:
@Controller('cats')
export class CatsController {
constructor(
private adapterHost: HttpAdapterHost,
) {}
@Get()
async findAll(@Req() req, @Res() res) {
req.url = '/cats/1'; // change URL here
const instance = this.host.httpAdapter.getInstance();
instance._router.handle(req, res, null);
}
@Get(':id')
findOne(id: number) {
// this will be called
}
}
Is this a complete solution? It seems like the approach @kamilmysliwiec listed would solve it for resolving one of the controller routes but in @Sturgelose example it seemed like they wanted to hit multiple controllers, manipulate the data and send back a combined response. Is that possible here?
Most helpful comment
Is this a complete solution? It seems like the approach @kamilmysliwiec listed would solve it for resolving one of the controller routes but in @Sturgelose example it seemed like they wanted to hit multiple controllers, manipulate the data and send back a combined response. Is that possible here?