@docbrasil/api-systemmanager 1.1.51 → 1.1.53

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
@@ -14,7 +14,10 @@ class Dispatch {
14
14
  self._cache = options.parent.options.cache;
15
15
  self._forceCache = options.parent.options.forceCache;
16
16
  self.parent = options.parent;
17
- self._client = Axios.create({baseURL: self.parent.options.uri, withCredentials: true});
17
+ self._client = Axios.create({
18
+ baseURL: self.parent.options.uri,
19
+ withCredentials: true
20
+ });
18
21
 
19
22
  // Add request interceptor for offline handling
20
23
  self._client.interceptors.request.use(
@@ -31,20 +34,13 @@ class Dispatch {
31
34
  });
32
35
 
33
36
  if (cachedData) {
34
- // Cancel the actual request and return cached data
35
- const dummyResponse = {
37
+ return Promise.resolve({
36
38
  status: 200,
37
39
  data: cachedData,
38
40
  headers: {},
39
41
  config,
40
42
  cached: true
41
- };
42
-
43
- // Throwing a special error that includes our cached response
44
- throw {
45
- __CACHE_HIT__: true,
46
- response: dummyResponse
47
- };
43
+ });
48
44
  }
49
45
 
50
46
  self.errorOffline();
@@ -53,11 +49,10 @@ class Dispatch {
53
49
  error => Promise.reject(error)
54
50
  );
55
51
 
56
- // Add response interceptor to handle cache hits and cache successful responses
52
+ // Add response interceptor to handle caching
57
53
  self._client.interceptors.response.use(
58
54
  async response => {
59
- // Cache successful responses when online
60
- if (response.status === 200 && self._isOnline() && self._cache) {
55
+ if (response.status === 200 && self._isOnline() && self._cache && !response.cached) {
61
56
  await self._setCache(response.config.url, {
62
57
  method: response.config.method,
63
58
  data: response.config.data,
@@ -67,12 +62,7 @@ class Dispatch {
67
62
  }
68
63
  return response;
69
64
  },
70
- error => {
71
- if (error.__CACHE_HIT__) {
72
- return error.response;
73
- }
74
- return Promise.reject(error);
75
- }
65
+ error => Promise.reject(error)
76
66
  );
77
67
  }
78
68
 
@@ -120,6 +120,45 @@ class Application {
120
120
  throw ex;
121
121
  }
122
122
  }
123
+
124
+ /**
125
+ * @author Myndware <augusto.pissarra@myndware.com>
126
+ * @description Gets the application and pages to start the cache process
127
+ * @param {object} params
128
+ * @param {object} params.orgId the orgId of this application
129
+ * @param {object} params.appId the application id
130
+ * @param {string} session Session, token JWT
131
+ * @returns {promise}
132
+ * @public
133
+ * @example
134
+ *
135
+ * const API = require('@docbrasil/api-systemmanager');
136
+ * const api = new API();
137
+ * const params = {
138
+ * orgId: '55e4a3bd6be6b45210833f78',
139
+ * appId: '55e4a3bd6be6b45210833fae',
140
+ * };
141
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
142
+ * await api.user.application.getCache(params, session);
143
+ */
144
+ async getCache(params, session) {
145
+ const self = this;
146
+
147
+ try {
148
+ Joi.assert(params, Joi.object().required(), 'Params to get task');
149
+ Joi.assert(params.orgId, Joi.string().required(), 'The organizations id');
150
+ Joi.assert(params.appId, Joi.string().required(), 'The application id');
151
+ Joi.assert(session, Joi.string().required(), 'Session token JWT');
152
+
153
+ const { orgId, appId} = params;
154
+ const apiCall = self._client
155
+ .get(`/organizations/${orgId}/applications/${appId}/cache`, self._setHeader(session));
156
+
157
+ return self._returnData(await apiCall);
158
+ } catch (ex) {
159
+ throw ex;
160
+ }
161
+ }
123
162
  }
124
163
 
125
164
  export default Application;
package/dist/bundle.cjs CHANGED
@@ -26,7 +26,10 @@ class Dispatch {
26
26
  self._cache = options.parent.options.cache;
27
27
  self._forceCache = options.parent.options.forceCache;
28
28
  self.parent = options.parent;
29
- self._client = Axios__default["default"].create({baseURL: self.parent.options.uri, withCredentials: true});
29
+ self._client = Axios__default["default"].create({
30
+ baseURL: self.parent.options.uri,
31
+ withCredentials: true
32
+ });
30
33
 
31
34
  // Add request interceptor for offline handling
32
35
  self._client.interceptors.request.use(
@@ -43,20 +46,13 @@ class Dispatch {
43
46
  });
44
47
 
45
48
  if (cachedData) {
46
- // Cancel the actual request and return cached data
47
- const dummyResponse = {
49
+ return Promise.resolve({
48
50
  status: 200,
49
51
  data: cachedData,
50
52
  headers: {},
51
53
  config,
52
54
  cached: true
53
- };
54
-
55
- // Throwing a special error that includes our cached response
56
- throw {
57
- __CACHE_HIT__: true,
58
- response: dummyResponse
59
- };
55
+ });
60
56
  }
61
57
 
62
58
  self.errorOffline();
@@ -65,11 +61,10 @@ class Dispatch {
65
61
  error => Promise.reject(error)
66
62
  );
67
63
 
68
- // Add response interceptor to handle cache hits and cache successful responses
64
+ // Add response interceptor to handle caching
69
65
  self._client.interceptors.response.use(
70
66
  async response => {
71
- // Cache successful responses when online
72
- if (response.status === 200 && self._isOnline() && self._cache) {
67
+ if (response.status === 200 && self._isOnline() && self._cache && !response.cached) {
73
68
  await self._setCache(response.config.url, {
74
69
  method: response.config.method,
75
70
  data: response.config.data,
@@ -79,12 +74,7 @@ class Dispatch {
79
74
  }
80
75
  return response;
81
76
  },
82
- error => {
83
- if (error.__CACHE_HIT__) {
84
- return error.response;
85
- }
86
- return Promise.reject(error);
87
- }
77
+ error => Promise.reject(error)
88
78
  );
89
79
  }
90
80
 
@@ -11416,6 +11406,45 @@ class Application {
11416
11406
  throw ex;
11417
11407
  }
11418
11408
  }
11409
+
11410
+ /**
11411
+ * @author Myndware <augusto.pissarra@myndware.com>
11412
+ * @description Gets the application and pages to start the cache process
11413
+ * @param {object} params
11414
+ * @param {object} params.orgId the orgId of this application
11415
+ * @param {object} params.appId the application id
11416
+ * @param {string} session Session, token JWT
11417
+ * @returns {promise}
11418
+ * @public
11419
+ * @example
11420
+ *
11421
+ * const API = require('@docbrasil/api-systemmanager');
11422
+ * const api = new API();
11423
+ * const params = {
11424
+ * orgId: '55e4a3bd6be6b45210833f78',
11425
+ * appId: '55e4a3bd6be6b45210833fae',
11426
+ * };
11427
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
11428
+ * await api.user.application.getCache(params, session);
11429
+ */
11430
+ async getCache(params, session) {
11431
+ const self = this;
11432
+
11433
+ try {
11434
+ Joi__default["default"].assert(params, Joi__default["default"].object().required(), 'Params to get task');
11435
+ Joi__default["default"].assert(params.orgId, Joi__default["default"].string().required(), 'The organizations id');
11436
+ Joi__default["default"].assert(params.appId, Joi__default["default"].string().required(), 'The application id');
11437
+ Joi__default["default"].assert(session, Joi__default["default"].string().required(), 'Session token JWT');
11438
+
11439
+ const { orgId, appId} = params;
11440
+ const apiCall = self._client
11441
+ .get(`/organizations/${orgId}/applications/${appId}/cache`, self._setHeader(session));
11442
+
11443
+ return self._returnData(await apiCall);
11444
+ } catch (ex) {
11445
+ throw ex;
11446
+ }
11447
+ }
11419
11448
  }
11420
11449
 
11421
11450
  /**