A NATS storage adapter for axios-cache-interceptor, enabling distributed caching using NATS JetStream Key-Value store.
- 🚀 NATS JetStream KV Store - Persistent, distributed caching
- 📦 Dual Module Support - Works with both ESM and CommonJS
- 🔧 Flexible Configuration - Customizable bucket, TTL, and serialization
- 🔑 Key Prefixing - Namespace support for multiple applications
- ⚡ TypeScript - Full type safety and IntelliSense support
- 🧪 Well Tested - Comprehensive test coverage
npm install axios-cache-nats-adapter axios-cache-interceptor natsor
yarn add axios-cache-nats-adapter axios-cache-interceptor natsor
pnpm add axios-cache-nats-adapter axios-cache-interceptor natsimport axios from 'axios';
import { setupCache } from 'axios-cache-interceptor';
import { NatsAdapter } from 'axios-cache-nats-adapter';
// Create the NATS adapter
const adapter = new NatsAdapter({
natsOptions: {
servers: 'nats://localhost:4222',
},
bucket: 'axios-cache',
ttl: 60000, // 1 minute default TTL
});
// Setup axios with cache
const cachedAxios = setupCache(axios, {
storage: adapter,
});
// Use it like normal axios
const response = await cachedAxios.get('https://api.example.com/data');
console.log(response.cached); // false on first request, true on subsequent
// Clean up when done
await adapter.close();const axios = require('axios');
const { setupCache } = require('axios-cache-interceptor');
const { NatsAdapter } = require('axios-cache-nats-adapter');
const adapter = new NatsAdapter({
natsOptions: {
servers: 'nats://localhost:4222',
},
bucket: 'axios-cache',
});
const cachedAxios = setupCache(axios, {
storage: adapter,
});
// Use it...interface NatsAdapterOptions {
/**
* NATS connection options
* @default { servers: 'nats://localhost:4222' }
*/
natsOptions?: ConnectionOptions;
/**
* Name of the JetStream KV bucket to use for caching
* @default 'axios-cache'
*/
bucket?: string;
/**
* Prefix to prepend to all cache keys for namespacing
* @default ''
*/
keyPrefix?: string;
/**
* Default TTL (time-to-live) in milliseconds for cached values
* Individual cache entries can override this
* @default 0 (no expiration)
*/
ttl?: number;
/**
* Custom serializer for encoding/decoding cached values
* @default JsonSerializer
*/
serializer?: Serializer;
/**
* Maximum number of history entries to keep in the KV bucket
* @default 1
*/
maxHistory?: number;
/**
* Whether to automatically create the KV bucket if it doesn't exist
* @default true
*/
autoCreateBucket?: boolean;
}You can provide a custom serializer for advanced use cases:
import { NatsAdapter, Serializer } from 'axios-cache-nats-adapter';
import type { StorageValue } from 'axios-cache-interceptor';
class CustomSerializer implements Serializer {
serialize(value: StorageValue): Uint8Array {
// Your custom serialization logic
return new TextEncoder().encode(JSON.stringify(value));
}
deserialize(data: Uint8Array): StorageValue {
// Your custom deserialization logic
return JSON.parse(new TextDecoder().decode(data));
}
}
const adapter = new NatsAdapter({
serializer: new CustomSerializer(),
});Use key prefixes to isolate caches for different applications:
// Application 1
const adapter1 = new NatsAdapter({
bucket: 'shared-cache',
keyPrefix: 'app1:',
});
// Application 2
const adapter2 = new NatsAdapter({
bucket: 'shared-cache',
keyPrefix: 'app2:',
});You can override the default TTL per request:
const cachedAxios = setupCache(axios, {
storage: adapter,
});
// Cache for 5 minutes
await cachedAxios.get('https://api.example.com/data', {
cache: {
ttl: 5 * 60 * 1000,
},
});Always clean up resources when done:
const adapter = new NatsAdapter({ /* options */ });
try {
// Use the adapter...
} finally {
await adapter.close();
}- Node.js >= 16.0.0
- A running NATS server with JetStream enabled
docker run -p 4222:4222 nats:latest -jsnats-server -jsget(key: string): Promise<StorageValue | undefined>- Get a cached valueset(key: string, value: StorageValue, requestConfig?: CacheProperties): Promise<void>- Set a cached valueremove(key: string): Promise<void>- Remove a cached valuefind(predicate: (key: string, value: StorageValue) => boolean): Promise<StorageValue[]>- Find cached values matching a predicateclose(): Promise<void>- Close the NATS connection
Contributions are welcome! Please feel free to submit a Pull Request.
MIT © Yasser Ouaftouh
- axios-cache-interceptor - The cache interceptor this adapter is built for
- nats.js - NATS client for Node.js
If you encounter any issues or have questions, please open an issue on GitHub.