From 626633126601811192c4bc9ee23da7b148fecd67 Mon Sep 17 00:00:00 2001
From: Brandon Clayton <bclayton@usgs.gov>
Date: Thu, 19 May 2022 10:19:48 -0600
Subject: [PATCH] add swagger javascript per application

---
 .../src/main/resources/swagger/index.js       | 42 +++++++++++++++++++
 .../src/main/resources/swagger/index.js       | 41 ++++++++++++++++++
 2 files changed, 83 insertions(+)
 create mode 100644 src/aashto/src/main/resources/swagger/index.js
 create mode 100644 src/hazard/src/main/resources/swagger/index.js

diff --git a/src/aashto/src/main/resources/swagger/index.js b/src/aashto/src/main/resources/swagger/index.js
new file mode 100644
index 0000000..dc24f90
--- /dev/null
+++ b/src/aashto/src/main/resources/swagger/index.js
@@ -0,0 +1,42 @@
+window.onload = function() {
+  let contextPath = window.location.pathname;
+  contextPath = contextPath.endsWith('/') ? contextPath.slice(0, -1) : contextPath;
+
+  const ui = SwaggerUIBundle({
+    defaultModelsExpandDepth: 0,
+    deepLinking: true,
+    docExpansion: 'full',
+    dom_id: '#swagger-ui',
+    layout: 'BaseLayout',
+    plugins: [SwaggerUIBundle.plugins.DownloadUrl, updateContextPath(contextPath)],
+    presets: [SwaggerUIBundle.presets.apis],
+    tagsSorter: 'alpha',
+    tryItOutEnabled: true,
+    validatorUrl: null,
+    url: `./swagger`,
+  });
+
+  window.ui = ui;
+};
+
+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);
+          },
+        },
+      },
+    },
+  };
+}
diff --git a/src/hazard/src/main/resources/swagger/index.js b/src/hazard/src/main/resources/swagger/index.js
new file mode 100644
index 0000000..2a6fbfd
--- /dev/null
+++ b/src/hazard/src/main/resources/swagger/index.js
@@ -0,0 +1,41 @@
+window.onload = function() {
+  let contextPath = window.location.pathname;
+  contextPath = contextPath.endsWith('/') ? contextPath.slice(0, -1) : contextPath;
+
+  const ui = SwaggerUIBundle({
+    defaultModelsExpandDepth: 0,
+    deepLinking: true,
+    dom_id: '#swagger-ui',
+    layout: 'BaseLayout',
+    plugins: [SwaggerUIBundle.plugins.DownloadUrl, updateContextPath(contextPath)],
+    presets: [SwaggerUIBundle.presets.apis],
+    tagsSorter: 'alpha',
+    tryItOutEnabled: true,
+    validatorUrl: null,
+    url: `./swagger`,
+  });
+
+  window.ui = ui;
+};
+
+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);
+          },
+        },
+      },
+    },
+  };
+}
-- 
GitLab