Files
Atay-Makhzan/web_src/js/standalone/swagger.ts
T

49 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-03-29 12:24:30 +02:00
import '../../css/standalone/swagger.css';
import SwaggerUI from 'swagger-ui-dist/swagger-ui-es-bundle.js';
import 'swagger-ui-dist/swagger-ui.css';
2026-01-26 10:34:38 +08:00
import {load as loadYaml} from 'js-yaml';
2020-01-14 19:02:08 +01:00
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
const apply = () => document.documentElement.classList.toggle('dark-mode', prefersDark.matches);
apply();
prefersDark.addEventListener('change', apply);
2020-01-14 19:02:08 +01:00
window.addEventListener('load', async () => {
2026-01-26 10:34:38 +08:00
const elSwaggerUi = document.querySelector('#swagger-ui')!;
const url = elSwaggerUi.getAttribute('data-source')!;
let spec: any;
if (url) {
2026-03-29 12:24:30 +02:00
const res = await fetch(url); // eslint-disable-line no-restricted-globals
2026-01-26 10:34:38 +08:00
spec = await res.json();
} else {
const elSpecContent = elSwaggerUi.querySelector<HTMLTextAreaElement>('.swagger-spec-content')!;
const filename = elSpecContent.getAttribute('data-spec-filename');
const isJson = filename?.toLowerCase().endsWith('.json');
spec = isJson ? JSON.parse(elSpecContent.value) : loadYaml(elSpecContent.value);
}
2020-01-14 19:02:08 +01:00
// Make the page's protocol be at the top of the schemes list
const proto = window.location.protocol.slice(0, -1);
2026-01-26 10:34:38 +08:00
if (spec?.schemes) {
spec.schemes.sort((a: string, b: string) => {
if (a === proto) return -1;
if (b === proto) return 1;
return 0;
});
}
2020-01-14 19:02:08 +01:00
2025-03-06 00:03:44 +08:00
SwaggerUI({
2020-01-14 19:02:08 +01:00
spec,
dom_id: '#swagger-ui',
deepLinking: true,
docExpansion: 'none',
defaultModelRendering: 'model', // don't show examples by default, because they may be incomplete
2020-01-14 19:02:08 +01:00
presets: [
SwaggerUI.presets.apis,
2020-01-14 19:02:08 +01:00
],
plugins: [
SwaggerUI.plugins.DownloadUrl,
],
2020-01-14 19:02:08 +01:00
});
});