@cacheable/net 1.0.2 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -13
- package/dist/index.cjs +1 -570
- package/dist/index.d.cts +141 -29
- package/dist/index.d.ts +141 -29
- package/dist/index.js +1 -530
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[<img align="center" src="https://cacheable.org/logo.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable)
|
|
2
2
|
|
|
3
|
-
> High Performance Network Caching for Node.js with fetch
|
|
3
|
+
> High Performance Network Caching for Node.js with fetch support and HTTP cache semantics
|
|
4
4
|
|
|
5
5
|
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
6
6
|
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
@@ -10,18 +10,16 @@
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
Features:
|
|
13
|
-
* `fetch` from [undici](https://github.com/nodejs/undici)
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* All the features of [cacheable](https://npmjs.com/package/cacheable) - layered caching, LRU, expiration, hooks, backed by Keyv, and more!
|
|
24
|
-
* Highly Tested and Maintained on a regular basis with a focus on performance and reliability
|
|
13
|
+
* `fetch` from [undici](https://github.com/nodejs/undici) with caching enabled via `cacheable`
|
|
14
|
+
* HTTP method helpers: `get`, `post`, `put`, `patch`, `delete`, and `head` for easier development
|
|
15
|
+
* [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching with `http-cache-semantics`
|
|
16
|
+
* Smart caching with automatic cache key generation
|
|
17
|
+
* Support for custom serialization/deserialization with `stringify` and `parse` functions
|
|
18
|
+
* Configurable cache policies - use HTTP cache semantics or simple TTL-based caching
|
|
19
|
+
* Full TypeScript support with comprehensive type definitions
|
|
20
|
+
* Request-level cache control with the `caching` option
|
|
21
|
+
* All the features of [cacheable](https://npmjs.com/package/cacheable) - layered caching, LRU, TTL expiration, and more!
|
|
22
|
+
* Extensively tested with 100% code coverage
|
|
25
23
|
|
|
26
24
|
# Table of Contents
|
|
27
25
|
* [Getting Started](#getting-started)
|
|
@@ -34,6 +32,151 @@ Features:
|
|
|
34
32
|
npm install @cacheable/net
|
|
35
33
|
```
|
|
36
34
|
|
|
35
|
+
## Basic Usage
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import { CacheableNet } from '@cacheable/net';
|
|
39
|
+
|
|
40
|
+
const net = new CacheableNet();
|
|
41
|
+
|
|
42
|
+
// Simple GET request with caching
|
|
43
|
+
const response = await net.get('https://api.example.com/data');
|
|
44
|
+
console.log(response.data);
|
|
45
|
+
|
|
46
|
+
// POST request with data
|
|
47
|
+
const result = await net.post('https://api.example.com/users', {
|
|
48
|
+
name: 'John Doe',
|
|
49
|
+
email: 'john@example.com'
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Using fetch directly with caching
|
|
53
|
+
const fetchResponse = await net.fetch('https://api.example.com/data', {
|
|
54
|
+
method: 'GET',
|
|
55
|
+
headers: {
|
|
56
|
+
'Authorization': 'Bearer token'
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Custom Serialization
|
|
62
|
+
|
|
63
|
+
You can provide custom `stringify` and `parse` functions for handling data serialization. This is particularly useful when working with complex data types that JSON doesn't natively support:
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
import { CacheableNet } from '@cacheable/net';
|
|
67
|
+
import superjson from 'superjson';
|
|
68
|
+
|
|
69
|
+
// Using superjson for enhanced serialization
|
|
70
|
+
// Supports Dates, BigInt, RegExp, Set, Map, Error and more
|
|
71
|
+
const net = new CacheableNet({
|
|
72
|
+
stringify: (value) => superjson.stringify(value),
|
|
73
|
+
parse: (text) => superjson.parse(text)
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Now you can work with complex data types
|
|
77
|
+
const response = await net.post('https://api.example.com/data', {
|
|
78
|
+
timestamp: new Date(),
|
|
79
|
+
userId: BigInt(12345),
|
|
80
|
+
pattern: /[a-z]+/gi,
|
|
81
|
+
metadata: new Map([['key', 'value']]),
|
|
82
|
+
tags: new Set(['important', 'urgent'])
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Or provide per-request custom serialization
|
|
86
|
+
const result = await net.get('https://api.example.com/data', {
|
|
87
|
+
parse: (text) => {
|
|
88
|
+
// Custom parsing with superjson for this request only
|
|
89
|
+
return superjson.parse(text);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Caching Control
|
|
95
|
+
|
|
96
|
+
You can control caching behavior at multiple levels:
|
|
97
|
+
|
|
98
|
+
```javascript
|
|
99
|
+
import { CacheableNet } from '@cacheable/net';
|
|
100
|
+
|
|
101
|
+
const net = new CacheableNet({
|
|
102
|
+
httpCachePolicy: true // Enable HTTP cache semantics globally (default)
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// GET requests are cached by default
|
|
106
|
+
const data1 = await net.get('https://api.example.com/data');
|
|
107
|
+
|
|
108
|
+
// Disable caching for a specific GET request
|
|
109
|
+
const data2 = await net.get('https://api.example.com/data', {
|
|
110
|
+
caching: false
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// POST requests are NOT cached by default
|
|
114
|
+
const result1 = await net.post('https://api.example.com/data', { value: 1 });
|
|
115
|
+
|
|
116
|
+
// Enable caching for a specific POST request
|
|
117
|
+
const result2 = await net.post('https://api.example.com/data', { value: 1 }, {
|
|
118
|
+
caching: true
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## API Reference
|
|
123
|
+
|
|
124
|
+
### CacheableNet Class
|
|
125
|
+
|
|
126
|
+
The main class that provides cached network operations.
|
|
127
|
+
|
|
128
|
+
#### Constructor Options
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
interface CacheableNetOptions {
|
|
132
|
+
cache?: Cacheable | CacheableOptions; // Cacheable instance or options
|
|
133
|
+
httpCachePolicy?: boolean; // Enable HTTP cache semantics (default: true)
|
|
134
|
+
stringify?: (value: unknown) => string; // Custom JSON stringifier (default: JSON.stringify)
|
|
135
|
+
parse?: (value: string) => unknown; // Custom JSON parser (default: JSON.parse)
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### Methods
|
|
140
|
+
|
|
141
|
+
All methods accept request options of type `FetchOptions` (excluding the `cache` property which is managed internally):
|
|
142
|
+
|
|
143
|
+
- **fetch(url: string, options?: FetchOptions)**: Fetch with caching support
|
|
144
|
+
- **get(url: string, options?: NetFetchOptions)**: GET request helper with caching control
|
|
145
|
+
- **post(url: string, data?: unknown, options?: NetFetchOptions)**: POST request helper with caching control
|
|
146
|
+
- **put(url: string, data?: unknown, options?: NetFetchOptions)**: PUT request helper with caching control
|
|
147
|
+
- **patch(url: string, data?: unknown, options?: NetFetchOptions)**: PATCH request helper with caching control
|
|
148
|
+
- **delete(url: string, data?: unknown, options?: NetFetchOptions)**: DELETE request helper with caching control
|
|
149
|
+
- **head(url: string, options?: NetFetchOptions)**: HEAD request helper with caching control
|
|
150
|
+
|
|
151
|
+
The `FetchOptions` type extends the standard fetch `RequestInit` options with additional caching controls:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
type FetchOptions = Omit<RequestInit, 'cache'> & {
|
|
155
|
+
cache?: Cacheable; // Optional cache instance (if not provided, no caching)
|
|
156
|
+
httpCachePolicy?: boolean; // Override instance-level HTTP cache setting
|
|
157
|
+
};
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The `NetFetchOptions` type (used by all HTTP method helpers) provides additional control:
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
type NetFetchOptions = {
|
|
164
|
+
caching?: boolean; // Enable/disable caching for this request
|
|
165
|
+
stringify?: (value: unknown) => string; // Custom JSON stringifier
|
|
166
|
+
parse?: (value: string) => unknown; // Custom JSON parser
|
|
167
|
+
} & Omit<FetchOptions, 'method' | 'cache'>;
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Note**: When using the CacheableNet methods, you don't need to provide the `cache` property as it's automatically injected from the instance.
|
|
171
|
+
|
|
172
|
+
#### Caching Behavior
|
|
173
|
+
|
|
174
|
+
By default:
|
|
175
|
+
- **GET** and **HEAD** requests are cached automatically
|
|
176
|
+
- **POST**, **PUT**, **PATCH**, and **DELETE** requests are NOT cached by default
|
|
177
|
+
- To enable caching for POST/PUT/PATCH/DELETE, set `caching: true` in the options
|
|
178
|
+
- To disable caching for GET/HEAD, set `caching: false` in the options
|
|
179
|
+
|
|
37
180
|
|
|
38
181
|
# How to Contribute
|
|
39
182
|
|