@docbrasil/api-systemmanager 1.1.44 → 1.1.45
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/dispatch.js +102 -4
- package/dist/bundle.cjs +102 -4
- package/dist/bundle.mjs +1 -1
- package/package.json +1 -1
package/api/dispatch.js
CHANGED
|
@@ -6,15 +6,72 @@ import Axios from 'axios';
|
|
|
6
6
|
* @class Api dispatch manager
|
|
7
7
|
*/
|
|
8
8
|
class Dispatch {
|
|
9
|
-
|
|
10
9
|
constructor(options) {
|
|
11
|
-
|
|
12
10
|
Joi.assert(options, Joi.object().required());
|
|
13
11
|
Joi.assert(options.parent, Joi.object().required());
|
|
14
12
|
|
|
15
13
|
const self = this;
|
|
16
14
|
self.parent = options.parent;
|
|
15
|
+
self._cache = options.cache;
|
|
17
16
|
self._client = Axios.create({baseURL: self.parent.options.uri, withCredentials: true});
|
|
17
|
+
|
|
18
|
+
// Add request interceptor for offline handling
|
|
19
|
+
self._client.interceptors.request.use(
|
|
20
|
+
async (config) => {
|
|
21
|
+
if (self._isOnline()) {
|
|
22
|
+
return config;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const cachedData = await self._getCache(config.url, {
|
|
26
|
+
method: config.method,
|
|
27
|
+
data: config.data,
|
|
28
|
+
params: config.params,
|
|
29
|
+
headers: config.headers
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (cachedData) {
|
|
33
|
+
// Cancel the actual request and return cached data
|
|
34
|
+
const dummyResponse = {
|
|
35
|
+
status: 200,
|
|
36
|
+
data: cachedData,
|
|
37
|
+
headers: {},
|
|
38
|
+
config,
|
|
39
|
+
cached: true
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Throwing a special error that includes our cached response
|
|
43
|
+
throw {
|
|
44
|
+
__CACHE_HIT__: true,
|
|
45
|
+
response: dummyResponse
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
throw new Error('Network error: No internet connection and no cached data available');
|
|
50
|
+
},
|
|
51
|
+
error => Promise.reject(error)
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// Add response interceptor to handle cache hits and cache successful responses
|
|
55
|
+
self._client.interceptors.response.use(
|
|
56
|
+
async response => {
|
|
57
|
+
// Cache successful responses when online
|
|
58
|
+
if (response.status === 200 && self._isOnline()) {
|
|
59
|
+
await self._setCache(response.config.url, {
|
|
60
|
+
method: response.config.method,
|
|
61
|
+
data: response.config.data,
|
|
62
|
+
params: response.config.params,
|
|
63
|
+
headers: response.config.headers
|
|
64
|
+
}, response.data);
|
|
65
|
+
}
|
|
66
|
+
return response;
|
|
67
|
+
},
|
|
68
|
+
error => {
|
|
69
|
+
if (error.__CACHE_HIT__) {
|
|
70
|
+
return error.response;
|
|
71
|
+
}
|
|
72
|
+
return Promise.reject(error);
|
|
73
|
+
}
|
|
74
|
+
);
|
|
18
75
|
}
|
|
19
76
|
|
|
20
77
|
/**
|
|
@@ -47,6 +104,47 @@ class Dispatch {
|
|
|
47
104
|
};
|
|
48
105
|
}
|
|
49
106
|
|
|
107
|
+
/**
|
|
108
|
+
* @description Check if the browser/client is currently online
|
|
109
|
+
* @return {boolean} True if online, false if offline
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
_isOnline() {
|
|
113
|
+
return typeof navigator !== 'undefined' && navigator.onLine && this._cache;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @description Get cached data for a specific request
|
|
118
|
+
* @param {string} url The request URL
|
|
119
|
+
* @param {object} options Request options including method, data, params, and headers
|
|
120
|
+
* @return {Promise<object|null>} Cached data or null if no cache exists
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
async _getCache(url, options) {
|
|
124
|
+
try {
|
|
125
|
+
return await this._cache.getCache(url, options);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.warn('Cache retrieval failed:', error);
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @description Set data in cache for a specific request
|
|
134
|
+
* @param {string} url The request URL
|
|
135
|
+
* @param {object} options Request options including method, data, params, and headers
|
|
136
|
+
* @param {object} data The data to cache
|
|
137
|
+
* @return {Promise<void>}
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
async _setCache(url, options, data) {
|
|
141
|
+
try {
|
|
142
|
+
await this._cache.setCache(url, options, data);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.warn('Cache storage failed:', error);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
50
148
|
/**
|
|
51
149
|
* Get the URL context
|
|
52
150
|
* @param url {string} Full url
|
|
@@ -72,8 +170,8 @@ class Dispatch {
|
|
|
72
170
|
|
|
73
171
|
const self = this;
|
|
74
172
|
const header = session ? self._setHeader(session) : {};
|
|
75
|
-
const apiCall = self._client.get(url, header);
|
|
76
|
-
return self._returnData(
|
|
173
|
+
const apiCall = await self._client.get(url, header);
|
|
174
|
+
return self._returnData(apiCall);
|
|
77
175
|
}
|
|
78
176
|
|
|
79
177
|
/**
|
package/dist/bundle.cjs
CHANGED
|
@@ -18,15 +18,72 @@ var Moment__default = /*#__PURE__*/_interopDefaultLegacy(Moment);
|
|
|
18
18
|
* @class Api dispatch manager
|
|
19
19
|
*/
|
|
20
20
|
class Dispatch {
|
|
21
|
-
|
|
22
21
|
constructor(options) {
|
|
23
|
-
|
|
24
22
|
Joi__default["default"].assert(options, Joi__default["default"].object().required());
|
|
25
23
|
Joi__default["default"].assert(options.parent, Joi__default["default"].object().required());
|
|
26
24
|
|
|
27
25
|
const self = this;
|
|
28
26
|
self.parent = options.parent;
|
|
27
|
+
self._cache = options.cache;
|
|
29
28
|
self._client = Axios__default["default"].create({baseURL: self.parent.options.uri, withCredentials: true});
|
|
29
|
+
|
|
30
|
+
// Add request interceptor for offline handling
|
|
31
|
+
self._client.interceptors.request.use(
|
|
32
|
+
async (config) => {
|
|
33
|
+
if (self._isOnline()) {
|
|
34
|
+
return config;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const cachedData = await self._getCache(config.url, {
|
|
38
|
+
method: config.method,
|
|
39
|
+
data: config.data,
|
|
40
|
+
params: config.params,
|
|
41
|
+
headers: config.headers
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (cachedData) {
|
|
45
|
+
// Cancel the actual request and return cached data
|
|
46
|
+
const dummyResponse = {
|
|
47
|
+
status: 200,
|
|
48
|
+
data: cachedData,
|
|
49
|
+
headers: {},
|
|
50
|
+
config,
|
|
51
|
+
cached: true
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Throwing a special error that includes our cached response
|
|
55
|
+
throw {
|
|
56
|
+
__CACHE_HIT__: true,
|
|
57
|
+
response: dummyResponse
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
throw new Error('Network error: No internet connection and no cached data available');
|
|
62
|
+
},
|
|
63
|
+
error => Promise.reject(error)
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Add response interceptor to handle cache hits and cache successful responses
|
|
67
|
+
self._client.interceptors.response.use(
|
|
68
|
+
async response => {
|
|
69
|
+
// Cache successful responses when online
|
|
70
|
+
if (response.status === 200 && self._isOnline()) {
|
|
71
|
+
await self._setCache(response.config.url, {
|
|
72
|
+
method: response.config.method,
|
|
73
|
+
data: response.config.data,
|
|
74
|
+
params: response.config.params,
|
|
75
|
+
headers: response.config.headers
|
|
76
|
+
}, response.data);
|
|
77
|
+
}
|
|
78
|
+
return response;
|
|
79
|
+
},
|
|
80
|
+
error => {
|
|
81
|
+
if (error.__CACHE_HIT__) {
|
|
82
|
+
return error.response;
|
|
83
|
+
}
|
|
84
|
+
return Promise.reject(error);
|
|
85
|
+
}
|
|
86
|
+
);
|
|
30
87
|
}
|
|
31
88
|
|
|
32
89
|
/**
|
|
@@ -59,6 +116,47 @@ class Dispatch {
|
|
|
59
116
|
};
|
|
60
117
|
}
|
|
61
118
|
|
|
119
|
+
/**
|
|
120
|
+
* @description Check if the browser/client is currently online
|
|
121
|
+
* @return {boolean} True if online, false if offline
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
_isOnline() {
|
|
125
|
+
return typeof navigator !== 'undefined' && navigator.onLine && this._cache;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @description Get cached data for a specific request
|
|
130
|
+
* @param {string} url The request URL
|
|
131
|
+
* @param {object} options Request options including method, data, params, and headers
|
|
132
|
+
* @return {Promise<object|null>} Cached data or null if no cache exists
|
|
133
|
+
* @private
|
|
134
|
+
*/
|
|
135
|
+
async _getCache(url, options) {
|
|
136
|
+
try {
|
|
137
|
+
return await this._cache.getCache(url, options);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.warn('Cache retrieval failed:', error);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* @description Set data in cache for a specific request
|
|
146
|
+
* @param {string} url The request URL
|
|
147
|
+
* @param {object} options Request options including method, data, params, and headers
|
|
148
|
+
* @param {object} data The data to cache
|
|
149
|
+
* @return {Promise<void>}
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
152
|
+
async _setCache(url, options, data) {
|
|
153
|
+
try {
|
|
154
|
+
await this._cache.setCache(url, options, data);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.warn('Cache storage failed:', error);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
62
160
|
/**
|
|
63
161
|
* Get the URL context
|
|
64
162
|
* @param url {string} Full url
|
|
@@ -84,8 +182,8 @@ class Dispatch {
|
|
|
84
182
|
|
|
85
183
|
const self = this;
|
|
86
184
|
const header = session ? self._setHeader(session) : {};
|
|
87
|
-
const apiCall = self._client.get(url, header);
|
|
88
|
-
return self._returnData(
|
|
185
|
+
const apiCall = await self._client.get(url, header);
|
|
186
|
+
return self._returnData(apiCall);
|
|
89
187
|
}
|
|
90
188
|
|
|
91
189
|
/**
|