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(type: "public" | "internal", path: string, startPage: number, endingPage?: number): Promise<any> {
20 let res: Array<any> = [];
21 const firstPage: {
22 meta: Record<string, any>,
23 data: Array<any>
24 } = await (await fetch(config.api.urls[type] + path + 1)).json();
25 res.push(...firstPage.data);
26
27 if (!endingPage) endingPage = firstPage.meta.lastPage;
28 for (let index = startPage + 1; index < endingPage!; index++) {
29 const page = await (await fetch(config.api.urls[type] + path + index)).json();
30 res.push(...page.data);
31 };
32 return res;
33};
34
35export async function batchAction(path: string, headers: Array<Record<string, any>>) {
36 for (const request of headers) {
37 fetch(config.api.urls.internal + path, request);
38 };
39};