@docbrasil/api-systemmanager 1.1.56 → 1.1.57

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