typescript-rest icon indicating copy to clipboard operation
typescript-rest copied to clipboard

Controllers using separate express routers clash with each other

Open lkjsavolainen opened this issue 6 years ago • 3 comments

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.

lkjsavolainen avatar Mar 06 '19 08:03 lkjsavolainen

I fixed it in my repo. https://github.com/LzpTec/typescript-rest

tripodsgames avatar Aug 14 '20 23:08 tripodsgames

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 ?

smalho4 avatar Aug 20 '20 18:08 smalho4

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

tripodsgames avatar Aug 21 '20 00:08 tripodsgames