import { asyncKV } from "@star-kitten/util"; export enum RefDataType { BLUEPRINT = 'blueprints', CATEGORY = 'cateogories', ATTRIBUTE = 'dogma_attributes', EFFECT = 'dogma_effects', GROUP = 'groups', ICON = 'icons', MARKET_GROUP = 'market_groups', META_GROUP = 'meta_groups', MUTAPLASMID = 'mutaplasmids', REGION = 'regions', SCHEMATIC = 'schematics', SKILL = 'skills', TYPE = 'types', UNIT = 'units', } const REF_DATA_BASE_URL = 'https://ref-data.everef.net'; export async function fetchReferenceData(id: number, type: RefDataType) { const key = `${type}/${id}`; if (await asyncKV.has(key)) { return JSON.parse(await asyncKV.get(key)) as T; } let url = new URL(REF_DATA_BASE_URL); url.pathname = `${type}/${id}`; const result = await fetch(url); if (result.ok) { const text = await result.text(); await asyncKV.set(key, text); return JSON.parse(text) as T; } throw new Error(`${type} id ${id} not found in reference data.`); } export async function fetchIdsOfType(type: RefDataType) { let url = new URL(REF_DATA_BASE_URL); url.pathname = type; const result = await fetch(url); if (result.ok) { return (await result.json()) as number[]; } } export async function loadAll(type: RefDataType) { const ids = await fetchIdsOfType(type); const promises = ids.map(id => fetchReferenceData(id, type)); await Promise.all(promises); }