scttcper / koa2-swagger-ui

Swagger UI as Koa v2 middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow to configure functional swagger option parameters and plugins

opened this issue · comments

Hello,

currently its necessary to configure requestInterceptor and also a plugin.
So I extends a local copy of your project to add the new functionality.

If you can adopt my changes to your code, I can switch back to your project.

Thx

//index.js
...
module.exports = function koaSwagger(config) {
...
  // added two helper to check and render functions directly
  Handlebars.registerHelper('strfnc', fnc => fnc)
  Handlebars.registerHelper('isset', function (conditional, options) { return !!conditional ? options.fn(this) : options.inverse(this) })
...
{{! index.hbs }}
...
  window.onload = function() {
    const options = {{{json swaggerOptions}}};
    {{! Check if is set and set the four functional options }}
    {{#isset swaggerOptions.requestInterceptor}}
      options.requestInterceptor = {{{strfnc swaggerOptions.requestInterceptor}}}
    {{/isset}}
    {{#isset swaggerOptions.responseInterceptor}}
    options.responseInterceptor = {{{strfnc swaggerOptions.responseInterceptor}}}
    {{/isset}}
    {{#isset swaggerOptions.responseInterceptor}}
    options.modelPropertyMacro = {{{strfnc swaggerOptions.modelPropertyMacro}}}
    {{/isset}}
    {{#isset swaggerOptions.parameterMacro}}
    options.parameterMacro = {{{strfnc swaggerOptions.parameterMacro}}}
    {{/isset}}


    {{! extends presets and plugins - both must be an array or undefined }}
    options.presets = [
      SwaggerUIBundle.presets.apis,
      // yay ES6 modules ↘
      Array.isArray(SwaggerUIStandalonePreset) ? SwaggerUIStandalonePreset : SwaggerUIStandalonePreset.default,
      {{{swaggerOptions.presets}}}
    ];
    options.plugins = [
      SwaggerUIBundle.plugins.DownloadUrl,
      {{#if hideTopbar}}
        HideTopbarPlugin
      {{/if}},
      {{{swaggerOptions.plugins}}}
    ];
...

Can you do a pr please?

published as 2.7.0. thanks for the PR