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 3type apiNames = "public" | "internal" | "extension"; 4 5export function getAPI(name: apiNames) { 6 const api = config.apis.find((api) => api.name == name)!; 7 8 return { 9 enabled: api.enabled, 10 url: api.url, 11 }; 12} 13 14export async function batch( 15 type: apiNames, 16 path: string, 17 ids: string[], 18 headers?: Record<string, any>, 19): Promise<any> { 20 const api = getAPI(type); 21 if (!api.enabled) { 22 return "disabled"; 23 } 24 25 if (!headers) headers = {}; 26 const res: Record<string, any> = {}; 27 28 for (const id of ids) { 29 const info = await (await fetch(api.url + path + id, headers)) 30 .json(); 31 res[id] = info; 32 } 33 return res; 34} 35 36export async function iterate( 37 type: apiNames, 38 path: string, 39 keys: { 40 data: string; 41 metadata: string; 42 } | null, 43 startPage: number, 44 endingPage?: number, 45): Promise<any> { 46 const api = getAPI(type); 47 if (!api.enabled) { 48 return "disabled"; 49 } 50 51 let res: Array<any> = []; 52 const firstPage: { [key: string]: any } = 53 await (await fetch(api.url + path + 1)).json(); 54 55 if (!keys) { 56 keys = { 57 data: "data", 58 metadata: "meta", 59 }; 60 } 61 res.push(...firstPage[keys.data]); 62 63 if (!endingPage) endingPage = firstPage[keys.metadata].lastPage; 64 for (let index = startPage + 1; index < endingPage!; index++) { 65 const page = await (await fetch(api.url + path + index)) 66 .json(); 67 res.push(...page[keys.data]); 68 } 69 return res; 70} 71 72export async function batchAction( 73 path: string, 74 headers: Array<Record<string, any>>, 75) { 76 const api = getAPI("internal"); 77 if (!api.enabled) { 78 return "disabled"; 79 } 80 81 for (const request of headers) { 82 fetch(api.url + path, request); 83 } 84}