Implemented api specification

This commit is contained in:
2022-01-24 13:34:38 +01:00
parent 0247169ad0
commit b09d19a6ad
3 changed files with 383 additions and 0 deletions

160
js/api.js Normal file
View File

@@ -0,0 +1,160 @@
const prefix = "";
async function unknownResponse(resp) {
let json = await resp.json();
try {
return new Error(`${json["message"]} (${resp.status})`);
}
catch (error) {
return new Error(`Server sent unknown error: ${await resp.text()} (${resp.status})`);
}
}
async function login(username, password) {
let result = await fetch(`${prefix}/api/login`, {
method: "POST",
body: JSON.stringify({ "username": username, "password": password })
});
switch (result.status) {
case 200:
return;
case 401:
throw new Error("Wrong username and/or password");
default:
throw await unknownResponse(result);
}
}
async function authors(name, limit) {
let result = await fetch(`${prefix}/api/authors?name=${name}&limit=${limit}`);
if (result.status == 200) {
return await result.json();
}
else {
throw await unknownResponse(result);
}
}
async function tags(name, limit) {
let result = await fetch(`${prefix}/api/tags?name=${name}&limit=${limit}`);
if (result.status == 200) {
return await result.json();
}
else {
throw await unknownResponse(result);
}
}
async function recent(limit = 20) {
let result = await fetch(`${prefix}/api/recent?limit=${limit}`);
if (result.status == 200) {
return await result.json();
}
else {
throw await unknownResponse(result);
}
}
async function search(query) {
let result = await fetch(`${prefix}/api/search?q=${query.query}&from=${query.from.getSeconds()}&to=${query.to.getSeconds()}&authors=${JSON.stringify(query.authors)}&tags=${JSON.stringify(query.tags)}&limit=${query.limit}`);
if (result.status == 200) {
return await result.json();
}
else {
throw unknownResponse(result);
}
}
async function uploadArticle(payload) {
let result = await fetch(`${prefix}/api/article`, {
method: "POST",
body: JSON.stringify(payload)
});
switch (result.status) {
case 201:
return await result.json();
case 401:
throw new Error("Not authorized");
case 409:
throw new Error("An article with the same title already exists");
default:
throw await unknownResponse(result);
}
}
async function editArticle(payload) {
let result = await fetch(`${prefix}/api/article`, {
method: "PATCH",
body: JSON.stringify(payload)
});
let json = await result.json();
switch (result.status) {
case 201:
return json;
case 401:
throw new Error("Not authorized");
case 404:
throw new Error("Could not find article");
case 409:
throw new Error("An article with the same title already exists");
default:
throw await unknownResponse(result);
}
}
async function deleteArticle(id) {
let result = await fetch(`${prefix}/api/article`, {
method: "DELETE",
body: JSON.stringify({ "id": id })
});
switch (result.status) {
case 200:
return;
case 401:
throw new Error("Not authorized");
case 404:
throw new Error("Could not find article");
default:
throw await unknownResponse(result);
}
}
async function getAssets(name, limit = 20) {
let result = await fetch(`${prefix}/api/assets?q=${name}&limit=${limit}`, {
method: "GET",
});
switch (result.status) {
case 200:
return await result.json();
case 401:
throw new Error("Not authorized");
default:
throw await unknownResponse(result);
}
}
async function addAsset(name, content) {
let result = await fetch(`${prefix}/api/assets`, {
method: "POST",
body: JSON.stringify({
"name": name,
"content": content,
})
});
switch (result.status) {
case 201:
return await result.json();
case 401:
throw new Error("Not authorized");
case 409:
throw new Error("An asset with the same name already exists");
default:
throw await unknownResponse(result);
}
}
async function deleteAsset(id) {
let result = await fetch(`${prefix}/api/assets`, {
method: "DELETE",
body: JSON.stringify({ "id": id })
});
switch (result.status) {
case 200:
return;
case 401:
throw new Error("Not authorized");
case 404:
throw new Error("An asset with this id does not exist");
default:
throw await unknownResponse(result);
}
}
//# sourceMappingURL=api.js.map