import config from "../config.json"; type apiNames = "public" | "internal" | "extension"; export function getAPI(name: apiNames) { const api = config.apis.find((api) => api.name == name)!; return { enabled: api.enabled, url: api.url, }; } export async function batch( type: apiNames, path: string, ids: string[], headers?: Record, ): Promise { const api = getAPI(type); if (!api.enabled) { return "disabled"; } if (!headers) headers = {}; const res: Record = {}; for (const id of ids) { const info = await (await fetch(api.url + path + id, headers)) .json(); res[id] = info; } return res; } export async function iterate( type: apiNames, path: string, keys: { data: string; metadata: string; } | null, startPage: number, endingPage?: number, ): Promise { const api = getAPI(type); if (!api.enabled) { return "disabled"; } let res: Array = []; const firstPage: { [key: string]: any } = await (await fetch(api.url + path + 1)).json(); if (!keys) { keys = { data: "data", metadata: "meta", }; } res.push(...firstPage[keys.data]); if (!endingPage) endingPage = firstPage[keys.metadata].lastPage; for (let index = startPage + 1; index < endingPage!; index++) { const page = await (await fetch(api.url + path + index)) .json(); res.push(...page[keys.data]); } return res; } export async function batchAction( path: string, headers: Array>, ) { const api = getAPI("internal"); if (!api.enabled) { return "disabled"; } for (const request of headers) { fetch(api.url + path, request); } }