scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.4k stars 225 forks source link

Ability to inject plugins #340

Open Tsilo opened 1 year ago

Tsilo commented 1 year ago

in order to pass preset/plugins we need to add checking in jsTplString so it can push new functions to array

for (var attrname in customOptions) {
    if(attrname == "presets" || attrname == "plugins"){
      swaggerOptions[attrname].push(customOptions[attrname]);
    }else{
      swaggerOptions[attrname] = customOptions[attrname];
    }
  }

here's inject example

var SwaggerHmacAuth = function (system) {
  return {
    statePlugins: {
      spec: {
        wrapActions: {
          setMutatedRequest: (oriAction, system) => (str) => {
            // here, you can hand the value to some function that exists outside of Swagger UI
            console.log('Here is my API definition', str, oriAction, system);
            return oriAction(str); // don't forget! otherwise, Swagger UI won't update
          },
        },
      },
    },
  };
};
let swaggerOptions = {
  presets: [SwaggerHmacAuth],
};