cache-api-local
Version:
A lib to use for caching the api res so your load to serve minimize. Plus it has revalidating feature ๐ฅ๐ฅ
107 lines (69 loc) โข 2.89 kB
Markdown
# ๐ฆ 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 `.json` files 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
```bash
npm install cache-api-local
```
## ๐งช Example Usage
```ts
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
```ts
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.json` files 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 `maxAge` to 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