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";
|
import { urljoin } from "$lib/util.js";
|
||||||
|
|
||||||
function doc_urljoin(path = null, 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);
|
return urljoin(import.meta.env.WEBSITE_DOC_URL, path, query);
|
||||||
}
|
}
|
||||||
|
|
||||||
function doc_check_err(json) {
|
// check JSON response and throw an error if it contains one
|
||||||
|
check_err(json) {
|
||||||
if ("error" in json)
|
if ("error" in json)
|
||||||
throw new Error(`Documentation server returned an error: ${json["error"]}`);
|
throw new Error(`Doc server returned an error: ${json["error"]}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function doc_http_get(fetch, url) {
|
// send a HTTP request to the documentation server
|
||||||
|
async GET(fetch, url) {
|
||||||
const res = await fetch(url);
|
const res = await fetch(url);
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
doc_check_err(json);
|
this.check_err(json);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function doc_get_list(fetch) {
|
// get a list of all the documentations
|
||||||
return await doc_http_get(fetch, doc_urljoin("/list"));
|
async list(fetch) {
|
||||||
|
return await this.GET(fetch, this.join("/list"));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function doc_get(fetch, name) {
|
// get a documentation
|
||||||
let url = doc_urljoin("/get");
|
async get(fetch, name) {
|
||||||
url = urljoin(url, name);
|
let url = this.join(`/get/${name}`);
|
||||||
return await doc_http_get(fetch, url);
|
return await this.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 }) {
|
export async function load({ fetch }) {
|
||||||
let projects = await api.projects(fetch);
|
let projects = await api.projects(fetch);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
projects: null === projects ? [] : projects,
|
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 }) {
|
export async function load({ fetch, params }) {
|
||||||
return {
|
return {
|
||||||
doc: await doc_get(fetch, params.name),
|
doc: await doc.get(fetch, params.name),
|
||||||
error: "",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -2,8 +2,8 @@ import api from "$lib/api.js";
|
|||||||
|
|
||||||
export async function load({ fetch }) {
|
export async function load({ fetch }) {
|
||||||
let services = await api.services(fetch);
|
let services = await api.services(fetch);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
services: null === services ? [] : services,
|
services: null === services ? [] : services,
|
||||||
error: "",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user