@react-pakistan/util-functions 1.25.25 → 1.25.26

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.
@@ -38,6 +38,7 @@ interface GetCachedDataOptions {
38
38
  interface ListResponse<T> {
39
39
  count: number;
40
40
  items: T[];
41
+ currentPage?: number;
41
42
  }
42
43
  /**
43
44
  * Get synchronous cached data for a module
@@ -166,10 +166,10 @@ exports.getCachedDataSync = getCachedDataSync;
166
166
  * const active = await getCachedData<UserBE>({ config, filters: { enabled: true }, pageLimit: 200 });
167
167
  */
168
168
  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, lastCacheVersion, raw, currentTime, cachedTime, ageInMs, cachedTime, ageInMs, itemsArray, 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;
171
- return __generator(this, function (_m) {
172
- switch (_m.label) {
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 (_p) {
172
+ switch (_p.label) {
173
173
  case 0:
174
174
  config = opts.config, params = opts.params, headers = opts.headers;
175
175
  cacheKey = config.cacheKey, apiUrl = config.apiUrl, responseKey = config.responseKey;
@@ -178,9 +178,9 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
178
178
  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
179
  filters = params === null || params === void 0 ? void 0 : params.filters;
180
180
  searchQuery = params === null || params === void 0 ? void 0 : params.searchQuery;
181
- _m.label = 1;
181
+ _p.label = 1;
182
182
  case 1:
183
- _m.trys.push([1, 5, , 6]);
183
+ _p.trys.push([1, 5, , 6]);
184
184
  if (!((searchQuery && searchQuery.trim()) ||
185
185
  (filters && Object.keys(filters).length > 0))) return [3 /*break*/, 3];
186
186
  paramsWithoutFilters_1 = __assign({}, (params || {}));
@@ -204,13 +204,12 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
204
204
  headers: headers,
205
205
  })];
206
206
  case 2:
207
- response_1 = _m.sent();
207
+ response_1 = _p.sent();
208
208
  items_1 = responseKey
209
209
  ? ((_f = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _f === void 0 ? void 0 : _f[responseKey]) || []
210
210
  : ((_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) || [];
211
- lastCacheVersion = (0, local_storage_1.getStorageValue)(cacheKey);
212
211
  return [2 /*return*/, {
213
- count: ((_h = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _h === void 0 ? void 0 : _h.count) || (lastCacheVersion === null || lastCacheVersion === void 0 ? void 0 : lastCacheVersion.count),
212
+ count: ((_h = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _h === void 0 ? void 0 : _h.count) || items_1.length,
214
213
  items: items_1,
215
214
  }];
216
215
  case 3:
@@ -220,7 +219,11 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
220
219
  cachedTime = new Date(raw.cachedAt).getTime();
221
220
  ageInMs = currentTime - cachedTime;
222
221
  if (ageInMs < expirationMs) {
223
- return [2 /*return*/, { count: raw.items.length, items: raw.items }];
222
+ return [2 /*return*/, {
223
+ count: raw.count,
224
+ items: raw.items,
225
+ currentPage: (_j = raw.currentPage) !== null && _j !== void 0 ? _j : undefined,
226
+ }];
224
227
  }
225
228
  }
226
229
  if (isCachedMapShape(raw)) {
@@ -228,7 +231,12 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
228
231
  ageInMs = currentTime - cachedTime;
229
232
  if (ageInMs < expirationMs) {
230
233
  itemsArray = Object.values(raw.items || {});
231
- return [2 /*return*/, { count: itemsArray.length, items: itemsArray }];
234
+ storedCount = raw.count;
235
+ return [2 /*return*/, {
236
+ count: storedCount !== null && storedCount !== void 0 ? storedCount : itemsArray.length,
237
+ items: itemsArray,
238
+ currentPage: (_k = raw.currentPage) !== null && _k !== void 0 ? _k : undefined,
239
+ }];
232
240
  }
233
241
  }
234
242
  paramsWithoutFilters_2 = __assign({}, (params || {}));
@@ -247,13 +255,13 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
247
255
  headers: headers,
248
256
  })];
249
257
  case 4:
250
- response = _m.sent();
251
- itemsRaw = (responseKey ? (_j = response === null || response === void 0 ? void 0 : response.data) === null || _j === void 0 ? void 0 : _j[responseKey] : (_k = response === null || response === void 0 ? void 0 : response.data) === null || _k === void 0 ? void 0 : _k.items) ||
258
+ response = _p.sent();
259
+ itemsRaw = (responseKey ? (_l = response === null || response === void 0 ? void 0 : response.data) === null || _l === void 0 ? void 0 : _l[responseKey] : (_m = response === null || response === void 0 ? void 0 : response.data) === null || _m === void 0 ? void 0 : _m.items) ||
252
260
  [];
253
261
  items = Array.isArray(itemsRaw)
254
262
  ? itemsRaw
255
263
  : [itemsRaw];
256
- count = (_l = response === null || response === void 0 ? void 0 : response.data) === null || _l === void 0 ? void 0 : _l.count;
264
+ count = (_o = response === null || response === void 0 ? void 0 : response.data) === null || _o === void 0 ? void 0 : _o.count;
257
265
  shouldCache = Array.isArray(itemsRaw) && count && items.length > 0;
258
266
  if (shouldCache) {
259
267
  itemsMap = items.reduce(function (acc, item, idx) {
@@ -277,7 +285,7 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
277
285
  items: items,
278
286
  }];
279
287
  case 5:
280
- error_1 = _m.sent();
288
+ error_1 = _p.sent();
281
289
  console.error("Error fetching data for ".concat(cacheKey, ":"), error_1);
282
290
  return [2 /*return*/, { count: 0, items: [] }];
283
291
  case 6: return [2 /*return*/];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pakistan/util-functions",
3
- "version": "1.25.25",
3
+ "version": "1.25.26",
4
4
  "description": "A library of all util functions",
5
5
  "main": "index.js",
6
6
  "scripts": {