cache-api-local
v1.1.1
Published
A lib to use for caching the api res so your load to serve minimize. Plus it has revalidating feature π₯π₯
Maintainers
Readme
π¦ cache-api-local
A simple file-based caching wrapper for HTTP APIs using Node.js β ideal for prototyping, testing, and reducing redundant API calls.
π Features
- β‘ Fast Local Caching: Save API responses to the filesystem as
.jsonfiles for quick retrieval. - β± Cache Expiration: Automatically refreshes data after a configurable
maxAge(in seconds). - π Safe File Naming: Sanitizes URL paths and query strings into valid, unique filenames.
- π Auto Fallback: If a live fetch fails, falls back to the most recent cached copy.
- π§ͺ Test-Friendly: Easy to plug into testing pipelines or local dev environments.
π₯ Installation
npm install cache-api-localπ§ͺ Example Usage
import CacheApi from "cache-api-local";
const api = new CacheApi("https://jsonplaceholder.typicode.com", "data", 300);
// Fetches from API (and caches)
const data1 = await api.getData("/photos/1?t=12", "photo");
// Within 300 seconds, this fetches from cache
const data2 = await api.getData("/photos/1?t=12", "photo");
console.log(data2);π§° Constructor
new CacheApi(baseUrl: string, cacheFolderName: string, maxAgeInSeconds?: number)Parameters:
| Name | Type | Description |
| ----------------- | -------- | ---------------------------------------------------- |
| baseUrl | string | The base API URL (e.g. https://api.example.com) |
| cacheFolderName | string | Folder (relative to caller) where cache is stored |
| maxAgeInSeconds | number | Optional. Time after which cache is considered stale |
π How It Works
- Responses are saved as JSON files under the specified folder.
- URLs are converted to safe filenames (e.g.
/posts/1?v=alphaβposts_1_v=alpha.json). - Metadata is stored in
.meta.jsonfiles to track freshness. - If data is older than
maxAge, it fetches fresh data and updates the cache. - If fetching fails, it tries using the cached version.
β Good Use Cases
- Avoid rate-limiting or repeated API calls in development.
- Improve speed of integration tests.
- Work offline with previously fetched data.
π§Ό Cache Cleanup
All cached data is stored under the directory you specify (e.g. data/, project_cache/), and can be safely deleted if needed.
π Notes
- Works in Node.js environments (not for browser).
- You can set
maxAgeto a large value (or leave it out) to avoid expiry.
π§± Example Project Structure
my-project/
βββ src/
β βββ index.ts
βββ data/
β βββ photo/
β βββ photos_1_t=12.json
βββ ...π License
MIT Β© You
