cleanup doc.js for a better export interface
Signed-off-by: ngn <ngn@ngn.tf>
This commit is contained in:
@@ -1,31 +1,36 @@
|
||||
// TODO: clean this up like api.js
|
||||
|
||||
import { urljoin } from "$lib/util.js";
|
||||
|
||||
function doc_urljoin(path = null, query = {}) {
|
||||
return urljoin(import.meta.env.WEBSITE_DOC_URL, path, query);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
function doc_check_err(json) {
|
||||
if ("error" in json)
|
||||
throw new Error(`Documentation server returned an error: ${json["error"]}`);
|
||||
}
|
||||
|
||||
async function doc_http_get(fetch, url) {
|
||||
const res = await fetch(url);
|
||||
const json = await res.json();
|
||||
doc_check_err(json);
|
||||
return json;
|
||||
}
|
||||
|
||||
async function doc_get_list(fetch) {
|
||||
return await doc_http_get(fetch, doc_urljoin("/list"));
|
||||
}
|
||||
|
||||
async function doc_get(fetch, name) {
|
||||
let url = doc_urljoin("/get");
|
||||
url = urljoin(url, name);
|
||||
return await doc_http_get(fetch, url);
|
||||
}
|
||||
|
||||
export { doc_urljoin, doc_get, doc_get_list };
|
||||
const doc = new Doc();
|
||||
export default doc;
|
||||
|
@@ -2,8 +2,8 @@ import api from "$lib/api.js";
|
||||
|
||||
export async function load({ fetch }) {
|
||||
let projects = await api.projects(fetch);
|
||||
|
||||
return {
|
||||
projects: null === projects ? [] : projects,
|
||||
error: "",
|
||||
};
|
||||
}
|
||||
|
@@ -1,8 +1,7 @@
|
||||
import { doc_get } from "$lib/doc";
|
||||
import doc from "$lib/doc";
|
||||
|
||||
export async function load({ fetch, params }) {
|
||||
return {
|
||||
doc: await doc_get(fetch, params.name),
|
||||
error: "",
|
||||
doc: await doc.get(fetch, params.name),
|
||||
};
|
||||
}
|
||||
|
@@ -2,8 +2,8 @@ import api from "$lib/api.js";
|
||||
|
||||
export async function load({ fetch }) {
|
||||
let services = await api.services(fetch);
|
||||
|
||||
return {
|
||||
services: null === services ? [] : services,
|
||||
error: "",
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user