@react-pakistan/util-functions 1.25.7 → 1.25.9
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/api/stellar-solutions/bank/cache.js +4 -3
- package/api/stellar-solutions/branch/cache.js +4 -3
- package/api/stellar-solutions/company/cache.js +4 -3
- package/api/stellar-solutions/constants.d.ts +0 -2
- package/api/stellar-solutions/constants.js +1 -3
- package/api/stellar-solutions/contact/cache.js +4 -3
- package/api/stellar-solutions/currency/cache.js +4 -3
- package/api/stellar-solutions/customer/cache.js +4 -3
- package/api/stellar-solutions/expense/cache.js +4 -3
- package/api/stellar-solutions/expense-category/cache.js +4 -3
- package/api/stellar-solutions/payment-mode/cache.js +4 -3
- package/api/stellar-solutions/preference/cache.js +4 -3
- package/api/stellar-solutions/product/cache.js +4 -3
- package/api/stellar-solutions/product-category/cache.js +4 -3
- package/api/stellar-solutions/tax/cache.js +4 -3
- package/constants/cache-time.d.ts +3 -0
- package/constants/cache-time.js +6 -0
- package/constants/index.d.ts +2 -1
- package/constants/index.js +2 -1
- package/general/generic-cache.d.ts +143 -0
- package/general/generic-cache.js +411 -0
- package/general/index.d.ts +1 -0
- package/general/index.js +1 -0
- package/hooks/use-fetch.d.ts +11 -0
- package/hooks/use-module-entity-v2.d.ts +11 -5
- package/hooks/use-module-entity-v2.js +12 -6
- package/package.json +1 -1
|
@@ -52,6 +52,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
52
52
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
53
53
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
54
54
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
55
|
+
var constants_2 = require("../../../constants");
|
|
55
56
|
/**
|
|
56
57
|
* Synchronous utility function to get banks from cache only
|
|
57
58
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -75,7 +76,7 @@ var getCachedBanksSync = function () {
|
|
|
75
76
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
76
77
|
var ageInMs = currentTime - cachedTime;
|
|
77
78
|
// If cached data is less than 1 week old, return it
|
|
78
|
-
if (ageInMs <
|
|
79
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
79
80
|
return {
|
|
80
81
|
count: cachedData.banks.length,
|
|
81
82
|
items: cachedData.banks,
|
|
@@ -142,7 +143,7 @@ var getCachedBanks = function (searchQuery_1) {
|
|
|
142
143
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
143
144
|
ageInMs = currentTime - cachedTime;
|
|
144
145
|
// If cached data is less than 1 week old, return it
|
|
145
|
-
if (ageInMs <
|
|
146
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
146
147
|
return [2 /*return*/, {
|
|
147
148
|
count: cachedData.banks.length,
|
|
148
149
|
items: cachedData.banks,
|
|
@@ -285,7 +286,7 @@ var isBanksCacheStale = function () {
|
|
|
285
286
|
var currentTime = new Date().getTime();
|
|
286
287
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
287
288
|
var ageInMs = currentTime - cachedTime;
|
|
288
|
-
return ageInMs >=
|
|
289
|
+
return ageInMs >= constants_2.ONE_WEEK_IN_MS;
|
|
289
290
|
}
|
|
290
291
|
catch (error) {
|
|
291
292
|
console.error('Error checking banks cache staleness:', error);
|
|
@@ -52,6 +52,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
52
52
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
53
53
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
54
54
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
55
|
+
var constants_2 = require("../../../constants");
|
|
55
56
|
/**
|
|
56
57
|
* Synchronous utility function to get branches from cache only
|
|
57
58
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -75,7 +76,7 @@ var getCachedBranchesSync = function () {
|
|
|
75
76
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
76
77
|
var ageInMs = currentTime - cachedTime;
|
|
77
78
|
// If cached data is less than 1 week old, return it
|
|
78
|
-
if (ageInMs <
|
|
79
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
79
80
|
return {
|
|
80
81
|
count: cachedData.branches.length,
|
|
81
82
|
items: cachedData.branches,
|
|
@@ -142,7 +143,7 @@ var getCachedBranches = function (searchQuery_1) {
|
|
|
142
143
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
143
144
|
ageInMs = currentTime - cachedTime;
|
|
144
145
|
// If cached data is less than 1 week old, return it
|
|
145
|
-
if (ageInMs <
|
|
146
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
146
147
|
return [2 /*return*/, {
|
|
147
148
|
count: cachedData.branches.length,
|
|
148
149
|
items: cachedData.branches,
|
|
@@ -285,7 +286,7 @@ var isBranchesCacheStale = function () {
|
|
|
285
286
|
var currentTime = new Date().getTime();
|
|
286
287
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
287
288
|
var ageInMs = currentTime - cachedTime;
|
|
288
|
-
return ageInMs >=
|
|
289
|
+
return ageInMs >= constants_2.ONE_WEEK_IN_MS;
|
|
289
290
|
}
|
|
290
291
|
catch (error) {
|
|
291
292
|
console.error('Error checking branches cache staleness:', error);
|
|
@@ -63,6 +63,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
63
63
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
64
64
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
65
65
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
66
|
+
var constants_2 = require("../../../constants");
|
|
66
67
|
/**
|
|
67
68
|
* Synchronous utility function to get companies from cache only
|
|
68
69
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -86,7 +87,7 @@ var getCachedCompaniesSync = function () {
|
|
|
86
87
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
87
88
|
var ageInMs = currentTime - cachedTime;
|
|
88
89
|
// If cached data is less than 1 day old, return it
|
|
89
|
-
if (ageInMs <
|
|
90
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
90
91
|
return {
|
|
91
92
|
count: cachedData.companies.length,
|
|
92
93
|
items: cachedData.companies,
|
|
@@ -154,7 +155,7 @@ var getCachedCompanies = function (searchQuery_1, filters_1) {
|
|
|
154
155
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
155
156
|
ageInMs = currentTime - cachedTime;
|
|
156
157
|
// If cached data is less than 1 day old, return it
|
|
157
|
-
if (ageInMs <
|
|
158
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
158
159
|
return [2 /*return*/, {
|
|
159
160
|
count: cachedData.companies.length,
|
|
160
161
|
items: cachedData.companies,
|
|
@@ -297,7 +298,7 @@ var isCompaniesCacheStale = function () {
|
|
|
297
298
|
var currentTime = new Date().getTime();
|
|
298
299
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
299
300
|
var ageInMs = currentTime - cachedTime;
|
|
300
|
-
return ageInMs >=
|
|
301
|
+
return ageInMs >= constants_2.ONE_DAY_IN_MS;
|
|
301
302
|
}
|
|
302
303
|
catch (error) {
|
|
303
304
|
console.error('Error checking companies cache staleness:', error);
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.API_ROUTES =
|
|
4
|
-
exports.ONE_DAY_IN_MS = 1 * 24 * 60 * 60 * 1000; // 1 day in milliseconds
|
|
5
|
-
exports.ONE_WEEK_IN_MS = 7 * 24 * 60 * 60 * 1000; // 1 week in milliseconds
|
|
3
|
+
exports.API_ROUTES = void 0;
|
|
6
4
|
exports.API_ROUTES = {
|
|
7
5
|
BANKS: '/api/banks',
|
|
8
6
|
BRANCHES: '/api/branches',
|
|
@@ -63,6 +63,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
63
63
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
64
64
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
65
65
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
66
|
+
var constants_2 = require("../../../constants");
|
|
66
67
|
/**
|
|
67
68
|
* Synchronous utility function to get contacts from cache only
|
|
68
69
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -86,7 +87,7 @@ var getCachedContactsSync = function () {
|
|
|
86
87
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
87
88
|
var ageInMs = currentTime - cachedTime;
|
|
88
89
|
// If cached data is less than 1 day old, return it
|
|
89
|
-
if (ageInMs <
|
|
90
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
90
91
|
return {
|
|
91
92
|
count: cachedData.contacts.length,
|
|
92
93
|
items: cachedData.contacts,
|
|
@@ -154,7 +155,7 @@ var getCachedContacts = function (searchQuery_1, filters_1) {
|
|
|
154
155
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
155
156
|
ageInMs = currentTime - cachedTime;
|
|
156
157
|
// If cached data is less than 1 day old, return it
|
|
157
|
-
if (ageInMs <
|
|
158
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
158
159
|
return [2 /*return*/, {
|
|
159
160
|
count: cachedData.contacts.length,
|
|
160
161
|
items: cachedData.contacts,
|
|
@@ -297,7 +298,7 @@ var isContactsCacheStale = function () {
|
|
|
297
298
|
var currentTime = new Date().getTime();
|
|
298
299
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
299
300
|
var ageInMs = currentTime - cachedTime;
|
|
300
|
-
return ageInMs >=
|
|
301
|
+
return ageInMs >= constants_2.ONE_DAY_IN_MS;
|
|
301
302
|
}
|
|
302
303
|
catch (error) {
|
|
303
304
|
console.error('Error checking contacts cache staleness:', error);
|
|
@@ -52,6 +52,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
52
52
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
53
53
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
54
54
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
55
|
+
var constants_2 = require("../../../constants");
|
|
55
56
|
/**
|
|
56
57
|
* Synchronous utility function to get currencies from cache only
|
|
57
58
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -75,7 +76,7 @@ var getCachedCurrenciesSync = function () {
|
|
|
75
76
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
76
77
|
var ageInMs = currentTime - cachedTime;
|
|
77
78
|
// If cached data is less than 1 week old, return it
|
|
78
|
-
if (ageInMs <
|
|
79
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
79
80
|
return {
|
|
80
81
|
count: cachedData.currencies.length,
|
|
81
82
|
items: cachedData.currencies,
|
|
@@ -142,7 +143,7 @@ var getCachedCurrencies = function (searchQuery_1) {
|
|
|
142
143
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
143
144
|
ageInMs = currentTime - cachedTime;
|
|
144
145
|
// If cached data is less than 1 week old, return it
|
|
145
|
-
if (ageInMs <
|
|
146
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
146
147
|
return [2 /*return*/, {
|
|
147
148
|
count: cachedData.currencies.length,
|
|
148
149
|
items: cachedData.currencies,
|
|
@@ -285,7 +286,7 @@ var isCurrenciesCacheStale = function () {
|
|
|
285
286
|
var currentTime = new Date().getTime();
|
|
286
287
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
287
288
|
var ageInMs = currentTime - cachedTime;
|
|
288
|
-
return ageInMs >=
|
|
289
|
+
return ageInMs >= constants_2.ONE_WEEK_IN_MS;
|
|
289
290
|
}
|
|
290
291
|
catch (error) {
|
|
291
292
|
console.error('Error checking currencies cache staleness:', error);
|
|
@@ -63,6 +63,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
63
63
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
64
64
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
65
65
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
66
|
+
var constants_2 = require("../../../constants");
|
|
66
67
|
/**
|
|
67
68
|
* Synchronous utility function to get customers from cache only
|
|
68
69
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -86,7 +87,7 @@ var getCachedCustomersSync = function () {
|
|
|
86
87
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
87
88
|
var ageInMs = currentTime - cachedTime;
|
|
88
89
|
// If cached data is less than 1 day old, return it
|
|
89
|
-
if (ageInMs <
|
|
90
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
90
91
|
return {
|
|
91
92
|
count: cachedData.customers.length,
|
|
92
93
|
items: cachedData.customers,
|
|
@@ -154,7 +155,7 @@ var getCachedCustomers = function (searchQuery_1, filters_1) {
|
|
|
154
155
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
155
156
|
ageInMs = currentTime - cachedTime;
|
|
156
157
|
// If cached data is less than 1 day old, return it
|
|
157
|
-
if (ageInMs <
|
|
158
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
158
159
|
return [2 /*return*/, {
|
|
159
160
|
count: cachedData.customers.length,
|
|
160
161
|
items: cachedData.customers,
|
|
@@ -297,7 +298,7 @@ var isCustomersCacheStale = function () {
|
|
|
297
298
|
var currentTime = new Date().getTime();
|
|
298
299
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
299
300
|
var ageInMs = currentTime - cachedTime;
|
|
300
|
-
return ageInMs >=
|
|
301
|
+
return ageInMs >= constants_2.ONE_DAY_IN_MS;
|
|
301
302
|
}
|
|
302
303
|
catch (error) {
|
|
303
304
|
console.error('Error checking customers cache staleness:', error);
|
|
@@ -63,6 +63,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
63
63
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
64
64
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
65
65
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
66
|
+
var constants_2 = require("../../../constants");
|
|
66
67
|
/**
|
|
67
68
|
* Synchronous utility function to get expenses from cache only
|
|
68
69
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -86,7 +87,7 @@ var getCachedExpensesSync = function () {
|
|
|
86
87
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
87
88
|
var ageInMs = currentTime - cachedTime;
|
|
88
89
|
// If cached data is less than 1 day old, return it
|
|
89
|
-
if (ageInMs <
|
|
90
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
90
91
|
return {
|
|
91
92
|
count: cachedData.expenses.length,
|
|
92
93
|
items: cachedData.expenses,
|
|
@@ -154,7 +155,7 @@ var getCachedExpenses = function (searchQuery_1, filters_1) {
|
|
|
154
155
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
155
156
|
ageInMs = currentTime - cachedTime;
|
|
156
157
|
// If cached data is less than 1 day old, return it
|
|
157
|
-
if (ageInMs <
|
|
158
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
158
159
|
return [2 /*return*/, {
|
|
159
160
|
count: cachedData.expenses.length,
|
|
160
161
|
items: cachedData.expenses,
|
|
@@ -297,7 +298,7 @@ var isExpensesCacheStale = function () {
|
|
|
297
298
|
var currentTime = new Date().getTime();
|
|
298
299
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
299
300
|
var ageInMs = currentTime - cachedTime;
|
|
300
|
-
return ageInMs >=
|
|
301
|
+
return ageInMs >= constants_2.ONE_DAY_IN_MS;
|
|
301
302
|
}
|
|
302
303
|
catch (error) {
|
|
303
304
|
console.error('Error checking expenses cache staleness:', error);
|
|
@@ -63,6 +63,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
63
63
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
64
64
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
65
65
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
66
|
+
var constants_2 = require("../../../constants");
|
|
66
67
|
/**
|
|
67
68
|
* Synchronous utility function to get expense categories from cache only
|
|
68
69
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -86,7 +87,7 @@ var getCachedExpenseCategoriesSync = function () {
|
|
|
86
87
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
87
88
|
var ageInMs = currentTime - cachedTime;
|
|
88
89
|
// If cached data is less than 1 day old, return it
|
|
89
|
-
if (ageInMs <
|
|
90
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
90
91
|
return {
|
|
91
92
|
count: cachedData.expenseCategories.length,
|
|
92
93
|
items: cachedData.expenseCategories,
|
|
@@ -154,7 +155,7 @@ var getCachedExpenseCategories = function (searchQuery_1, filters_1) {
|
|
|
154
155
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
155
156
|
ageInMs = currentTime - cachedTime;
|
|
156
157
|
// If cached data is less than 1 day old, return it
|
|
157
|
-
if (ageInMs <
|
|
158
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
158
159
|
return [2 /*return*/, {
|
|
159
160
|
count: cachedData.expenseCategories.length,
|
|
160
161
|
items: cachedData.expenseCategories,
|
|
@@ -270,7 +271,7 @@ var isExpenseCategoriesCacheStale = function () {
|
|
|
270
271
|
var currentTime = new Date().getTime();
|
|
271
272
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
272
273
|
var ageInMs = currentTime - cachedTime;
|
|
273
|
-
return ageInMs >=
|
|
274
|
+
return ageInMs >= constants_2.ONE_DAY_IN_MS;
|
|
274
275
|
}
|
|
275
276
|
catch (error) {
|
|
276
277
|
console.error('Error checking expense categories cache staleness:', error);
|
|
@@ -52,6 +52,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
52
52
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
53
53
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
54
54
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
55
|
+
var constants_2 = require("../../../constants");
|
|
55
56
|
/**
|
|
56
57
|
* Synchronous utility function to get payment modes from cache only
|
|
57
58
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -75,7 +76,7 @@ var getCachedPaymentModesSync = function () {
|
|
|
75
76
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
76
77
|
var ageInMs = currentTime - cachedTime;
|
|
77
78
|
// If cached data is less than 1 week old, return it
|
|
78
|
-
if (ageInMs <
|
|
79
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
79
80
|
return {
|
|
80
81
|
count: cachedData.paymentModes.length,
|
|
81
82
|
items: cachedData.paymentModes,
|
|
@@ -142,7 +143,7 @@ var getCachedPaymentModes = function (searchQuery_1) {
|
|
|
142
143
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
143
144
|
ageInMs = currentTime - cachedTime;
|
|
144
145
|
// If cached data is less than 1 week old, return it
|
|
145
|
-
if (ageInMs <
|
|
146
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
146
147
|
return [2 /*return*/, {
|
|
147
148
|
count: cachedData.paymentModes.length,
|
|
148
149
|
items: cachedData.paymentModes,
|
|
@@ -285,7 +286,7 @@ var isPaymentModesCacheStale = function () {
|
|
|
285
286
|
var currentTime = new Date().getTime();
|
|
286
287
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
287
288
|
var ageInMs = currentTime - cachedTime;
|
|
288
|
-
return ageInMs >=
|
|
289
|
+
return ageInMs >= constants_2.ONE_WEEK_IN_MS;
|
|
289
290
|
}
|
|
290
291
|
catch (error) {
|
|
291
292
|
console.error('Error checking payment modes cache staleness:', error);
|
|
@@ -52,6 +52,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
52
52
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
53
53
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
54
54
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
55
|
+
var constants_2 = require("../../../constants");
|
|
55
56
|
/**
|
|
56
57
|
* Synchronous utility function to get preferences from cache only
|
|
57
58
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -75,7 +76,7 @@ var getCachedPreferencesSync = function () {
|
|
|
75
76
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
76
77
|
var ageInMs = currentTime - cachedTime;
|
|
77
78
|
// If cached data is less than 1 day old, return it
|
|
78
|
-
if (ageInMs <
|
|
79
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
79
80
|
return {
|
|
80
81
|
count: cachedData.preferences.length,
|
|
81
82
|
items: cachedData.preferences,
|
|
@@ -142,7 +143,7 @@ var getCachedPreferences = function (searchQuery_1) {
|
|
|
142
143
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
143
144
|
ageInMs = currentTime - cachedTime;
|
|
144
145
|
// If cached data is less than 1 week old, return it
|
|
145
|
-
if (ageInMs <
|
|
146
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
146
147
|
return [2 /*return*/, {
|
|
147
148
|
count: cachedData.preferences.length,
|
|
148
149
|
items: cachedData.preferences,
|
|
@@ -258,7 +259,7 @@ var isPreferencesCacheStale = function () {
|
|
|
258
259
|
var currentTime = new Date().getTime();
|
|
259
260
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
260
261
|
var ageInMs = currentTime - cachedTime;
|
|
261
|
-
return ageInMs >=
|
|
262
|
+
return ageInMs >= constants_2.ONE_WEEK_IN_MS;
|
|
262
263
|
}
|
|
263
264
|
catch (error) {
|
|
264
265
|
console.error('Error checking preferences cache staleness:', error);
|
|
@@ -63,6 +63,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
63
63
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
64
64
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
65
65
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
66
|
+
var constants_2 = require("../../../constants");
|
|
66
67
|
/**
|
|
67
68
|
* Synchronous utility function to get products from cache only
|
|
68
69
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -86,7 +87,7 @@ var getCachedProductsSync = function () {
|
|
|
86
87
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
87
88
|
var ageInMs = currentTime - cachedTime;
|
|
88
89
|
// If cached data is less than 1 day old, return it
|
|
89
|
-
if (ageInMs <
|
|
90
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
90
91
|
return {
|
|
91
92
|
count: cachedData.products.length,
|
|
92
93
|
items: cachedData.products,
|
|
@@ -154,7 +155,7 @@ var getCachedProducts = function (searchQuery_1, filters_1) {
|
|
|
154
155
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
155
156
|
ageInMs = currentTime - cachedTime;
|
|
156
157
|
// If cached data is less than 1 day old, return it
|
|
157
|
-
if (ageInMs <
|
|
158
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
158
159
|
return [2 /*return*/, {
|
|
159
160
|
count: cachedData.products.length,
|
|
160
161
|
items: cachedData.products,
|
|
@@ -270,7 +271,7 @@ var isProductsCacheStale = function () {
|
|
|
270
271
|
var currentTime = new Date().getTime();
|
|
271
272
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
272
273
|
var ageInMs = currentTime - cachedTime;
|
|
273
|
-
return ageInMs >=
|
|
274
|
+
return ageInMs >= constants_2.ONE_DAY_IN_MS;
|
|
274
275
|
}
|
|
275
276
|
catch (error) {
|
|
276
277
|
console.error('Error checking products cache staleness:', error);
|
|
@@ -63,6 +63,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
63
63
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
64
64
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
65
65
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
66
|
+
var constants_2 = require("../../../constants");
|
|
66
67
|
/**
|
|
67
68
|
* Synchronous utility function to get product categories from cache only
|
|
68
69
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -86,7 +87,7 @@ var getCachedProductCategoriesSync = function () {
|
|
|
86
87
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
87
88
|
var ageInMs = currentTime - cachedTime;
|
|
88
89
|
// If cached data is less than 1 week old, return it
|
|
89
|
-
if (ageInMs <
|
|
90
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
90
91
|
return {
|
|
91
92
|
count: cachedData.productCategories.length,
|
|
92
93
|
items: cachedData.productCategories,
|
|
@@ -154,7 +155,7 @@ var getCachedProductCategories = function (searchQuery_1, filters_1) {
|
|
|
154
155
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
155
156
|
ageInMs = currentTime - cachedTime;
|
|
156
157
|
// If cached data is less than 1 week old, return it
|
|
157
|
-
if (ageInMs <
|
|
158
|
+
if (ageInMs < constants_2.ONE_DAY_IN_MS) {
|
|
158
159
|
return [2 /*return*/, {
|
|
159
160
|
count: cachedData.productCategories.length,
|
|
160
161
|
items: cachedData.productCategories,
|
|
@@ -270,7 +271,7 @@ var isProductCategoriesCacheStale = function () {
|
|
|
270
271
|
var currentTime = new Date().getTime();
|
|
271
272
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
272
273
|
var ageInMs = currentTime - cachedTime;
|
|
273
|
-
return ageInMs >=
|
|
274
|
+
return ageInMs >= constants_2.ONE_DAY_IN_MS;
|
|
274
275
|
}
|
|
275
276
|
catch (error) {
|
|
276
277
|
console.error('Error checking product categories cache staleness:', error);
|
|
@@ -52,6 +52,7 @@ var api_methods_1 = require("../../../constants/api-methods");
|
|
|
52
52
|
var fetch_data_1 = require("../../../general/fetch-data");
|
|
53
53
|
var get_storage_value_1 = require("../../../local-storage/get-storage-value");
|
|
54
54
|
var set_storage_value_1 = require("../../../local-storage/set-storage-value");
|
|
55
|
+
var constants_2 = require("../../../constants");
|
|
55
56
|
/**
|
|
56
57
|
* Synchronous utility function to get taxes from cache only
|
|
57
58
|
* Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
@@ -75,7 +76,7 @@ var getCachedTaxesSync = function () {
|
|
|
75
76
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
76
77
|
var ageInMs = currentTime - cachedTime;
|
|
77
78
|
// If cached data is less than 1 week old, return it
|
|
78
|
-
if (ageInMs <
|
|
79
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
79
80
|
return {
|
|
80
81
|
count: cachedData.taxes.length,
|
|
81
82
|
items: cachedData.taxes,
|
|
@@ -142,7 +143,7 @@ var getCachedTaxes = function (searchQuery_1) {
|
|
|
142
143
|
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
143
144
|
ageInMs = currentTime - cachedTime;
|
|
144
145
|
// If cached data is less than 1 week old, return it
|
|
145
|
-
if (ageInMs <
|
|
146
|
+
if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
|
|
146
147
|
return [2 /*return*/, {
|
|
147
148
|
count: cachedData.taxes.length,
|
|
148
149
|
items: cachedData.taxes,
|
|
@@ -285,7 +286,7 @@ var isTaxesCacheStale = function () {
|
|
|
285
286
|
var currentTime = new Date().getTime();
|
|
286
287
|
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
287
288
|
var ageInMs = currentTime - cachedTime;
|
|
288
|
-
return ageInMs >=
|
|
289
|
+
return ageInMs >= constants_2.ONE_WEEK_IN_MS;
|
|
289
290
|
}
|
|
290
291
|
catch (error) {
|
|
291
292
|
console.error('Error checking taxes cache staleness:', error);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ONE_MONTH_IN_MS = exports.ONE_WEEK_IN_MS = exports.ONE_DAY_IN_MS = void 0;
|
|
4
|
+
exports.ONE_DAY_IN_MS = 1 * 24 * 60 * 60 * 1000; // 1 day in milliseconds
|
|
5
|
+
exports.ONE_WEEK_IN_MS = 7 * 24 * 60 * 60 * 1000; // 1 week in milliseconds
|
|
6
|
+
exports.ONE_MONTH_IN_MS = 30 * 24 * 60 * 60 * 1000; // 1 month in milliseconds
|
package/constants/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export * from './api-methods';
|
|
2
|
+
export * from './cache-time';
|
|
2
3
|
export * from './countries-timezones';
|
|
3
4
|
export * from './currencies';
|
|
4
5
|
export * from './layout-direction';
|
|
5
|
-
export * from './react-pakistan';
|
|
6
6
|
export * from './react-pakistan-meta';
|
|
7
|
+
export * from './react-pakistan';
|
|
7
8
|
export * from './select-value-delimiter';
|
|
8
9
|
export * from './social-media-colors';
|
|
9
10
|
export * from './social-media-sizes';
|
package/constants/index.js
CHANGED
|
@@ -15,11 +15,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./api-methods"), exports);
|
|
18
|
+
__exportStar(require("./cache-time"), exports);
|
|
18
19
|
__exportStar(require("./countries-timezones"), exports);
|
|
19
20
|
__exportStar(require("./currencies"), exports);
|
|
20
21
|
__exportStar(require("./layout-direction"), exports);
|
|
21
|
-
__exportStar(require("./react-pakistan"), exports);
|
|
22
22
|
__exportStar(require("./react-pakistan-meta"), exports);
|
|
23
|
+
__exportStar(require("./react-pakistan"), exports);
|
|
23
24
|
__exportStar(require("./select-value-delimiter"), exports);
|
|
24
25
|
__exportStar(require("./social-media-colors"), exports);
|
|
25
26
|
__exportStar(require("./social-media-sizes"), exports);
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic Cache Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides reusable localStorage-based caching for all modules with:
|
|
5
|
+
* - 1-day cache expiration
|
|
6
|
+
* - Automatic cache invalidation
|
|
7
|
+
* - Search and filter bypass (always fetch fresh)
|
|
8
|
+
* - Synchronous and asynchronous access patterns
|
|
9
|
+
* - Support for both list and single item caching
|
|
10
|
+
*
|
|
11
|
+
* Organization:
|
|
12
|
+
* - Types
|
|
13
|
+
* - List Cache Functions
|
|
14
|
+
* - Single Item Cache Functions
|
|
15
|
+
* - Cache Utilities
|
|
16
|
+
*/
|
|
17
|
+
interface CacheConfig {
|
|
18
|
+
cacheKey: string;
|
|
19
|
+
apiUrl: string;
|
|
20
|
+
expirationMs?: number;
|
|
21
|
+
responseKey?: string;
|
|
22
|
+
}
|
|
23
|
+
interface ListResponse<T> {
|
|
24
|
+
count: number;
|
|
25
|
+
items: T[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Get synchronous cached data for a module
|
|
29
|
+
* Returns empty result if cache is missing or stale
|
|
30
|
+
*
|
|
31
|
+
* @param cacheKey - localStorage key for the cache
|
|
32
|
+
* @param expirationMs - Cache expiration time in milliseconds (default: 1 day)
|
|
33
|
+
* @returns {count: number, items: T[]} - Cached data or empty array
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const users = getCachedDataSync<UserBE>(LS_KEYS.USERS);
|
|
37
|
+
* if (users.items.length > 0) {
|
|
38
|
+
* console.log(users.items[0].name);
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
export declare const getCachedDataSync: <T>(cacheKey: string, expirationMs?: number) => ListResponse<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Get cached data with API fallback for a module
|
|
44
|
+
* Automatically handles cache validation and updates
|
|
45
|
+
*
|
|
46
|
+
* @param config - Cache configuration (cacheKey, apiUrl, expirationMs, responseKey)
|
|
47
|
+
* @param searchQuery - Optional search query (bypasses cache)
|
|
48
|
+
* @param filters - Optional filters object (bypasses cache)
|
|
49
|
+
* @param pageLimit - Number of items to fetch (default: 100)
|
|
50
|
+
* @returns Promise<{count: number, items: T[]}> - Cached or fresh data
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* const config = { cacheKey: LS_KEYS.USERS, apiUrl: USER_API_ROUTES.LIST, responseKey: 'users' };
|
|
54
|
+
* const users = await getCachedData<UserBE>(config);
|
|
55
|
+
*
|
|
56
|
+
* // With search (bypasses cache)
|
|
57
|
+
* const filtered = await getCachedData<UserBE>(config, 'John');
|
|
58
|
+
*
|
|
59
|
+
* // With filters (bypasses cache)
|
|
60
|
+
* const active = await getCachedData<UserBE>(config, undefined, { enabled: true });
|
|
61
|
+
*/
|
|
62
|
+
export declare const getCachedData: <T>(config: CacheConfig, searchQuery?: string, filters?: Record<string, unknown>, pageLimit?: number) => Promise<ListResponse<T>>;
|
|
63
|
+
/**
|
|
64
|
+
* Get single cached item by ID from a list cache
|
|
65
|
+
*
|
|
66
|
+
* @param cacheKey - localStorage key for the cache
|
|
67
|
+
* @param itemId - ID of the item to retrieve
|
|
68
|
+
* @returns T | null - Cached item or null
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const user = getCachedItemById<UserBE>(LS_KEYS.USERS, 'user-123');
|
|
72
|
+
* if (user) {
|
|
73
|
+
* console.log(user.name);
|
|
74
|
+
* }
|
|
75
|
+
*/
|
|
76
|
+
export declare const getCachedItemById: <T extends {
|
|
77
|
+
id: string;
|
|
78
|
+
}>(cacheKey: string, itemId: string) => T | null;
|
|
79
|
+
/**
|
|
80
|
+
* Get synchronous cached single item
|
|
81
|
+
* Returns null if cache is missing or stale
|
|
82
|
+
*
|
|
83
|
+
* @param cacheKey - localStorage key for the cache
|
|
84
|
+
* @param expirationMs - Cache expiration time in milliseconds (default: 1 day)
|
|
85
|
+
* @returns T | null - Cached item or null
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* const workspace = getCachedSingleItemSync<WorkspaceBE>(LS_KEYS.WORKSPACE);
|
|
89
|
+
* if (workspace) {
|
|
90
|
+
* console.log(workspace.name);
|
|
91
|
+
* }
|
|
92
|
+
*/
|
|
93
|
+
export declare const getCachedSingleItemSync: <T>(cacheKey: string, expirationMs?: number) => T | null;
|
|
94
|
+
/**
|
|
95
|
+
* Get cached single item with API fallback
|
|
96
|
+
* Useful for workspace, profile, settings, etc.
|
|
97
|
+
*
|
|
98
|
+
* @param config - Cache configuration (cacheKey, apiUrl, expirationMs, responseKey)
|
|
99
|
+
* @param params - Optional query parameters for API request
|
|
100
|
+
* @returns Promise<T | null> - Cached or fresh item
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* const config = { cacheKey: LS_KEYS.WORKSPACE, apiUrl: '/api/workspace', responseKey: 'workspace' };
|
|
104
|
+
* const workspace = await getCachedSingleItem<WorkspaceBE>(config, { subdomain: 'school1' });
|
|
105
|
+
*/
|
|
106
|
+
export declare const getCachedSingleItem: <T>(config: CacheConfig, params?: Record<string, string>) => Promise<T | null>;
|
|
107
|
+
/**
|
|
108
|
+
* Invalidate (remove) cache for a module
|
|
109
|
+
* Useful after create/update/delete operations
|
|
110
|
+
*
|
|
111
|
+
* @param cacheKey - localStorage key for the cache
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* invalidateCache(LS_KEYS.USERS);
|
|
115
|
+
* const freshUsers = await getCachedData(config);
|
|
116
|
+
*/
|
|
117
|
+
export declare const invalidateCache: (cacheKey: string) => void;
|
|
118
|
+
/**
|
|
119
|
+
* Preload cache for a module
|
|
120
|
+
* Useful to call on app initialization or login
|
|
121
|
+
*
|
|
122
|
+
* @param config - Cache configuration
|
|
123
|
+
* @returns Promise<ListResponse<T>> - Preloaded data
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* const config = { cacheKey: LS_KEYS.USERS, apiUrl: USER_API_ROUTES.LIST, responseKey: 'users' };
|
|
127
|
+
* await preloadCache(config);
|
|
128
|
+
*/
|
|
129
|
+
export declare const preloadCache: <T>(config: CacheConfig) => Promise<ListResponse<T>>;
|
|
130
|
+
/**
|
|
131
|
+
* Check if cache is stale (older than expiration time)
|
|
132
|
+
*
|
|
133
|
+
* @param cacheKey - localStorage key for the cache
|
|
134
|
+
* @param expirationMs - Cache expiration time in milliseconds (default: 1 day)
|
|
135
|
+
* @returns boolean - True if cache is stale or doesn't exist
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* if (isCacheStale(LS_KEYS.USERS)) {
|
|
139
|
+
* await getCachedData(config); // This will fetch fresh data
|
|
140
|
+
* }
|
|
141
|
+
*/
|
|
142
|
+
export declare const isCacheStale: (cacheKey: string, expirationMs?: number) => boolean;
|
|
143
|
+
export {};
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Generic Cache Utilities
|
|
4
|
+
*
|
|
5
|
+
* Provides reusable localStorage-based caching for all modules with:
|
|
6
|
+
* - 1-day cache expiration
|
|
7
|
+
* - Automatic cache invalidation
|
|
8
|
+
* - Search and filter bypass (always fetch fresh)
|
|
9
|
+
* - Synchronous and asynchronous access patterns
|
|
10
|
+
* - Support for both list and single item caching
|
|
11
|
+
*
|
|
12
|
+
* Organization:
|
|
13
|
+
* - Types
|
|
14
|
+
* - List Cache Functions
|
|
15
|
+
* - Single Item Cache Functions
|
|
16
|
+
* - Cache Utilities
|
|
17
|
+
*/
|
|
18
|
+
var __assign = (this && this.__assign) || function () {
|
|
19
|
+
__assign = Object.assign || function(t) {
|
|
20
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
21
|
+
s = arguments[i];
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
23
|
+
t[p] = s[p];
|
|
24
|
+
}
|
|
25
|
+
return t;
|
|
26
|
+
};
|
|
27
|
+
return __assign.apply(this, arguments);
|
|
28
|
+
};
|
|
29
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
30
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
31
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
32
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
33
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
34
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
35
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
39
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
40
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
41
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
42
|
+
function step(op) {
|
|
43
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
44
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
45
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
46
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
47
|
+
switch (op[0]) {
|
|
48
|
+
case 0: case 1: t = op; break;
|
|
49
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
50
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
51
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
52
|
+
default:
|
|
53
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
54
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
55
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
56
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
57
|
+
if (t[2]) _.ops.pop();
|
|
58
|
+
_.trys.pop(); continue;
|
|
59
|
+
}
|
|
60
|
+
op = body.call(thisArg, _);
|
|
61
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
62
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
63
|
+
}
|
|
64
|
+
};
|
|
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
|
+
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;
|
|
76
|
+
var constants_1 = require("../constants");
|
|
77
|
+
var fetch_data_1 = require("./fetch-data");
|
|
78
|
+
var local_storage_1 = require("../local-storage");
|
|
79
|
+
// ============================================================================
|
|
80
|
+
// LIST CACHE FUNCTIONS
|
|
81
|
+
// ============================================================================
|
|
82
|
+
/**
|
|
83
|
+
* Get synchronous cached data for a module
|
|
84
|
+
* Returns empty result if cache is missing or stale
|
|
85
|
+
*
|
|
86
|
+
* @param cacheKey - localStorage key for the cache
|
|
87
|
+
* @param expirationMs - Cache expiration time in milliseconds (default: 1 day)
|
|
88
|
+
* @returns {count: number, items: T[]} - Cached data or empty array
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* const users = getCachedDataSync<UserBE>(LS_KEYS.USERS);
|
|
92
|
+
* if (users.items.length > 0) {
|
|
93
|
+
* console.log(users.items[0].name);
|
|
94
|
+
* }
|
|
95
|
+
*/
|
|
96
|
+
var getCachedDataSync = function (cacheKey, expirationMs) {
|
|
97
|
+
if (expirationMs === void 0) { expirationMs = constants_1.ONE_DAY_IN_MS; }
|
|
98
|
+
try {
|
|
99
|
+
var cachedData = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
100
|
+
if (!cachedData) {
|
|
101
|
+
return { count: 0, items: [] };
|
|
102
|
+
}
|
|
103
|
+
var currentTime = new Date().getTime();
|
|
104
|
+
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
105
|
+
var ageInMs = currentTime - cachedTime;
|
|
106
|
+
if (ageInMs < expirationMs) {
|
|
107
|
+
return {
|
|
108
|
+
count: cachedData.items.length,
|
|
109
|
+
items: cachedData.items,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return { count: 0, items: [] };
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error("Error getting cached data for ".concat(cacheKey, ":"), error);
|
|
116
|
+
return { count: 0, items: [] };
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
exports.getCachedDataSync = getCachedDataSync;
|
|
120
|
+
/**
|
|
121
|
+
* Get cached data with API fallback for a module
|
|
122
|
+
* Automatically handles cache validation and updates
|
|
123
|
+
*
|
|
124
|
+
* @param config - Cache configuration (cacheKey, apiUrl, expirationMs, responseKey)
|
|
125
|
+
* @param searchQuery - Optional search query (bypasses cache)
|
|
126
|
+
* @param filters - Optional filters object (bypasses cache)
|
|
127
|
+
* @param pageLimit - Number of items to fetch (default: 100)
|
|
128
|
+
* @returns Promise<{count: number, items: T[]}> - Cached or fresh data
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* const config = { cacheKey: LS_KEYS.USERS, apiUrl: USER_API_ROUTES.LIST, responseKey: 'users' };
|
|
132
|
+
* const users = await getCachedData<UserBE>(config);
|
|
133
|
+
*
|
|
134
|
+
* // With search (bypasses cache)
|
|
135
|
+
* const filtered = await getCachedData<UserBE>(config, 'John');
|
|
136
|
+
*
|
|
137
|
+
* // With filters (bypasses cache)
|
|
138
|
+
* const active = await getCachedData<UserBE>(config, undefined, { enabled: true });
|
|
139
|
+
*/
|
|
140
|
+
var getCachedData = function (config_1, searchQuery_1, filters_1) {
|
|
141
|
+
var args_1 = [];
|
|
142
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
143
|
+
args_1[_i - 3] = arguments[_i];
|
|
144
|
+
}
|
|
145
|
+
return __awaiter(void 0, __spreadArray([config_1, searchQuery_1, filters_1], args_1, true), void 0, function (config, searchQuery, filters, pageLimit) {
|
|
146
|
+
var cacheKey, apiUrl, _a, expirationMs, responseKey, params_1, response_1, items_1, cachedData, currentTime, cachedTime, ageInMs, params, response, items, count, updatedCache, error_1;
|
|
147
|
+
var _b, _c, _d, _e, _f, _g;
|
|
148
|
+
if (pageLimit === void 0) { pageLimit = 100; }
|
|
149
|
+
return __generator(this, function (_h) {
|
|
150
|
+
switch (_h.label) {
|
|
151
|
+
case 0:
|
|
152
|
+
cacheKey = config.cacheKey, apiUrl = config.apiUrl, _a = config.expirationMs, expirationMs = _a === void 0 ? constants_1.ONE_DAY_IN_MS : _a, responseKey = config.responseKey;
|
|
153
|
+
_h.label = 1;
|
|
154
|
+
case 1:
|
|
155
|
+
_h.trys.push([1, 5, , 6]);
|
|
156
|
+
if (!((searchQuery && searchQuery.trim()) ||
|
|
157
|
+
(filters && Object.keys(filters).length > 0))) return [3 /*break*/, 3];
|
|
158
|
+
params_1 = new URLSearchParams(__assign(__assign({ pageLimit: pageLimit.toString(), currentPage: '1' }, (searchQuery && { searchQuery: searchQuery })), (filters &&
|
|
159
|
+
Object.keys(filters).reduce(function (acc, key) {
|
|
160
|
+
acc[key] = String(filters[key]);
|
|
161
|
+
return acc;
|
|
162
|
+
}, {}))));
|
|
163
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
164
|
+
url: "".concat(apiUrl, "?").concat(params_1.toString()),
|
|
165
|
+
method: constants_1.API_METHODS.GET,
|
|
166
|
+
})];
|
|
167
|
+
case 2:
|
|
168
|
+
response_1 = _h.sent();
|
|
169
|
+
items_1 = responseKey
|
|
170
|
+
? ((_b = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _b === void 0 ? void 0 : _b[responseKey]) || []
|
|
171
|
+
: ((_c = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _c === void 0 ? void 0 : _c.items) || (response_1 === null || response_1 === void 0 ? void 0 : response_1.data) || [];
|
|
172
|
+
return [2 /*return*/, {
|
|
173
|
+
count: ((_d = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _d === void 0 ? void 0 : _d.count) || items_1.length,
|
|
174
|
+
items: items_1,
|
|
175
|
+
}];
|
|
176
|
+
case 3:
|
|
177
|
+
cachedData = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
178
|
+
currentTime = new Date().getTime();
|
|
179
|
+
if (cachedData) {
|
|
180
|
+
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
181
|
+
ageInMs = currentTime - cachedTime;
|
|
182
|
+
if (ageInMs < expirationMs) {
|
|
183
|
+
return [2 /*return*/, {
|
|
184
|
+
count: cachedData.items.length,
|
|
185
|
+
items: cachedData.items,
|
|
186
|
+
}];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
params = new URLSearchParams({
|
|
190
|
+
pageLimit: pageLimit.toString(),
|
|
191
|
+
currentPage: '1',
|
|
192
|
+
});
|
|
193
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
194
|
+
url: "".concat(apiUrl, "?").concat(params.toString()),
|
|
195
|
+
method: constants_1.API_METHODS.GET,
|
|
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);
|
|
209
|
+
}
|
|
210
|
+
return [2 /*return*/, { count: count, items: items }];
|
|
211
|
+
case 5:
|
|
212
|
+
error_1 = _h.sent();
|
|
213
|
+
console.error("Error fetching data for ".concat(cacheKey, ":"), error_1);
|
|
214
|
+
return [2 /*return*/, { count: 0, items: [] }];
|
|
215
|
+
case 6: return [2 /*return*/];
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
exports.getCachedData = getCachedData;
|
|
221
|
+
/**
|
|
222
|
+
* Get single cached item by ID from a list cache
|
|
223
|
+
*
|
|
224
|
+
* @param cacheKey - localStorage key for the cache
|
|
225
|
+
* @param itemId - ID of the item to retrieve
|
|
226
|
+
* @returns T | null - Cached item or null
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* const user = getCachedItemById<UserBE>(LS_KEYS.USERS, 'user-123');
|
|
230
|
+
* if (user) {
|
|
231
|
+
* console.log(user.name);
|
|
232
|
+
* }
|
|
233
|
+
*/
|
|
234
|
+
var getCachedItemById = function (cacheKey, itemId) {
|
|
235
|
+
if (!itemId) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
var items = (0, exports.getCachedDataSync)(cacheKey).items;
|
|
240
|
+
return items.find(function (item) { return item.id === itemId; }) || null;
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
console.error("Error getting cached item by ID for ".concat(cacheKey, ":"), error);
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
exports.getCachedItemById = getCachedItemById;
|
|
248
|
+
// ============================================================================
|
|
249
|
+
// SINGLE ITEM CACHE FUNCTIONS
|
|
250
|
+
// ============================================================================
|
|
251
|
+
/**
|
|
252
|
+
* Get synchronous cached single item
|
|
253
|
+
* Returns null if cache is missing or stale
|
|
254
|
+
*
|
|
255
|
+
* @param cacheKey - localStorage key for the cache
|
|
256
|
+
* @param expirationMs - Cache expiration time in milliseconds (default: 1 day)
|
|
257
|
+
* @returns T | null - Cached item or null
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* const workspace = getCachedSingleItemSync<WorkspaceBE>(LS_KEYS.WORKSPACE);
|
|
261
|
+
* if (workspace) {
|
|
262
|
+
* console.log(workspace.name);
|
|
263
|
+
* }
|
|
264
|
+
*/
|
|
265
|
+
var getCachedSingleItemSync = function (cacheKey, expirationMs) {
|
|
266
|
+
if (expirationMs === void 0) { expirationMs = constants_1.ONE_DAY_IN_MS; }
|
|
267
|
+
try {
|
|
268
|
+
var cachedData = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
269
|
+
if (!cachedData) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
var currentTime = new Date().getTime();
|
|
273
|
+
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
274
|
+
var ageInMs = currentTime - cachedTime;
|
|
275
|
+
if (ageInMs < expirationMs) {
|
|
276
|
+
return cachedData.item;
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
console.error("Error getting cached single item for ".concat(cacheKey, ":"), error);
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
exports.getCachedSingleItemSync = getCachedSingleItemSync;
|
|
286
|
+
/**
|
|
287
|
+
* Get cached single item with API fallback
|
|
288
|
+
* Useful for workspace, profile, settings, etc.
|
|
289
|
+
*
|
|
290
|
+
* @param config - Cache configuration (cacheKey, apiUrl, expirationMs, responseKey)
|
|
291
|
+
* @param params - Optional query parameters for API request
|
|
292
|
+
* @returns Promise<T | null> - Cached or fresh item
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* const config = { cacheKey: LS_KEYS.WORKSPACE, apiUrl: '/api/workspace', responseKey: 'workspace' };
|
|
296
|
+
* const workspace = await getCachedSingleItem<WorkspaceBE>(config, { subdomain: 'school1' });
|
|
297
|
+
*/
|
|
298
|
+
var getCachedSingleItem = function (config, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
299
|
+
var cacheKey, apiUrl, _a, expirationMs, cachedData, currentTime, cachedTime, ageInMs, queryParams, response, item, updatedCache, error_2;
|
|
300
|
+
return __generator(this, function (_b) {
|
|
301
|
+
switch (_b.label) {
|
|
302
|
+
case 0:
|
|
303
|
+
cacheKey = config.cacheKey, apiUrl = config.apiUrl, _a = config.expirationMs, expirationMs = _a === void 0 ? constants_1.ONE_DAY_IN_MS : _a;
|
|
304
|
+
_b.label = 1;
|
|
305
|
+
case 1:
|
|
306
|
+
_b.trys.push([1, 3, , 4]);
|
|
307
|
+
cachedData = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
308
|
+
currentTime = new Date().getTime();
|
|
309
|
+
if (cachedData) {
|
|
310
|
+
cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
311
|
+
ageInMs = currentTime - cachedTime;
|
|
312
|
+
if (ageInMs < expirationMs) {
|
|
313
|
+
return [2 /*return*/, cachedData.item];
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
queryParams = params
|
|
317
|
+
? "?".concat(new URLSearchParams(params).toString())
|
|
318
|
+
: '';
|
|
319
|
+
return [4 /*yield*/, (0, fetch_data_1.fetchData)({
|
|
320
|
+
url: "".concat(apiUrl).concat(queryParams),
|
|
321
|
+
method: constants_1.API_METHODS.GET,
|
|
322
|
+
})];
|
|
323
|
+
case 2:
|
|
324
|
+
response = _b.sent();
|
|
325
|
+
item = (response === null || response === void 0 ? void 0 : response.data) || null;
|
|
326
|
+
if (item) {
|
|
327
|
+
updatedCache = {
|
|
328
|
+
item: item,
|
|
329
|
+
cachedAt: new Date().toISOString(),
|
|
330
|
+
};
|
|
331
|
+
(0, local_storage_1.setStorageValue)(cacheKey, updatedCache);
|
|
332
|
+
}
|
|
333
|
+
return [2 /*return*/, item];
|
|
334
|
+
case 3:
|
|
335
|
+
error_2 = _b.sent();
|
|
336
|
+
console.error("Error fetching single item for ".concat(cacheKey, ":"), error_2);
|
|
337
|
+
return [2 /*return*/, null];
|
|
338
|
+
case 4: return [2 /*return*/];
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}); };
|
|
342
|
+
exports.getCachedSingleItem = getCachedSingleItem;
|
|
343
|
+
// ============================================================================
|
|
344
|
+
// CACHE UTILITIES
|
|
345
|
+
// ============================================================================
|
|
346
|
+
/**
|
|
347
|
+
* Invalidate (remove) cache for a module
|
|
348
|
+
* Useful after create/update/delete operations
|
|
349
|
+
*
|
|
350
|
+
* @param cacheKey - localStorage key for the cache
|
|
351
|
+
*
|
|
352
|
+
* @example
|
|
353
|
+
* invalidateCache(LS_KEYS.USERS);
|
|
354
|
+
* const freshUsers = await getCachedData(config);
|
|
355
|
+
*/
|
|
356
|
+
var invalidateCache = function (cacheKey) {
|
|
357
|
+
try {
|
|
358
|
+
localStorage.removeItem(cacheKey);
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
console.error("Error invalidating cache for ".concat(cacheKey, ":"), error);
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
exports.invalidateCache = invalidateCache;
|
|
365
|
+
/**
|
|
366
|
+
* Preload cache for a module
|
|
367
|
+
* Useful to call on app initialization or login
|
|
368
|
+
*
|
|
369
|
+
* @param config - Cache configuration
|
|
370
|
+
* @returns Promise<ListResponse<T>> - Preloaded data
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* const config = { cacheKey: LS_KEYS.USERS, apiUrl: USER_API_ROUTES.LIST, responseKey: 'users' };
|
|
374
|
+
* await preloadCache(config);
|
|
375
|
+
*/
|
|
376
|
+
var preloadCache = function (config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
377
|
+
return __generator(this, function (_a) {
|
|
378
|
+
return [2 /*return*/, (0, exports.getCachedData)(config)];
|
|
379
|
+
});
|
|
380
|
+
}); };
|
|
381
|
+
exports.preloadCache = preloadCache;
|
|
382
|
+
/**
|
|
383
|
+
* Check if cache is stale (older than expiration time)
|
|
384
|
+
*
|
|
385
|
+
* @param cacheKey - localStorage key for the cache
|
|
386
|
+
* @param expirationMs - Cache expiration time in milliseconds (default: 1 day)
|
|
387
|
+
* @returns boolean - True if cache is stale or doesn't exist
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* if (isCacheStale(LS_KEYS.USERS)) {
|
|
391
|
+
* await getCachedData(config); // This will fetch fresh data
|
|
392
|
+
* }
|
|
393
|
+
*/
|
|
394
|
+
var isCacheStale = function (cacheKey, expirationMs) {
|
|
395
|
+
if (expirationMs === void 0) { expirationMs = constants_1.ONE_DAY_IN_MS; }
|
|
396
|
+
try {
|
|
397
|
+
var cachedData = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
398
|
+
if (!cachedData) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
var currentTime = new Date().getTime();
|
|
402
|
+
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
403
|
+
var ageInMs = currentTime - cachedTime;
|
|
404
|
+
return ageInMs >= expirationMs;
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
console.error("Error checking cache staleness for ".concat(cacheKey, ":"), error);
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
exports.isCacheStale = isCacheStale;
|
package/general/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export * from './generate-faq-schema';
|
|
|
24
24
|
export * from './generate-grid';
|
|
25
25
|
export * from './generate-organization-schema';
|
|
26
26
|
export * from './generate-ref';
|
|
27
|
+
export * from './generic-cache';
|
|
27
28
|
export * from './get-available-page-limit';
|
|
28
29
|
export * from './get-calendar-current-month';
|
|
29
30
|
export * from './get-calendar-dates';
|
package/general/index.js
CHANGED
|
@@ -40,6 +40,7 @@ __exportStar(require("./generate-faq-schema"), exports);
|
|
|
40
40
|
__exportStar(require("./generate-grid"), exports);
|
|
41
41
|
__exportStar(require("./generate-organization-schema"), exports);
|
|
42
42
|
__exportStar(require("./generate-ref"), exports);
|
|
43
|
+
__exportStar(require("./generic-cache"), exports);
|
|
43
44
|
__exportStar(require("./get-available-page-limit"), exports);
|
|
44
45
|
__exportStar(require("./get-calendar-current-month"), exports);
|
|
45
46
|
__exportStar(require("./get-calendar-dates"), exports);
|
package/hooks/use-fetch.d.ts
CHANGED
|
@@ -13,6 +13,17 @@ export interface FetchConfig extends Omit<RequestInit, 'credentials'> {
|
|
|
13
13
|
* (e.g. 'omit' | 'same-origin' | 'include').
|
|
14
14
|
*/
|
|
15
15
|
credentials?: boolean | RequestCredentials;
|
|
16
|
+
/**
|
|
17
|
+
* Query parameters to append to GET requests.
|
|
18
|
+
* Supports strings, numbers, booleans, arrays, and objects.
|
|
19
|
+
* - Primitives (string, number, boolean) are converted to strings
|
|
20
|
+
* - Arrays are repeated as multiple params with the same key
|
|
21
|
+
* - Objects are JSON-stringified
|
|
22
|
+
* - null and undefined values are skipped
|
|
23
|
+
*
|
|
24
|
+
* Example: { active: true, page: 1, tags: ['a', 'b'] }
|
|
25
|
+
* Results in: ?active=true&page=1&tags=a&tags=b
|
|
26
|
+
*/
|
|
16
27
|
params?: Record<string, any>;
|
|
17
28
|
}
|
|
18
29
|
export interface Return {
|
|
@@ -19,12 +19,18 @@
|
|
|
19
19
|
* using a short timeout (default 300ms) to avoid unnecessary list reloads.
|
|
20
20
|
*
|
|
21
21
|
* Parameters (object)
|
|
22
|
-
* - `byIdCallback(data)`, `byIdDeps`, `byIdParams`
|
|
23
|
-
* - `deleteCallback(data)`, `deleteDeps`, `deleteParams`
|
|
24
|
-
* - `listCallback(data)`, `listDeps`, `listParams`, `listUrl`
|
|
22
|
+
* - `byIdCallback(data)`, `byIdDeps`, `byIdParams` — params for GET by ID request
|
|
23
|
+
* - `deleteCallback(data)`, `deleteDeps`, `deleteParams` — body for DELETE request
|
|
24
|
+
* - `listCallback(data)`, `listDeps`, `listParams`, `listUrl` — params for GET list
|
|
25
25
|
* - `searchQuery` (string) — debounced before influencing the list fetch
|
|
26
26
|
* - `unitByIdUrl`, `unitUrl`
|
|
27
|
-
* - `updateCallback(data)`, `updateDeps`, `updateParams`
|
|
27
|
+
* - `updateCallback(data)`, `updateDeps`, `updateParams` — body for PUT request
|
|
28
|
+
*
|
|
29
|
+
* Parameter types
|
|
30
|
+
* - `listParams` and `byIdParams` support all primitive types including booleans.
|
|
31
|
+
* They are appended as URL query parameters for GET requests.
|
|
32
|
+
* Example: { active: true, page: 1 } → ?active=true&page=1
|
|
33
|
+
* - `updateParams` and `deleteParams` are sent as JSON in request body.
|
|
28
34
|
*
|
|
29
35
|
* Return values
|
|
30
36
|
* - `byIdFetchNow(url?, config?)`, `byIdLoading`, `byIdError`
|
|
@@ -44,7 +50,7 @@
|
|
|
44
50
|
* Example
|
|
45
51
|
* const { listFetchNow, updateFetchNow } = useModuleEntityV2({
|
|
46
52
|
* listUrl: '/items',
|
|
47
|
-
* listParams: { page: 1 },
|
|
53
|
+
* listParams: { page: 1, active: true },
|
|
48
54
|
* listCallback: (res) => console.log(res),
|
|
49
55
|
* unitUrl: '/items',
|
|
50
56
|
* updateCallback: () => {},
|
|
@@ -20,12 +20,18 @@
|
|
|
20
20
|
* using a short timeout (default 300ms) to avoid unnecessary list reloads.
|
|
21
21
|
*
|
|
22
22
|
* Parameters (object)
|
|
23
|
-
* - `byIdCallback(data)`, `byIdDeps`, `byIdParams`
|
|
24
|
-
* - `deleteCallback(data)`, `deleteDeps`, `deleteParams`
|
|
25
|
-
* - `listCallback(data)`, `listDeps`, `listParams`, `listUrl`
|
|
23
|
+
* - `byIdCallback(data)`, `byIdDeps`, `byIdParams` — params for GET by ID request
|
|
24
|
+
* - `deleteCallback(data)`, `deleteDeps`, `deleteParams` — body for DELETE request
|
|
25
|
+
* - `listCallback(data)`, `listDeps`, `listParams`, `listUrl` — params for GET list
|
|
26
26
|
* - `searchQuery` (string) — debounced before influencing the list fetch
|
|
27
27
|
* - `unitByIdUrl`, `unitUrl`
|
|
28
|
-
* - `updateCallback(data)`, `updateDeps`, `updateParams`
|
|
28
|
+
* - `updateCallback(data)`, `updateDeps`, `updateParams` — body for PUT request
|
|
29
|
+
*
|
|
30
|
+
* Parameter types
|
|
31
|
+
* - `listParams` and `byIdParams` support all primitive types including booleans.
|
|
32
|
+
* They are appended as URL query parameters for GET requests.
|
|
33
|
+
* Example: { active: true, page: 1 } → ?active=true&page=1
|
|
34
|
+
* - `updateParams` and `deleteParams` are sent as JSON in request body.
|
|
29
35
|
*
|
|
30
36
|
* Return values
|
|
31
37
|
* - `byIdFetchNow(url?, config?)`, `byIdLoading`, `byIdError`
|
|
@@ -45,7 +51,7 @@
|
|
|
45
51
|
* Example
|
|
46
52
|
* const { listFetchNow, updateFetchNow } = useModuleEntityV2({
|
|
47
53
|
* listUrl: '/items',
|
|
48
|
-
* listParams: { page: 1 },
|
|
54
|
+
* listParams: { page: 1, active: true },
|
|
49
55
|
* listCallback: (res) => console.log(res),
|
|
50
56
|
* unitUrl: '/items',
|
|
51
57
|
* updateCallback: () => {},
|
|
@@ -94,7 +100,7 @@ var useModuleEntityV2 = function (_a) {
|
|
|
94
100
|
try {
|
|
95
101
|
// pass current list params explicitly so the fetch uses the latest values
|
|
96
102
|
listFetchNow(undefined, {
|
|
97
|
-
method: constants_1.API_METHODS.
|
|
103
|
+
method: constants_1.API_METHODS.GET,
|
|
98
104
|
params: listParams,
|
|
99
105
|
});
|
|
100
106
|
}
|