UNPKG

query-registry

Version:

Query the npm registry for packuments, manifests, packages and download counts

15 lines (14 loc) • 532 B
import { cache } from "./cache.js"; export async function fetchData(schema, url, headers) { const cacheKey = JSON.stringify({ url, headers }); const cachedJson = cache.get(cacheKey); if (cachedJson) return schema.parse(cachedJson); const response = await fetch(url, { headers }); if (!response.ok) { throw new Error(`fetchData: ${response.status} ${response.statusText} for ${url}`); } const json = (await response.json()); cache.set(cacheKey, json); return schema.parse(json); }