Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

FIX: Dynamically patch the BaseURL on load (API)

Katos6b938a88cf68
diff --git a/docs/en/api.md b/docs/en/api.md
index fb9d1536..add0ad99 100644
--- a/docs/en/api.md
+++ b/docs/en/api.md
@@ -4,27 +4,28 @@ sidebar: false
---
<script setup lang="ts">
-import { ref, watch, onMounted, onBeforeUnmount } from 'vue';
+import { ref, onMounted, watch, onBeforeUnmount } from 'vue';
import { useData } from 'vitepress';
-// Reactive key for re-rendering the elements-api component
-const componentKey = ref(0);
-
-// Set BaseURL
-const BaseURL = "https://demo.homebox.software/api";
-
-// Access dark mode setting from VitePress
-const { isDark } = useData();
-const theme = ref(isDark.value ? 'dark' : 'light');
+const apiSpec = ref(null);
+const demoBaseUrl = "https://demo.homebox.software/api";
-// Watch for changes to the dark mode value and force a re-render when it changes
-watch(isDark, (newVal) => {
- theme.value = newVal ? 'dark' : 'light';
- // Increment key to force a refresh of the Stoplight component and its CSS
- componentKey.value++;
+// Fetch and patch the spec on mount
+onMounted(async () => {
+ try {
+ const res = await fetch('https://cdn.jsdelivr.net/gh/sysadminsmedia/homebox@main/docs/docs/api/openapi-2.0.json');
+ const spec = await res.json();
+ // Override the host and basePath
+ spec.host = "demo.homebox.software";
+ spec.basePath = "/api";
+ apiSpec.value = spec;
+ } catch (error) {
+ console.error("Error fetching the OpenAPI spec:", error);
+ }
});
-// Use a native hashchange listener (as before) to refresh on navigation changes
+// For navigation refresh
+const componentKey = ref(0);
const handleHashChange = () => {
componentKey.value++;
};
@@ -36,7 +37,15 @@ onBeforeUnmount(() => {
window.removeEventListener('hashchange', handleHashChange);
});
-// Append the Stoplight Elements script and stylesheet
+// Handle theme changes to re-render the component
+const { isDark } = useData();
+const theme = ref(isDark.value ? 'dark' : 'light');
+watch(isDark, (newVal) => {
+ theme.value = newVal ? 'dark' : 'light';
+ componentKey.value++;
+});
+
+// Append Stoplight Elements script and stylesheet
const elementScript = document.createElement('script');
elementScript.src = 'https://unpkg.com/@stoplight/elements/web-components.min.js';
document.head.appendChild(elementScript);
@@ -48,13 +57,15 @@ document.head.appendChild(elementStyle);
</script>
<client-only>
+ <!-- Render only after the spec is loaded -->
<elements-api
+ v-if="apiSpec"
:key="componentKey"
- apiDescriptionUrl="https://cdn.jsdelivr.net/gh/sysadminsmedia/homebox@main/docs/docs/api/openapi-2.0.json"
+ :apiDescription="apiSpec"
router="hash"
layout="responsive"
hideSchemas="true"
:data-theme="theme"
- :tryItBaseUrl="BaseURL"
+ tryItBaseUrl="https://demo.homebox.software/api"
/>
</client-only>