camouflage icon indicating copy to clipboard operation
camouflage copied to clipboard

Question: Can I add a root path to the app

Open NikhilNanjappa opened this issue 2 years ago • 6 comments

Is your feature request related to a problem? Please describe. I currently have all my endpoints as /products, /products/:id etc.

I would like to add a root path to all of these endpoints such as /api/v1 so every endpoint gets that path prepended to them - api/v1/products, api/v1/products/:id etc.

To give an idea, you could do within basic express app using:

app.use('/api/v1', allRoutes);

Describe the solution you'd like I would prefer a configuration parameter such as rootPath which then behaves as mentioned above. Alternatively, I wouldnt mind if there is a document on how to add plugins/middlewares to make this happen.

Describe alternatives you've considered Alternatively, you could just add the /api/v1 on every single routes but thats not an ideal solution because if I have something around 30+ endpoints, its tedious.

Additional context No additional context

NikhilNanjappa avatar May 19 '22 08:05 NikhilNanjappa

Hi @NikhilNanjappa,

To have a root path for all the mocked routes, you simply need to create a folder structure that can replicate it. For example, to mock the routes /api/v1/users, /api/v1/orders and /api/v1/products, you'd start by creating a folder api and a subfolder v1 under your mocks_dir. and the proceed to create users, orders and products directory under v1.

api
  v1
    users
    orders
    products

Let me know in case I have misunderstood your requirement.

shubhendumadhukar avatar Jun 28 '22 11:06 shubhendumadhukar

Hi @NikhilNanjappa, I added a config change via MR (#184). Maybe this is what you ask for:

config.yml
rootPathPrefix: /api/v1 

GET http://localhost:8080/api/v1/products -> maps to folder <mock_dirs>/products/GET.mock

It is just a simple prefix config and it is not possible to add multiple prefixes, but maybe it helps to reduce deep folder structures.

@shubhendumadhukar please take a look, thx! Olli

OllisGit avatar Jul 17 '22 08:07 OllisGit

Thanks a lot @shubhendumadhukar , that’s exactly what I wanted.

Although, it would be great if you could review Olli’s MR to have an alternative, without changing folder structure. Thanks @OllisGit

NikhilNanjappa avatar Jul 17 '22 09:07 NikhilNanjappa

If I am being honest, I am not really convinced with this solution. While it does work, I think it would be counterproductive having an additional config just to add a root path. The same can be achieved by creating an appropriate folder structure.

I do like the idea of being able to inject some middlewares as proposed in the issue earlier, since that approach could have utilities not limited to only adding a root path.

Proposal We introduce a file called middleware.js, which contains an IIFE declaring/configuring required middlewares. At startup, Camouflage checks if the file middleware.js exists at the root of the project. (and if injection is enabled). If both conditions are fulfilled, the middleware IIFE is executed.

Example middleware.js

(()=>{
  const actuator = require('express-actuator');

  app.use(actuator());
  app.use('/api/v1', allRoutes);
});

There would require a significant refactoring to expose allRoutes, but if we were to implement this, middleware injection would be a more elegant solution in my opinion.

shubhendumadhukar avatar Jul 18 '22 03:07 shubhendumadhukar

I like the middleware approach. It’s neat and concise. However, has the caveat of exposing allRoutes.

NikhilNanjappa avatar Jul 18 '22 07:07 NikhilNanjappa

I have implemented the required changes, however I'd like to run few more tests before I merge it to main.

If you'd like to test it yourself, feel free to build it from source

shubhendumadhukar avatar Jul 18 '22 08:07 shubhendumadhukar

Closed in #185

shubhendumadhukar avatar Aug 21 '22 12:08 shubhendumadhukar