@react-pakistan/util-functions 1.25.26 → 1.25.27
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/general/generic-cache.js +112 -31
- package/package.json +1 -1
package/general/generic-cache.js
CHANGED
|
@@ -112,6 +112,7 @@ function isCachedMapShape(v) {
|
|
|
112
112
|
* }
|
|
113
113
|
*/
|
|
114
114
|
var getCachedDataSync = function (cacheKey, expirationMs) {
|
|
115
|
+
var _a, _b, _c;
|
|
115
116
|
if (expirationMs === void 0) { expirationMs = constants_1.ONE_DAY_IN_MS; }
|
|
116
117
|
try {
|
|
117
118
|
var raw = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
@@ -122,7 +123,11 @@ var getCachedDataSync = function (cacheKey, expirationMs) {
|
|
|
122
123
|
var cachedTime = new Date(raw.cachedAt).getTime();
|
|
123
124
|
var ageInMs = currentTime - cachedTime;
|
|
124
125
|
if (ageInMs < expirationMs) {
|
|
125
|
-
return {
|
|
126
|
+
return {
|
|
127
|
+
count: (_a = raw.count) !== null && _a !== void 0 ? _a : raw.items.length,
|
|
128
|
+
items: raw.items,
|
|
129
|
+
currentPage: (_b = raw.currentPage) !== null && _b !== void 0 ? _b : undefined,
|
|
130
|
+
};
|
|
126
131
|
}
|
|
127
132
|
return { count: 0, items: [] };
|
|
128
133
|
}
|
|
@@ -131,7 +136,12 @@ var getCachedDataSync = function (cacheKey, expirationMs) {
|
|
|
131
136
|
var ageInMs = currentTime - cachedTime;
|
|
132
137
|
if (ageInMs < expirationMs) {
|
|
133
138
|
var itemsArray = Object.values(raw.items || {});
|
|
134
|
-
|
|
139
|
+
var storedCount = raw.count;
|
|
140
|
+
return {
|
|
141
|
+
count: storedCount !== null && storedCount !== void 0 ? storedCount : itemsArray.length,
|
|
142
|
+
items: itemsArray,
|
|
143
|
+
currentPage: (_c = raw.currentPage) !== null && _c !== void 0 ? _c : undefined,
|
|
144
|
+
};
|
|
135
145
|
}
|
|
136
146
|
return { count: 0, items: [] };
|
|
137
147
|
}
|
|
@@ -166,10 +176,10 @@ exports.getCachedDataSync = getCachedDataSync;
|
|
|
166
176
|
* const active = await getCachedData<UserBE>({ config, filters: { enabled: true }, pageLimit: 200 });
|
|
167
177
|
*/
|
|
168
178
|
var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
|
|
169
|
-
var config, params, headers, cacheKey, apiUrl, responseKey, expirationMs, fallBackPageLimit, fallBackCurrentPage, filters, searchQuery, paramsWithoutFilters_1, otherParams_1, queryObj_1, searchParams_1, response_1, items_1, raw, currentTime, cachedTime, ageInMs, cachedTime, ageInMs, itemsArray, storedCount, paramsWithoutFilters_2, otherParams, queryObj, searchParams, response, itemsRaw, items, count, shouldCache, itemsMap, updatedCache, error_1;
|
|
170
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
171
|
-
return __generator(this, function (
|
|
172
|
-
switch (
|
|
179
|
+
var config, params, headers, cacheKey, apiUrl, responseKey, expirationMs, fallBackPageLimit, fallBackCurrentPage, filters, searchQuery, paramsWithoutFilters_1, otherParams_1, queryObj_1, searchParams_1, response_1, items_1, respCurrentPageRaw_1, respCurrentPage_1, requestCurrentPage_1, raw, currentTime, cachedTime, ageInMs, cachedTime, ageInMs, itemsArray, storedCount, cachedCurrentPage, requestedCurrentPage, paramsWithoutFilters_2, otherParams_2, queryObj_2, searchParams_2, response_2, itemsRaw_1, fetchedItems, respCount, respCurrentPageRaw_2, respCurrentPage_2, newItemsMap, mergedItemsMap, mergedItemsArray, newCount, newCurrentPage, shouldCache_1, updatedCache, paramsWithoutFilters_3, otherParams, queryObj, searchParams, response, itemsRaw, items, count, respCurrentPageRaw, respCurrentPage, requestCurrentPage, shouldCache, itemsMap, updatedCache, error_1;
|
|
180
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
181
|
+
return __generator(this, function (_v) {
|
|
182
|
+
switch (_v.label) {
|
|
173
183
|
case 0:
|
|
174
184
|
config = opts.config, params = opts.params, headers = opts.headers;
|
|
175
185
|
cacheKey = config.cacheKey, apiUrl = config.apiUrl, responseKey = config.responseKey;
|
|
@@ -178,9 +188,9 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
178
188
|
fallBackCurrentPage = (_e = (_d = params === null || params === void 0 ? void 0 : params.currentPage) === null || _d === void 0 ? void 0 : _d.toString()) !== null && _e !== void 0 ? _e : '1';
|
|
179
189
|
filters = params === null || params === void 0 ? void 0 : params.filters;
|
|
180
190
|
searchQuery = params === null || params === void 0 ? void 0 : params.searchQuery;
|
|
181
|
-
|
|
191
|
+
_v.label = 1;
|
|
182
192
|
case 1:
|
|
183
|
-
|
|
193
|
+
_v.trys.push([1, 8, , 9]);
|
|
184
194
|
if (!((searchQuery && searchQuery.trim()) ||
|
|
185
195
|
(filters && Object.keys(filters).length > 0))) return [3 /*break*/, 3];
|
|
186
196
|
paramsWithoutFilters_1 = __assign({}, (params || {}));
|
|
@@ -204,13 +214,19 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
204
214
|
headers: headers,
|
|
205
215
|
})];
|
|
206
216
|
case 2:
|
|
207
|
-
response_1 =
|
|
217
|
+
response_1 = _v.sent();
|
|
208
218
|
items_1 = responseKey
|
|
209
219
|
? ((_f = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _f === void 0 ? void 0 : _f[responseKey]) || []
|
|
210
220
|
: ((_g = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _g === void 0 ? void 0 : _g.items) || (response_1 === null || response_1 === void 0 ? void 0 : response_1.data) || [];
|
|
221
|
+
respCurrentPageRaw_1 = (_h = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _h === void 0 ? void 0 : _h.currentPage;
|
|
222
|
+
respCurrentPage_1 = respCurrentPageRaw_1 == null ? undefined : Number(respCurrentPageRaw_1);
|
|
223
|
+
requestCurrentPage_1 = (params === null || params === void 0 ? void 0 : params.currentPage)
|
|
224
|
+
? Number(params.currentPage)
|
|
225
|
+
: undefined;
|
|
211
226
|
return [2 /*return*/, {
|
|
212
|
-
count: ((
|
|
227
|
+
count: ((_j = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _j === void 0 ? void 0 : _j.count) || items_1.length,
|
|
213
228
|
items: items_1,
|
|
229
|
+
currentPage: respCurrentPage_1 !== null && respCurrentPage_1 !== void 0 ? respCurrentPage_1 : requestCurrentPage_1,
|
|
214
230
|
}];
|
|
215
231
|
case 3:
|
|
216
232
|
raw = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
@@ -222,31 +238,89 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
222
238
|
return [2 /*return*/, {
|
|
223
239
|
count: raw.count,
|
|
224
240
|
items: raw.items,
|
|
225
|
-
currentPage: (_j = raw.currentPage) !== null && _j !== void 0 ? _j : undefined,
|
|
226
|
-
}];
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
if (isCachedMapShape(raw)) {
|
|
230
|
-
cachedTime = new Date(raw.cachedAt).getTime();
|
|
231
|
-
ageInMs = currentTime - cachedTime;
|
|
232
|
-
if (ageInMs < expirationMs) {
|
|
233
|
-
itemsArray = Object.values(raw.items || {});
|
|
234
|
-
storedCount = raw.count;
|
|
235
|
-
return [2 /*return*/, {
|
|
236
|
-
count: storedCount !== null && storedCount !== void 0 ? storedCount : itemsArray.length,
|
|
237
|
-
items: itemsArray,
|
|
238
241
|
currentPage: (_k = raw.currentPage) !== null && _k !== void 0 ? _k : undefined,
|
|
239
242
|
}];
|
|
240
243
|
}
|
|
241
244
|
}
|
|
245
|
+
if (!isCachedMapShape(raw)) return [3 /*break*/, 6];
|
|
246
|
+
cachedTime = new Date(raw.cachedAt).getTime();
|
|
247
|
+
ageInMs = currentTime - cachedTime;
|
|
248
|
+
if (!(ageInMs < expirationMs)) return [3 /*break*/, 6];
|
|
249
|
+
itemsArray = Object.values(raw.items || {});
|
|
250
|
+
storedCount = raw.count;
|
|
251
|
+
cachedCurrentPage = raw.currentPage;
|
|
252
|
+
requestedCurrentPage = (params === null || params === void 0 ? void 0 : params.currentPage)
|
|
253
|
+
? Number(params.currentPage)
|
|
254
|
+
: undefined;
|
|
255
|
+
if (!(requestedCurrentPage !== undefined &&
|
|
256
|
+
requestedCurrentPage !== cachedCurrentPage)) return [3 /*break*/, 5];
|
|
242
257
|
paramsWithoutFilters_2 = __assign({}, (params || {}));
|
|
243
258
|
if (paramsWithoutFilters_2.filters !== undefined) {
|
|
244
259
|
delete paramsWithoutFilters_2.filters;
|
|
245
260
|
}
|
|
246
|
-
|
|
261
|
+
otherParams_2 = Object.keys(paramsWithoutFilters_2).reduce(function (acc, key) {
|
|
247
262
|
acc[key] = String(paramsWithoutFilters_2[key]);
|
|
248
263
|
return acc;
|
|
249
264
|
}, {});
|
|
265
|
+
queryObj_2 = __assign({ pageLimit: fallBackPageLimit, currentPage: String(requestedCurrentPage) }, otherParams_2);
|
|
266
|
+
searchParams_2 = new URLSearchParams(queryObj_2);
|
|
267
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
268
|
+
url: "".concat(apiUrl, "?").concat(searchParams_2.toString()),
|
|
269
|
+
method: constants_1.API_METHODS.GET,
|
|
270
|
+
headers: headers,
|
|
271
|
+
})];
|
|
272
|
+
case 4:
|
|
273
|
+
response_2 = _v.sent();
|
|
274
|
+
itemsRaw_1 = (responseKey
|
|
275
|
+
? (_l = response_2 === null || response_2 === void 0 ? void 0 : response_2.data) === null || _l === void 0 ? void 0 : _l[responseKey]
|
|
276
|
+
: (_m = response_2 === null || response_2 === void 0 ? void 0 : response_2.data) === null || _m === void 0 ? void 0 : _m.items) || [];
|
|
277
|
+
fetchedItems = Array.isArray(itemsRaw_1)
|
|
278
|
+
? itemsRaw_1
|
|
279
|
+
: [itemsRaw_1];
|
|
280
|
+
respCount = (_o = response_2 === null || response_2 === void 0 ? void 0 : response_2.data) === null || _o === void 0 ? void 0 : _o.count;
|
|
281
|
+
respCurrentPageRaw_2 = (_p = response_2 === null || response_2 === void 0 ? void 0 : response_2.data) === null || _p === void 0 ? void 0 : _p.currentPage;
|
|
282
|
+
respCurrentPage_2 = respCurrentPageRaw_2 == null ? undefined : Number(respCurrentPageRaw_2);
|
|
283
|
+
newItemsMap = fetchedItems.reduce(function (acc, item, idx) {
|
|
284
|
+
var _a, _b;
|
|
285
|
+
var obj = item;
|
|
286
|
+
var keyCandidate = (_b = (_a = obj === null || obj === void 0 ? void 0 : obj.id) !== null && _a !== void 0 ? _a : obj === null || obj === void 0 ? void 0 : obj.uid) !== null && _b !== void 0 ? _b : idx;
|
|
287
|
+
var key = String(keyCandidate);
|
|
288
|
+
acc[key] = item;
|
|
289
|
+
return acc;
|
|
290
|
+
}, {});
|
|
291
|
+
mergedItemsMap = Object.assign({}, raw.items || {}, newItemsMap);
|
|
292
|
+
mergedItemsArray = Object.values(mergedItemsMap);
|
|
293
|
+
newCount = respCount !== null && respCount !== void 0 ? respCount : mergedItemsArray.length;
|
|
294
|
+
newCurrentPage = respCurrentPage_2 !== null && respCurrentPage_2 !== void 0 ? respCurrentPage_2 : requestedCurrentPage;
|
|
295
|
+
shouldCache_1 = Array.isArray(itemsRaw_1) && respCount && fetchedItems.length > 0;
|
|
296
|
+
if (shouldCache_1) {
|
|
297
|
+
updatedCache = {
|
|
298
|
+
items: mergedItemsMap,
|
|
299
|
+
cachedAt: new Date().toISOString(),
|
|
300
|
+
count: newCount,
|
|
301
|
+
currentPage: newCurrentPage,
|
|
302
|
+
};
|
|
303
|
+
(0, local_storage_1.setStorageValue)(cacheKey, updatedCache);
|
|
304
|
+
}
|
|
305
|
+
return [2 /*return*/, {
|
|
306
|
+
count: newCount,
|
|
307
|
+
items: mergedItemsArray,
|
|
308
|
+
currentPage: newCurrentPage,
|
|
309
|
+
}];
|
|
310
|
+
case 5: return [2 /*return*/, {
|
|
311
|
+
count: storedCount !== null && storedCount !== void 0 ? storedCount : itemsArray.length,
|
|
312
|
+
items: itemsArray,
|
|
313
|
+
currentPage: (_q = raw.currentPage) !== null && _q !== void 0 ? _q : undefined,
|
|
314
|
+
}];
|
|
315
|
+
case 6:
|
|
316
|
+
paramsWithoutFilters_3 = __assign({}, (params || {}));
|
|
317
|
+
if (paramsWithoutFilters_3.filters !== undefined) {
|
|
318
|
+
delete paramsWithoutFilters_3.filters;
|
|
319
|
+
}
|
|
320
|
+
otherParams = Object.keys(paramsWithoutFilters_3).reduce(function (acc, key) {
|
|
321
|
+
acc[key] = String(paramsWithoutFilters_3[key]);
|
|
322
|
+
return acc;
|
|
323
|
+
}, {});
|
|
250
324
|
queryObj = __assign({ pageLimit: fallBackPageLimit, currentPage: fallBackCurrentPage }, otherParams);
|
|
251
325
|
searchParams = new URLSearchParams(queryObj);
|
|
252
326
|
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
@@ -254,14 +328,19 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
254
328
|
method: constants_1.API_METHODS.GET,
|
|
255
329
|
headers: headers,
|
|
256
330
|
})];
|
|
257
|
-
case
|
|
258
|
-
response =
|
|
259
|
-
itemsRaw = (responseKey ? (
|
|
331
|
+
case 7:
|
|
332
|
+
response = _v.sent();
|
|
333
|
+
itemsRaw = (responseKey ? (_r = response === null || response === void 0 ? void 0 : response.data) === null || _r === void 0 ? void 0 : _r[responseKey] : (_s = response === null || response === void 0 ? void 0 : response.data) === null || _s === void 0 ? void 0 : _s.items) ||
|
|
260
334
|
[];
|
|
261
335
|
items = Array.isArray(itemsRaw)
|
|
262
336
|
? itemsRaw
|
|
263
337
|
: [itemsRaw];
|
|
264
|
-
count = (
|
|
338
|
+
count = (_t = response === null || response === void 0 ? void 0 : response.data) === null || _t === void 0 ? void 0 : _t.count;
|
|
339
|
+
respCurrentPageRaw = (_u = response === null || response === void 0 ? void 0 : response.data) === null || _u === void 0 ? void 0 : _u.currentPage;
|
|
340
|
+
respCurrentPage = respCurrentPageRaw == null ? undefined : Number(respCurrentPageRaw);
|
|
341
|
+
requestCurrentPage = (params === null || params === void 0 ? void 0 : params.currentPage)
|
|
342
|
+
? Number(params.currentPage)
|
|
343
|
+
: undefined;
|
|
265
344
|
shouldCache = Array.isArray(itemsRaw) && count && items.length > 0;
|
|
266
345
|
if (shouldCache) {
|
|
267
346
|
itemsMap = items.reduce(function (acc, item, idx) {
|
|
@@ -276,6 +355,7 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
276
355
|
items: itemsMap,
|
|
277
356
|
cachedAt: new Date().toISOString(),
|
|
278
357
|
count: count,
|
|
358
|
+
currentPage: respCurrentPage !== null && respCurrentPage !== void 0 ? respCurrentPage : requestCurrentPage,
|
|
279
359
|
};
|
|
280
360
|
(0, local_storage_1.setStorageValue)(cacheKey, updatedCache);
|
|
281
361
|
}
|
|
@@ -283,12 +363,13 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
283
363
|
return [2 /*return*/, {
|
|
284
364
|
count: count,
|
|
285
365
|
items: items,
|
|
366
|
+
currentPage: respCurrentPage !== null && respCurrentPage !== void 0 ? respCurrentPage : requestCurrentPage,
|
|
286
367
|
}];
|
|
287
|
-
case
|
|
288
|
-
error_1 =
|
|
368
|
+
case 8:
|
|
369
|
+
error_1 = _v.sent();
|
|
289
370
|
console.error("Error fetching data for ".concat(cacheKey, ":"), error_1);
|
|
290
371
|
return [2 /*return*/, { count: 0, items: [] }];
|
|
291
|
-
case
|
|
372
|
+
case 9: return [2 /*return*/];
|
|
292
373
|
}
|
|
293
374
|
});
|
|
294
375
|
}); };
|