Skip to content
Snippets Groups Projects
index.js 1.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
    window.onload = function() {
    
    Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
      let contextPath = window.location.pathname;
      contextPath = contextPath.endsWith('/') ? contextPath.slice(0, -1) : contextPath;
    
    Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
    
      const ui = SwaggerUIBundle({
    
        defaultModelsExpandDepth: 0,
        deepLinking: true,
        // docExpansion: 'full',
    
    Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
        dom_id: '#swagger-ui',
    
        layout: 'BaseLayout',
    
    Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
        plugins: [SwaggerUIBundle.plugins.DownloadUrl, updateContextPath(contextPath)],
    
        presets: [SwaggerUIBundle.presets.apis],
        tagsSorter: 'alpha',
        tryItOutEnabled: true,
    
    Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
        validatorUrl: null,
    
        url: `./swagger`,
    
    Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
      window.ui = ui;
    };
    
    Clayton, Brandon Scott's avatar
    Clayton, Brandon Scott committed
    
    function updateContextPath(contextPath) {
      return {
        statePlugins: {
          spec: {
            wrapActions: {
              updateJsonSpec: (oriAction) => (...args) => {
                const [spec] = args;
                if (spec && spec.paths) {
                  const newPaths = {};
                  Object.entries(spec.paths).forEach(
                    ([path, value]) => (newPaths[contextPath + path] = value)
                  );
                  spec.paths = newPaths;
                }
                oriAction(...args);
              },
            },
          },
        },
      };
    }