@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.d.cts
CHANGED
|
@@ -1,37 +1,295 @@
|
|
|
1
1
|
import { Cacheable, CacheableOptions } from 'cacheable';
|
|
2
2
|
import { HookifiedOptions, Hookified } from 'hookified';
|
|
3
|
-
import { Response as Response$1
|
|
4
|
-
export { RequestInit as FetchRequestInit } from 'undici';
|
|
3
|
+
import { RequestInit, Response as Response$1 } from 'undici';
|
|
5
4
|
|
|
6
5
|
type FetchOptions = Omit<RequestInit, "cache"> & {
|
|
7
|
-
cache
|
|
6
|
+
cache?: Cacheable;
|
|
7
|
+
/**
|
|
8
|
+
* Enable HTTP cache semantics for intelligent response caching.
|
|
9
|
+
*
|
|
10
|
+
* When enabled (default), the fetch function will:
|
|
11
|
+
* - Respect standard HTTP cache headers (Cache-Control, ETag, Last-Modified, Expires)
|
|
12
|
+
* - Store and validate cache policies according to RFC 7234
|
|
13
|
+
* - Handle conditional requests with If-None-Match and If-Modified-Since headers
|
|
14
|
+
* - Process 304 Not Modified responses to update cached entries
|
|
15
|
+
* - Only cache responses that are considered "storable" per HTTP specifications
|
|
16
|
+
* - Automatically revalidate stale cache entries when needed
|
|
17
|
+
* - **Set cache TTL based on HTTP headers (e.g., max-age directive)**
|
|
18
|
+
* - Refresh TTL when receiving 304 Not Modified responses
|
|
19
|
+
*
|
|
20
|
+
* When disabled, the fetch function will:
|
|
21
|
+
* - Use simple key-based caching without considering HTTP headers
|
|
22
|
+
* - Cache all successful GET responses regardless of cache directives
|
|
23
|
+
* - Never revalidate cached entries
|
|
24
|
+
* - Ignore cache-control directives from the server
|
|
25
|
+
* - **Use the default TTL from the Cacheable instance**
|
|
26
|
+
*
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
httpCachePolicy?: boolean;
|
|
8
30
|
};
|
|
9
31
|
/**
|
|
10
32
|
* Fetch data from a URL with optional request options.
|
|
33
|
+
*
|
|
34
|
+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
|
|
35
|
+
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
|
|
36
|
+
* the default TTL from the Cacheable instance is used.
|
|
37
|
+
*
|
|
38
|
+
* If no cache is provided, the request will be made without any caching.
|
|
39
|
+
*
|
|
11
40
|
* @param {string} url The URL to fetch.
|
|
12
|
-
* @param {FetchOptions} options Optional request options.
|
|
13
|
-
* instance of `Cacheable` or a `CacheableOptions` object.
|
|
41
|
+
* @param {FetchOptions} options Optional request options. If `cache` is not provided, no caching will be used.
|
|
14
42
|
* @returns {Promise<UndiciResponse>} The response from the fetch.
|
|
15
43
|
*/
|
|
16
44
|
declare function fetch(url: string, options: FetchOptions): Promise<Response$1>;
|
|
45
|
+
type DataResponse<T = unknown> = {
|
|
46
|
+
data: T;
|
|
47
|
+
response: Response$1;
|
|
48
|
+
};
|
|
49
|
+
type GetResponse<T = unknown> = DataResponse<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Perform a GET request to a URL with optional request options.
|
|
52
|
+
* @param {string} url The URL to fetch.
|
|
53
|
+
* @param {Omit<FetchOptions, 'method'>} options Optional request options. The `cache` property is required.
|
|
54
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
55
|
+
*/
|
|
56
|
+
declare function get<T = unknown>(url: string, options: Omit<FetchOptions, "method">): Promise<DataResponse<T>>;
|
|
57
|
+
/**
|
|
58
|
+
* Perform a POST request to a URL with data and optional request options.
|
|
59
|
+
* @param {string} url The URL to fetch.
|
|
60
|
+
* @param {unknown} data The data to send in the request body.
|
|
61
|
+
* @param {Omit<FetchOptions, 'method' | 'body'>} options Optional request options. The `cache` property is required.
|
|
62
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
63
|
+
*/
|
|
64
|
+
declare function post<T = unknown>(url: string, data: unknown, options: Omit<FetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
65
|
+
/**
|
|
66
|
+
* Perform a PATCH request to a URL with data and optional request options.
|
|
67
|
+
* @param {string} url The URL to fetch.
|
|
68
|
+
* @param {unknown} data The data to send in the request body.
|
|
69
|
+
* @param {Omit<FetchOptions, 'method' | 'body'>} options Optional request options. The `cache` property is required.
|
|
70
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
71
|
+
*/
|
|
72
|
+
declare function patch<T = unknown>(url: string, data: unknown, options: Omit<FetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
73
|
+
/**
|
|
74
|
+
* Perform a DELETE request to a URL with optional data and request options.
|
|
75
|
+
* @param {string} url The URL to fetch.
|
|
76
|
+
* @param {unknown} data Optional data to send in the request body.
|
|
77
|
+
* @param {Omit<FetchOptions, 'method' | 'body'>} options Optional request options. The `cache` property is required.
|
|
78
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
79
|
+
*/
|
|
80
|
+
declare function del<T = unknown>(url: string, data?: unknown, options?: Omit<FetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
81
|
+
/**
|
|
82
|
+
* Perform a HEAD request to a URL with optional request options.
|
|
83
|
+
* @param {string} url The URL to fetch.
|
|
84
|
+
* @param {Omit<FetchOptions, 'method'>} options Optional request options. The `cache` property is required.
|
|
85
|
+
* @returns {Promise<UndiciResponse>} The response from the fetch (no body).
|
|
86
|
+
*/
|
|
87
|
+
declare function head(url: string, options: Omit<FetchOptions, "method">): Promise<Response$1>;
|
|
17
88
|
type Response = Response$1;
|
|
18
89
|
|
|
90
|
+
type NetFetchOptions = {
|
|
91
|
+
/**
|
|
92
|
+
* Controls whether caching is enabled for this specific request.
|
|
93
|
+
* - `true`: Enable caching for this request
|
|
94
|
+
* - `false`: Disable caching for this request
|
|
95
|
+
* - `undefined`: Use default caching behavior based on the method (GET/HEAD are cached by default)
|
|
96
|
+
* @default undefined
|
|
97
|
+
*/
|
|
98
|
+
caching?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Custom function for converting JavaScript values to strings for this request.
|
|
101
|
+
* Overrides the instance-level stringify function if provided.
|
|
102
|
+
* @example
|
|
103
|
+
* // Use custom serialization for this request only
|
|
104
|
+
* stringify: (value) => superjson.stringify(value)
|
|
105
|
+
*/
|
|
106
|
+
stringify?: StringifyType;
|
|
107
|
+
/**
|
|
108
|
+
* Custom function for parsing strings back to JavaScript values for this request.
|
|
109
|
+
* Overrides the instance-level parse function if provided.
|
|
110
|
+
* @example
|
|
111
|
+
* // Use custom parsing for this request only
|
|
112
|
+
* parse: (text) => superjson.parse(text)
|
|
113
|
+
*/
|
|
114
|
+
parse?: ParseType;
|
|
115
|
+
} & Omit<FetchOptions, "method" | "cache">;
|
|
19
116
|
type CacheableNetOptions = {
|
|
117
|
+
/**
|
|
118
|
+
* The cache instance or configuration options for caching responses.
|
|
119
|
+
* Can be either an existing Cacheable instance or options to create a new one.
|
|
120
|
+
* If not provided, a default Cacheable instance will be created.
|
|
121
|
+
* @default new Cacheable()
|
|
122
|
+
*/
|
|
20
123
|
cache?: Cacheable | CacheableOptions;
|
|
124
|
+
/**
|
|
125
|
+
* Enable HTTP cache semantics for intelligent response caching.
|
|
126
|
+
*
|
|
127
|
+
* When enabled (default), fetch operations will:
|
|
128
|
+
* - Respect standard HTTP cache headers (Cache-Control, ETag, Last-Modified, Expires)
|
|
129
|
+
* - Store and validate cache policies according to RFC 7234
|
|
130
|
+
* - Handle conditional requests with If-None-Match and If-Modified-Since headers
|
|
131
|
+
* - Process 304 Not Modified responses to update cached entries
|
|
132
|
+
* - Only cache responses that are considered "storable" per HTTP specifications
|
|
133
|
+
* - Automatically revalidate stale cache entries when needed
|
|
134
|
+
* - **Set cache TTL based on HTTP headers (e.g., max-age directive)**
|
|
135
|
+
* - Refresh TTL when receiving 304 Not Modified responses
|
|
136
|
+
*
|
|
137
|
+
* When disabled, fetch operations will:
|
|
138
|
+
* - Use simple key-based caching without considering HTTP headers
|
|
139
|
+
* - Cache all successful GET responses regardless of cache directives
|
|
140
|
+
* - Never revalidate cached entries
|
|
141
|
+
* - Ignore cache-control directives from the server
|
|
142
|
+
* - **Use the default TTL from the Cacheable instance**
|
|
143
|
+
*
|
|
144
|
+
* @default true
|
|
145
|
+
*/
|
|
146
|
+
httpCachePolicy?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Custom function for converting JavaScript values to strings.
|
|
149
|
+
* This is used when serializing request bodies for POST, PUT, PATCH, and DELETE methods.
|
|
150
|
+
* Useful for handling complex data types that JSON.stringify doesn't support natively.
|
|
151
|
+
* @default JSON.stringify
|
|
152
|
+
* @example
|
|
153
|
+
* // Using superjson for enhanced serialization
|
|
154
|
+
* stringify: (value) => superjson.stringify(value)
|
|
155
|
+
*/
|
|
156
|
+
stringify?: StringifyType;
|
|
157
|
+
/**
|
|
158
|
+
* Custom function for parsing strings back to JavaScript values.
|
|
159
|
+
* This is used when deserializing response bodies.
|
|
160
|
+
* Should be compatible with the stringify function for proper round-trip serialization.
|
|
161
|
+
* @default JSON.parse
|
|
162
|
+
* @example
|
|
163
|
+
* // Using superjson for enhanced parsing
|
|
164
|
+
* parse: (text) => superjson.parse(text)
|
|
165
|
+
*/
|
|
166
|
+
parse?: ParseType;
|
|
21
167
|
} & HookifiedOptions;
|
|
168
|
+
/**
|
|
169
|
+
* Function type for converting JavaScript values to strings.
|
|
170
|
+
* Used for serializing request bodies and data.
|
|
171
|
+
* @param value - The JavaScript value to stringify
|
|
172
|
+
* @returns The string representation of the value
|
|
173
|
+
*/
|
|
174
|
+
type StringifyType = (value: unknown) => string;
|
|
175
|
+
/**
|
|
176
|
+
* Function type for parsing strings back to JavaScript values.
|
|
177
|
+
* Used for deserializing response bodies.
|
|
178
|
+
* @param value - The string to parse
|
|
179
|
+
* @returns The parsed JavaScript value
|
|
180
|
+
*/
|
|
181
|
+
type ParseType = (value: string) => unknown;
|
|
22
182
|
declare class CacheableNet extends Hookified {
|
|
23
183
|
private _cache;
|
|
184
|
+
private _httpCachePolicy;
|
|
185
|
+
private _stringify;
|
|
186
|
+
private _parse;
|
|
24
187
|
constructor(options?: CacheableNetOptions);
|
|
188
|
+
/**
|
|
189
|
+
* Get the stringify function used for converting objects to strings.
|
|
190
|
+
* @returns {StringifyType} The current stringify function
|
|
191
|
+
*/
|
|
192
|
+
get stringify(): StringifyType;
|
|
193
|
+
/**
|
|
194
|
+
* Set the stringify function for converting objects to strings.
|
|
195
|
+
* @param {StringifyType} value - The stringify function to use
|
|
196
|
+
*/
|
|
197
|
+
set stringify(value: StringifyType);
|
|
198
|
+
/**
|
|
199
|
+
* Get the parse function used for converting strings to objects.
|
|
200
|
+
* @returns {ParseType} The current parse function
|
|
201
|
+
*/
|
|
202
|
+
get parse(): ParseType;
|
|
203
|
+
/**
|
|
204
|
+
* Set the parse function for converting strings to objects.
|
|
205
|
+
* @param {ParseType} value - The parse function to use
|
|
206
|
+
*/
|
|
207
|
+
set parse(value: ParseType);
|
|
208
|
+
/**
|
|
209
|
+
* Get the Cacheable instance used for caching fetch operations.
|
|
210
|
+
* @returns {Cacheable} The current Cacheable instance
|
|
211
|
+
*/
|
|
25
212
|
get cache(): Cacheable;
|
|
213
|
+
/**
|
|
214
|
+
* Set the Cacheable instance for caching fetch operations.
|
|
215
|
+
* @param {Cacheable} value - The Cacheable instance to use for caching
|
|
216
|
+
*/
|
|
26
217
|
set cache(value: Cacheable);
|
|
218
|
+
/**
|
|
219
|
+
* Get the current HTTP cache policy setting.
|
|
220
|
+
* @returns {boolean} Whether HTTP cache semantics are enabled
|
|
221
|
+
*/
|
|
222
|
+
get httpCachePolicy(): boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Set whether to use HTTP cache semantics.
|
|
225
|
+
* @param {boolean} value - Enable or disable HTTP cache semantics
|
|
226
|
+
*/
|
|
227
|
+
set httpCachePolicy(value: boolean);
|
|
27
228
|
/**
|
|
28
229
|
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
230
|
+
*
|
|
231
|
+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
|
|
232
|
+
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
|
|
233
|
+
* the default TTL from the Cacheable instance is used.
|
|
234
|
+
*
|
|
29
235
|
* @param {string} url The URL to fetch.
|
|
30
|
-
* @param {
|
|
236
|
+
* @param {Omit<FetchOptions, "cache">} options Optional request options.
|
|
31
237
|
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
32
238
|
*/
|
|
33
|
-
fetch(url: string, options?:
|
|
239
|
+
fetch(url: string, options?: Omit<FetchOptions, "cache">): Promise<Response>;
|
|
240
|
+
/**
|
|
241
|
+
* Perform a GET request to a URL with optional request options. By default caching is enabled on all requests. To
|
|
242
|
+
* disable set `options.caching` to false.
|
|
243
|
+
* @param {string} url The URL to fetch.
|
|
244
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to GET).
|
|
245
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
246
|
+
*/
|
|
247
|
+
get<T = unknown>(url: string, options?: NetFetchOptions): Promise<DataResponse<T>>;
|
|
248
|
+
/**
|
|
249
|
+
* Perform a POST request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
250
|
+
* set `options.caching` to true. Note, setting caching to tru means it will not post if the data is the same.
|
|
251
|
+
* @param {string} url The URL to fetch.
|
|
252
|
+
* @param {unknown} data The data to send in the request body.
|
|
253
|
+
* @param {Omit<NetFetchOptions, "method" | "body" >} options Optional request options (method and body will be set).
|
|
254
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
255
|
+
*/
|
|
256
|
+
post<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
257
|
+
/**
|
|
258
|
+
* Perform a HEAD request to a URL with optional request options. By default caching is enabled on all requests. To
|
|
259
|
+
* disable set `options.caching` to false.
|
|
260
|
+
* @param {string} url The URL to fetch.
|
|
261
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to HEAD).
|
|
262
|
+
* @returns {Promise<FetchResponse>} The response from the fetch (no body).
|
|
263
|
+
*/
|
|
264
|
+
head(url: string, options?: NetFetchOptions): Promise<Response>;
|
|
265
|
+
/**
|
|
266
|
+
* Perform a PUT request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
267
|
+
* set `options.caching` to true. Note, setting caching to true means it will not put if the data is the same.
|
|
268
|
+
* @param {string} url The URL to fetch.
|
|
269
|
+
* @param {unknown} data The data to send in the request body.
|
|
270
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
271
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
272
|
+
*/
|
|
273
|
+
put<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
274
|
+
/**
|
|
275
|
+
* Perform a PATCH request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
276
|
+
* set `options.caching` to true. Note, setting caching to true means it will not patch if the data is the same.
|
|
277
|
+
* @param {string} url The URL to fetch.
|
|
278
|
+
* @param {unknown} data The data to send in the request body.
|
|
279
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
280
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
281
|
+
*/
|
|
282
|
+
patch<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
283
|
+
/**
|
|
284
|
+
* Perform a DELETE request to a URL with optional data and request options. By default caching is not enabled. To enable it
|
|
285
|
+
* set `options.caching` to true. Note, setting caching to true means it will not delete if the data is the same.
|
|
286
|
+
* @param {string} url The URL to fetch.
|
|
287
|
+
* @param {unknown} data Optional data to send in the request body.
|
|
288
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
289
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
290
|
+
*/
|
|
291
|
+
delete<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
34
292
|
}
|
|
35
293
|
declare const Net: typeof CacheableNet;
|
|
36
294
|
|
|
37
|
-
export { CacheableNet, type CacheableNetOptions, type FetchOptions, type Response as FetchResponse, Net, fetch };
|
|
295
|
+
export { CacheableNet, type CacheableNetOptions, type DataResponse, type FetchOptions, type Response as FetchResponse, type GetResponse, Net, type NetFetchOptions, type ParseType, type StringifyType, del, fetch, get, head, patch, post };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,37 +1,295 @@
|
|
|
1
1
|
import { Cacheable, CacheableOptions } from 'cacheable';
|
|
2
2
|
import { HookifiedOptions, Hookified } from 'hookified';
|
|
3
|
-
import { Response as Response$1
|
|
4
|
-
export { RequestInit as FetchRequestInit } from 'undici';
|
|
3
|
+
import { RequestInit, Response as Response$1 } from 'undici';
|
|
5
4
|
|
|
6
5
|
type FetchOptions = Omit<RequestInit, "cache"> & {
|
|
7
|
-
cache
|
|
6
|
+
cache?: Cacheable;
|
|
7
|
+
/**
|
|
8
|
+
* Enable HTTP cache semantics for intelligent response caching.
|
|
9
|
+
*
|
|
10
|
+
* When enabled (default), the fetch function will:
|
|
11
|
+
* - Respect standard HTTP cache headers (Cache-Control, ETag, Last-Modified, Expires)
|
|
12
|
+
* - Store and validate cache policies according to RFC 7234
|
|
13
|
+
* - Handle conditional requests with If-None-Match and If-Modified-Since headers
|
|
14
|
+
* - Process 304 Not Modified responses to update cached entries
|
|
15
|
+
* - Only cache responses that are considered "storable" per HTTP specifications
|
|
16
|
+
* - Automatically revalidate stale cache entries when needed
|
|
17
|
+
* - **Set cache TTL based on HTTP headers (e.g., max-age directive)**
|
|
18
|
+
* - Refresh TTL when receiving 304 Not Modified responses
|
|
19
|
+
*
|
|
20
|
+
* When disabled, the fetch function will:
|
|
21
|
+
* - Use simple key-based caching without considering HTTP headers
|
|
22
|
+
* - Cache all successful GET responses regardless of cache directives
|
|
23
|
+
* - Never revalidate cached entries
|
|
24
|
+
* - Ignore cache-control directives from the server
|
|
25
|
+
* - **Use the default TTL from the Cacheable instance**
|
|
26
|
+
*
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
httpCachePolicy?: boolean;
|
|
8
30
|
};
|
|
9
31
|
/**
|
|
10
32
|
* Fetch data from a URL with optional request options.
|
|
33
|
+
*
|
|
34
|
+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
|
|
35
|
+
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
|
|
36
|
+
* the default TTL from the Cacheable instance is used.
|
|
37
|
+
*
|
|
38
|
+
* If no cache is provided, the request will be made without any caching.
|
|
39
|
+
*
|
|
11
40
|
* @param {string} url The URL to fetch.
|
|
12
|
-
* @param {FetchOptions} options Optional request options.
|
|
13
|
-
* instance of `Cacheable` or a `CacheableOptions` object.
|
|
41
|
+
* @param {FetchOptions} options Optional request options. If `cache` is not provided, no caching will be used.
|
|
14
42
|
* @returns {Promise<UndiciResponse>} The response from the fetch.
|
|
15
43
|
*/
|
|
16
44
|
declare function fetch(url: string, options: FetchOptions): Promise<Response$1>;
|
|
45
|
+
type DataResponse<T = unknown> = {
|
|
46
|
+
data: T;
|
|
47
|
+
response: Response$1;
|
|
48
|
+
};
|
|
49
|
+
type GetResponse<T = unknown> = DataResponse<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Perform a GET request to a URL with optional request options.
|
|
52
|
+
* @param {string} url The URL to fetch.
|
|
53
|
+
* @param {Omit<FetchOptions, 'method'>} options Optional request options. The `cache` property is required.
|
|
54
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
55
|
+
*/
|
|
56
|
+
declare function get<T = unknown>(url: string, options: Omit<FetchOptions, "method">): Promise<DataResponse<T>>;
|
|
57
|
+
/**
|
|
58
|
+
* Perform a POST request to a URL with data and optional request options.
|
|
59
|
+
* @param {string} url The URL to fetch.
|
|
60
|
+
* @param {unknown} data The data to send in the request body.
|
|
61
|
+
* @param {Omit<FetchOptions, 'method' | 'body'>} options Optional request options. The `cache` property is required.
|
|
62
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
63
|
+
*/
|
|
64
|
+
declare function post<T = unknown>(url: string, data: unknown, options: Omit<FetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
65
|
+
/**
|
|
66
|
+
* Perform a PATCH request to a URL with data and optional request options.
|
|
67
|
+
* @param {string} url The URL to fetch.
|
|
68
|
+
* @param {unknown} data The data to send in the request body.
|
|
69
|
+
* @param {Omit<FetchOptions, 'method' | 'body'>} options Optional request options. The `cache` property is required.
|
|
70
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
71
|
+
*/
|
|
72
|
+
declare function patch<T = unknown>(url: string, data: unknown, options: Omit<FetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
73
|
+
/**
|
|
74
|
+
* Perform a DELETE request to a URL with optional data and request options.
|
|
75
|
+
* @param {string} url The URL to fetch.
|
|
76
|
+
* @param {unknown} data Optional data to send in the request body.
|
|
77
|
+
* @param {Omit<FetchOptions, 'method' | 'body'>} options Optional request options. The `cache` property is required.
|
|
78
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
79
|
+
*/
|
|
80
|
+
declare function del<T = unknown>(url: string, data?: unknown, options?: Omit<FetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
81
|
+
/**
|
|
82
|
+
* Perform a HEAD request to a URL with optional request options.
|
|
83
|
+
* @param {string} url The URL to fetch.
|
|
84
|
+
* @param {Omit<FetchOptions, 'method'>} options Optional request options. The `cache` property is required.
|
|
85
|
+
* @returns {Promise<UndiciResponse>} The response from the fetch (no body).
|
|
86
|
+
*/
|
|
87
|
+
declare function head(url: string, options: Omit<FetchOptions, "method">): Promise<Response$1>;
|
|
17
88
|
type Response = Response$1;
|
|
18
89
|
|
|
90
|
+
type NetFetchOptions = {
|
|
91
|
+
/**
|
|
92
|
+
* Controls whether caching is enabled for this specific request.
|
|
93
|
+
* - `true`: Enable caching for this request
|
|
94
|
+
* - `false`: Disable caching for this request
|
|
95
|
+
* - `undefined`: Use default caching behavior based on the method (GET/HEAD are cached by default)
|
|
96
|
+
* @default undefined
|
|
97
|
+
*/
|
|
98
|
+
caching?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Custom function for converting JavaScript values to strings for this request.
|
|
101
|
+
* Overrides the instance-level stringify function if provided.
|
|
102
|
+
* @example
|
|
103
|
+
* // Use custom serialization for this request only
|
|
104
|
+
* stringify: (value) => superjson.stringify(value)
|
|
105
|
+
*/
|
|
106
|
+
stringify?: StringifyType;
|
|
107
|
+
/**
|
|
108
|
+
* Custom function for parsing strings back to JavaScript values for this request.
|
|
109
|
+
* Overrides the instance-level parse function if provided.
|
|
110
|
+
* @example
|
|
111
|
+
* // Use custom parsing for this request only
|
|
112
|
+
* parse: (text) => superjson.parse(text)
|
|
113
|
+
*/
|
|
114
|
+
parse?: ParseType;
|
|
115
|
+
} & Omit<FetchOptions, "method" | "cache">;
|
|
19
116
|
type CacheableNetOptions = {
|
|
117
|
+
/**
|
|
118
|
+
* The cache instance or configuration options for caching responses.
|
|
119
|
+
* Can be either an existing Cacheable instance or options to create a new one.
|
|
120
|
+
* If not provided, a default Cacheable instance will be created.
|
|
121
|
+
* @default new Cacheable()
|
|
122
|
+
*/
|
|
20
123
|
cache?: Cacheable | CacheableOptions;
|
|
124
|
+
/**
|
|
125
|
+
* Enable HTTP cache semantics for intelligent response caching.
|
|
126
|
+
*
|
|
127
|
+
* When enabled (default), fetch operations will:
|
|
128
|
+
* - Respect standard HTTP cache headers (Cache-Control, ETag, Last-Modified, Expires)
|
|
129
|
+
* - Store and validate cache policies according to RFC 7234
|
|
130
|
+
* - Handle conditional requests with If-None-Match and If-Modified-Since headers
|
|
131
|
+
* - Process 304 Not Modified responses to update cached entries
|
|
132
|
+
* - Only cache responses that are considered "storable" per HTTP specifications
|
|
133
|
+
* - Automatically revalidate stale cache entries when needed
|
|
134
|
+
* - **Set cache TTL based on HTTP headers (e.g., max-age directive)**
|
|
135
|
+
* - Refresh TTL when receiving 304 Not Modified responses
|
|
136
|
+
*
|
|
137
|
+
* When disabled, fetch operations will:
|
|
138
|
+
* - Use simple key-based caching without considering HTTP headers
|
|
139
|
+
* - Cache all successful GET responses regardless of cache directives
|
|
140
|
+
* - Never revalidate cached entries
|
|
141
|
+
* - Ignore cache-control directives from the server
|
|
142
|
+
* - **Use the default TTL from the Cacheable instance**
|
|
143
|
+
*
|
|
144
|
+
* @default true
|
|
145
|
+
*/
|
|
146
|
+
httpCachePolicy?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Custom function for converting JavaScript values to strings.
|
|
149
|
+
* This is used when serializing request bodies for POST, PUT, PATCH, and DELETE methods.
|
|
150
|
+
* Useful for handling complex data types that JSON.stringify doesn't support natively.
|
|
151
|
+
* @default JSON.stringify
|
|
152
|
+
* @example
|
|
153
|
+
* // Using superjson for enhanced serialization
|
|
154
|
+
* stringify: (value) => superjson.stringify(value)
|
|
155
|
+
*/
|
|
156
|
+
stringify?: StringifyType;
|
|
157
|
+
/**
|
|
158
|
+
* Custom function for parsing strings back to JavaScript values.
|
|
159
|
+
* This is used when deserializing response bodies.
|
|
160
|
+
* Should be compatible with the stringify function for proper round-trip serialization.
|
|
161
|
+
* @default JSON.parse
|
|
162
|
+
* @example
|
|
163
|
+
* // Using superjson for enhanced parsing
|
|
164
|
+
* parse: (text) => superjson.parse(text)
|
|
165
|
+
*/
|
|
166
|
+
parse?: ParseType;
|
|
21
167
|
} & HookifiedOptions;
|
|
168
|
+
/**
|
|
169
|
+
* Function type for converting JavaScript values to strings.
|
|
170
|
+
* Used for serializing request bodies and data.
|
|
171
|
+
* @param value - The JavaScript value to stringify
|
|
172
|
+
* @returns The string representation of the value
|
|
173
|
+
*/
|
|
174
|
+
type StringifyType = (value: unknown) => string;
|
|
175
|
+
/**
|
|
176
|
+
* Function type for parsing strings back to JavaScript values.
|
|
177
|
+
* Used for deserializing response bodies.
|
|
178
|
+
* @param value - The string to parse
|
|
179
|
+
* @returns The parsed JavaScript value
|
|
180
|
+
*/
|
|
181
|
+
type ParseType = (value: string) => unknown;
|
|
22
182
|
declare class CacheableNet extends Hookified {
|
|
23
183
|
private _cache;
|
|
184
|
+
private _httpCachePolicy;
|
|
185
|
+
private _stringify;
|
|
186
|
+
private _parse;
|
|
24
187
|
constructor(options?: CacheableNetOptions);
|
|
188
|
+
/**
|
|
189
|
+
* Get the stringify function used for converting objects to strings.
|
|
190
|
+
* @returns {StringifyType} The current stringify function
|
|
191
|
+
*/
|
|
192
|
+
get stringify(): StringifyType;
|
|
193
|
+
/**
|
|
194
|
+
* Set the stringify function for converting objects to strings.
|
|
195
|
+
* @param {StringifyType} value - The stringify function to use
|
|
196
|
+
*/
|
|
197
|
+
set stringify(value: StringifyType);
|
|
198
|
+
/**
|
|
199
|
+
* Get the parse function used for converting strings to objects.
|
|
200
|
+
* @returns {ParseType} The current parse function
|
|
201
|
+
*/
|
|
202
|
+
get parse(): ParseType;
|
|
203
|
+
/**
|
|
204
|
+
* Set the parse function for converting strings to objects.
|
|
205
|
+
* @param {ParseType} value - The parse function to use
|
|
206
|
+
*/
|
|
207
|
+
set parse(value: ParseType);
|
|
208
|
+
/**
|
|
209
|
+
* Get the Cacheable instance used for caching fetch operations.
|
|
210
|
+
* @returns {Cacheable} The current Cacheable instance
|
|
211
|
+
*/
|
|
25
212
|
get cache(): Cacheable;
|
|
213
|
+
/**
|
|
214
|
+
* Set the Cacheable instance for caching fetch operations.
|
|
215
|
+
* @param {Cacheable} value - The Cacheable instance to use for caching
|
|
216
|
+
*/
|
|
26
217
|
set cache(value: Cacheable);
|
|
218
|
+
/**
|
|
219
|
+
* Get the current HTTP cache policy setting.
|
|
220
|
+
* @returns {boolean} Whether HTTP cache semantics are enabled
|
|
221
|
+
*/
|
|
222
|
+
get httpCachePolicy(): boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Set whether to use HTTP cache semantics.
|
|
225
|
+
* @param {boolean} value - Enable or disable HTTP cache semantics
|
|
226
|
+
*/
|
|
227
|
+
set httpCachePolicy(value: boolean);
|
|
27
228
|
/**
|
|
28
229
|
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
230
|
+
*
|
|
231
|
+
* When `httpCachePolicy` is enabled (default), cache entries will have their TTL
|
|
232
|
+
* set based on HTTP cache headers (e.g., Cache-Control: max-age). When disabled,
|
|
233
|
+
* the default TTL from the Cacheable instance is used.
|
|
234
|
+
*
|
|
29
235
|
* @param {string} url The URL to fetch.
|
|
30
|
-
* @param {
|
|
236
|
+
* @param {Omit<FetchOptions, "cache">} options Optional request options.
|
|
31
237
|
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
32
238
|
*/
|
|
33
|
-
fetch(url: string, options?:
|
|
239
|
+
fetch(url: string, options?: Omit<FetchOptions, "cache">): Promise<Response>;
|
|
240
|
+
/**
|
|
241
|
+
* Perform a GET request to a URL with optional request options. By default caching is enabled on all requests. To
|
|
242
|
+
* disable set `options.caching` to false.
|
|
243
|
+
* @param {string} url The URL to fetch.
|
|
244
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to GET).
|
|
245
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
246
|
+
*/
|
|
247
|
+
get<T = unknown>(url: string, options?: NetFetchOptions): Promise<DataResponse<T>>;
|
|
248
|
+
/**
|
|
249
|
+
* Perform a POST request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
250
|
+
* set `options.caching` to true. Note, setting caching to tru means it will not post if the data is the same.
|
|
251
|
+
* @param {string} url The URL to fetch.
|
|
252
|
+
* @param {unknown} data The data to send in the request body.
|
|
253
|
+
* @param {Omit<NetFetchOptions, "method" | "body" >} options Optional request options (method and body will be set).
|
|
254
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
255
|
+
*/
|
|
256
|
+
post<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
257
|
+
/**
|
|
258
|
+
* Perform a HEAD request to a URL with optional request options. By default caching is enabled on all requests. To
|
|
259
|
+
* disable set `options.caching` to false.
|
|
260
|
+
* @param {string} url The URL to fetch.
|
|
261
|
+
* @param {NetFetchOptions} options Optional request options (method will be set to HEAD).
|
|
262
|
+
* @returns {Promise<FetchResponse>} The response from the fetch (no body).
|
|
263
|
+
*/
|
|
264
|
+
head(url: string, options?: NetFetchOptions): Promise<Response>;
|
|
265
|
+
/**
|
|
266
|
+
* Perform a PUT request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
267
|
+
* set `options.caching` to true. Note, setting caching to true means it will not put if the data is the same.
|
|
268
|
+
* @param {string} url The URL to fetch.
|
|
269
|
+
* @param {unknown} data The data to send in the request body.
|
|
270
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
271
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
272
|
+
*/
|
|
273
|
+
put<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
274
|
+
/**
|
|
275
|
+
* Perform a PATCH request to a URL with data and optional request options. By default caching is not enabled. To enable it
|
|
276
|
+
* set `options.caching` to true. Note, setting caching to true means it will not patch if the data is the same.
|
|
277
|
+
* @param {string} url The URL to fetch.
|
|
278
|
+
* @param {unknown} data The data to send in the request body.
|
|
279
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
280
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
281
|
+
*/
|
|
282
|
+
patch<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
283
|
+
/**
|
|
284
|
+
* Perform a DELETE request to a URL with optional data and request options. By default caching is not enabled. To enable it
|
|
285
|
+
* set `options.caching` to true. Note, setting caching to true means it will not delete if the data is the same.
|
|
286
|
+
* @param {string} url The URL to fetch.
|
|
287
|
+
* @param {unknown} data Optional data to send in the request body.
|
|
288
|
+
* @param {Omit<NetFetchOptions, 'method' | 'body'>} options Optional request options (method and body will be set).
|
|
289
|
+
* @returns {Promise<DataResponse<T>>} The typed data and response from the fetch.
|
|
290
|
+
*/
|
|
291
|
+
delete<T = unknown>(url: string, data?: unknown, options?: Omit<NetFetchOptions, "method" | "body">): Promise<DataResponse<T>>;
|
|
34
292
|
}
|
|
35
293
|
declare const Net: typeof CacheableNet;
|
|
36
294
|
|
|
37
|
-
export { CacheableNet, type CacheableNetOptions, type FetchOptions, type Response as FetchResponse, Net, fetch };
|
|
295
|
+
export { CacheableNet, type CacheableNetOptions, type DataResponse, type FetchOptions, type Response as FetchResponse, type GetResponse, Net, type NetFetchOptions, type ParseType, type StringifyType, del, fetch, get, head, patch, post };
|