Skip to content

Commit e49a2b8

Browse files
committed
fix: build-time cache tags
1 parent fd40c99 commit e49a2b8

4 files changed

Lines changed: 45 additions & 18 deletions

File tree

api-docs-fetcher.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const fetchUrls = async () => {
2+
const res = await fetch('https://api.github.com/repos/gotify/server/tags', {
3+
headers: {Accept: 'application/vnd.github.v3+json'},
4+
});
5+
if (!res.ok) {
6+
throw Error('could not fetch tags');
7+
}
8+
const tags: {name: string}[] = await res.json();
9+
return tags.map((tag) => ({
10+
url: `https://raw.githubusercontent.com/gotify/server/${tag.name}/docs/spec.json`,
11+
name: tag.name,
12+
}));
13+
};

api-docs.data.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {fetchUrls} from './api-docs-fetcher';
2+
3+
// This is evaluated at build-time: https://vitepress.dev/guide/data-loading
4+
export default {
5+
async load() {
6+
return fetchUrls();
7+
},
8+
};

api-docs.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,30 @@ head:
55
- [link, {rel: stylesheet, href: /swagger/swagger-ui.css}]
66
- [script, {src: /swagger/swagger-ui-bundle.js}]
77
- [script, {src: /swagger/swagger-ui-standalone-preset.js}]
8-
- [script, {src: /swagger/init.js}]
98
---
109

1110
<div id="swagger-ui"></div>
11+
12+
<script setup>
13+
import { data as urls } from './api-docs.data.ts'
14+
import { onMounted } from 'vue'
15+
import { fetchUrls } from './api-docs-fetcher.ts'
16+
17+
onMounted(() => {
18+
fetchUrls()
19+
.catch((e) => {
20+
console.log("Failed to load tags", e)
21+
return urls;
22+
})
23+
.then(urls => {
24+
window.SwaggerUIBundle({
25+
urls,
26+
dom_id: '#swagger-ui',
27+
deepLinking: true,
28+
presets: [window.SwaggerUIBundle.presets.apis, window.SwaggerUIStandalonePreset],
29+
plugins: [window.SwaggerUIBundle.plugins.DownloadUrl],
30+
layout: 'StandaloneLayout',
31+
})
32+
})
33+
})
34+
</script>

public/swagger/init.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)