/
requests.ts
75 lines (65 loc) · 1.68 KB
/
requests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* eslint no-unused-vars: ["warn", {"args":"none"}] */
import $ from "jquery";
import { currentBoard, currentThread } from "../postinfo";
interface BoardsList {
boards: any[];
currentBoard: string;
}
const nullBoardsList: BoardsList = {
boards: [],
currentBoard: ""
};
export async function getBoardList() {
try {
const data = await $.ajax({
url: webroot + "boards.json",
cache: false,
dataType: "json",
error: function(_err, _status, statusText) {
console.error("Error getting board list: " + statusText);
return nullBoardsList;
},
});
return { boards: data.boards, currentBoard: currentBoard() };
} catch(e) {
return nullBoardsList;
}
}
export async function getCatalog(board = "") {
const useBoard = (board !== "")?board:currentBoard();
const data = await $.ajax({
url: webroot + useBoard + "/catalog.json",
cache: false,
dataType: "json",
error: function (err, status, statusText) {
console.error(`Error getting catalog for /${board}/: ${statusText}`);
}
});
if(data.length === 0)
return [];
if(data[0] === null)
data.shift();
return data;
}
export async function getThread(board = "", thread = 0) {
const threadInfo = currentThread();
if(board !== "")
threadInfo.board = board;
if(thread > 0)
threadInfo.id = thread;
if(threadInfo.board === "") {
return Promise.reject("not in a board");
}
if(threadInfo.id < 1) {
return Promise.reject("not in a thread");
}
const data = await $.ajax({
url: `${webroot}${threadInfo.board}/res/${threadInfo.id}.json`,
cache: false,
dataType: "json",
error: function (err, status, statusText) {
console.error(`Error getting catalog for /${board}/: ${statusText}`);
}
});
return data;
}