@react-pakistan/util-functions 1.25.18 → 1.25.20
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/fetch-data.d.ts
CHANGED
|
@@ -3,8 +3,9 @@ export interface FetchDataParams {
|
|
|
3
3
|
url: string;
|
|
4
4
|
method?: API_METHODS;
|
|
5
5
|
body?: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
6
7
|
}
|
|
7
|
-
export declare const fetchData: ({ url, method, body, }: FetchDataParams) => Promise<{
|
|
8
|
+
export declare const fetchData: ({ url, method, body, headers, }: FetchDataParams) => Promise<{
|
|
8
9
|
data: any | undefined;
|
|
9
10
|
error: Error | undefined;
|
|
10
11
|
}>;
|
package/general/fetch-data.js
CHANGED
|
@@ -51,12 +51,12 @@ exports.fetchData = void 0;
|
|
|
51
51
|
var api_methods_1 = require("../constants/api-methods");
|
|
52
52
|
var fetchData = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
53
53
|
var data, error, res, err_1;
|
|
54
|
-
var url = _b.url, _c = _b.method, method = _c === void 0 ? api_methods_1.API_METHODS.GET : _c, body = _b.body;
|
|
54
|
+
var url = _b.url, _c = _b.method, method = _c === void 0 ? api_methods_1.API_METHODS.GET : _c, body = _b.body, headers = _b.headers;
|
|
55
55
|
return __generator(this, function (_d) {
|
|
56
56
|
switch (_d.label) {
|
|
57
57
|
case 0:
|
|
58
58
|
_d.trys.push([0, 3, , 4]);
|
|
59
|
-
return [4 /*yield*/, fetch(url, __assign({ method: method }, (method !== api_methods_1.API_METHODS.GET && body && { body: body })))];
|
|
59
|
+
return [4 /*yield*/, fetch(url, __assign(__assign({ method: method }, (headers ? { headers: headers } : {})), (method !== api_methods_1.API_METHODS.GET && body && { body: body })))];
|
|
60
60
|
case 1:
|
|
61
61
|
res = _d.sent();
|
|
62
62
|
return [4 /*yield*/, res.json()];
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
* The main async helper `getCachedData` now accepts a single options object
|
|
14
14
|
* (`GetCachedDataOptions`) which includes `config`, optional `searchQuery`,
|
|
15
15
|
* `filters`, `pageLimit` and extra `params` for the list API. Module cache
|
|
16
|
-
* wrappers should call `getCachedData({ config, searchQuery, filters, pageLimit, params })`.
|
|
16
|
+
* wrappers should call `getCachedData({ config, searchQuery, filters, pageLimit, params, headers })`.
|
|
17
|
+
* Both `getCachedData` and `getCachedSingleItem` accept an optional `headers`
|
|
18
|
+
* object which will be forwarded to the underlying `fetchData` calls.
|
|
17
19
|
*
|
|
18
20
|
* Organization:
|
|
19
21
|
* - Types
|
|
@@ -31,6 +33,7 @@ interface CacheConfig {
|
|
|
31
33
|
interface GetCachedDataOptions {
|
|
32
34
|
config: CacheConfig;
|
|
33
35
|
params?: Record<string, unknown>;
|
|
36
|
+
headers?: Record<string, string>;
|
|
34
37
|
}
|
|
35
38
|
interface ListResponse<T> {
|
|
36
39
|
count: number;
|
|
@@ -117,7 +120,7 @@ export declare const getCachedSingleItemSync: <T>(cacheKey: string, expirationMs
|
|
|
117
120
|
* const config = { cacheKey: LS_KEYS.WORKSPACE, apiUrl: '/api/workspace', responseKey: 'workspace' };
|
|
118
121
|
* const workspace = await getCachedSingleItem<WorkspaceBE>(config, { subdomain: 'school1' });
|
|
119
122
|
*/
|
|
120
|
-
export declare const getCachedSingleItem: <T>(config: CacheConfig, params?: Record<string, string>) => Promise<T | null>;
|
|
123
|
+
export declare const getCachedSingleItem: <T>(config: CacheConfig, params?: Record<string, string>, headers?: Record<string, string>) => Promise<T | null>;
|
|
121
124
|
/**
|
|
122
125
|
* Invalidate (remove) cache for a module
|
|
123
126
|
* Useful after create/update/delete operations
|
package/general/generic-cache.js
CHANGED
|
@@ -14,7 +14,9 @@
|
|
|
14
14
|
* The main async helper `getCachedData` now accepts a single options object
|
|
15
15
|
* (`GetCachedDataOptions`) which includes `config`, optional `searchQuery`,
|
|
16
16
|
* `filters`, `pageLimit` and extra `params` for the list API. Module cache
|
|
17
|
-
* wrappers should call `getCachedData({ config, searchQuery, filters, pageLimit, params })`.
|
|
17
|
+
* wrappers should call `getCachedData({ config, searchQuery, filters, pageLimit, params, headers })`.
|
|
18
|
+
* Both `getCachedData` and `getCachedSingleItem` accept an optional `headers`
|
|
19
|
+
* object which will be forwarded to the underlying `fetchData` calls.
|
|
18
20
|
*
|
|
19
21
|
* Organization:
|
|
20
22
|
* - Types
|
|
@@ -79,7 +81,9 @@ function isCachedArrayShape(v) {
|
|
|
79
81
|
if (!v || typeof v !== 'object')
|
|
80
82
|
return false;
|
|
81
83
|
var o = v;
|
|
82
|
-
return Array.isArray(o.items) &&
|
|
84
|
+
return (Array.isArray(o.items) &&
|
|
85
|
+
typeof o.cachedAt === 'string' &&
|
|
86
|
+
typeof o.count === 'number');
|
|
83
87
|
}
|
|
84
88
|
function isCachedMapShape(v) {
|
|
85
89
|
if (!v || typeof v !== 'object')
|
|
@@ -162,12 +166,12 @@ exports.getCachedDataSync = getCachedDataSync;
|
|
|
162
166
|
* const active = await getCachedData<UserBE>({ config, filters: { enabled: true }, pageLimit: 200 });
|
|
163
167
|
*/
|
|
164
168
|
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, 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, itemsMap, updatedCache, error_1;
|
|
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;
|
|
166
170
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
167
171
|
return __generator(this, function (_m) {
|
|
168
172
|
switch (_m.label) {
|
|
169
173
|
case 0:
|
|
170
|
-
config = opts.config, params = opts.params;
|
|
174
|
+
config = opts.config, params = opts.params, headers = opts.headers;
|
|
171
175
|
cacheKey = config.cacheKey, apiUrl = config.apiUrl, responseKey = config.responseKey;
|
|
172
176
|
expirationMs = (_a = config.expirationMs) !== null && _a !== void 0 ? _a : constants_1.ONE_DAY_IN_MS;
|
|
173
177
|
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';
|
|
@@ -197,6 +201,7 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
197
201
|
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
198
202
|
url: "".concat(apiUrl, "?").concat(searchParams_1.toString()),
|
|
199
203
|
method: constants_1.API_METHODS.GET,
|
|
204
|
+
headers: headers,
|
|
200
205
|
})];
|
|
201
206
|
case 2:
|
|
202
207
|
response_1 = _m.sent();
|
|
@@ -238,17 +243,18 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
238
243
|
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
239
244
|
url: "".concat(apiUrl, "?").concat(searchParams.toString()),
|
|
240
245
|
method: constants_1.API_METHODS.GET,
|
|
246
|
+
headers: headers,
|
|
241
247
|
})];
|
|
242
248
|
case 4:
|
|
243
249
|
response = _m.sent();
|
|
244
|
-
itemsRaw = (responseKey
|
|
245
|
-
|
|
246
|
-
: ((_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)) || [];
|
|
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) ||
|
|
251
|
+
[];
|
|
247
252
|
items = Array.isArray(itemsRaw)
|
|
248
253
|
? itemsRaw
|
|
249
254
|
: [itemsRaw];
|
|
250
|
-
count = (
|
|
251
|
-
|
|
255
|
+
count = (_l = response === null || response === void 0 ? void 0 : response.data) === null || _l === void 0 ? void 0 : _l.count;
|
|
256
|
+
shouldCache = Array.isArray(itemsRaw) && count && items.length > 0;
|
|
257
|
+
if (shouldCache) {
|
|
252
258
|
itemsMap = items.reduce(function (acc, item, idx) {
|
|
253
259
|
var _a, _b;
|
|
254
260
|
var obj = item;
|
|
@@ -260,10 +266,15 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
260
266
|
updatedCache = {
|
|
261
267
|
items: itemsMap,
|
|
262
268
|
cachedAt: new Date().toISOString(),
|
|
269
|
+
count: count,
|
|
263
270
|
};
|
|
264
271
|
(0, local_storage_1.setStorageValue)(cacheKey, updatedCache);
|
|
265
272
|
}
|
|
266
|
-
|
|
273
|
+
// Return count as number when present, otherwise fallback to items.length
|
|
274
|
+
return [2 /*return*/, {
|
|
275
|
+
count: count,
|
|
276
|
+
items: items,
|
|
277
|
+
}];
|
|
267
278
|
case 5:
|
|
268
279
|
error_1 = _m.sent();
|
|
269
280
|
console.error("Error fetching data for ".concat(cacheKey, ":"), error_1);
|
|
@@ -359,7 +370,7 @@ exports.getCachedSingleItemSync = getCachedSingleItemSync;
|
|
|
359
370
|
* const config = { cacheKey: LS_KEYS.WORKSPACE, apiUrl: '/api/workspace', responseKey: 'workspace' };
|
|
360
371
|
* const workspace = await getCachedSingleItem<WorkspaceBE>(config, { subdomain: 'school1' });
|
|
361
372
|
*/
|
|
362
|
-
var getCachedSingleItem = function (config, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
373
|
+
var getCachedSingleItem = function (config, params, headers) { return __awaiter(void 0, void 0, void 0, function () {
|
|
363
374
|
var cacheKey, apiUrl, expirationMs, cachedData, currentTime, cachedTime, ageInMs, queryParams, response, item, updatedCache, error_2;
|
|
364
375
|
var _a;
|
|
365
376
|
return __generator(this, function (_b) {
|
|
@@ -385,6 +396,7 @@ var getCachedSingleItem = function (config, params) { return __awaiter(void 0, v
|
|
|
385
396
|
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
386
397
|
url: "".concat(apiUrl).concat(queryParams),
|
|
387
398
|
method: constants_1.API_METHODS.GET,
|
|
399
|
+
headers: headers,
|
|
388
400
|
})];
|
|
389
401
|
case 2:
|
|
390
402
|
response = _b.sent();
|
|
@@ -75,6 +75,8 @@ interface Params {
|
|
|
75
75
|
listDeps?: Array<any> | Array<string>;
|
|
76
76
|
listParams: object;
|
|
77
77
|
listUrl: string;
|
|
78
|
+
/** Optional headers to include on every fetch made by this hook */
|
|
79
|
+
headers?: Record<string, string>;
|
|
78
80
|
searchQuery: string;
|
|
79
81
|
unitByIdUrl: string;
|
|
80
82
|
unitUrl: string;
|
|
@@ -96,5 +98,5 @@ interface Return {
|
|
|
96
98
|
updateFetchNow: (url?: string, config?: FetchConfig) => void;
|
|
97
99
|
updateLoading: boolean;
|
|
98
100
|
}
|
|
99
|
-
export declare const useModuleEntityV2: ({ byIdCallback, byIdDeps, byIdParams, deleteCallback, deleteDeps, deleteParams, listCallback, listDeps, listParams, listUrl, searchQuery, unitByIdUrl, unitUrl, updateCallback, updateDeps, updateParams, }: Params) => Return;
|
|
101
|
+
export declare const useModuleEntityV2: ({ byIdCallback, byIdDeps, byIdParams, deleteCallback, deleteDeps, deleteParams, listCallback, listDeps, listParams, listUrl, headers, searchQuery, unitByIdUrl, unitUrl, updateCallback, updateDeps, updateParams, }: Params) => Return;
|
|
100
102
|
export {};
|
|
@@ -74,7 +74,7 @@ var constants_1 = require("../constants");
|
|
|
74
74
|
var use_fetch_1 = require("./use-fetch");
|
|
75
75
|
var use_debounce_1 = require("./use-debounce");
|
|
76
76
|
var useModuleEntityV2 = function (_a) {
|
|
77
|
-
var byIdCallback = _a.byIdCallback, _b = _a.byIdDeps, byIdDeps = _b === void 0 ? [] : _b, byIdParams = _a.byIdParams, deleteCallback = _a.deleteCallback, _c = _a.deleteDeps, deleteDeps = _c === void 0 ? [] : _c, deleteParams = _a.deleteParams, listCallback = _a.listCallback, _d = _a.listDeps, listDeps = _d === void 0 ? [] : _d, listParams = _a.listParams, listUrl = _a.listUrl, searchQuery = _a.searchQuery, unitByIdUrl = _a.unitByIdUrl, unitUrl = _a.unitUrl, updateCallback = _a.updateCallback, _e = _a.updateDeps, updateDeps = _e === void 0 ? [] : _e, updateParams = _a.updateParams;
|
|
77
|
+
var byIdCallback = _a.byIdCallback, _b = _a.byIdDeps, byIdDeps = _b === void 0 ? [] : _b, byIdParams = _a.byIdParams, deleteCallback = _a.deleteCallback, _c = _a.deleteDeps, deleteDeps = _c === void 0 ? [] : _c, deleteParams = _a.deleteParams, listCallback = _a.listCallback, _d = _a.listDeps, listDeps = _d === void 0 ? [] : _d, listParams = _a.listParams, listUrl = _a.listUrl, headers = _a.headers, searchQuery = _a.searchQuery, unitByIdUrl = _a.unitByIdUrl, unitUrl = _a.unitUrl, updateCallback = _a.updateCallback, _e = _a.updateDeps, updateDeps = _e === void 0 ? [] : _e, updateParams = _a.updateParams;
|
|
78
78
|
var debouncedQuery = (0, use_debounce_1.useDebounce)(searchQuery, 800);
|
|
79
79
|
// debounced list refresh to batch multiple updates/deletes
|
|
80
80
|
var refreshTimerRef = (0, react_1.useRef)(null);
|
|
@@ -88,6 +88,7 @@ var useModuleEntityV2 = function (_a) {
|
|
|
88
88
|
var _f = (0, use_fetch_1.useFetch)(listUrl, {
|
|
89
89
|
method: constants_1.API_METHODS.GET,
|
|
90
90
|
params: listParams,
|
|
91
|
+
headers: headers,
|
|
91
92
|
callback: listCallback,
|
|
92
93
|
credentials: true,
|
|
93
94
|
}, __spreadArray([debouncedQuery, listParams], listDeps, true)), listError = _f.error, listFetchNow = _f.fetchNow, listLoading = _f.loading;
|
|
@@ -102,6 +103,7 @@ var useModuleEntityV2 = function (_a) {
|
|
|
102
103
|
listFetchNow(undefined, {
|
|
103
104
|
method: constants_1.API_METHODS.GET,
|
|
104
105
|
params: listParams,
|
|
106
|
+
headers: headers,
|
|
105
107
|
});
|
|
106
108
|
}
|
|
107
109
|
catch (_a) {
|
|
@@ -114,6 +116,7 @@ var useModuleEntityV2 = function (_a) {
|
|
|
114
116
|
var _g = (0, use_fetch_1.useFetch)(unitUrl, {
|
|
115
117
|
method: constants_1.API_METHODS.PUT,
|
|
116
118
|
body: JSON.stringify(updateParams),
|
|
119
|
+
headers: headers,
|
|
117
120
|
callback: function (d) {
|
|
118
121
|
if (updateCallback) {
|
|
119
122
|
try {
|
|
@@ -139,12 +142,14 @@ var useModuleEntityV2 = function (_a) {
|
|
|
139
142
|
var _h = (0, use_fetch_1.useFetch)(unitByIdUrl, {
|
|
140
143
|
method: constants_1.API_METHODS.GET,
|
|
141
144
|
params: byIdParams,
|
|
145
|
+
headers: headers,
|
|
142
146
|
callback: byIdCallback,
|
|
143
147
|
}, __spreadArray([], byIdDeps, true)), byIdError = _h.error, byIdLoading = _h.loading, byIdFetchNow = _h.fetchNow;
|
|
144
148
|
// delete
|
|
145
149
|
var _j = (0, use_fetch_1.useFetch)(unitUrl, {
|
|
146
150
|
method: constants_1.API_METHODS.DELETE,
|
|
147
151
|
body: JSON.stringify(deleteParams),
|
|
152
|
+
headers: headers,
|
|
148
153
|
callback: function (d) {
|
|
149
154
|
if (deleteCallback) {
|
|
150
155
|
try {
|