A rewrite of Poly+, my quality-of-life browser extension for Polytoria. Built entirely fresh using the WXT extension framework, Typescript, and with added better overall code quality.
extension
1import config from "./config.json";
2
3export async function batch(
4 type: "public" | "internal",
5 path: string,
6 ids: string[],
7 headers?: Record<string, any>
8): Promise<any> {
9 if (!headers) headers = {};
10 const res: Record<string, any> = {};
11
12 for (const id of ids) {
13 const info = await (await fetch(config.api.urls[type] + path + id, headers)).json();
14 res[id] = info;
15 };
16 return res;
17};
18
19export async function iterate(
20 type: "public" | "internal",
21 path: string,
22 keys: {
23 data: string,
24 metadata: string
25 } | null,
26 startPage: number,
27 endingPage?: number
28): Promise<any> {
29 let res: Array<any> = [];
30 const firstPage: { [key: string]: any } = await (await fetch(config.api.urls[type] + path + 1)).json();
31
32 if (!keys) {
33 keys = {
34 data: "data",
35 metadata: "meta"
36 };
37 };
38 res.push(...firstPage[keys.data]);
39
40 if (!endingPage) endingPage = firstPage[keys.metadata].lastPage;
41 for (let index = startPage + 1; index < endingPage!; index++) {
42 const page = await (await fetch(config.api.urls[type] + path + index)).json();
43 res.push(...page[keys.data]);
44 };
45 return res;
46};
47
48export async function batchAction(path: string, headers: Array<Record<string, any>>) {
49 for (const request of headers) {
50 fetch(config.api.urls.internal + path, request);
51 };
52};