Implemented api specification
This commit is contained in:
160
js/api.js
Normal file
160
js/api.js
Normal 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
|
||||||
1
js/api.js.map
Normal file
1
js/api.js.map
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"api.js","sourceRoot":"","sources":["api.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,EAAE,CAAA;AAGjB,KAAK,UAAU,eAAe,CAAC,IAAc;IACzC,IAAI,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAA;IAC5B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;QAC7C,MAAM,EAAE,QAAQ;QAChB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAC,IAAI,EAAE,EAAE,EAAC,CAAC;KACnC,CAAC,CAAA;IACF,QAAQ,MAAM,CAAC,MAAM,EAAE;QACnB,KAAK,GAAG;YACJ,OAAM;QACV,KAAK,GAAG;YACJ,MAAM,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAA;QACrC,KAAK,GAAG;YACJ,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;QAC3D;YACI,MAAM,MAAM,eAAe,CAAC,MAAM,CAAC,CAAA;KAC1C;AACL,CAAC"}
|
||||||
222
js/api.ts
Normal file
222
js/api.ts
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
const prefix = ""
|
||||||
|
|
||||||
|
|
||||||
|
async function unknownResponse(resp: Response): Promise<Error> {
|
||||||
|
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})`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Author {
|
||||||
|
id: number,
|
||||||
|
name: string,
|
||||||
|
information: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ArticleSummary {
|
||||||
|
id: number,
|
||||||
|
title: string,
|
||||||
|
summary: string
|
||||||
|
authors: Author[],
|
||||||
|
image?: string,
|
||||||
|
tags: string[],
|
||||||
|
created: Date,
|
||||||
|
modified?: Date,
|
||||||
|
link: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Asset {
|
||||||
|
id: number,
|
||||||
|
name: string,
|
||||||
|
link: string
|
||||||
|
}
|
||||||
|
|
||||||
|
async function login(username: string, password: string): Promise<void> {
|
||||||
|
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?: string, limit?: number): Promise<Author[]> {
|
||||||
|
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?: string, limit?: number): Promise<string[]> {
|
||||||
|
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: number = 20): Promise<ArticleSummary[]> {
|
||||||
|
let result = await fetch(`${prefix}/api/recent?limit=${limit}`)
|
||||||
|
if (result.status == 200) {
|
||||||
|
return await result.json()
|
||||||
|
} else {
|
||||||
|
throw await unknownResponse(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SearchQuery {
|
||||||
|
query?: string,
|
||||||
|
from?: Date,
|
||||||
|
to?: Date,
|
||||||
|
authors?: number[],
|
||||||
|
tags?: string[],
|
||||||
|
limit?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
async function search(query: SearchQuery): Promise<ArticleSummary[]> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ArticleUploadPayload {
|
||||||
|
title: string,
|
||||||
|
summary: string,
|
||||||
|
authors: number[],
|
||||||
|
image?: string,
|
||||||
|
tags: string[],
|
||||||
|
link?: string
|
||||||
|
content: string
|
||||||
|
}
|
||||||
|
|
||||||
|
async function uploadArticle(payload: ArticleUploadPayload): Promise<ArticleSummary> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ArticleEditPayload {
|
||||||
|
id: number,
|
||||||
|
title?: string,
|
||||||
|
summary?: string,
|
||||||
|
authors?: number[],
|
||||||
|
image?: string,
|
||||||
|
tags?: string[],
|
||||||
|
link?: string
|
||||||
|
content?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
async function editArticle(payload: ArticleEditPayload): Promise<ArticleSummary> {
|
||||||
|
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: number): Promise<void> {
|
||||||
|
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?: string, limit: number = 20): Promise<Asset[]> {
|
||||||
|
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: string, content: string): Promise<Asset> {
|
||||||
|
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: number): Promise<void> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user