Implemented article get endpoint

This commit is contained in:
2022-02-02 16:15:01 +01:00
parent a0a6a3b799
commit 026bf1c3bc
3 changed files with 47 additions and 3 deletions

View File

@@ -28,7 +28,7 @@ async function login(username, password) {
let result = await fetch(`${prefix}/api/login`, { let result = await fetch(`${prefix}/api/login`, {
method: "POST", method: "POST",
body: JSON.stringify({ "username": username, "password": password }), body: JSON.stringify({ "username": username, "password": password }),
credentials: "include" credentials: "same-origin"
}); });
switch (result.status) { switch (result.status) {
case 200: case 200:
@@ -79,6 +79,22 @@ async function search(q) {
throw unknownResponse(result); throw unknownResponse(result);
} }
} }
async function getArticle(id) {
let query = [["id", id]];
let result = await fetch(`${prefix}/api/article?${buildQuery(query)}`, {
method: "GET"
});
switch (result.status) {
case 200:
return await result.json();
case 401:
throw new Error("Not authorized");
case 404:
throw new Error("Article not found");
default:
throw await unknownResponse(result);
}
}
async function uploadArticle(payload) { async function uploadArticle(payload) {
let result = await fetch(`${prefix}/api/article`, { let result = await fetch(`${prefix}/api/article`, {
method: "POST", method: "POST",

File diff suppressed because one or more lines are too long

View File

@@ -54,7 +54,7 @@ async function login(username: string, password: string): Promise<void> {
let result = await fetch(`${prefix}/api/login`, { let result = await fetch(`${prefix}/api/login`, {
method: "POST", method: "POST",
body: JSON.stringify({"username": username, "password": password}), body: JSON.stringify({"username": username, "password": password}),
credentials: "include" credentials: "same-origin"
}) })
switch (result.status) { switch (result.status) {
case 200: case 200:
@@ -119,6 +119,34 @@ async function search(q: SearchQuery): Promise<ArticleSummary[]> {
} }
} }
interface ArticleGetPayload {
title: string,
summary: string,
authors: number[],
image: string,
tags: string[],
link: string
content: string
}
async function getArticle(id: number): Promise<ArticleGetPayload> {
let query = [["id", id]]
let result = await fetch(`${prefix}/api/article?${buildQuery(query)}`, {
method: "GET"
})
switch (result.status) {
case 200:
return await result.json()
case 401:
throw new Error("Not authorized")
case 404:
throw new Error("Article not found")
default:
throw await unknownResponse(result)
}
}
interface ArticleUploadPayload { interface ArticleUploadPayload {
title: string, title: string,
summary: string, summary: string,