Skip to main content

Practice ยท 2 of 2

Caching API Client

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)

Difficulty: intermediate

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