Files
website/app/src/lib/doc.js
2025-07-24 14:27:27 +03:00

37 lines
905 B
JavaScript

import { urljoin } from "$lib/util.js";
class Doc {
// join given path and queries with a document server URL
join(path = null, query = {}) {
return urljoin(import.meta.env.WEBSITE_DOC_URL, path, query);
}
// check JSON response and throw an error if it contains one
check_err(json) {
if ("error" in json)
throw new Error(`Doc server returned an error: ${json["error"]}`);
}
// send a HTTP request to the documentation server
async GET(fetch, url) {
const res = await fetch(url);
const json = await res.json();
this.check_err(json);
return json;
}
// get a list of all the documentations
async list(fetch) {
return await this.GET(fetch, this.join("/list"));
}
// get a documentation
async get(fetch, name) {
let url = this.join(`/get/${name}`);
return await this.GET(fetch, url);
}
}
const doc = new Doc();
export default doc;