@docbrasil/api-systemmanager 1.1.54 → 1.1.56

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 CHANGED
@@ -19,13 +19,15 @@ class Dispatch {
19
19
  withCredentials: true
20
20
  });
21
21
 
22
- // Add request interceptor for offline handling
22
+ // Add request interceptor for offline handling.
23
23
  self._client.interceptors.request.use(
24
24
  async (config) => {
25
+ // If online or no cache is provided, proceed normally.
25
26
  if (self._isOnline() || !self._cache) {
26
27
  return config;
27
28
  }
28
29
 
30
+ // We're offline and caching is enabled – try to retrieve cached data.
29
31
  const cachedData = await self._getCache(config.url, {
30
32
  method: config.method,
31
33
  data: config.data,
@@ -34,59 +36,75 @@ class Dispatch {
34
36
  });
35
37
 
36
38
  if (cachedData) {
37
- return {
38
- url: config.url,
39
- method: config.method,
40
- data: cachedData,
41
- params: config.params,
42
- headers: config.headers,
43
- cached: true
44
- };
39
+ // Instead of returning the modified config (which would trigger a network call),
40
+ // return a rejected promise with a flag and a fake response.
41
+ return Promise.reject({
42
+ __fromCache: true,
43
+ response: {
44
+ data: cachedData,
45
+ status: 200,
46
+ statusText: 'OK',
47
+ headers: config.headers,
48
+ config: config,
49
+ request: {} // empty placeholder
50
+ }
51
+ });
45
52
  }
46
53
 
54
+ // No cached data found – signal an offline error.
47
55
  self.errorOffline();
48
- throw new Error('Network error: No internet connection and no cached data available');
56
+ return Promise.reject(new Error('Network error: No internet connection and no cached data available'));
49
57
  },
50
58
  error => Promise.reject(error)
51
59
  );
52
60
 
53
- // Add response interceptor to handle caching
61
+ // Add response interceptor to handle caching and to return a cached response when available.
54
62
  self._client.interceptors.response.use(
55
63
  async response => {
56
- if (response.status === 200 && self._isOnline() && self._cache && !response.cached) {
57
- await self._setCache(response.config.url, {
58
- method: response.config.method,
59
- data: response.config.data,
60
- params: response.config.params,
61
- headers: response.config.headers
62
- }, response.data);
64
+ // If online, the response is OK, caching is enabled, and this wasn’t a cached response,
65
+ // then cache the new response data.
66
+ if (response.status === 200 && self._isOnline() && self._cache && !response.config.cached) {
67
+ await self._setCache(
68
+ response.config.url,
69
+ {
70
+ method: response.config.method,
71
+ data: response.config.data,
72
+ params: response.config.params,
73
+ headers: response.config.headers
74
+ },
75
+ response.data
76
+ );
63
77
  }
64
78
  return response;
65
79
  },
66
- error => Promise.reject(error)
80
+ error => {
81
+ // If the error was generated because we had a cached response, then return that response.
82
+ if (error.__fromCache && error.response) {
83
+ return Promise.resolve(error.response);
84
+ }
85
+ return Promise.reject(error);
86
+ }
67
87
  );
68
88
  }
69
89
 
70
90
  /**
71
- * @author Augusto Pissarra <abernardo.br@gmail.com>
72
- * @description Get the return data and check for errors
73
- * @param {object} retData Response HTTP
91
+ * @description Get the return data and check for errors.
92
+ * @param {object} retData Response HTTP.
93
+ * @param {*} [def={}] Default value to return if no data is found.
74
94
  * @return {*}
75
95
  * @private
76
96
  */
77
97
  _returnData(retData, def = {}) {
78
98
  if (retData.status !== 200) {
79
- throw Boom.badRequest(_.get(retData, 'message', 'No error message reported!'))
80
- } else {
81
- return _.get(retData, 'data', def);
99
+ throw new Error(_.get(retData, 'message', 'No error message reported!'));
82
100
  }
101
+ return _.get(retData, 'data', def);
83
102
  }
84
103
 
85
104
  /**
86
- * @author Myndware <augusto.pissarra@myndware.com>
87
- * @description Set header with new session
88
- * @param {string} session Session, token JWT
89
- * @return {object} header with new session
105
+ * @description Set header with new session.
106
+ * @param {string} session Session token (JWT).
107
+ * @return {object} Header object with the new session.
90
108
  * @private
91
109
  */
92
110
  _setHeader(session) {
@@ -98,20 +116,20 @@ class Dispatch {
98
116
  }
99
117
 
100
118
  /**
101
- * @description Check if the browser/client is currently online
102
- * @return {boolean} True if online, false if offline
119
+ * @description Check if the browser/client is currently online.
120
+ * @return {boolean} True if online, false if offline.
103
121
  * @private
104
122
  */
105
123
  _isOnline() {
106
- if(this._forceCache) return false;
124
+ if (this._forceCache) return false;
107
125
  return typeof navigator !== 'undefined' && navigator.onLine;
108
126
  }
109
127
 
110
128
  /**
111
- * @description Get cached data for a specific request
112
- * @param {string} url The request URL
113
- * @param {object} options Request options including method, data, params, and headers
114
- * @return {Promise<object|null>} Cached data or null if no cache exists
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.
115
133
  * @private
116
134
  */
117
135
  async _getCache(url, options) {
@@ -124,10 +142,10 @@ class Dispatch {
124
142
  }
125
143
 
126
144
  /**
127
- * @description Set data in cache for a specific request
128
- * @param {string} url The request URL
129
- * @param {object} options Request options including method, data, params, and headers
130
- * @param {object} data The data to cache
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.
131
149
  * @return {Promise<void>}
132
150
  * @private
133
151
  */
@@ -139,28 +157,28 @@ class Dispatch {
139
157
  }
140
158
  }
141
159
 
160
+ /**
161
+ * Called when no cache is available and the client is offline.
162
+ */
142
163
  errorOffline() {
143
- this._cache.errorOffline();
164
+ if (this._cache && typeof this._cache.errorOffline === 'function') {
165
+ this._cache.errorOffline();
166
+ }
144
167
  }
145
168
 
146
169
  /**
147
- * Get the URL context
148
- * @param url {string} Full url
149
- * @param session {session} Session, token JWT
150
- * @return {Promise<object>} The full data context of the URL
170
+ * Get the URL context.
171
+ * @param {string} url Full URL.
172
+ * @param {string|null} [session=null] Session token (JWT).
173
+ * @return {Promise<object>} The full data context of the URL.
151
174
  * @public
152
175
  * @async
153
- * @example
154
- *
155
- * const API = require('@docbrasil/api-systemmanager');
156
- * const api = new API();
157
- * const retContext = await api.dispatch.getContext('http://myndware.io/login/myorg);
158
- *
159
176
  */
160
177
  async getContext(url, session = null) {
161
178
  Joi.assert(url, Joi.string().required());
162
179
 
163
- if(url.includes('?')) {
180
+ // Append the json flag to the URL.
181
+ if (url.includes('?')) {
164
182
  url = `${url}&json=true`;
165
183
  } else {
166
184
  url = `${url}?json=true`;
@@ -173,24 +191,12 @@ class Dispatch {
173
191
  }
174
192
 
175
193
  /**
176
- * @author Myndware <augusto.pissarra@myndware.com>
177
- * @description Get client Axios
178
- * @return {promise} return client axios
194
+ * @description Get the Axios client.
195
+ * @return {AxiosInstance} The Axios client.
179
196
  * @public
180
- * @async
181
- * @example
182
- *
183
- * const API = require('@docbrasil/api-systemmanager');
184
- * const api = new API();
185
- * await api.dispatch.getClient();
186
197
  */
187
198
  getClient() {
188
- try {
189
- const self = this;
190
- return self._client;
191
- } catch (ex) {
192
- return ex;
193
- }
199
+ return this._client;
194
200
  }
195
201
  }
196
202
 
package/dist/bundle.cjs CHANGED
@@ -46,14 +46,9 @@ class Dispatch {
46
46
  });
47
47
 
48
48
  if (cachedData) {
49
- return {
50
- url: config.url,
51
- method: config.method,
52
- data: cachedData,
53
- params: config.params,
54
- headers: config.headers,
55
- cached: true
56
- };
49
+ config.cached = true;
50
+ config.data = cachedData;
51
+ return config;
57
52
  }
58
53
 
59
54
  self.errorOffline();