Skip to main content

Practice · 2 of 2

Caching API Client

CHALLENGE
Difficulty: intermediate+25 XP

Write createClient(fetchImpl) — an API client with: - get(url) — uses a per-client cache: the first call fetches and caches the PROMISE; repeat calls return the same promise (one flight per URL until invalidated) - invalidate(url) — drops the cache entry for that URL (next get re-fetches) - invalidateAll() — clears everything - post(url, body) — never cached; sends { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' } } and resolves with the parsed response - all fetches go through the injected fetchImpl(url, opts)

Back to lesson: Practice: Mini Build: API-Powered Widget