Skip to content

Commit 1fe44f1

Browse files
authored
net - feat: renaming useHttpCache to httpCachePolicy (#1371)
* net - feat: renaming useHttpCache to httpCachePolicy * Update README.md
1 parent 9a46b16 commit 1fe44f1

5 files changed

Lines changed: 102 additions & 67 deletions

File tree

packages/net/README.md

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[<img align="center" src="https://cacheable.org/logo.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable)
22

3-
> High Performance Network Caching for Node.js with fetch, request, http 1.1, and http 2 support
3+
> High Performance Network Caching for Node.js with fetch support and HTTP cache semantics
44
55
[![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable)
66
[![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
@@ -10,18 +10,16 @@
1010

1111

1212
Features:
13-
* `fetch` from [undici](https://github.com/nodejs/undici) cache enabled via `cacheable`
14-
* `fetch` quick helpers such as `get`, `post`, `put`, and `delete` for easier development
15-
* `request` from [undici](https://github.com/nodejs/undici) cache enabled via `cacheable`
16-
* HTTP/1.1 and HTTP/2 caching support via Node.js `http` and `https` modules
17-
* [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching for native Node.js HTTP/HTTPS requests
18-
* Drop in replacement for `http` `https`, `fetch` modules with caching enabled
19-
* DNS caching for `dns.lookup` and `dns.resolve` methods via `cacheable`
20-
* WHOIS caching for `whois.lookup` method via `cacheable`
21-
* Advanced key generation via built in hashing and custom key generation functions
22-
* Benchmarks for performance comparison
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
2523

2624
# Table of Contents
2725
* [Getting Started](#getting-started)
@@ -93,6 +91,34 @@ const result = await net.get('https://api.example.com/data', {
9391
});
9492
```
9593

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+
96122
## API Reference
97123

98124
### CacheableNet Class
@@ -103,10 +129,11 @@ The main class that provides cached network operations.
103129

104130
```typescript
105131
interface CacheableNetOptions {
106-
cache?: Cacheable | CacheableOptions; // Cacheable instance or options
107-
useHttpCache?: boolean; // Enable HTTP cache semantics (default: true)
132+
cache?: Cacheable | CacheableOptions; // Cacheable instance or options
133+
httpCachePolicy?: boolean; // Enable HTTP cache semantics (default: true)
108134
stringify?: (value: unknown) => string; // Custom JSON stringifier (default: JSON.stringify)
109135
parse?: (value: string) => unknown; // Custom JSON parser (default: JSON.parse)
136+
}
110137
```
111138

112139
#### Methods
@@ -126,11 +153,11 @@ The `FetchOptions` type extends the standard fetch `RequestInit` options with ad
126153
```typescript
127154
type FetchOptions = Omit<RequestInit, 'cache'> & {
128155
cache?: Cacheable; // Optional cache instance (if not provided, no caching)
129-
useHttpCache?: boolean; // Override instance-level HTTP cache setting
156+
httpCachePolicy?: boolean; // Override instance-level HTTP cache setting
130157
};
131158
```
132159

133-
The `NetFetchOptions` type (used by `get()` and `head()` methods) provides additional control:
160+
The `NetFetchOptions` type (used by all HTTP method helpers) provides additional control:
134161

135162
```typescript
136163
type NetFetchOptions = {
@@ -142,6 +169,14 @@ type NetFetchOptions = {
142169

143170
**Note**: When using the CacheableNet methods, you don't need to provide the `cache` property as it's automatically injected from the instance.
144171

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+
145180

146181
# How to Contribute
147182

packages/net/src/fetch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export type FetchOptions = Omit<RequestInit, "cache"> & {
3030
*
3131
* @default true
3232
*/
33-
useHttpCache?: boolean;
33+
httpCachePolicy?: boolean;
3434
};
3535

3636
/**
3737
* Fetch data from a URL with optional request options.
3838
*
39-
* When `useHttpCache` is enabled (default), cache entries will have their TTL
39+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
4040
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
4141
* the default TTL from the Cacheable instance is used.
4242
*
@@ -80,13 +80,13 @@ export async function fetch(
8080
return response;
8181
}
8282

83-
const useHttpCache = options.useHttpCache !== false; // Default to true
83+
const httpCachePolicy = options.httpCachePolicy !== false; // Default to true
8484
const method = options.method || "GET";
8585

8686
// Create a cache key that includes the method
8787
const cacheKey = `${method}:${url}`;
8888

89-
if (!useHttpCache) {
89+
if (!httpCachePolicy) {
9090
// Simple caching without HTTP cache semantics
9191
const cachedData = await options.cache.getOrSet(cacheKey, async () => {
9292
// Perform the fetch operation

packages/net/src/index.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export type CacheableNetOptions = {
6464
*
6565
* @default true
6666
*/
67-
useHttpCache?: boolean;
67+
httpCachePolicy?: boolean;
6868
/**
6969
* Custom function for converting JavaScript values to strings.
7070
* This is used when serializing request bodies for POST, PUT, PATCH, and DELETE methods.
@@ -105,7 +105,7 @@ export type ParseType = (value: string) => unknown;
105105

106106
export class CacheableNet extends Hookified {
107107
private _cache: Cacheable = new Cacheable();
108-
private _useHttpCache = true;
108+
private _httpCachePolicy = true;
109109
private _stringify: StringifyType = JSON.stringify;
110110
private _parse: ParseType = JSON.parse;
111111

@@ -119,8 +119,8 @@ export class CacheableNet extends Hookified {
119119
: new Cacheable(options.cache);
120120
}
121121

122-
if (options?.useHttpCache !== undefined) {
123-
this._useHttpCache = options.useHttpCache;
122+
if (options?.httpCachePolicy !== undefined) {
123+
this._httpCachePolicy = options.httpCachePolicy;
124124
}
125125

126126
if (options?.stringify) {
@@ -181,25 +181,25 @@ export class CacheableNet extends Hookified {
181181
}
182182

183183
/**
184-
* Get the current HTTP cache setting.
184+
* Get the current HTTP cache policy setting.
185185
* @returns {boolean} Whether HTTP cache semantics are enabled
186186
*/
187-
public get useHttpCache(): boolean {
188-
return this._useHttpCache;
187+
public get httpCachePolicy(): boolean {
188+
return this._httpCachePolicy;
189189
}
190190

191191
/**
192192
* Set whether to use HTTP cache semantics.
193193
* @param {boolean} value - Enable or disable HTTP cache semantics
194194
*/
195-
public set useHttpCache(value: boolean) {
196-
this._useHttpCache = value;
195+
public set httpCachePolicy(value: boolean) {
196+
this._httpCachePolicy = value;
197197
}
198198

199199
/**
200200
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
201201
*
202-
* When `useHttpCache` is enabled (default), cache entries will have their TTL
202+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
203203
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
204204
* the default TTL from the Cacheable instance is used.
205205
*
@@ -214,7 +214,7 @@ export class CacheableNet extends Hookified {
214214
const fetchOptions: FetchOptions = {
215215
...options,
216216
cache: this._cache,
217-
useHttpCache: this._useHttpCache,
217+
httpCachePolicy: this._httpCachePolicy,
218218
};
219219

220220
return fetch(url, fetchOptions);
@@ -234,7 +234,7 @@ export class CacheableNet extends Hookified {
234234
const fetchOptions: FetchOptions = {
235235
...options,
236236
cache: this._cache,
237-
useHttpCache: this._useHttpCache,
237+
httpCachePolicy: this._httpCachePolicy,
238238
method: "GET",
239239
};
240240

@@ -311,7 +311,7 @@ export class CacheableNet extends Hookified {
311311
...options,
312312
headers,
313313
body: body as FetchOptions["body"],
314-
useHttpCache: this._useHttpCache,
314+
httpCachePolicy: this._httpCachePolicy,
315315
method: "POST",
316316
};
317317

@@ -361,7 +361,7 @@ export class CacheableNet extends Hookified {
361361
const fetchOptions: FetchOptions = {
362362
...options,
363363
cache: this._cache,
364-
useHttpCache: this._useHttpCache,
364+
httpCachePolicy: this._httpCachePolicy,
365365
method: "HEAD",
366366
};
367367

@@ -414,7 +414,7 @@ export class CacheableNet extends Hookified {
414414
...options,
415415
headers,
416416
body: body as FetchOptions["body"],
417-
useHttpCache: this._useHttpCache,
417+
httpCachePolicy: this._httpCachePolicy,
418418
method: "PUT",
419419
};
420420

@@ -490,7 +490,7 @@ export class CacheableNet extends Hookified {
490490
...options,
491491
headers,
492492
body: body as FetchOptions["body"],
493-
useHttpCache: this._useHttpCache,
493+
httpCachePolicy: this._httpCachePolicy,
494494
method: "PATCH",
495495
};
496496

@@ -568,7 +568,7 @@ export class CacheableNet extends Hookified {
568568
...options,
569569
headers,
570570
body: body as FetchOptions["body"],
571-
useHttpCache: this._useHttpCache,
571+
httpCachePolicy: this._httpCachePolicy,
572572
method: "DELETE",
573573
};
574574

0 commit comments

Comments
 (0)