Search code examples
javascriptfetch-apiswagger-uiopenapi

fetch API, forbidden headers required in OpenAPI


I'm currently working on creating an OpenAPI file based on documentation to set up a user-friendly API playground using SwaggerUI. However, I've run into issue with the fetch API as it restricts certain headers, including a required Date header for one of the APIs calls.

Is it possible to override this behavior or replace Swagger UI's fetch API with another compatible solution? Based on my understanding of the documentation, it appears that swagger-client is the only library offering a custom fetch feature, but you can't combine it with SwaggerUI.

API documentation that mention Date header https://rest.websupport.sk/docs/v1.intro


Solution

  • Unfortunately, as of version v5.1.0, there is no built-in capability to override the behavior of Swagger UI's fetch API or replace it with another compatible fetch API.

    Currently, the swagger-client library is initialized and wrapped only for the preFetch and postFetch options, limiting its customization possibilities.

    // swagger-ui/core/plugins/swagger-js/index.js
    // ...
    import Http, { makeHttp, serializeRes } from "swagger-client/es/http"
    
    export default function({ configs, getConfigs }) {
      return {
        fn: {
          fetch: makeHttp(Http, configs.preFetch, configs.postFetch),
          buildRequest,
          execute,
          resolve: makeResolve
        }
        // ...
      }
    }
    

    By making this dependency configurable, it could open up the potential for replacing the default fetch API behavior in Swagger UI and allowing the required Date header and other headers to be used as needed.

    I'll update this post if I manage to submit a pull request for this feature request.