UNPKG

ntms

Version:

A dead simple way to add i18n to your Next.js app using the Notion API and Deepl

51 lines (39 loc) 944 B
class KeyTimeout { timeoutHandleForKey: any; constructor() { this.timeoutHandleForKey = {}; } clearTimeout(key) { clearTimeout(this.timeoutHandleForKey[key]); } updateTimeout(key, durationMs, callback) { this.clearTimeout(key); this.timeoutHandleForKey[key] = setTimeout(() => { callback(); }, durationMs); } } let cache = {}; let ttl = 1000 * 60; class MemoryCache { keyTimeout: KeyTimeout; constructor() { this.keyTimeout = new KeyTimeout(); } get(key) { return cache[key]; } remove(key) { this.keyTimeout.clearTimeout(key); delete cache[key]; } set(key, value) { Object.assign(cache, { [key]: value }); if (typeof ttl === "number") { this.keyTimeout.updateTimeout(key, ttl, () => this.remove(key)); } } } const { fetchBuilder } = require("node-fetch-cache"); const fetch = fetchBuilder.withCache(new MemoryCache()); export default fetch;