@cacheable/net 1.0.2 → 2.0.0
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 +185 -41
- package/dist/index.d.cts +141 -30
- package/dist/index.d.ts +141 -30
- package/dist/index.js +185 -41
- package/package.json +6 -6
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
|
|
package/dist/index.cjs
CHANGED
|
@@ -47,13 +47,17 @@ var import_hookified = require("hookified");
|
|
|
47
47
|
var import_http_cache_semantics = __toESM(require("http-cache-semantics"), 1);
|
|
48
48
|
var import_undici = require("undici");
|
|
49
49
|
async function fetch(url, options) {
|
|
50
|
-
if (!options.cache) {
|
|
51
|
-
throw new Error("Fetch options must include a cache instance or options.");
|
|
52
|
-
}
|
|
53
50
|
const fetchOptions = {
|
|
54
51
|
...options,
|
|
55
52
|
cache: "no-cache"
|
|
56
53
|
};
|
|
54
|
+
if (!options.cache) {
|
|
55
|
+
const response2 = await (0, import_undici.fetch)(url, fetchOptions);
|
|
56
|
+
if (!response2.ok) {
|
|
57
|
+
throw new Error(`Fetch failed with status ${response2.status}`);
|
|
58
|
+
}
|
|
59
|
+
return response2;
|
|
60
|
+
}
|
|
57
61
|
if (options.method === "POST" || options.method === "PATCH" || options.method === "DELETE" || options.method === "HEAD") {
|
|
58
62
|
const response2 = await (0, import_undici.fetch)(url, fetchOptions);
|
|
59
63
|
if (!response2.ok) {
|
|
@@ -61,10 +65,10 @@ async function fetch(url, options) {
|
|
|
61
65
|
}
|
|
62
66
|
return response2;
|
|
63
67
|
}
|
|
64
|
-
const
|
|
68
|
+
const httpCachePolicy = options.httpCachePolicy !== false;
|
|
65
69
|
const method = options.method || "GET";
|
|
66
70
|
const cacheKey = `${method}:${url}`;
|
|
67
|
-
if (!
|
|
71
|
+
if (!httpCachePolicy) {
|
|
68
72
|
const cachedData = await options.cache.getOrSet(cacheKey, async () => {
|
|
69
73
|
const response2 = await (0, import_undici.fetch)(url, fetchOptions);
|
|
70
74
|
if (!response2.ok) {
|
|
@@ -340,67 +344,122 @@ async function head(url, options) {
|
|
|
340
344
|
// src/index.ts
|
|
341
345
|
var CacheableNet = class extends import_hookified.Hookified {
|
|
342
346
|
_cache = new import_cacheable.Cacheable();
|
|
343
|
-
|
|
347
|
+
_httpCachePolicy = true;
|
|
348
|
+
_stringify = JSON.stringify;
|
|
349
|
+
_parse = JSON.parse;
|
|
344
350
|
constructor(options) {
|
|
345
351
|
super(options);
|
|
346
352
|
if (options?.cache) {
|
|
347
353
|
this._cache = options.cache instanceof import_cacheable.Cacheable ? options.cache : new import_cacheable.Cacheable(options.cache);
|
|
348
354
|
}
|
|
349
|
-
if (options?.
|
|
350
|
-
this.
|
|
355
|
+
if (options?.httpCachePolicy !== void 0) {
|
|
356
|
+
this._httpCachePolicy = options.httpCachePolicy;
|
|
357
|
+
}
|
|
358
|
+
if (options?.stringify) {
|
|
359
|
+
this._stringify = options?.stringify;
|
|
351
360
|
}
|
|
361
|
+
if (options?.parse) {
|
|
362
|
+
this._parse = options?.parse;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Get the stringify function used for converting objects to strings.
|
|
367
|
+
* @returns {StringifyType} The current stringify function
|
|
368
|
+
*/
|
|
369
|
+
get stringify() {
|
|
370
|
+
return this._stringify;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Set the stringify function for converting objects to strings.
|
|
374
|
+
* @param {StringifyType} value - The stringify function to use
|
|
375
|
+
*/
|
|
376
|
+
set stringify(value) {
|
|
377
|
+
this._stringify = value;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Get the parse function used for converting strings to objects.
|
|
381
|
+
* @returns {ParseType} The current parse function
|
|
382
|
+
*/
|
|
383
|
+
get parse() {
|
|
384
|
+
return this._parse;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Set the parse function for converting strings to objects.
|
|
388
|
+
* @param {ParseType} value - The parse function to use
|
|
389
|
+
*/
|
|
390
|
+
set parse(value) {
|
|
391
|
+
this._parse = value;
|
|
352
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* Get the Cacheable instance used for caching fetch operations.
|
|
395
|
+
* @returns {Cacheable} The current Cacheable instance
|
|
396
|
+
*/
|
|
353
397
|
get cache() {
|
|
354
398
|
return this._cache;
|
|
355
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Set the Cacheable instance for caching fetch operations.
|
|
402
|
+
* @param {Cacheable} value - The Cacheable instance to use for caching
|
|
403
|
+
*/
|
|
356
404
|
set cache(value) {
|
|
357
405
|
this._cache = value;
|
|
358
406
|
}
|
|
359
407
|
/**
|
|
360
|
-
* Get the current HTTP cache setting.
|
|
408
|
+
* Get the current HTTP cache policy setting.
|
|
361
409
|
* @returns {boolean} Whether HTTP cache semantics are enabled
|
|
362
410
|
*/
|
|
363
|
-
get
|
|
364
|
-
return this.
|
|
411
|
+
get httpCachePolicy() {
|
|
412
|
+
return this._httpCachePolicy;
|
|
365
413
|
}
|
|
366
414
|
/**
|
|
367
415
|
* Set whether to use HTTP cache semantics.
|
|
368
416
|
* @param {boolean} value - Enable or disable HTTP cache semantics
|
|
369
417
|
*/
|
|
370
|
-
set
|
|
371
|
-
this.
|
|
418
|
+
set httpCachePolicy(value) {
|
|
419
|
+
this._httpCachePolicy = value;
|
|
372
420
|
}
|
|
373
421
|
/**
|
|
374
422
|
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
375
423
|
*
|
|
376
|
-
* When `
|
|
424
|
+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
|
|
377
425
|
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
|
|
378
426
|
* the default TTL from the Cacheable instance is used.
|
|
379
427
|
*
|
|
380
428
|
* @param {string} url The URL to fetch.
|
|
381
|
-
* @param {
|
|
429
|
+
* @param {Omit<FetchOptions, "cache">} options Optional request options.
|
|
382
430
|
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
383
431
|
*/
|
|
384
432
|
async fetch(url, options) {
|
|
385
433
|
const fetchOptions = {
|
|
386
434
|
...options,
|
|
387
435
|
cache: this._cache,
|
|
388
|
-
|
|
436
|
+
httpCachePolicy: this._httpCachePolicy
|
|
389
437
|
};
|
|
390
438
|
return fetch(url, fetchOptions);
|
|
391
439
|
}
|
|
392
440
|
/**
|
|
393
|
-
* Perform a GET request to a URL with optional request options.
|
|
441
|
+
* Perform a GET request to a URL with optional request options. By default caching is enabled on all requests. To
|
|
442
|
+
* disable set `options.caching` to false.
|
|
394
443
|
* @param {string} url The URL to fetch.
|
|
395
|
-
* @param {
|
|
444
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to GET).
|
|
396
445
|
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
397
446
|
*/
|
|
398
447
|
async get(url, options) {
|
|
399
|
-
const
|
|
448
|
+
const fetchOptions = {
|
|
449
|
+
...options,
|
|
450
|
+
cache: this._cache,
|
|
451
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
452
|
+
method: "GET"
|
|
453
|
+
};
|
|
454
|
+
if (options?.caching !== void 0) {
|
|
455
|
+
delete fetchOptions.cache;
|
|
456
|
+
}
|
|
457
|
+
const response = await fetch(url, fetchOptions);
|
|
400
458
|
const text = await response.text();
|
|
401
459
|
let data;
|
|
460
|
+
const parseFn = options?.parse || this._parse;
|
|
402
461
|
try {
|
|
403
|
-
data =
|
|
462
|
+
data = parseFn(text);
|
|
404
463
|
} catch {
|
|
405
464
|
data = text;
|
|
406
465
|
}
|
|
@@ -415,10 +474,11 @@ var CacheableNet = class extends import_hookified.Hookified {
|
|
|
415
474
|
};
|
|
416
475
|
}
|
|
417
476
|
/**
|
|
418
|
-
* Perform a POST request to a URL with data and optional request options.
|
|
477
|
+
* Perform a POST request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
478
|
+
* set `options.caching` to true. Note, setting caching to tru means it will not post if the data is the same.
|
|
419
479
|
* @param {string} url The URL to fetch.
|
|
420
480
|
* @param {unknown} data The data to send in the request body.
|
|
421
|
-
* @param {Omit<
|
|
481
|
+
* @param {Omit<NetFetchOptions, "method" | "body" >} options Optional request options (method and body will be set).
|
|
422
482
|
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
423
483
|
*/
|
|
424
484
|
async post(url, data, options) {
|
|
@@ -429,21 +489,28 @@ var CacheableNet = class extends import_hookified.Hookified {
|
|
|
429
489
|
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
430
490
|
body = data;
|
|
431
491
|
} else {
|
|
432
|
-
|
|
492
|
+
const stringifyFn = options?.stringify || this._stringify;
|
|
493
|
+
body = stringifyFn(data);
|
|
433
494
|
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
434
495
|
headers["Content-Type"] = "application/json";
|
|
435
496
|
}
|
|
436
497
|
}
|
|
437
|
-
const
|
|
498
|
+
const fetchOptions = {
|
|
438
499
|
...options,
|
|
439
500
|
headers,
|
|
440
501
|
body,
|
|
502
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
441
503
|
method: "POST"
|
|
442
|
-
}
|
|
504
|
+
};
|
|
505
|
+
if (options?.caching === true) {
|
|
506
|
+
fetchOptions.cache = this._cache;
|
|
507
|
+
}
|
|
508
|
+
const response = await fetch(url, fetchOptions);
|
|
443
509
|
const text = await response.text();
|
|
444
510
|
let responseData;
|
|
511
|
+
const parseFn = options?.parse || this._parse;
|
|
445
512
|
try {
|
|
446
|
-
responseData =
|
|
513
|
+
responseData = parseFn(text);
|
|
447
514
|
} catch {
|
|
448
515
|
responseData = text;
|
|
449
516
|
}
|
|
@@ -458,20 +525,82 @@ var CacheableNet = class extends import_hookified.Hookified {
|
|
|
458
525
|
};
|
|
459
526
|
}
|
|
460
527
|
/**
|
|
461
|
-
* Perform a HEAD request to a URL with optional request options.
|
|
528
|
+
* Perform a HEAD request to a URL with optional request options. By default caching is enabled on all requests. To
|
|
529
|
+
* disable set `options.caching` to false.
|
|
462
530
|
* @param {string} url The URL to fetch.
|
|
463
|
-
* @param {
|
|
531
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to HEAD).
|
|
464
532
|
* @returns {Promise<FetchResponse>} The response from the fetch (no body).
|
|
465
533
|
*/
|
|
466
534
|
async head(url, options) {
|
|
467
|
-
const
|
|
535
|
+
const fetchOptions = {
|
|
536
|
+
...options,
|
|
537
|
+
cache: this._cache,
|
|
538
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
539
|
+
method: "HEAD"
|
|
540
|
+
};
|
|
541
|
+
if (options?.caching !== void 0 && !options.caching) {
|
|
542
|
+
delete fetchOptions.cache;
|
|
543
|
+
}
|
|
544
|
+
const response = await fetch(url, fetchOptions);
|
|
468
545
|
return response;
|
|
469
546
|
}
|
|
470
547
|
/**
|
|
471
|
-
* Perform a
|
|
548
|
+
* Perform a PUT request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
549
|
+
* set `options.caching` to true. Note, setting caching to true means it will not put if the data is the same.
|
|
550
|
+
* @param {string} url The URL to fetch.
|
|
551
|
+
* @param {unknown} data The data to send in the request body.
|
|
552
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
553
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
554
|
+
*/
|
|
555
|
+
async put(url, data, options) {
|
|
556
|
+
let body;
|
|
557
|
+
const headers = { ...options?.headers };
|
|
558
|
+
if (typeof data === "string") {
|
|
559
|
+
body = data;
|
|
560
|
+
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
561
|
+
body = data;
|
|
562
|
+
} else {
|
|
563
|
+
const stringifyFn = options?.stringify || this._stringify;
|
|
564
|
+
body = stringifyFn(data);
|
|
565
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
566
|
+
headers["Content-Type"] = "application/json";
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const fetchOptions = {
|
|
570
|
+
...options,
|
|
571
|
+
headers,
|
|
572
|
+
body,
|
|
573
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
574
|
+
method: "PUT"
|
|
575
|
+
};
|
|
576
|
+
if (options?.caching === true) {
|
|
577
|
+
fetchOptions.cache = this._cache;
|
|
578
|
+
}
|
|
579
|
+
const response = await fetch(url, fetchOptions);
|
|
580
|
+
const text = await response.text();
|
|
581
|
+
let responseData;
|
|
582
|
+
const parseFn = options?.parse || this._parse;
|
|
583
|
+
try {
|
|
584
|
+
responseData = parseFn(text);
|
|
585
|
+
} catch {
|
|
586
|
+
responseData = text;
|
|
587
|
+
}
|
|
588
|
+
const newResponse = new Response(text, {
|
|
589
|
+
status: response.status,
|
|
590
|
+
statusText: response.statusText,
|
|
591
|
+
headers: response.headers
|
|
592
|
+
});
|
|
593
|
+
return {
|
|
594
|
+
data: responseData,
|
|
595
|
+
response: newResponse
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Perform a PATCH request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
600
|
+
* set `options.caching` to true. Note, setting caching to true means it will not patch if the data is the same.
|
|
472
601
|
* @param {string} url The URL to fetch.
|
|
473
602
|
* @param {unknown} data The data to send in the request body.
|
|
474
|
-
* @param {Omit<
|
|
603
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
475
604
|
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
476
605
|
*/
|
|
477
606
|
async patch(url, data, options) {
|
|
@@ -482,21 +611,28 @@ var CacheableNet = class extends import_hookified.Hookified {
|
|
|
482
611
|
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
483
612
|
body = data;
|
|
484
613
|
} else {
|
|
485
|
-
|
|
614
|
+
const stringifyFn = options?.stringify || this._stringify;
|
|
615
|
+
body = stringifyFn(data);
|
|
486
616
|
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
487
617
|
headers["Content-Type"] = "application/json";
|
|
488
618
|
}
|
|
489
619
|
}
|
|
490
|
-
const
|
|
620
|
+
const fetchOptions = {
|
|
491
621
|
...options,
|
|
492
622
|
headers,
|
|
493
623
|
body,
|
|
624
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
494
625
|
method: "PATCH"
|
|
495
|
-
}
|
|
626
|
+
};
|
|
627
|
+
if (options?.caching === true) {
|
|
628
|
+
fetchOptions.cache = this._cache;
|
|
629
|
+
}
|
|
630
|
+
const response = await fetch(url, fetchOptions);
|
|
496
631
|
const text = await response.text();
|
|
497
632
|
let responseData;
|
|
633
|
+
const parseFn = options?.parse || this._parse;
|
|
498
634
|
try {
|
|
499
|
-
responseData =
|
|
635
|
+
responseData = parseFn(text);
|
|
500
636
|
} catch {
|
|
501
637
|
responseData = text;
|
|
502
638
|
}
|
|
@@ -511,10 +647,11 @@ var CacheableNet = class extends import_hookified.Hookified {
|
|
|
511
647
|
};
|
|
512
648
|
}
|
|
513
649
|
/**
|
|
514
|
-
* Perform a DELETE request to a URL with optional data and request options.
|
|
650
|
+
* Perform a DELETE request to a URL with optional data and request options. By default caching is not enabled. To enable it
|
|
651
|
+
* set `options.caching` to true. Note, setting caching to true means it will not delete if the data is the same.
|
|
515
652
|
* @param {string} url The URL to fetch.
|
|
516
653
|
* @param {unknown} data Optional data to send in the request body.
|
|
517
|
-
* @param {Omit<
|
|
654
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
518
655
|
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
519
656
|
*/
|
|
520
657
|
async delete(url, data, options) {
|
|
@@ -526,22 +663,29 @@ var CacheableNet = class extends import_hookified.Hookified {
|
|
|
526
663
|
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
527
664
|
body = data;
|
|
528
665
|
} else {
|
|
529
|
-
|
|
666
|
+
const stringifyFn = options?.stringify || this._stringify;
|
|
667
|
+
body = stringifyFn(data);
|
|
530
668
|
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
531
669
|
headers["Content-Type"] = "application/json";
|
|
532
670
|
}
|
|
533
671
|
}
|
|
534
672
|
}
|
|
535
|
-
const
|
|
673
|
+
const fetchOptions = {
|
|
536
674
|
...options,
|
|
537
675
|
headers,
|
|
538
676
|
body,
|
|
677
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
539
678
|
method: "DELETE"
|
|
540
|
-
}
|
|
679
|
+
};
|
|
680
|
+
if (options?.caching === true) {
|
|
681
|
+
fetchOptions.cache = this._cache;
|
|
682
|
+
}
|
|
683
|
+
const response = await fetch(url, fetchOptions);
|
|
541
684
|
const text = await response.text();
|
|
542
685
|
let responseData;
|
|
686
|
+
const parseFn = options?.parse || this._parse;
|
|
543
687
|
try {
|
|
544
|
-
responseData =
|
|
688
|
+
responseData = parseFn(text);
|
|
545
689
|
} catch {
|
|
546
690
|
responseData = text;
|
|
547
691
|
}
|