typescript-rest
typescript-rest copied to clipboard
How to override @Path?
I'm trying to implement object-oriented style for my endpoints, but I end up having issues with @Path() because of that. Example:
// users.endpoint.ts
@Path('users')
class UsersEndpoint extends CRUD {
@Path('me')
@GET
async getMyData() {
// my logic here
}
}
// crud.endpoint.ts
class CRUD {
@GET
async readAll() {
// return all items
}
@Path(':id')
@GET
async read(@PathParam('id') id: string) {
// return single item
}
// All other CRUD endpoints (DELETE, UPDATE, etc)...
}
The issue here is that when I try to access GET /users/me it instead triggers first GET /users/:id, which is in my CRUD class.
Is there a way to prevent this from happening? I just want one route (GET /users/me) triggering, if it's not found, then it could go to the other route (GET /users/:id).
There is no way that the @Path decorator would be able to tell that "me" wasn't a valid value for :id
However it is still based on order, so if you just put the GET /users/me function above the GET /users/:id function, it would match to that first.
But afaik methods inherited from CRUD class will always be included first in my UsersEndpoint. Which means is basically "impossible" for me to put any @Path method before them.
For me this is a problem because one of the most beautiful things about JS6+/TypeScript (class inheritance) can't be used properly for creating fast CRUD endpoints.
But you are still asking the code to understand the difference between an arbitrary string which could be the string "me" and another string which definitely is "me".