@studiocms/cfetch 0.3.1 → 0.4.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 +4 -0
- package/dist/wrappers.d.mts +9 -0
- package/dist/wrappers.mjs +10 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -103,6 +103,7 @@ interface CachedResponse<T> {
|
|
|
103
103
|
|
|
104
104
|
```ts
|
|
105
105
|
interface CFetchConfig {
|
|
106
|
+
forceCache?: true | undefined;
|
|
106
107
|
ttl?: Duration.DurationInput;
|
|
107
108
|
tags?: string[];
|
|
108
109
|
key?: string;
|
|
@@ -110,6 +111,9 @@ interface CFetchConfig {
|
|
|
110
111
|
}
|
|
111
112
|
```
|
|
112
113
|
|
|
114
|
+
> [!NOTE]
|
|
115
|
+
> By default only `GET` and `HEAD` requests are cached. You can change that by setting `forceCache` to `true`.
|
|
116
|
+
|
|
113
117
|
##### `InvalidateCacheOptions` type
|
|
114
118
|
|
|
115
119
|
```ts
|
package/dist/wrappers.d.mts
CHANGED
|
@@ -25,6 +25,7 @@ interface CachedResponse<T> {
|
|
|
25
25
|
* Configuration options for cached fetch requests.
|
|
26
26
|
*/
|
|
27
27
|
interface CFetchConfig {
|
|
28
|
+
forceCache?: true | undefined;
|
|
28
29
|
ttl?: Duration$1.DurationInput;
|
|
29
30
|
tags?: string[];
|
|
30
31
|
key?: string;
|
|
@@ -58,6 +59,7 @@ declare const noOpParser: <U>(_: Response) => Promise<U>;
|
|
|
58
59
|
* @param parser - A function that parses the Response object into type T
|
|
59
60
|
* @param options - Optional RequestInit configuration for the fetch request
|
|
60
61
|
* @param cacheConfig - Optional cache configuration object
|
|
62
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
61
63
|
* @param cacheConfig.ttl - Time-to-live duration for the cached entry
|
|
62
64
|
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
63
65
|
* @param cacheConfig.key - Custom cache key (defaults to URL and options hash)
|
|
@@ -112,6 +114,7 @@ declare const invalidateCacheEffect: (opts: InvalidateCacheOptions) => Effect.Ef
|
|
|
112
114
|
* @param url - The URL to fetch data from
|
|
113
115
|
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
114
116
|
* @param cacheConfig - Optional cache configuration
|
|
117
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
115
118
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
116
119
|
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
117
120
|
* @param cacheConfig.key - Custom cache key to use instead of the default URL-based key
|
|
@@ -133,6 +136,7 @@ declare const cFetchEffectJson: <T>(url: string | URL, options?: RequestInit, ca
|
|
|
133
136
|
* @param url - The URL to fetch the resource from.
|
|
134
137
|
* @param options - Optional fetch configuration including method, headers, body, etc.
|
|
135
138
|
* @param cacheConfig - Optional cache configuration.
|
|
139
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
136
140
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response.
|
|
137
141
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes.
|
|
138
142
|
* @param cacheConfig.key - Custom cache key. If not provided, the URL will be used as the key.
|
|
@@ -156,6 +160,7 @@ declare const cFetchEffectText: (url: string | URL, options?: RequestInit, cache
|
|
|
156
160
|
* @param url - The URL of the resource to fetch
|
|
157
161
|
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
158
162
|
* @param cacheConfig - Optional cache configuration object
|
|
163
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
159
164
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
160
165
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry
|
|
161
166
|
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
@@ -180,6 +185,7 @@ declare const cFetchEffectBlob: (url: string | URL, options?: RequestInit, cache
|
|
|
180
185
|
* @param parser - A function that parses the Response object into the desired type T
|
|
181
186
|
* @param options - Optional fetch request configuration (headers, method, body, etc.)
|
|
182
187
|
* @param cacheConfig - Optional cache configuration object
|
|
188
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
183
189
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
184
190
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
185
191
|
* @param cacheConfig.key - Custom cache key; if not provided, a key will be generated from the URL and options
|
|
@@ -203,6 +209,7 @@ declare const cFetch: <T>(url: string | URL, parser: (response: Response) => Pro
|
|
|
203
209
|
* @param url - The URL to fetch data from
|
|
204
210
|
* @param options - Optional fetch configuration options (headers, method, body, etc.)
|
|
205
211
|
* @param cacheConfig - Optional cache configuration object
|
|
212
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
206
213
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
207
214
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
208
215
|
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
@@ -226,6 +233,7 @@ declare const cFetchJson: <T>(url: string | URL, options?: RequestInit, cacheCon
|
|
|
226
233
|
* @param url - The URL to fetch
|
|
227
234
|
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
228
235
|
* @param cacheConfig - Optional cache configuration
|
|
236
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
229
237
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
230
238
|
* @param cacheConfig.tags - Tags to associate with the cached response for invalidation
|
|
231
239
|
* @param cacheConfig.key - Custom cache key (defaults to URL if not provided)
|
|
@@ -248,6 +256,7 @@ declare const cFetchText: (url: string | URL, options?: RequestInit, cacheConfig
|
|
|
248
256
|
* @param url - The URL to fetch the Blob resource from
|
|
249
257
|
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
250
258
|
* @param cacheConfig - Optional cache configuration object
|
|
259
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
251
260
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
252
261
|
* @param cacheConfig.tags - Array of cache tags for cache invalidation
|
|
253
262
|
* @param cacheConfig.key - Custom cache key for storing the response
|
package/dist/wrappers.mjs
CHANGED
|
@@ -107,6 +107,7 @@ const fetchAndParse = (url, parser, options) => Effect.gen(function* () {
|
|
|
107
107
|
* @param parser - A function that parses the Response object into type T
|
|
108
108
|
* @param options - Optional RequestInit configuration for the fetch request
|
|
109
109
|
* @param cacheConfig - Optional cache configuration object
|
|
110
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
110
111
|
* @param cacheConfig.ttl - Time-to-live duration for the cached entry
|
|
111
112
|
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
112
113
|
* @param cacheConfig.key - Custom cache key (defaults to URL and options hash)
|
|
@@ -137,9 +138,9 @@ const fetchAndParse = (url, parser, options) => Effect.gen(function* () {
|
|
|
137
138
|
*/
|
|
138
139
|
const cFetchEffect = (url, parser, options, cacheConfig) => Effect.gen(function* () {
|
|
139
140
|
const cache = yield* CacheService;
|
|
140
|
-
const { key, verbose = false, ...cacheOpts } = cacheConfig || {};
|
|
141
|
+
const { forceCache = false, key, verbose = false, ...cacheOpts } = cacheConfig || {};
|
|
141
142
|
const method = options?.method?.toUpperCase() || "GET";
|
|
142
|
-
if (!cacheableMethods.includes(method)) {
|
|
143
|
+
if (!cacheableMethods.includes(method) && !forceCache) {
|
|
143
144
|
if (verbose) console.log(`[c:fetch] Bypassing cache for non-cacheable method: ${method}`);
|
|
144
145
|
return yield* fetchAndParse(url, parser, options);
|
|
145
146
|
}
|
|
@@ -193,6 +194,7 @@ const invalidateCacheEffect = (opts) => Effect.gen(function* () {
|
|
|
193
194
|
* @param url - The URL to fetch data from
|
|
194
195
|
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
195
196
|
* @param cacheConfig - Optional cache configuration
|
|
197
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
196
198
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
197
199
|
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
198
200
|
* @param cacheConfig.key - Custom cache key to use instead of the default URL-based key
|
|
@@ -214,6 +216,7 @@ const cFetchEffectJson = (url, options, cacheConfig) => cFetchEffect(url, (res)
|
|
|
214
216
|
* @param url - The URL to fetch the resource from.
|
|
215
217
|
* @param options - Optional fetch configuration including method, headers, body, etc.
|
|
216
218
|
* @param cacheConfig - Optional cache configuration.
|
|
219
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
217
220
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response.
|
|
218
221
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes.
|
|
219
222
|
* @param cacheConfig.key - Custom cache key. If not provided, the URL will be used as the key.
|
|
@@ -237,6 +240,7 @@ const cFetchEffectText = (url, options, cacheConfig) => cFetchEffect(url, (res)
|
|
|
237
240
|
* @param url - The URL of the resource to fetch
|
|
238
241
|
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
239
242
|
* @param cacheConfig - Optional cache configuration object
|
|
243
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
240
244
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
241
245
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry
|
|
242
246
|
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
@@ -261,6 +265,7 @@ const cFetchEffectBlob = (url, options, cacheConfig) => cFetchEffect(url, (res)
|
|
|
261
265
|
* @param parser - A function that parses the Response object into the desired type T
|
|
262
266
|
* @param options - Optional fetch request configuration (headers, method, body, etc.)
|
|
263
267
|
* @param cacheConfig - Optional cache configuration object
|
|
268
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
264
269
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
265
270
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
266
271
|
* @param cacheConfig.key - Custom cache key; if not provided, a key will be generated from the URL and options
|
|
@@ -284,6 +289,7 @@ const cFetch = (url, parser, options, cacheConfig) => runEffect(cFetchEffect(url
|
|
|
284
289
|
* @param url - The URL to fetch data from
|
|
285
290
|
* @param options - Optional fetch configuration options (headers, method, body, etc.)
|
|
286
291
|
* @param cacheConfig - Optional cache configuration object
|
|
292
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
287
293
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
288
294
|
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
289
295
|
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
@@ -307,6 +313,7 @@ const cFetchJson = (url, options, cacheConfig) => runEffect(cFetchEffectJson(url
|
|
|
307
313
|
* @param url - The URL to fetch
|
|
308
314
|
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
309
315
|
* @param cacheConfig - Optional cache configuration
|
|
316
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
310
317
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
311
318
|
* @param cacheConfig.tags - Tags to associate with the cached response for invalidation
|
|
312
319
|
* @param cacheConfig.key - Custom cache key (defaults to URL if not provided)
|
|
@@ -329,6 +336,7 @@ const cFetchText = (url, options, cacheConfig) => runEffect(cFetchEffectText(url
|
|
|
329
336
|
* @param url - The URL to fetch the Blob resource from
|
|
330
337
|
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
331
338
|
* @param cacheConfig - Optional cache configuration object
|
|
339
|
+
* @param cacheConfig.forceCache - When set to `true`, forces caching even for non-cacheable HTTP methods (e.g. POST)
|
|
332
340
|
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
333
341
|
* @param cacheConfig.tags - Array of cache tags for cache invalidation
|
|
334
342
|
* @param cacheConfig.key - Custom cache key for storing the response
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@studiocms/cfetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Astro integration that allows you to have a cached fetch function in your Astro SSR project.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://studiocms.dev",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@inox-tools/astro-tests": "^0.8.1",
|
|
54
54
|
"@types/node": "^22.0.0",
|
|
55
55
|
"jest-extended": "^7.0.0",
|
|
56
|
-
"astro": "^5.17.
|
|
56
|
+
"astro": "^5.17.2",
|
|
57
57
|
"effect": "^3.19.16"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|