@cacheable/net 1.0.1 → 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 +641 -12
- package/dist/index.d.cts +266 -8
- package/dist/index.d.ts +266 -8
- package/dist/index.js +625 -11
- package/package.json +8 -6
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -22,64 +32,683 @@ var index_exports = {};
|
|
|
22
32
|
__export(index_exports, {
|
|
23
33
|
CacheableNet: () => CacheableNet,
|
|
24
34
|
Net: () => Net,
|
|
25
|
-
|
|
35
|
+
del: () => del,
|
|
36
|
+
fetch: () => fetch,
|
|
37
|
+
get: () => get,
|
|
38
|
+
head: () => head,
|
|
39
|
+
patch: () => patch,
|
|
40
|
+
post: () => post
|
|
26
41
|
});
|
|
27
42
|
module.exports = __toCommonJS(index_exports);
|
|
28
43
|
var import_cacheable = require("cacheable");
|
|
29
44
|
var import_hookified = require("hookified");
|
|
30
45
|
|
|
31
46
|
// src/fetch.ts
|
|
47
|
+
var import_http_cache_semantics = __toESM(require("http-cache-semantics"), 1);
|
|
32
48
|
var import_undici = require("undici");
|
|
33
49
|
async function fetch(url, options) {
|
|
34
|
-
if (!options.cache) {
|
|
35
|
-
throw new Error("Fetch options must include a cache instance or options.");
|
|
36
|
-
}
|
|
37
50
|
const fetchOptions = {
|
|
38
51
|
...options,
|
|
39
52
|
cache: "no-cache"
|
|
40
53
|
};
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
if (!
|
|
44
|
-
throw new Error(`Fetch failed with status ${
|
|
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}`);
|
|
45
58
|
}
|
|
46
|
-
return
|
|
59
|
+
return response2;
|
|
60
|
+
}
|
|
61
|
+
if (options.method === "POST" || options.method === "PATCH" || options.method === "DELETE" || options.method === "HEAD") {
|
|
62
|
+
const response2 = await (0, import_undici.fetch)(url, fetchOptions);
|
|
63
|
+
if (!response2.ok) {
|
|
64
|
+
throw new Error(`Fetch failed with status ${response2.status}`);
|
|
65
|
+
}
|
|
66
|
+
return response2;
|
|
67
|
+
}
|
|
68
|
+
const httpCachePolicy = options.httpCachePolicy !== false;
|
|
69
|
+
const method = options.method || "GET";
|
|
70
|
+
const cacheKey = `${method}:${url}`;
|
|
71
|
+
if (!httpCachePolicy) {
|
|
72
|
+
const cachedData = await options.cache.getOrSet(cacheKey, async () => {
|
|
73
|
+
const response2 = await (0, import_undici.fetch)(url, fetchOptions);
|
|
74
|
+
if (!response2.ok) {
|
|
75
|
+
throw new Error(`Fetch failed with status ${response2.status}`);
|
|
76
|
+
}
|
|
77
|
+
const body2 = await response2.text();
|
|
78
|
+
return {
|
|
79
|
+
body: body2,
|
|
80
|
+
status: response2.status,
|
|
81
|
+
statusText: response2.statusText,
|
|
82
|
+
headers: Object.fromEntries(response2.headers.entries())
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
if (!cachedData) {
|
|
86
|
+
throw new Error("Failed to get or set cache data");
|
|
87
|
+
}
|
|
88
|
+
return new Response(cachedData.body, {
|
|
89
|
+
status: cachedData.status,
|
|
90
|
+
statusText: cachedData.statusText,
|
|
91
|
+
headers: cachedData.headers
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const policyKey = `${cacheKey}:policy`;
|
|
95
|
+
const [cachedResponse, cachedPolicyData] = await Promise.all([
|
|
96
|
+
options.cache.get(cacheKey),
|
|
97
|
+
options.cache.get(policyKey)
|
|
98
|
+
]);
|
|
99
|
+
let policy;
|
|
100
|
+
let cachedBody;
|
|
101
|
+
let cachedStatus;
|
|
102
|
+
let cachedStatusText;
|
|
103
|
+
let cachedHeaders;
|
|
104
|
+
if (cachedPolicyData && cachedResponse) {
|
|
105
|
+
policy = import_http_cache_semantics.default.fromObject(
|
|
106
|
+
cachedPolicyData
|
|
107
|
+
);
|
|
108
|
+
cachedBody = cachedResponse.body;
|
|
109
|
+
cachedStatus = cachedResponse.status;
|
|
110
|
+
cachedStatusText = cachedResponse.statusText;
|
|
111
|
+
cachedHeaders = cachedResponse.headers;
|
|
112
|
+
}
|
|
113
|
+
const requestHeaders = fetchOptions.headers || {};
|
|
114
|
+
const request = {
|
|
115
|
+
url,
|
|
116
|
+
method,
|
|
117
|
+
headers: requestHeaders
|
|
118
|
+
};
|
|
119
|
+
if (policy?.satisfiesWithoutRevalidation(request)) {
|
|
120
|
+
const headers = policy.responseHeaders();
|
|
121
|
+
return new Response(cachedBody, {
|
|
122
|
+
status: cachedStatus,
|
|
123
|
+
statusText: cachedStatusText,
|
|
124
|
+
headers
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
let revalidationHeaders = {};
|
|
128
|
+
if (policy?.revalidationHeaders(request)) {
|
|
129
|
+
revalidationHeaders = policy.revalidationHeaders(request);
|
|
130
|
+
}
|
|
131
|
+
const response = await (0, import_undici.fetch)(url, {
|
|
132
|
+
...fetchOptions,
|
|
133
|
+
headers: {
|
|
134
|
+
...fetchOptions.headers,
|
|
135
|
+
...revalidationHeaders
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
if (response.status === 304 && policy) {
|
|
139
|
+
const { policy: updatedPolicy, modified } = policy.revalidatedPolicy(
|
|
140
|
+
request,
|
|
141
|
+
{
|
|
142
|
+
status: response.status,
|
|
143
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
if (!modified) {
|
|
147
|
+
const ttl = updatedPolicy.timeToLive();
|
|
148
|
+
await options.cache.set(policyKey, updatedPolicy.toObject(), ttl);
|
|
149
|
+
await options.cache.set(
|
|
150
|
+
cacheKey,
|
|
151
|
+
{
|
|
152
|
+
body: cachedBody,
|
|
153
|
+
status: cachedStatus,
|
|
154
|
+
statusText: cachedStatusText,
|
|
155
|
+
headers: cachedHeaders
|
|
156
|
+
},
|
|
157
|
+
ttl
|
|
158
|
+
);
|
|
159
|
+
const headers = updatedPolicy.responseHeaders();
|
|
160
|
+
return new Response(cachedBody, {
|
|
161
|
+
status: cachedStatus,
|
|
162
|
+
statusText: cachedStatusText,
|
|
163
|
+
headers
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (!response.ok && response.status !== 304) {
|
|
168
|
+
throw new Error(`Fetch failed with status ${response.status}`);
|
|
169
|
+
}
|
|
170
|
+
const body = await response.text();
|
|
171
|
+
const responseForPolicy = {
|
|
172
|
+
status: response.status,
|
|
173
|
+
statusText: response.statusText,
|
|
174
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
175
|
+
};
|
|
176
|
+
const newPolicy = new import_http_cache_semantics.default(request, responseForPolicy);
|
|
177
|
+
if (newPolicy.storable()) {
|
|
178
|
+
const ttl = newPolicy.timeToLive();
|
|
179
|
+
await Promise.all([
|
|
180
|
+
options.cache.set(
|
|
181
|
+
cacheKey,
|
|
182
|
+
{
|
|
183
|
+
body,
|
|
184
|
+
status: response.status,
|
|
185
|
+
statusText: response.statusText,
|
|
186
|
+
headers: responseForPolicy.headers
|
|
187
|
+
},
|
|
188
|
+
ttl
|
|
189
|
+
),
|
|
190
|
+
options.cache.set(policyKey, newPolicy.toObject(), ttl)
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
return new Response(body, {
|
|
194
|
+
status: response.status,
|
|
195
|
+
statusText: response.statusText,
|
|
196
|
+
headers: response.headers
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
async function get(url, options) {
|
|
200
|
+
const response = await fetch(url, { ...options, method: "GET" });
|
|
201
|
+
const text = await response.text();
|
|
202
|
+
let data;
|
|
203
|
+
try {
|
|
204
|
+
data = JSON.parse(text);
|
|
205
|
+
} catch {
|
|
206
|
+
data = text;
|
|
207
|
+
}
|
|
208
|
+
const newResponse = new Response(text, {
|
|
209
|
+
status: response.status,
|
|
210
|
+
statusText: response.statusText,
|
|
211
|
+
headers: response.headers
|
|
212
|
+
});
|
|
213
|
+
return {
|
|
214
|
+
data,
|
|
215
|
+
response: newResponse
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
async function post(url, data, options) {
|
|
219
|
+
let body;
|
|
220
|
+
const headers = { ...options.headers };
|
|
221
|
+
if (typeof data === "string") {
|
|
222
|
+
body = data;
|
|
223
|
+
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
224
|
+
body = data;
|
|
225
|
+
} else {
|
|
226
|
+
body = JSON.stringify(data);
|
|
227
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
228
|
+
headers["Content-Type"] = "application/json";
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const response = await fetch(url, {
|
|
232
|
+
...options,
|
|
233
|
+
headers,
|
|
234
|
+
body,
|
|
235
|
+
method: "POST"
|
|
236
|
+
});
|
|
237
|
+
const text = await response.text();
|
|
238
|
+
let responseData;
|
|
239
|
+
try {
|
|
240
|
+
responseData = JSON.parse(text);
|
|
241
|
+
} catch {
|
|
242
|
+
responseData = text;
|
|
243
|
+
}
|
|
244
|
+
const newResponse = new Response(text, {
|
|
245
|
+
status: response.status,
|
|
246
|
+
statusText: response.statusText,
|
|
247
|
+
headers: response.headers
|
|
248
|
+
});
|
|
249
|
+
return {
|
|
250
|
+
data: responseData,
|
|
251
|
+
response: newResponse
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
async function patch(url, data, options) {
|
|
255
|
+
let body;
|
|
256
|
+
const headers = { ...options.headers };
|
|
257
|
+
if (typeof data === "string") {
|
|
258
|
+
body = data;
|
|
259
|
+
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
260
|
+
body = data;
|
|
261
|
+
} else {
|
|
262
|
+
body = JSON.stringify(data);
|
|
263
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
264
|
+
headers["Content-Type"] = "application/json";
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const response = await fetch(url, {
|
|
268
|
+
...options,
|
|
269
|
+
headers,
|
|
270
|
+
body,
|
|
271
|
+
method: "PATCH"
|
|
272
|
+
});
|
|
273
|
+
const text = await response.text();
|
|
274
|
+
let responseData;
|
|
275
|
+
try {
|
|
276
|
+
responseData = JSON.parse(text);
|
|
277
|
+
} catch {
|
|
278
|
+
responseData = text;
|
|
279
|
+
}
|
|
280
|
+
const newResponse = new Response(text, {
|
|
281
|
+
status: response.status,
|
|
282
|
+
statusText: response.statusText,
|
|
283
|
+
headers: response.headers
|
|
284
|
+
});
|
|
285
|
+
return {
|
|
286
|
+
data: responseData,
|
|
287
|
+
response: newResponse
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
async function del(url, data, options) {
|
|
291
|
+
let actualData;
|
|
292
|
+
let actualOptions;
|
|
293
|
+
if (data !== void 0 && typeof data === "object" && data !== null && "cache" in data) {
|
|
294
|
+
actualData = void 0;
|
|
295
|
+
actualOptions = data;
|
|
296
|
+
} else if (options) {
|
|
297
|
+
actualData = data;
|
|
298
|
+
actualOptions = options;
|
|
299
|
+
} else {
|
|
300
|
+
throw new Error("Fetch options must include a cache instance or options.");
|
|
301
|
+
}
|
|
302
|
+
let body;
|
|
303
|
+
const headers = { ...actualOptions.headers };
|
|
304
|
+
if (actualData !== void 0) {
|
|
305
|
+
if (typeof actualData === "string") {
|
|
306
|
+
body = actualData;
|
|
307
|
+
} else if (actualData instanceof FormData || actualData instanceof URLSearchParams || actualData instanceof Blob) {
|
|
308
|
+
body = actualData;
|
|
309
|
+
} else {
|
|
310
|
+
body = JSON.stringify(actualData);
|
|
311
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
312
|
+
headers["Content-Type"] = "application/json";
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const response = await fetch(url, {
|
|
317
|
+
...actualOptions,
|
|
318
|
+
headers,
|
|
319
|
+
body,
|
|
320
|
+
method: "DELETE"
|
|
321
|
+
});
|
|
322
|
+
const text = await response.text();
|
|
323
|
+
let responseData;
|
|
324
|
+
try {
|
|
325
|
+
responseData = JSON.parse(text);
|
|
326
|
+
} catch {
|
|
327
|
+
responseData = text;
|
|
328
|
+
}
|
|
329
|
+
const newResponse = new Response(text, {
|
|
330
|
+
status: response.status,
|
|
331
|
+
statusText: response.statusText,
|
|
332
|
+
headers: response.headers
|
|
47
333
|
});
|
|
334
|
+
return {
|
|
335
|
+
data: responseData,
|
|
336
|
+
response: newResponse
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
async function head(url, options) {
|
|
340
|
+
const response = await fetch(url, { ...options, method: "HEAD" });
|
|
341
|
+
return response;
|
|
48
342
|
}
|
|
49
343
|
|
|
50
344
|
// src/index.ts
|
|
51
345
|
var CacheableNet = class extends import_hookified.Hookified {
|
|
52
346
|
_cache = new import_cacheable.Cacheable();
|
|
347
|
+
_httpCachePolicy = true;
|
|
348
|
+
_stringify = JSON.stringify;
|
|
349
|
+
_parse = JSON.parse;
|
|
53
350
|
constructor(options) {
|
|
54
351
|
super(options);
|
|
55
352
|
if (options?.cache) {
|
|
56
353
|
this._cache = options.cache instanceof import_cacheable.Cacheable ? options.cache : new import_cacheable.Cacheable(options.cache);
|
|
57
354
|
}
|
|
355
|
+
if (options?.httpCachePolicy !== void 0) {
|
|
356
|
+
this._httpCachePolicy = options.httpCachePolicy;
|
|
357
|
+
}
|
|
358
|
+
if (options?.stringify) {
|
|
359
|
+
this._stringify = options?.stringify;
|
|
360
|
+
}
|
|
361
|
+
if (options?.parse) {
|
|
362
|
+
this._parse = options?.parse;
|
|
363
|
+
}
|
|
58
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;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Get the Cacheable instance used for caching fetch operations.
|
|
395
|
+
* @returns {Cacheable} The current Cacheable instance
|
|
396
|
+
*/
|
|
59
397
|
get cache() {
|
|
60
398
|
return this._cache;
|
|
61
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Set the Cacheable instance for caching fetch operations.
|
|
402
|
+
* @param {Cacheable} value - The Cacheable instance to use for caching
|
|
403
|
+
*/
|
|
62
404
|
set cache(value) {
|
|
63
405
|
this._cache = value;
|
|
64
406
|
}
|
|
407
|
+
/**
|
|
408
|
+
* Get the current HTTP cache policy setting.
|
|
409
|
+
* @returns {boolean} Whether HTTP cache semantics are enabled
|
|
410
|
+
*/
|
|
411
|
+
get httpCachePolicy() {
|
|
412
|
+
return this._httpCachePolicy;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Set whether to use HTTP cache semantics.
|
|
416
|
+
* @param {boolean} value - Enable or disable HTTP cache semantics
|
|
417
|
+
*/
|
|
418
|
+
set httpCachePolicy(value) {
|
|
419
|
+
this._httpCachePolicy = value;
|
|
420
|
+
}
|
|
65
421
|
/**
|
|
66
422
|
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
423
|
+
*
|
|
424
|
+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
|
|
425
|
+
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
|
|
426
|
+
* the default TTL from the Cacheable instance is used.
|
|
427
|
+
*
|
|
67
428
|
* @param {string} url The URL to fetch.
|
|
68
|
-
* @param {
|
|
429
|
+
* @param {Omit<FetchOptions, "cache">} options Optional request options.
|
|
69
430
|
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
70
431
|
*/
|
|
71
432
|
async fetch(url, options) {
|
|
72
433
|
const fetchOptions = {
|
|
73
434
|
...options,
|
|
74
|
-
cache: this._cache
|
|
435
|
+
cache: this._cache,
|
|
436
|
+
httpCachePolicy: this._httpCachePolicy
|
|
75
437
|
};
|
|
76
438
|
return fetch(url, fetchOptions);
|
|
77
439
|
}
|
|
440
|
+
/**
|
|
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.
|
|
443
|
+
* @param {string} url The URL to fetch.
|
|
444
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to GET).
|
|
445
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
446
|
+
*/
|
|
447
|
+
async get(url, options) {
|
|
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);
|
|
458
|
+
const text = await response.text();
|
|
459
|
+
let data;
|
|
460
|
+
const parseFn = options?.parse || this._parse;
|
|
461
|
+
try {
|
|
462
|
+
data = parseFn(text);
|
|
463
|
+
} catch {
|
|
464
|
+
data = text;
|
|
465
|
+
}
|
|
466
|
+
const newResponse = new Response(text, {
|
|
467
|
+
status: response.status,
|
|
468
|
+
statusText: response.statusText,
|
|
469
|
+
headers: response.headers
|
|
470
|
+
});
|
|
471
|
+
return {
|
|
472
|
+
data,
|
|
473
|
+
response: newResponse
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
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.
|
|
479
|
+
* @param {string} url The URL to fetch.
|
|
480
|
+
* @param {unknown} data The data to send in the request body.
|
|
481
|
+
* @param {Omit<NetFetchOptions, "method" | "body" >} options Optional request options (method and body will be set).
|
|
482
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
483
|
+
*/
|
|
484
|
+
async post(url, data, options) {
|
|
485
|
+
let body;
|
|
486
|
+
const headers = { ...options?.headers };
|
|
487
|
+
if (typeof data === "string") {
|
|
488
|
+
body = data;
|
|
489
|
+
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
490
|
+
body = data;
|
|
491
|
+
} else {
|
|
492
|
+
const stringifyFn = options?.stringify || this._stringify;
|
|
493
|
+
body = stringifyFn(data);
|
|
494
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
495
|
+
headers["Content-Type"] = "application/json";
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
const fetchOptions = {
|
|
499
|
+
...options,
|
|
500
|
+
headers,
|
|
501
|
+
body,
|
|
502
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
503
|
+
method: "POST"
|
|
504
|
+
};
|
|
505
|
+
if (options?.caching === true) {
|
|
506
|
+
fetchOptions.cache = this._cache;
|
|
507
|
+
}
|
|
508
|
+
const response = await fetch(url, fetchOptions);
|
|
509
|
+
const text = await response.text();
|
|
510
|
+
let responseData;
|
|
511
|
+
const parseFn = options?.parse || this._parse;
|
|
512
|
+
try {
|
|
513
|
+
responseData = parseFn(text);
|
|
514
|
+
} catch {
|
|
515
|
+
responseData = text;
|
|
516
|
+
}
|
|
517
|
+
const newResponse = new Response(text, {
|
|
518
|
+
status: response.status,
|
|
519
|
+
statusText: response.statusText,
|
|
520
|
+
headers: response.headers
|
|
521
|
+
});
|
|
522
|
+
return {
|
|
523
|
+
data: responseData,
|
|
524
|
+
response: newResponse
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
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.
|
|
530
|
+
* @param {string} url The URL to fetch.
|
|
531
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to HEAD).
|
|
532
|
+
* @returns {Promise<FetchResponse>} The response from the fetch (no body).
|
|
533
|
+
*/
|
|
534
|
+
async head(url, options) {
|
|
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);
|
|
545
|
+
return response;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
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.
|
|
601
|
+
* @param {string} url The URL to fetch.
|
|
602
|
+
* @param {unknown} data The data to send in the request body.
|
|
603
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
604
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
605
|
+
*/
|
|
606
|
+
async patch(url, data, options) {
|
|
607
|
+
let body;
|
|
608
|
+
const headers = { ...options?.headers };
|
|
609
|
+
if (typeof data === "string") {
|
|
610
|
+
body = data;
|
|
611
|
+
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
612
|
+
body = data;
|
|
613
|
+
} else {
|
|
614
|
+
const stringifyFn = options?.stringify || this._stringify;
|
|
615
|
+
body = stringifyFn(data);
|
|
616
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
617
|
+
headers["Content-Type"] = "application/json";
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
const fetchOptions = {
|
|
621
|
+
...options,
|
|
622
|
+
headers,
|
|
623
|
+
body,
|
|
624
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
625
|
+
method: "PATCH"
|
|
626
|
+
};
|
|
627
|
+
if (options?.caching === true) {
|
|
628
|
+
fetchOptions.cache = this._cache;
|
|
629
|
+
}
|
|
630
|
+
const response = await fetch(url, fetchOptions);
|
|
631
|
+
const text = await response.text();
|
|
632
|
+
let responseData;
|
|
633
|
+
const parseFn = options?.parse || this._parse;
|
|
634
|
+
try {
|
|
635
|
+
responseData = parseFn(text);
|
|
636
|
+
} catch {
|
|
637
|
+
responseData = text;
|
|
638
|
+
}
|
|
639
|
+
const newResponse = new Response(text, {
|
|
640
|
+
status: response.status,
|
|
641
|
+
statusText: response.statusText,
|
|
642
|
+
headers: response.headers
|
|
643
|
+
});
|
|
644
|
+
return {
|
|
645
|
+
data: responseData,
|
|
646
|
+
response: newResponse
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
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.
|
|
652
|
+
* @param {string} url The URL to fetch.
|
|
653
|
+
* @param {unknown} data Optional data to send in the request body.
|
|
654
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
655
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
656
|
+
*/
|
|
657
|
+
async delete(url, data, options) {
|
|
658
|
+
let body;
|
|
659
|
+
const headers = { ...options?.headers };
|
|
660
|
+
if (data !== void 0) {
|
|
661
|
+
if (typeof data === "string") {
|
|
662
|
+
body = data;
|
|
663
|
+
} else if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob) {
|
|
664
|
+
body = data;
|
|
665
|
+
} else {
|
|
666
|
+
const stringifyFn = options?.stringify || this._stringify;
|
|
667
|
+
body = stringifyFn(data);
|
|
668
|
+
if (!headers["Content-Type"] && !headers["content-type"]) {
|
|
669
|
+
headers["Content-Type"] = "application/json";
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const fetchOptions = {
|
|
674
|
+
...options,
|
|
675
|
+
headers,
|
|
676
|
+
body,
|
|
677
|
+
httpCachePolicy: this._httpCachePolicy,
|
|
678
|
+
method: "DELETE"
|
|
679
|
+
};
|
|
680
|
+
if (options?.caching === true) {
|
|
681
|
+
fetchOptions.cache = this._cache;
|
|
682
|
+
}
|
|
683
|
+
const response = await fetch(url, fetchOptions);
|
|
684
|
+
const text = await response.text();
|
|
685
|
+
let responseData;
|
|
686
|
+
const parseFn = options?.parse || this._parse;
|
|
687
|
+
try {
|
|
688
|
+
responseData = parseFn(text);
|
|
689
|
+
} catch {
|
|
690
|
+
responseData = text;
|
|
691
|
+
}
|
|
692
|
+
const newResponse = new Response(text, {
|
|
693
|
+
status: response.status,
|
|
694
|
+
statusText: response.statusText,
|
|
695
|
+
headers: response.headers
|
|
696
|
+
});
|
|
697
|
+
return {
|
|
698
|
+
data: responseData,
|
|
699
|
+
response: newResponse
|
|
700
|
+
};
|
|
701
|
+
}
|
|
78
702
|
};
|
|
79
703
|
var Net = CacheableNet;
|
|
80
704
|
// Annotate the CommonJS export names for ESM import in node:
|
|
81
705
|
0 && (module.exports = {
|
|
82
706
|
CacheableNet,
|
|
83
707
|
Net,
|
|
84
|
-
|
|
708
|
+
del,
|
|
709
|
+
fetch,
|
|
710
|
+
get,
|
|
711
|
+
head,
|
|
712
|
+
patch,
|
|
713
|
+
post
|
|
85
714
|
});
|