import TCGdex from './tcgdex' import { version } from './version' export default class Request { // 1 hour of TTL by default public static ttl = 1000 * 60 * 60 private static cache: Record = {} public static async fetch(url: string): Promise { let request = this.cache[url] const now = new Date().getTime() if (!request || now - request.time > this.ttl) { const unfetch = TCGdex.fetch const resp = await unfetch(url, { headers: { 'user-agent': `@tcgdex/javascript-sdk/${version}` } }) if (resp.status !== 200) { return undefined } this.cache[url] = { response: await resp.json(), time: now } request = this.cache[url] } return request.response } }