@react-pakistan/util-functions 1.25.24 → 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, 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,7 +204,7 @@ 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) || [];
@@ -219,7 +219,11 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
219
219
  cachedTime = new Date(raw.cachedAt).getTime();
220
220
  ageInMs = currentTime - cachedTime;
221
221
  if (ageInMs < expirationMs) {
222
- 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
+ }];
223
227
  }
224
228
  }
225
229
  if (isCachedMapShape(raw)) {
@@ -227,7 +231,12 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
227
231
  ageInMs = currentTime - cachedTime;
228
232
  if (ageInMs < expirationMs) {
229
233
  itemsArray = Object.values(raw.items || {});
230
- 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
+ }];
231
240
  }
232
241
  }
233
242
  paramsWithoutFilters_2 = __assign({}, (params || {}));
@@ -246,13 +255,13 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
246
255
  headers: headers,
247
256
  })];
248
257
  case 4:
249
- response = _m.sent();
250
- 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) ||
251
260
  [];
252
261
  items = Array.isArray(itemsRaw)
253
262
  ? itemsRaw
254
263
  : [itemsRaw];
255
- 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;
256
265
  shouldCache = Array.isArray(itemsRaw) && count && items.length > 0;
257
266
  if (shouldCache) {
258
267
  itemsMap = items.reduce(function (acc, item, idx) {
@@ -276,7 +285,7 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
276
285
  items: items,
277
286
  }];
278
287
  case 5:
279
- error_1 = _m.sent();
288
+ error_1 = _p.sent();
280
289
  console.error("Error fetching data for ".concat(cacheKey, ":"), error_1);
281
290
  return [2 /*return*/, { count: 0, items: [] }];
282
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.24",
3
+ "version": "1.25.26",
4
4
  "description": "A library of all util functions",
5
5
  "main": "index.js",
6
6
  "scripts": {