sonic-express icon indicating copy to clipboard operation
sonic-express copied to clipboard

Whitelist or blacklist paths

Open oneEyedSunday opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. Naa, not a problem, just an extension for my use case.

Describe the solution you'd like An option (via config or a function) to whitelist or blacklist paths

Describe alternatives you've considered I've considered adding a post-processing step after the tests that will strip out paths from the doc. This is likely trivial, but other items like tags, schemas and responses (schemas and responses are auto-generated, so very tricky to remove in a post-processing step).

Another alternative is to use an unless middleware that only runs the getResponseExpress(...) on select routes

/**
 * Helper to apply middleware selectively to routes
 * @param {*} prefixes
 * @param {*} middleware
 */
function unless(prefixes = [], middleware) {
  return function (req, res, next) {
    if (prefixes.includes(req.url)) {
      return next();
    } else {
      return middleware(req, res, next);
    }
  };
}

This could be run in userland, but I'm not sure if sonic-express has any expectations regarding how its attached as a middleware.

Additional context We have an API, housed in a single codebase that will feed both internal and external consumers. For now, this distinction is via route groups and different authorizers. I'm using sonic-express to update the swagger doc we expose to internal consumers. I need a way to not include routes that are meant for other consumers.

oneEyedSunday avatar May 06 '21 06:05 oneEyedSunday

Hello

Currently, what I do for this particular use case is to just import the json via a node script and remove the routes directly.

Since this use case would be very common, I can add a step or two to perform this.

Do you already have a solution?

tiemma avatar Jul 09 '21 11:07 tiemma

I wrapped the middleware step in the unless middleware effectively only calling sonic-express for the routes I need. I'm curious to see how your solution looks like.

oneEyedSunday avatar Jul 09 '21 15:07 oneEyedSunday

Thanks

I'd add that in as a feature in a new MR.

Reopening this issue so I can link it

tiemma avatar Aug 16 '21 09:08 tiemma