@react-pakistan/util-functions 1.25.13 → 1.25.16
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.d.ts +30 -16
- package/general/generic-cache.js +172 -120
- package/general/get-available-page-limit.js +1 -1
- package/package.json +1 -1
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generic Cache Utilities
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* -
|
|
7
|
-
* - Search and filter bypass
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
4
|
+
* A lightweight localStorage-backed cache used by module-level cache wrappers.
|
|
5
|
+
* Key behaviors:
|
|
6
|
+
* - Default expiration: 1 day (configurable per cache)
|
|
7
|
+
* - Search and filter requests bypass the cache and always fetch fresh data
|
|
8
|
+
* - Lists are stored as a normalized map (id -> item) for fast lookups
|
|
9
|
+
* - Provides both synchronous reads (`getCachedDataSync`) and async reads
|
|
10
|
+
* with API fallback (`getCachedData`)
|
|
11
|
+
* - Single-item helpers available (`getCachedSingleItem`, `getCachedSingleItemSync`)
|
|
12
|
+
*
|
|
13
|
+
* The main async helper `getCachedData` now accepts a single options object
|
|
14
|
+
* (`GetCachedDataOptions`) which includes `config`, optional `searchQuery`,
|
|
15
|
+
* `filters`, `pageLimit` and extra `params` for the list API. Module cache
|
|
16
|
+
* wrappers should call `getCachedData({ config, searchQuery, filters, pageLimit, params })`.
|
|
10
17
|
*
|
|
11
18
|
* Organization:
|
|
12
19
|
* - Types
|
|
@@ -19,6 +26,11 @@ interface CacheConfig {
|
|
|
19
26
|
apiUrl: string;
|
|
20
27
|
expirationMs?: number;
|
|
21
28
|
responseKey?: string;
|
|
29
|
+
params?: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
interface GetCachedDataOptions {
|
|
32
|
+
config: CacheConfig;
|
|
33
|
+
params?: Record<string, unknown>;
|
|
22
34
|
}
|
|
23
35
|
interface ListResponse<T> {
|
|
24
36
|
count: number;
|
|
@@ -41,25 +53,27 @@ interface ListResponse<T> {
|
|
|
41
53
|
export declare const getCachedDataSync: <T>(cacheKey: string, expirationMs?: number) => ListResponse<T>;
|
|
42
54
|
/**
|
|
43
55
|
* Get cached data with API fallback for a module
|
|
44
|
-
* Automatically handles cache validation and updates
|
|
45
56
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
57
|
+
* Accepts a single `GetCachedDataOptions` object. The function will return
|
|
58
|
+
* cached results when fresh, or fetch from the API when the cache is stale
|
|
59
|
+
* or when `searchQuery`/`filters` are present (those always bypass cache).
|
|
60
|
+
*
|
|
61
|
+
* @param opts - `GetCachedDataOptions` containing `config`, optional
|
|
62
|
+
* `searchQuery`, `filters`, `pageLimit`, and extra `params`
|
|
50
63
|
* @returns Promise<{count: number, items: T[]}> - Cached or fresh data
|
|
51
64
|
*
|
|
52
65
|
* @example
|
|
53
66
|
* const config = { cacheKey: LS_KEYS.USERS, apiUrl: USER_API_ROUTES.LIST, responseKey: 'users' };
|
|
54
|
-
*
|
|
67
|
+
* // Basic usage (reads cache if fresh)
|
|
68
|
+
* const users = await getCachedData<UserBE>({ config });
|
|
55
69
|
*
|
|
56
70
|
* // With search (bypasses cache)
|
|
57
|
-
* const filtered = await getCachedData<UserBE>(config, 'John');
|
|
71
|
+
* const filtered = await getCachedData<UserBE>({ config, searchQuery: 'John' });
|
|
58
72
|
*
|
|
59
|
-
* // With filters
|
|
60
|
-
* const active = await getCachedData<UserBE>(config,
|
|
73
|
+
* // With filters and custom page limit
|
|
74
|
+
* const active = await getCachedData<UserBE>({ config, filters: { enabled: true }, pageLimit: 200 });
|
|
61
75
|
*/
|
|
62
|
-
export declare const getCachedData: <T>(
|
|
76
|
+
export declare const getCachedData: <T>(opts: GetCachedDataOptions) => Promise<ListResponse<T>>;
|
|
63
77
|
/**
|
|
64
78
|
* Get single cached item by ID from a list cache
|
|
65
79
|
*
|
package/general/generic-cache.js
CHANGED
|
@@ -2,12 +2,19 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Generic Cache Utilities
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* -
|
|
8
|
-
* - Search and filter bypass
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
5
|
+
* A lightweight localStorage-backed cache used by module-level cache wrappers.
|
|
6
|
+
* Key behaviors:
|
|
7
|
+
* - Default expiration: 1 day (configurable per cache)
|
|
8
|
+
* - Search and filter requests bypass the cache and always fetch fresh data
|
|
9
|
+
* - Lists are stored as a normalized map (id -> item) for fast lookups
|
|
10
|
+
* - Provides both synchronous reads (`getCachedDataSync`) and async reads
|
|
11
|
+
* with API fallback (`getCachedData`)
|
|
12
|
+
* - Single-item helpers available (`getCachedSingleItem`, `getCachedSingleItemSync`)
|
|
13
|
+
*
|
|
14
|
+
* The main async helper `getCachedData` now accepts a single options object
|
|
15
|
+
* (`GetCachedDataOptions`) which includes `config`, optional `searchQuery`,
|
|
16
|
+
* `filters`, `pageLimit` and extra `params` for the list API. Module cache
|
|
17
|
+
* wrappers should call `getCachedData({ config, searchQuery, filters, pageLimit, params })`.
|
|
11
18
|
*
|
|
12
19
|
* Organization:
|
|
13
20
|
* - Types
|
|
@@ -62,20 +69,27 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
62
69
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
63
70
|
}
|
|
64
71
|
};
|
|
65
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
72
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
75
73
|
exports.isCacheStale = exports.preloadCache = exports.invalidateCache = exports.getCachedSingleItem = exports.getCachedSingleItemSync = exports.getCachedItemById = exports.getCachedData = exports.getCachedDataSync = void 0;
|
|
76
74
|
var constants_1 = require("../constants");
|
|
77
75
|
var fetch_data_1 = require("./fetch-data");
|
|
78
76
|
var local_storage_1 = require("../local-storage");
|
|
77
|
+
// Type guards for cached shapes
|
|
78
|
+
function isCachedArrayShape(v) {
|
|
79
|
+
if (!v || typeof v !== 'object')
|
|
80
|
+
return false;
|
|
81
|
+
var o = v;
|
|
82
|
+
return Array.isArray(o.items) && typeof o.cachedAt === 'string';
|
|
83
|
+
}
|
|
84
|
+
function isCachedMapShape(v) {
|
|
85
|
+
if (!v || typeof v !== 'object')
|
|
86
|
+
return false;
|
|
87
|
+
var o = v;
|
|
88
|
+
return (o.items !== undefined &&
|
|
89
|
+
typeof o.items === 'object' &&
|
|
90
|
+
!Array.isArray(o.items) &&
|
|
91
|
+
typeof o.cachedAt === 'string');
|
|
92
|
+
}
|
|
79
93
|
// ============================================================================
|
|
80
94
|
// LIST CACHE FUNCTIONS
|
|
81
95
|
// ============================================================================
|
|
@@ -96,18 +110,26 @@ var local_storage_1 = require("../local-storage");
|
|
|
96
110
|
var getCachedDataSync = function (cacheKey, expirationMs) {
|
|
97
111
|
if (expirationMs === void 0) { expirationMs = constants_1.ONE_DAY_IN_MS; }
|
|
98
112
|
try {
|
|
99
|
-
var
|
|
100
|
-
if (
|
|
113
|
+
var raw = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
114
|
+
if (raw == null)
|
|
101
115
|
return { count: 0, items: [] };
|
|
102
|
-
}
|
|
103
116
|
var currentTime = new Date().getTime();
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
count:
|
|
109
|
-
|
|
110
|
-
};
|
|
117
|
+
if (isCachedArrayShape(raw)) {
|
|
118
|
+
var cachedTime = new Date(raw.cachedAt).getTime();
|
|
119
|
+
var ageInMs = currentTime - cachedTime;
|
|
120
|
+
if (ageInMs < expirationMs) {
|
|
121
|
+
return { count: raw.items.length, items: raw.items };
|
|
122
|
+
}
|
|
123
|
+
return { count: 0, items: [] };
|
|
124
|
+
}
|
|
125
|
+
if (isCachedMapShape(raw)) {
|
|
126
|
+
var cachedTime = new Date(raw.cachedAt).getTime();
|
|
127
|
+
var ageInMs = currentTime - cachedTime;
|
|
128
|
+
if (ageInMs < expirationMs) {
|
|
129
|
+
var itemsArray = Object.values(raw.items || {});
|
|
130
|
+
return { count: itemsArray.length, items: itemsArray };
|
|
131
|
+
}
|
|
132
|
+
return { count: 0, items: [] };
|
|
111
133
|
}
|
|
112
134
|
return { count: 0, items: [] };
|
|
113
135
|
}
|
|
@@ -119,104 +141,121 @@ var getCachedDataSync = function (cacheKey, expirationMs) {
|
|
|
119
141
|
exports.getCachedDataSync = getCachedDataSync;
|
|
120
142
|
/**
|
|
121
143
|
* Get cached data with API fallback for a module
|
|
122
|
-
* Automatically handles cache validation and updates
|
|
123
144
|
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
145
|
+
* Accepts a single `GetCachedDataOptions` object. The function will return
|
|
146
|
+
* cached results when fresh, or fetch from the API when the cache is stale
|
|
147
|
+
* or when `searchQuery`/`filters` are present (those always bypass cache).
|
|
148
|
+
*
|
|
149
|
+
* @param opts - `GetCachedDataOptions` containing `config`, optional
|
|
150
|
+
* `searchQuery`, `filters`, `pageLimit`, and extra `params`
|
|
128
151
|
* @returns Promise<{count: number, items: T[]}> - Cached or fresh data
|
|
129
152
|
*
|
|
130
153
|
* @example
|
|
131
154
|
* const config = { cacheKey: LS_KEYS.USERS, apiUrl: USER_API_ROUTES.LIST, responseKey: 'users' };
|
|
132
|
-
*
|
|
155
|
+
* // Basic usage (reads cache if fresh)
|
|
156
|
+
* const users = await getCachedData<UserBE>({ config });
|
|
133
157
|
*
|
|
134
158
|
* // With search (bypasses cache)
|
|
135
|
-
* const filtered = await getCachedData<UserBE>(config, 'John');
|
|
159
|
+
* const filtered = await getCachedData<UserBE>({ config, searchQuery: 'John' });
|
|
136
160
|
*
|
|
137
|
-
* // With filters
|
|
138
|
-
* const active = await getCachedData<UserBE>(config,
|
|
161
|
+
* // With filters and custom page limit
|
|
162
|
+
* const active = await getCachedData<UserBE>({ config, filters: { enabled: true }, pageLimit: 200 });
|
|
139
163
|
*/
|
|
140
|
-
var getCachedData = function (
|
|
141
|
-
var
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}];
|
|
187
|
-
}
|
|
164
|
+
var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
|
|
165
|
+
var config, params, cacheKey, apiUrl, responseKey, expirationMs, fallBackPageLimit, fallBackCurrentPage, filters, searchQuery, queryObj_1, searchParams_1, response_1, items_1, raw, currentTime, cachedTime, ageInMs, cachedTime, ageInMs, itemsArray, queryObj, searchParams, response, itemsRaw, items, count, itemsMap, updatedCache, error_1;
|
|
166
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
167
|
+
return __generator(this, function (_m) {
|
|
168
|
+
switch (_m.label) {
|
|
169
|
+
case 0:
|
|
170
|
+
config = opts.config, params = opts.params;
|
|
171
|
+
cacheKey = config.cacheKey, apiUrl = config.apiUrl, responseKey = config.responseKey;
|
|
172
|
+
expirationMs = (_a = config.expirationMs) !== null && _a !== void 0 ? _a : constants_1.ONE_DAY_IN_MS;
|
|
173
|
+
fallBackPageLimit = (_c = (_b = params === null || params === void 0 ? void 0 : params.pageLimit) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : '100';
|
|
174
|
+
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';
|
|
175
|
+
filters = params === null || params === void 0 ? void 0 : params.filters;
|
|
176
|
+
searchQuery = params === null || params === void 0 ? void 0 : params.searchQuery;
|
|
177
|
+
_m.label = 1;
|
|
178
|
+
case 1:
|
|
179
|
+
_m.trys.push([1, 5, , 6]);
|
|
180
|
+
if (!((searchQuery && searchQuery.trim()) ||
|
|
181
|
+
(filters && Object.keys(filters).length > 0))) return [3 /*break*/, 3];
|
|
182
|
+
queryObj_1 = __assign(__assign(__assign({ pageLimit: fallBackPageLimit, currentPage: fallBackCurrentPage }, (searchQuery && { searchQuery: searchQuery })), (filters
|
|
183
|
+
? Object.keys(filters).reduce(function (acc, key) {
|
|
184
|
+
acc[key] = String(filters[key]);
|
|
185
|
+
return acc;
|
|
186
|
+
}, {})
|
|
187
|
+
: {})), (params || {}));
|
|
188
|
+
searchParams_1 = new URLSearchParams(queryObj_1);
|
|
189
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
190
|
+
url: "".concat(apiUrl, "?").concat(searchParams_1.toString()),
|
|
191
|
+
method: constants_1.API_METHODS.GET,
|
|
192
|
+
})];
|
|
193
|
+
case 2:
|
|
194
|
+
response_1 = _m.sent();
|
|
195
|
+
items_1 = responseKey
|
|
196
|
+
? ((_f = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _f === void 0 ? void 0 : _f[responseKey]) || []
|
|
197
|
+
: ((_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) || [];
|
|
198
|
+
return [2 /*return*/, {
|
|
199
|
+
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,
|
|
200
|
+
items: items_1,
|
|
201
|
+
}];
|
|
202
|
+
case 3:
|
|
203
|
+
raw = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
204
|
+
currentTime = new Date().getTime();
|
|
205
|
+
if (isCachedArrayShape(raw)) {
|
|
206
|
+
cachedTime = new Date(raw.cachedAt).getTime();
|
|
207
|
+
ageInMs = currentTime - cachedTime;
|
|
208
|
+
if (ageInMs < expirationMs) {
|
|
209
|
+
return [2 /*return*/, { count: raw.items.length, items: raw.items }];
|
|
188
210
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
})];
|
|
197
|
-
case 4:
|
|
198
|
-
response = _h.sent();
|
|
199
|
-
items = responseKey
|
|
200
|
-
? ((_e = response === null || response === void 0 ? void 0 : response.data) === null || _e === void 0 ? void 0 : _e[responseKey]) || []
|
|
201
|
-
: ((_f = response === null || response === void 0 ? void 0 : response.data) === null || _f === void 0 ? void 0 : _f.items) || (response === null || response === void 0 ? void 0 : response.data) || [];
|
|
202
|
-
count = ((_g = response === null || response === void 0 ? void 0 : response.data) === null || _g === void 0 ? void 0 : _g.count) || items.length;
|
|
203
|
-
if (items.length > 0) {
|
|
204
|
-
updatedCache = {
|
|
205
|
-
items: items,
|
|
206
|
-
cachedAt: new Date().toISOString(),
|
|
207
|
-
};
|
|
208
|
-
(0, local_storage_1.setStorageValue)(cacheKey, updatedCache);
|
|
211
|
+
}
|
|
212
|
+
if (isCachedMapShape(raw)) {
|
|
213
|
+
cachedTime = new Date(raw.cachedAt).getTime();
|
|
214
|
+
ageInMs = currentTime - cachedTime;
|
|
215
|
+
if (ageInMs < expirationMs) {
|
|
216
|
+
itemsArray = Object.values(raw.items || {});
|
|
217
|
+
return [2 /*return*/, { count: itemsArray.length, items: itemsArray }];
|
|
209
218
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
219
|
+
}
|
|
220
|
+
queryObj = __assign({ pageLimit: fallBackPageLimit, currentPage: fallBackCurrentPage }, (params || {}));
|
|
221
|
+
searchParams = new URLSearchParams(queryObj);
|
|
222
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
223
|
+
url: "".concat(apiUrl, "?").concat(searchParams.toString()),
|
|
224
|
+
method: constants_1.API_METHODS.GET,
|
|
225
|
+
})];
|
|
226
|
+
case 4:
|
|
227
|
+
response = _m.sent();
|
|
228
|
+
itemsRaw = (responseKey
|
|
229
|
+
? (_j = response === null || response === void 0 ? void 0 : response.data) === null || _j === void 0 ? void 0 : _j[responseKey]
|
|
230
|
+
: ((_k = response === null || response === void 0 ? void 0 : response.data) === null || _k === void 0 ? void 0 : _k.items) || (response === null || response === void 0 ? void 0 : response.data)) || [];
|
|
231
|
+
items = Array.isArray(itemsRaw)
|
|
232
|
+
? itemsRaw
|
|
233
|
+
: [itemsRaw];
|
|
234
|
+
count = ((_l = response === null || response === void 0 ? void 0 : response.data) === null || _l === void 0 ? void 0 : _l.count) || items.length;
|
|
235
|
+
if (items.length > 0) {
|
|
236
|
+
itemsMap = items.reduce(function (acc, item, idx) {
|
|
237
|
+
var _a, _b;
|
|
238
|
+
var obj = item;
|
|
239
|
+
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;
|
|
240
|
+
var key = String(keyCandidate);
|
|
241
|
+
acc[key] = item;
|
|
242
|
+
return acc;
|
|
243
|
+
}, {});
|
|
244
|
+
updatedCache = {
|
|
245
|
+
items: itemsMap,
|
|
246
|
+
cachedAt: new Date().toISOString(),
|
|
247
|
+
};
|
|
248
|
+
(0, local_storage_1.setStorageValue)(cacheKey, updatedCache);
|
|
249
|
+
}
|
|
250
|
+
return [2 /*return*/, { count: count, items: items }];
|
|
251
|
+
case 5:
|
|
252
|
+
error_1 = _m.sent();
|
|
253
|
+
console.error("Error fetching data for ".concat(cacheKey, ":"), error_1);
|
|
254
|
+
return [2 /*return*/, { count: 0, items: [] }];
|
|
255
|
+
case 6: return [2 /*return*/];
|
|
256
|
+
}
|
|
218
257
|
});
|
|
219
|
-
};
|
|
258
|
+
}); };
|
|
220
259
|
exports.getCachedData = getCachedData;
|
|
221
260
|
/**
|
|
222
261
|
* Get single cached item by ID from a list cache
|
|
@@ -236,6 +275,15 @@ var getCachedItemById = function (cacheKey, itemId) {
|
|
|
236
275
|
return null;
|
|
237
276
|
}
|
|
238
277
|
try {
|
|
278
|
+
// Try to read normalized cache first for O(1) lookup
|
|
279
|
+
var raw = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
280
|
+
if (isCachedArrayShape(raw)) {
|
|
281
|
+
return raw.items.find(function (i) { return i.id === itemId; }) || null;
|
|
282
|
+
}
|
|
283
|
+
if (isCachedMapShape(raw)) {
|
|
284
|
+
return (raw.items && raw.items[itemId]) || null;
|
|
285
|
+
}
|
|
286
|
+
// Fallback to sync reader
|
|
239
287
|
var items = (0, exports.getCachedDataSync)(cacheKey).items;
|
|
240
288
|
return items.find(function (item) { return item.id === itemId; }) || null;
|
|
241
289
|
}
|
|
@@ -296,11 +344,13 @@ exports.getCachedSingleItemSync = getCachedSingleItemSync;
|
|
|
296
344
|
* const workspace = await getCachedSingleItem<WorkspaceBE>(config, { subdomain: 'school1' });
|
|
297
345
|
*/
|
|
298
346
|
var getCachedSingleItem = function (config, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
299
|
-
var cacheKey, apiUrl,
|
|
347
|
+
var cacheKey, apiUrl, expirationMs, cachedData, currentTime, cachedTime, ageInMs, queryParams, response, item, updatedCache, error_2;
|
|
348
|
+
var _a;
|
|
300
349
|
return __generator(this, function (_b) {
|
|
301
350
|
switch (_b.label) {
|
|
302
351
|
case 0:
|
|
303
|
-
cacheKey = config.cacheKey, apiUrl = config.apiUrl
|
|
352
|
+
cacheKey = config.cacheKey, apiUrl = config.apiUrl;
|
|
353
|
+
expirationMs = (_a = config.expirationMs) !== null && _a !== void 0 ? _a : constants_1.ONE_DAY_IN_MS;
|
|
304
354
|
_b.label = 1;
|
|
305
355
|
case 1:
|
|
306
356
|
_b.trys.push([1, 3, , 4]);
|
|
@@ -375,7 +425,7 @@ exports.invalidateCache = invalidateCache;
|
|
|
375
425
|
*/
|
|
376
426
|
var preloadCache = function (config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
377
427
|
return __generator(this, function (_a) {
|
|
378
|
-
return [2 /*return*/, (0, exports.getCachedData)(config)];
|
|
428
|
+
return [2 /*return*/, (0, exports.getCachedData)({ config: config })];
|
|
379
429
|
});
|
|
380
430
|
}); };
|
|
381
431
|
exports.preloadCache = preloadCache;
|
|
@@ -394,14 +444,16 @@ exports.preloadCache = preloadCache;
|
|
|
394
444
|
var isCacheStale = function (cacheKey, expirationMs) {
|
|
395
445
|
if (expirationMs === void 0) { expirationMs = constants_1.ONE_DAY_IN_MS; }
|
|
396
446
|
try {
|
|
397
|
-
var
|
|
398
|
-
if (
|
|
447
|
+
var raw = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
448
|
+
if (raw == null)
|
|
399
449
|
return true;
|
|
450
|
+
if (isCachedArrayShape(raw) || isCachedMapShape(raw)) {
|
|
451
|
+
var cachedTime = new Date(raw.cachedAt).getTime();
|
|
452
|
+
var currentTime = new Date().getTime();
|
|
453
|
+
var ageInMs = currentTime - cachedTime;
|
|
454
|
+
return ageInMs >= expirationMs;
|
|
400
455
|
}
|
|
401
|
-
|
|
402
|
-
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
403
|
-
var ageInMs = currentTime - cachedTime;
|
|
404
|
-
return ageInMs >= expirationMs;
|
|
456
|
+
return true;
|
|
405
457
|
}
|
|
406
458
|
catch (error) {
|
|
407
459
|
console.error("Error checking cache staleness for ".concat(cacheKey, ":"), error);
|
|
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.getAvailablePageLimits = void 0;
|
|
13
13
|
var dynamic_page_limit_1 = require("./dynamic-page-limit");
|
|
14
14
|
var getAvailablePageLimits = function (totalRecords) {
|
|
15
|
-
var allOptions = [
|
|
15
|
+
var allOptions = [10, 15, 25, 100];
|
|
16
16
|
// If totalRecords is less than the smallest option, return only the smallest option
|
|
17
17
|
if (totalRecords < allOptions[0]) {
|
|
18
18
|
return (0, dynamic_page_limit_1.dynamicPageLimit)([allOptions[0]]);
|