bv-ui-core 2.9.7 → 2.9.8
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/lib/bvFetch/README.md +16 -7
- package/lib/bvFetch/index.js +46 -9
- package/package.json +1 -1
package/lib/bvFetch/README.md
CHANGED
@@ -9,6 +9,8 @@ The BvFetch module provides methods to cache duplicate API calls and interact wi
|
|
9
9
|
`shouldCache (Function):` A function that takes the API response JSON as input and returns a boolean indicating whether to cache the response or not. This allows you to implement custom logic based on the response content. If caching is desired, the function should return true; otherwise, false.
|
10
10
|
`cacheName (String):` Optional. Specifies the name of the cache to be used. If not provided, the default cache name 'bvCache' will be used.
|
11
11
|
`cacheLimit (Integer)`: Optional. Specifies the cache size limit for the cache storage. Its value should be in MB. Default value is 10 MB.
|
12
|
+
`excludeHeaders (Array[String])`: Includes the list of headers that are excluded from the cacheKey
|
13
|
+
|
12
14
|
|
13
15
|
## bvFetchFunc Method Parameters
|
14
16
|
`url (String):` The URL of the API endpoint to fetch data from.
|
@@ -17,11 +19,16 @@ The BvFetch module provides methods to cache duplicate API calls and interact wi
|
|
17
19
|
## bvFetchFunc Return Value
|
18
20
|
`Promise<Response>:` A promise that resolves to the API response. If the response is cached, it returns the cached response. Otherwise, it fetches data from the API endpoint, caches the response according to the caching logic, and returns the fetched response.
|
19
21
|
|
20
|
-
## generateCacheKey Method
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
## generateCacheKey Method
|
23
|
+
|
24
|
+
Generates a cache key as a Request object for a given URL and options, filtering out any headers specified in `excludeHeaders`.
|
25
|
+
|
26
|
+
**Parameters:**
|
27
|
+
- `url (String)`: The URL of the API endpoint.
|
28
|
+
- `options (Object)`: *(Optional)* Request options (e.g., headers, method, etc.).
|
29
|
+
|
30
|
+
**Returns:**
|
31
|
+
`Request`: The generated Request object to be used as a cache key.
|
25
32
|
|
26
33
|
## retrievecachedRequests Method
|
27
34
|
Retrieves cached Requests from the cache storage associated with the provided cache name.
|
@@ -77,8 +84,10 @@ var BvFetch = require('bv-ui-core/lib/bvFetch')
|
|
77
84
|
|
78
85
|
// Initialize BV Fetch instance
|
79
86
|
const bvFetch = new BVFetch({
|
80
|
-
|
81
|
-
|
87
|
+
shouldCache: function(responseJson) => { /* return true or false */ }, // optional, callback function to check if the response is cachable
|
88
|
+
cacheName: "bvCache" // optional, default is "bvCache"
|
89
|
+
cacheLimit: 10, // optional but default is 10
|
90
|
+
excludeHeaders: [], // optional, list of headers to be excluded from cache
|
82
91
|
});
|
83
92
|
|
84
93
|
// Make API calls using bvFetchFunc method
|
package/lib/bvFetch/index.js
CHANGED
@@ -5,12 +5,13 @@
|
|
5
5
|
|
6
6
|
const { fetch } = require('../polyfills/fetch')
|
7
7
|
|
8
|
-
module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit }) {
|
8
|
+
module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit, excludeHeaders = [] }) {
|
9
9
|
this.shouldCache = shouldCache;
|
10
10
|
this.cacheName = cacheName || 'bvCache';
|
11
11
|
this.cacheLimit = cacheLimit * 1024 * 1024 || 10 * 1024 * 1024;
|
12
12
|
this.fetchPromises = new Map();
|
13
13
|
this.cachedRequests = new Set();
|
14
|
+
this.excludeHeaders = excludeHeaders;
|
14
15
|
|
15
16
|
/**
|
16
17
|
* Checks if a request is present in a set of cached URLs.
|
@@ -37,7 +38,7 @@ module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit }) {
|
|
37
38
|
if (cachedHeaders.length !== keyHeaders.length) {
|
38
39
|
return false; // Different number of headers
|
39
40
|
}
|
40
|
-
|
41
|
+
// Check if all headers match
|
41
42
|
return cachedHeaders.every(([key, value]) => {
|
42
43
|
return cacheKey.headers.get(key) === value;
|
43
44
|
});
|
@@ -58,11 +59,40 @@ module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit }) {
|
|
58
59
|
* @returns {Request} The created Request object.
|
59
60
|
*/
|
60
61
|
|
62
|
+
/**
|
63
|
+
* Generates a cache key as a Request object for the given URL and options.
|
64
|
+
* Filters out headers specified in `excludeHeaders` from the options before creating the Request.
|
65
|
+
*
|
66
|
+
* @param {string} url - The URL of the API endpoint.
|
67
|
+
* @param {Object} options - Optional request options (e.g., headers, method, etc.).
|
68
|
+
* @returns {Request} The generated Request object to be used as a cache key.
|
69
|
+
*/
|
61
70
|
this.generateCacheKey = (url, options) => {
|
62
|
-
const
|
71
|
+
const filteredOptions = Object.assign({}, options);
|
72
|
+
this.excludeHeaders.forEach(header => {
|
73
|
+
if (filteredOptions.headers && filteredOptions.headers[header]) {
|
74
|
+
delete filteredOptions.headers[header];
|
75
|
+
}
|
76
|
+
}
|
77
|
+
);
|
78
|
+
const key = new Request(url, filteredOptions);
|
63
79
|
return key;
|
64
80
|
};
|
65
81
|
|
82
|
+
/**
|
83
|
+
* Serializes a request object into a JSON string for use as a cache key.
|
84
|
+
* The serialization includes the URL, method, and sorted headers of the request.
|
85
|
+
* @param {Request} request - The request object to serialize.
|
86
|
+
* @returns {string} A JSON string representing the serialized request.
|
87
|
+
*/
|
88
|
+
this.serializeRequestKey = (request) => {
|
89
|
+
const url = request.url;
|
90
|
+
const method = request.method || 'GET';
|
91
|
+
// Convert headers to a sorted array of [key, value] pairs for consistency
|
92
|
+
const headersArr = Array.from(request.headers.entries()).sort(([a], [b]) => a.localeCompare(b));
|
93
|
+
return JSON.stringify([url, method, { headers: headersArr }]);
|
94
|
+
}
|
95
|
+
|
66
96
|
/**
|
67
97
|
* Retrieves cached Requests from the cache storage associated with the provided cache name.
|
68
98
|
* @returns {void}
|
@@ -76,7 +106,9 @@ module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit }) {
|
|
76
106
|
keys.forEach(request => {
|
77
107
|
const headers = {};
|
78
108
|
request.headers.forEach((value, key) => {
|
79
|
-
|
109
|
+
if (!this.excludeHeaders.includes(key)) {
|
110
|
+
headers[key] = value;
|
111
|
+
}
|
80
112
|
});
|
81
113
|
|
82
114
|
// Generate the cache key with headers and URL
|
@@ -119,6 +151,11 @@ module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit }) {
|
|
119
151
|
this.cacheData = (response, cacheKey) => {
|
120
152
|
const errJson = response.clone();
|
121
153
|
let canBeCached = true;
|
154
|
+
if (!response.ok) {
|
155
|
+
// If the response is not ok, we don't cache it
|
156
|
+
canBeCached = false;
|
157
|
+
return;
|
158
|
+
}
|
122
159
|
// Check for error in response obj
|
123
160
|
errJson.json().then(json => {
|
124
161
|
if (typeof this.shouldCache === 'function') {
|
@@ -206,11 +243,11 @@ module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit }) {
|
|
206
243
|
|
207
244
|
const cacheKey = this.generateCacheKey(url, options);
|
208
245
|
// If an ongoing fetch promise exists for the URL, return it
|
209
|
-
if (this.fetchPromises.has(cacheKey)) {
|
210
|
-
return this.fetchPromises.get(cacheKey).then(res => res.clone());
|
246
|
+
if (this.fetchPromises.has(this.serializeRequestKey(cacheKey))) {
|
247
|
+
return this.fetchPromises.get(this.serializeRequestKey(cacheKey)).then(res => res.clone());
|
211
248
|
}
|
212
249
|
|
213
|
-
|
250
|
+
// Check if response is available in cache
|
214
251
|
const newPromise = this.fetchFromCache(cacheKey)
|
215
252
|
.then((cachedResponse) => {
|
216
253
|
// If response found in cache, return it
|
@@ -222,14 +259,14 @@ module.exports = function BvFetch ({ shouldCache, cacheName, cacheLimit }) {
|
|
222
259
|
});
|
223
260
|
|
224
261
|
// Store the ongoing fetch promise
|
225
|
-
this.fetchPromises.set(cacheKey, newPromise);
|
262
|
+
this.fetchPromises.set(this.serializeRequestKey(cacheKey), newPromise);
|
226
263
|
|
227
264
|
//initiate cache cleanUp
|
228
265
|
this.debounceCleanupExpiredCache();
|
229
266
|
|
230
267
|
// When fetch completes or fails, remove the promise from the store
|
231
268
|
newPromise.finally(() => {
|
232
|
-
this.fetchPromises.delete(cacheKey);
|
269
|
+
this.fetchPromises.delete(this.serializeRequestKey(cacheKey));
|
233
270
|
});
|
234
271
|
|
235
272
|
return newPromise.then(res => res.clone());
|