@react-pakistan/util-functions 1.25.39 → 1.25.40
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 +3 -1
- package/general/fetch-data.js +17 -2
- package/general/generic-cache.d.ts +21 -0
- package/general/generic-cache.js +81 -1
- package/package.json +1 -1
package/general/fetch-data.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { API_METHODS } from '../constants/api-methods';
|
|
2
|
+
export type FetchDataParamValue = string | number | boolean | null | undefined;
|
|
2
3
|
export interface FetchDataParams {
|
|
3
4
|
url: string;
|
|
4
5
|
method?: API_METHODS;
|
|
5
6
|
body?: string;
|
|
6
7
|
headers?: Record<string, string>;
|
|
8
|
+
params?: Record<string, FetchDataParamValue>;
|
|
7
9
|
}
|
|
8
|
-
export declare const fetchData: ({ url, method, body, headers, }: FetchDataParams) => Promise<{
|
|
10
|
+
export declare const fetchData: ({ url, method, body, headers, params, }: FetchDataParams) => Promise<{
|
|
9
11
|
data: any | undefined;
|
|
10
12
|
error: Error | undefined;
|
|
11
13
|
}>;
|
package/general/fetch-data.js
CHANGED
|
@@ -49,14 +49,29 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
49
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
50
|
exports.fetchData = void 0;
|
|
51
51
|
var api_methods_1 = require("../constants/api-methods");
|
|
52
|
+
var buildUrl = function (url, params) {
|
|
53
|
+
if (!params)
|
|
54
|
+
return url;
|
|
55
|
+
var filteredEntries = Object.entries(params).filter(function (entry) {
|
|
56
|
+
return entry[1] !== null && entry[1] !== undefined;
|
|
57
|
+
});
|
|
58
|
+
if (filteredEntries.length === 0)
|
|
59
|
+
return url;
|
|
60
|
+
var searchParams = new URLSearchParams(filteredEntries.map(function (_a) {
|
|
61
|
+
var key = _a[0], value = _a[1];
|
|
62
|
+
return [key, String(value)];
|
|
63
|
+
}));
|
|
64
|
+
var separator = url.includes('?') ? '&' : '?';
|
|
65
|
+
return "".concat(url).concat(separator).concat(searchParams.toString());
|
|
66
|
+
};
|
|
52
67
|
var fetchData = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
53
68
|
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, headers = _b.headers;
|
|
69
|
+
var url = _b.url, _c = _b.method, method = _c === void 0 ? api_methods_1.API_METHODS.GET : _c, body = _b.body, headers = _b.headers, params = _b.params;
|
|
55
70
|
return __generator(this, function (_d) {
|
|
56
71
|
switch (_d.label) {
|
|
57
72
|
case 0:
|
|
58
73
|
_d.trys.push([0, 3, , 4]);
|
|
59
|
-
return [4 /*yield*/, fetch(url, __assign(__assign({ method: method }, (headers ? { headers: headers } : {})), (method !== api_methods_1.API_METHODS.GET && body && { body: body })))];
|
|
74
|
+
return [4 /*yield*/, fetch(buildUrl(url, params), __assign(__assign({ method: method }, (headers ? { headers: headers } : {})), (method !== api_methods_1.API_METHODS.GET && body && { body: body })))];
|
|
60
75
|
case 1:
|
|
61
76
|
res = _d.sent();
|
|
62
77
|
return [4 /*yield*/, res.json()];
|
|
@@ -132,6 +132,27 @@ export declare const getCachedSingleItemSync: <T>(cacheKey: string, expirationMs
|
|
|
132
132
|
* const workspace = await getCachedSingleItem<WorkspaceBE>(config, { subdomain: 'school1' });
|
|
133
133
|
*/
|
|
134
134
|
export declare const getCachedSingleItem: <T>(config: CacheConfig, params?: Record<string, string>, headers?: Record<string, string>) => Promise<T | null>;
|
|
135
|
+
/**
|
|
136
|
+
* Get cached data but fall back to the API when sync cache is empty.
|
|
137
|
+
*
|
|
138
|
+
* This helper first performs a quick synchronous cache check via
|
|
139
|
+
* `getCachedDataSync`. If that returns an empty result it will call
|
|
140
|
+
* the async `getCachedData` to fetch fresh data from the API and return
|
|
141
|
+
* the result. Use this when callers want a fast-path cache read but also
|
|
142
|
+
* need the ability to recall the API transparently when cache is missing.
|
|
143
|
+
*/
|
|
144
|
+
export declare const getCachedDataWithFallback: <T>(config: CacheConfig, params?: Record<string, unknown>, headers?: Record<string, string>) => Promise<ListResponse<T>>;
|
|
145
|
+
/**
|
|
146
|
+
* Async variant to get a single item by ID with API fallback.
|
|
147
|
+
*
|
|
148
|
+
* Unlike `getCachedItemById` (sync), this function will call the API
|
|
149
|
+
* (via `getCachedDataWithFallback`) when the item is not available in
|
|
150
|
+
* the local cache. Requires a full `CacheConfig` so the API URL is
|
|
151
|
+
* available for fetching.
|
|
152
|
+
*/
|
|
153
|
+
export declare const getCachedItemByIdAsync: <T extends {
|
|
154
|
+
id: string;
|
|
155
|
+
}>(config: CacheConfig, itemId: string, params?: Record<string, unknown>, headers?: Record<string, string>) => Promise<T | null>;
|
|
135
156
|
/**
|
|
136
157
|
* Invalidate (remove) cache for a module
|
|
137
158
|
* Useful after create/update/delete operations
|
package/general/generic-cache.js
CHANGED
|
@@ -72,7 +72,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
72
72
|
}
|
|
73
73
|
};
|
|
74
74
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
75
|
-
exports.isCacheStale = exports.preloadCache = exports.invalidateCache = exports.getCachedSingleItem = exports.getCachedSingleItemSync = exports.getCachedItemById = exports.getCachedData = exports.getCachedDataSync = void 0;
|
|
75
|
+
exports.isCacheStale = exports.preloadCache = exports.invalidateCache = exports.getCachedItemByIdAsync = exports.getCachedDataWithFallback = exports.getCachedSingleItem = exports.getCachedSingleItemSync = exports.getCachedItemById = exports.getCachedData = exports.getCachedDataSync = void 0;
|
|
76
76
|
var constants_1 = require("../constants");
|
|
77
77
|
var fetch_data_1 = require("./fetch-data");
|
|
78
78
|
var local_storage_1 = require("../local-storage");
|
|
@@ -508,6 +508,86 @@ var getCachedSingleItem = function (config, params, headers) { return __awaiter(
|
|
|
508
508
|
});
|
|
509
509
|
}); };
|
|
510
510
|
exports.getCachedSingleItem = getCachedSingleItem;
|
|
511
|
+
/**
|
|
512
|
+
* Get cached data but fall back to the API when sync cache is empty.
|
|
513
|
+
*
|
|
514
|
+
* This helper first performs a quick synchronous cache check via
|
|
515
|
+
* `getCachedDataSync`. If that returns an empty result it will call
|
|
516
|
+
* the async `getCachedData` to fetch fresh data from the API and return
|
|
517
|
+
* the result. Use this when callers want a fast-path cache read but also
|
|
518
|
+
* need the ability to recall the API transparently when cache is missing.
|
|
519
|
+
*/
|
|
520
|
+
var getCachedDataWithFallback = function (config, params, headers) { return __awaiter(void 0, void 0, void 0, function () {
|
|
521
|
+
var expirationMs, sync, error_3;
|
|
522
|
+
var _a;
|
|
523
|
+
return __generator(this, function (_b) {
|
|
524
|
+
switch (_b.label) {
|
|
525
|
+
case 0:
|
|
526
|
+
expirationMs = (_a = config.expirationMs) !== null && _a !== void 0 ? _a : constants_1.ONE_DAY_IN_MS;
|
|
527
|
+
_b.label = 1;
|
|
528
|
+
case 1:
|
|
529
|
+
_b.trys.push([1, 3, , 4]);
|
|
530
|
+
sync = (0, exports.getCachedDataSync)(config.cacheKey, expirationMs);
|
|
531
|
+
if (sync &&
|
|
532
|
+
sync.count > 0 &&
|
|
533
|
+
Array.isArray(sync.items) &&
|
|
534
|
+
sync.items.length > 0) {
|
|
535
|
+
return [2 /*return*/, sync];
|
|
536
|
+
}
|
|
537
|
+
return [4 /*yield*/, (0, exports.getCachedData)({ config: config, params: params, headers: headers })];
|
|
538
|
+
case 2:
|
|
539
|
+
// Sync cache empty — fetch from API and return result
|
|
540
|
+
return [2 /*return*/, _b.sent()];
|
|
541
|
+
case 3:
|
|
542
|
+
error_3 = _b.sent();
|
|
543
|
+
console.error("Error in getCachedDataWithFallback for ".concat(config.cacheKey, ":"), error_3);
|
|
544
|
+
return [2 /*return*/, { count: 0, items: [] }];
|
|
545
|
+
case 4: return [2 /*return*/];
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}); };
|
|
549
|
+
exports.getCachedDataWithFallback = getCachedDataWithFallback;
|
|
550
|
+
/**
|
|
551
|
+
* Async variant to get a single item by ID with API fallback.
|
|
552
|
+
*
|
|
553
|
+
* Unlike `getCachedItemById` (sync), this function will call the API
|
|
554
|
+
* (via `getCachedDataWithFallback`) when the item is not available in
|
|
555
|
+
* the local cache. Requires a full `CacheConfig` so the API URL is
|
|
556
|
+
* available for fetching.
|
|
557
|
+
*/
|
|
558
|
+
var getCachedItemByIdAsync = function (config, itemId, params, headers) { return __awaiter(void 0, void 0, void 0, function () {
|
|
559
|
+
var raw, found, list, error_4;
|
|
560
|
+
return __generator(this, function (_a) {
|
|
561
|
+
switch (_a.label) {
|
|
562
|
+
case 0:
|
|
563
|
+
if (!itemId)
|
|
564
|
+
return [2 /*return*/, null];
|
|
565
|
+
_a.label = 1;
|
|
566
|
+
case 1:
|
|
567
|
+
_a.trys.push([1, 3, , 4]);
|
|
568
|
+
raw = (0, local_storage_1.getStorageValue)(config.cacheKey);
|
|
569
|
+
if (isCachedArrayShape(raw)) {
|
|
570
|
+
found = raw.items.find(function (i) { return i.id === itemId; });
|
|
571
|
+
if (found)
|
|
572
|
+
return [2 /*return*/, found];
|
|
573
|
+
}
|
|
574
|
+
if (isCachedMapShape(raw)) {
|
|
575
|
+
if (raw.items && raw.items[itemId])
|
|
576
|
+
return [2 /*return*/, raw.items[itemId]];
|
|
577
|
+
}
|
|
578
|
+
return [4 /*yield*/, (0, exports.getCachedDataWithFallback)(config, params, headers)];
|
|
579
|
+
case 2:
|
|
580
|
+
list = _a.sent();
|
|
581
|
+
return [2 /*return*/, list.items.find(function (i) { return i.id === itemId; }) || null];
|
|
582
|
+
case 3:
|
|
583
|
+
error_4 = _a.sent();
|
|
584
|
+
console.error("Error in getCachedItemByIdAsync for ".concat(config.cacheKey, ":"), error_4);
|
|
585
|
+
return [2 /*return*/, null];
|
|
586
|
+
case 4: return [2 /*return*/];
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
}); };
|
|
590
|
+
exports.getCachedItemByIdAsync = getCachedItemByIdAsync;
|
|
511
591
|
// ============================================================================
|
|
512
592
|
// CACHE UTILITIES
|
|
513
593
|
// ============================================================================
|