typescript-rest
typescript-rest copied to clipboard
Controllers using separate express routers clash with each other
Hello,
My current use case is that I have a service that provides two separate APIs. However even though these APIs are using different express routers and path prefixes typescript-rest fails with a runtime error.
For example:
# First API module
@Path('/v1')
export class FirstAPI extends AbstractController {
@GET
@Path('/')
public async get(): Promise<Result> {
}
}
const firstAPIRouter = express.Router();
Server.buildServices(firstAPIRouter, [FirstAPI]);
export default firstAPIRouter
# Second API module
@Path('/v1')
export class SecondAPI extends AbstractController {
@GET
@Path('/')
public async get(): Promise<Result> {
}
}
const secondAPIRouter = express.Router();
Server.buildServices(secondAPIRouter);
export default secondAPIRouter;
# Express configuration
const app = express();
app.use('/first-api', managementRouter);
app.use('/second-api', deliveryRouter);
As long as the two controllers are using different paths everything works fine but if any of the controller level paths are the same like in this example the following error is thrown at runtime:
[ERROR] 10:25:21 Error: Duplicated declaration for path [/v1/], method [1]. Function.InternalServer.resolvePath (/Volumes/repositories/promotions-api/node_modules/typescript-rest/dist/server-container.js:539:19)
Since these APIs are separated at express level there should be a way to avoid this. I have looked through the documentation but didnt see anything related to this.
I fixed it in my repo. https://github.com/LzpTec/typescript-rest
i'm facing the same issue. @EiZei did you find a solution for this ? @tripodsgames is there any way to achieve this without actually making changes to the library ?
i'm facing the same issue. @EiZei did you find a solution for this ? @tripodsgames is there any way to achieve this without actually making changes to the library ?
These are the changes I made to correct this problem. https://github.com/LzpTec/typescript-rest/commit/ceee1a97b80f9186332ee6e3c959219452b80961