UNPKG

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
# ๐Ÿ“ฆ 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