@react-pakistan/util-functions 1.25.2 → 1.25.3
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.
|
@@ -24,13 +24,18 @@ export declare const getCachedCurrenciesSync: () => {
|
|
|
24
24
|
* - If currencies exist but are older than 1 week, fetch fresh data and update cache
|
|
25
25
|
* - If currencies don't exist in cache, fetch from API and cache them
|
|
26
26
|
*
|
|
27
|
+
* @param searchQuery - Optional search query to filter currencies
|
|
28
|
+
* @param pageLimit - Number of currencies to fetch (default: 100)
|
|
27
29
|
* @returns Promise<{count:number, items: CurrencyBE[]}> - Paged currencies
|
|
28
30
|
*
|
|
29
31
|
* @example
|
|
30
32
|
* const currencies = await getCachedCurrencies();
|
|
31
33
|
* console.log(currencies.items[0].label);
|
|
34
|
+
*
|
|
35
|
+
* // With search
|
|
36
|
+
* const filtered = await getCachedCurrencies('USD');
|
|
32
37
|
*/
|
|
33
|
-
export declare const getCachedCurrencies: () => Promise<{
|
|
38
|
+
export declare const getCachedCurrencies: (searchQuery?: string, pageLimit?: number) => Promise<{
|
|
34
39
|
count: number;
|
|
35
40
|
items: CurrencyBE[];
|
|
36
41
|
}>;
|
|
@@ -35,6 +35,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
35
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
+
if (ar || !(i in from)) {
|
|
41
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
+
ar[i] = from[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
+
};
|
|
38
47
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
48
|
exports.isCurrenciesCacheStale = exports.preloadCurrencies = exports.invalidateCurrenciesCache = exports.getCachedCurrencyByCode = exports.getCachedCurrencyById = exports.getCachedCurrencies = exports.getCachedCurrenciesSync = void 0;
|
|
40
49
|
var constants_1 = require("../constants");
|
|
@@ -88,56 +97,86 @@ exports.getCachedCurrenciesSync = getCachedCurrenciesSync;
|
|
|
88
97
|
* - If currencies exist but are older than 1 week, fetch fresh data and update cache
|
|
89
98
|
* - If currencies don't exist in cache, fetch from API and cache them
|
|
90
99
|
*
|
|
100
|
+
* @param searchQuery - Optional search query to filter currencies
|
|
101
|
+
* @param pageLimit - Number of currencies to fetch (default: 100)
|
|
91
102
|
* @returns Promise<{count:number, items: CurrencyBE[]}> - Paged currencies
|
|
92
103
|
*
|
|
93
104
|
* @example
|
|
94
105
|
* const currencies = await getCachedCurrencies();
|
|
95
106
|
* console.log(currencies.items[0].label);
|
|
107
|
+
*
|
|
108
|
+
* // With search
|
|
109
|
+
* const filtered = await getCachedCurrencies('USD');
|
|
96
110
|
*/
|
|
97
|
-
var getCachedCurrencies = function () {
|
|
98
|
-
var
|
|
99
|
-
var
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
var getCachedCurrencies = function (searchQuery_1) {
|
|
112
|
+
var args_1 = [];
|
|
113
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
114
|
+
args_1[_i - 1] = arguments[_i];
|
|
115
|
+
}
|
|
116
|
+
return __awaiter(void 0, __spreadArray([searchQuery_1], args_1, true), void 0, function (searchQuery, pageLimit) {
|
|
117
|
+
var response_1, cachedData, currentTime, cachedTime, ageInMs, response, updatedCache, error_1;
|
|
118
|
+
var _a;
|
|
119
|
+
if (pageLimit === void 0) { pageLimit = 100; }
|
|
120
|
+
return __generator(this, function (_b) {
|
|
121
|
+
switch (_b.label) {
|
|
122
|
+
case 0:
|
|
123
|
+
_b.trys.push([0, 4, , 5]);
|
|
124
|
+
if (!(searchQuery && searchQuery.trim())) return [3 /*break*/, 2];
|
|
125
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
126
|
+
url: constants_1.API_ROUTES.CURRENCIES,
|
|
127
|
+
body: JSON.stringify({
|
|
128
|
+
searchQuery: searchQuery,
|
|
129
|
+
pageLimit: pageLimit,
|
|
130
|
+
currentPage: 1,
|
|
131
|
+
}),
|
|
132
|
+
method: api_methods_1.API_METHODS.POST,
|
|
133
|
+
})];
|
|
134
|
+
case 1:
|
|
135
|
+
response_1 = _b.sent();
|
|
136
|
+
return [2 /*return*/, (response_1 === null || response_1 === void 0 ? void 0 : response_1.data) || { count: 0, items: [] }];
|
|
137
|
+
case 2:
|
|
138
|
+
cachedData = (0, get_storage_value_1.getStorageValue)(type_1.LS_KEYS.CURRENCIES);
|
|
139
|
+
currentTime = new Date().getTime();
|
|
140
|
+
// Check if cached data exists and is still fresh
|
|
141
|
+
if (cachedData) {
|
|
142
|
+
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
143
|
+
ageInMs = currentTime - cachedTime;
|
|
144
|
+
// If cached data is less than 1 week old, return it
|
|
145
|
+
if (ageInMs < constants_1.ONE_WEEK_IN_MS) {
|
|
146
|
+
return [2 /*return*/, {
|
|
147
|
+
count: cachedData.currencies.length,
|
|
148
|
+
items: cachedData.currencies,
|
|
149
|
+
}];
|
|
150
|
+
}
|
|
116
151
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
152
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
153
|
+
url: constants_1.API_ROUTES.CURRENCIES,
|
|
154
|
+
body: JSON.stringify({
|
|
155
|
+
pageLimit: pageLimit,
|
|
156
|
+
currentPage: 1,
|
|
157
|
+
}),
|
|
158
|
+
method: api_methods_1.API_METHODS.POST,
|
|
159
|
+
})];
|
|
160
|
+
case 3:
|
|
161
|
+
response = _b.sent();
|
|
162
|
+
if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) {
|
|
163
|
+
updatedCache = {
|
|
164
|
+
currencies: response.data.items,
|
|
165
|
+
cachedAt: new Date().toISOString(),
|
|
166
|
+
};
|
|
167
|
+
(0, set_storage_value_1.setStorageValue)(type_1.LS_KEYS.CURRENCIES, updatedCache);
|
|
168
|
+
return [2 /*return*/, response.data];
|
|
169
|
+
}
|
|
170
|
+
return [2 /*return*/, { count: 0, items: [] }];
|
|
171
|
+
case 4:
|
|
172
|
+
error_1 = _b.sent();
|
|
173
|
+
console.error('Error fetching currencies:', error_1);
|
|
174
|
+
return [2 /*return*/, { count: 0, items: [] }];
|
|
175
|
+
case 5: return [2 /*return*/];
|
|
176
|
+
}
|
|
177
|
+
});
|
|
139
178
|
});
|
|
140
|
-
}
|
|
179
|
+
};
|
|
141
180
|
exports.getCachedCurrencies = getCachedCurrencies;
|
|
142
181
|
/**
|
|
143
182
|
* Utility function to get a specific currency by ID from cache
|