232 |
GhostCache Example in React
233 | {pokemon ? (
234 |
{JSON.stringify(pokemon, null, 2)}
235 | ) : (
236 |
Loading...
237 | )}
238 |
239 | );
240 | };
241 |
242 | export default App;
243 | ```
244 |
245 | ### Node.js Example
246 |
247 | If you use GhostCache in a Node.js environment (using Axios), you can enable caching similarly. Note that persistent caching may require a server-side storage adapter like Redis.
248 |
249 | ```ts
250 | // node-example.ts
251 | import axios from "axios";
252 | import { enableGhostCache, registerAxios } from "ghost-cache";
253 |
254 | // Enable caching (TTL: 60 sec, in-memory caching)
255 | enableGhostCache({ ttl: 60000 });
256 |
257 | // Create and register an Axios instance
258 | const api = axios.create({ baseURL: "https://pokeapi.co/api/v2" });
259 | registerAxios(api);
260 |
261 | // Use Axios to make requests
262 | api
263 | .get("/pokemon/ditto")
264 | .then((response) => {
265 | console.log("Node.js Axios fetched:", response.data);
266 | })
267 | .catch((error) => {
268 | console.error("Error:", error);
269 | });
270 | ```
271 |
272 | ## API Reference
273 |
274 | - **enableGhostCache(options?: GhostCacheOptions): void**
275 | Enables GhostCache. Automatically intercepts HTTP requests made with `fetch()` and Axios.
276 |
277 | - **clearGhostCache(): void**
278 | Clears all cache entries from memory (and persistent storage if enabled).
279 |
280 | - **disableGhostCache(): void**
281 | Disables GhostCache and restores the original HTTP request methods.
282 |
283 | - **setCache(key: string, value: any): Promise