@docbrasil/api-systemmanager 1.1.50 → 1.1.52

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/doc/api.md CHANGED
@@ -1374,6 +1374,7 @@ Class for Applications, permission user
1374
1374
  * [Application](#Application)
1375
1375
  * [.list(params, session)](#Application+list) ⇒ <code>promise</code>
1376
1376
  * [.changeApplication(params, session)](#Application+changeApplication) ⇒ <code>promise</code>
1377
+ * [.getCache(params, session)](#Application+getCache) ⇒ <code>promise</code>
1377
1378
 
1378
1379
  <a name="Application+list"></a>
1379
1380
 
@@ -1425,6 +1426,33 @@ const params = {
1425
1426
  const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
1426
1427
  await api.user.application.changeApplication(params, session);
1427
1428
  ```
1429
+ <a name="Application+getCache"></a>
1430
+
1431
+ ### application.getCache(params, session) ⇒ <code>promise</code>
1432
+ Gets the application and pages to start the cache process
1433
+
1434
+ **Kind**: instance method of [<code>Application</code>](#Application)
1435
+ **Access**: public
1436
+ **Author**: Myndware <augusto.pissarra@myndware.com>
1437
+
1438
+ | Param | Type | Description |
1439
+ | --- | --- | --- |
1440
+ | params | <code>object</code> | |
1441
+ | params.orgId | <code>object</code> | the orgId of this application |
1442
+ | params.appId | <code>object</code> | the application id |
1443
+ | session | <code>string</code> | Session, token JWT |
1444
+
1445
+ **Example**
1446
+ ```js
1447
+ const API = require('@docbrasil/api-systemmanager');
1448
+ const api = new API();
1449
+ const params = {
1450
+ orgId: '55e4a3bd6be6b45210833f78',
1451
+ appId: '55e4a3bd6be6b45210833fae',
1452
+ };
1453
+ const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
1454
+ await api.user.application.getCache(params, session);
1455
+ ```
1428
1456
  <a name="Chart"></a>
1429
1457
 
1430
1458
  ## Chart
@@ -464,6 +464,276 @@ await api.user.application.changeApplication(params, session);</code></pre>
464
464
 
465
465
 
466
466
 
467
+ </div>
468
+
469
+ <div class="member">
470
+
471
+
472
+
473
+ <h4 class="name" id="getCache">
474
+ <a class="href-link" href="#getCache">#</a>
475
+
476
+
477
+ <span class='tag'>async</span>
478
+
479
+
480
+ <span class="code-name">
481
+
482
+ getCache<span class="signature">(params, session)</span><span class="type-signature"> &rarr; {promise}</span>
483
+
484
+ </span>
485
+ </h4>
486
+
487
+
488
+
489
+
490
+ <div class="description">
491
+ Gets the application and pages to start the cache process
492
+ </div>
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+
502
+
503
+ <h5>Parameters:</h5>
504
+
505
+ <div class="table-container">
506
+ <table class="params table">
507
+ <thead>
508
+ <tr>
509
+
510
+ <th>Name</th>
511
+
512
+
513
+ <th>Type</th>
514
+
515
+
516
+
517
+
518
+
519
+ <th class="last">Description</th>
520
+ </tr>
521
+ </thead>
522
+
523
+ <tbody>
524
+
525
+
526
+
527
+ <tr class="deep-level-0">
528
+
529
+ <td class="name"><code>params</code></td>
530
+
531
+
532
+ <td class="type">
533
+
534
+
535
+ <code class="param-type">object</code>
536
+
537
+
538
+
539
+ </td>
540
+
541
+
542
+
543
+
544
+
545
+ <td class="description last"></td>
546
+ </tr>
547
+
548
+
549
+
550
+
551
+ <tr class="deep-level-1">
552
+
553
+ <td class="name"><code>orgId</code></td>
554
+
555
+
556
+ <td class="type">
557
+
558
+
559
+ <code class="param-type">object</code>
560
+
561
+
562
+
563
+ </td>
564
+
565
+
566
+
567
+
568
+
569
+ <td class="description last">the orgId of this application</td>
570
+ </tr>
571
+
572
+
573
+
574
+ <tr class="deep-level-1">
575
+
576
+ <td class="name"><code>appId</code></td>
577
+
578
+
579
+ <td class="type">
580
+
581
+
582
+ <code class="param-type">object</code>
583
+
584
+
585
+
586
+ </td>
587
+
588
+
589
+
590
+
591
+
592
+ <td class="description last">the application id</td>
593
+ </tr>
594
+
595
+
596
+
597
+
598
+
599
+
600
+
601
+ <tr class="deep-level-0">
602
+
603
+ <td class="name"><code>session</code></td>
604
+
605
+
606
+ <td class="type">
607
+
608
+
609
+ <code class="param-type">string</code>
610
+
611
+
612
+
613
+ </td>
614
+
615
+
616
+
617
+
618
+
619
+ <td class="description last">Session, token JWT</td>
620
+ </tr>
621
+
622
+
623
+
624
+ </tbody>
625
+ </table>
626
+ </div>
627
+
628
+
629
+
630
+
631
+
632
+ <dl class="details">
633
+
634
+
635
+
636
+
637
+
638
+
639
+
640
+
641
+
642
+
643
+
644
+
645
+
646
+
647
+
648
+
649
+
650
+
651
+ <dt class="tag-author">Author:</dt>
652
+ <dd class="tag-author">
653
+ <ul>
654
+ <li><a href="mailto:augusto.pissarra@myndware.com">Myndware</a></li>
655
+ </ul>
656
+ </dd>
657
+
658
+
659
+
660
+
661
+
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+
671
+ <p class="tag-source">
672
+ <a href="user_application.js.html" class="button">View Source</a>
673
+ <span>
674
+ <a href="user_application.js.html">user/application.js</a>, <a href="user_application.js.html#line144">line 144</a>
675
+ </span>
676
+ </p>
677
+
678
+ </dl>
679
+
680
+
681
+
682
+
683
+
684
+
685
+
686
+
687
+
688
+
689
+
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+ <div class='columns method-parameter'>
698
+ <div class="column is-2"><label>Returns:</label></div>
699
+ <div class="column is-10">
700
+
701
+
702
+
703
+ <div class="columns">
704
+
705
+
706
+ <div class='column is-5 has-text-left'>
707
+ <label>Type: </label>
708
+
709
+ <code class="param-type">promise</code>
710
+
711
+
712
+ </div>
713
+
714
+ </div>
715
+
716
+
717
+ </div>
718
+ </div>
719
+
720
+
721
+
722
+
723
+ <h5>Example</h5>
724
+
725
+
726
+ <pre class="prettyprint"><code>const API = require('@docbrasil/api-systemmanager');
727
+ const api = new API();
728
+ const params = {
729
+ orgId: '55e4a3bd6be6b45210833f78',
730
+ appId: '55e4a3bd6be6b45210833fae',
731
+ };
732
+ const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
733
+ await api.user.application.getCache(params, session);</code></pre>
734
+
735
+
736
+
467
737
  </div>
468
738
 
469
739
  <div class="member">
@@ -300,7 +300,7 @@
300
300
  <p class="tag-source">
301
301
  <a href="dispatch.js.html" class="button">View Source</a>
302
302
  <span>
303
- <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line91">line 91</a>
303
+ <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line196">line 196</a>
304
304
  </span>
305
305
  </p>
306
306
 
@@ -523,7 +523,7 @@ await api.dispatch.getClient();</code></pre>
523
523
  <p class="tag-source">
524
524
  <a href="dispatch.js.html" class="button">View Source</a>
525
525
  <span>
526
- <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line64">line 64</a>
526
+ <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line169">line 169</a>
527
527
  </span>
528
528
  </p>
529
529
 
@@ -93,15 +93,74 @@ import Axios from 'axios';
93
93
  * @class Api dispatch manager
94
94
  */
95
95
  class Dispatch {
96
-
97
96
  constructor(options) {
98
-
99
97
  Joi.assert(options, Joi.object().required());
100
98
  Joi.assert(options.parent, Joi.object().required());
101
99
 
102
100
  const self = this;
101
+ self._cache = options.parent.options.cache;
102
+ self._forceCache = options.parent.options.forceCache;
103
103
  self.parent = options.parent;
104
104
  self._client = Axios.create({baseURL: self.parent.options.uri, withCredentials: true});
105
+
106
+ // Add request interceptor for offline handling
107
+ self._client.interceptors.request.use(
108
+ async (config) => {
109
+ if (self._isOnline() || !self._cache) {
110
+ return config;
111
+ }
112
+
113
+ const cachedData = await self._getCache(config.url, {
114
+ method: config.method,
115
+ data: config.data,
116
+ params: config.params,
117
+ headers: config.headers
118
+ });
119
+
120
+ if (cachedData) {
121
+ // Cancel the actual request and return cached data
122
+ const dummyResponse = {
123
+ status: 200,
124
+ data: cachedData,
125
+ headers: {},
126
+ config,
127
+ cached: true
128
+ };
129
+
130
+ // Throwing a special error that includes our cached response
131
+ throw {
132
+ __CACHE_HIT__: true,
133
+ response: dummyResponse
134
+ };
135
+ }
136
+
137
+ self.errorOffline();
138
+ throw new Error('Network error: No internet connection and no cached data available');
139
+ },
140
+ error => Promise.reject(error)
141
+ );
142
+
143
+ // Add response interceptor to handle cache hits and cache successful responses
144
+ self._client.interceptors.response.use(
145
+ async response => {
146
+ // Cache successful responses when online
147
+ if (response.status === 200 &amp;&amp; self._isOnline() &amp;&amp; self._cache) {
148
+ await self._setCache(response.config.url, {
149
+ method: response.config.method,
150
+ data: response.config.data,
151
+ params: response.config.params,
152
+ headers: response.config.headers
153
+ }, response.data);
154
+ }
155
+ return response;
156
+ },
157
+ error => {
158
+ if (error.__CACHE_HIT__) {
159
+ return error.response;
160
+ }
161
+ return Promise.reject(error);
162
+ }
163
+ );
105
164
  }
106
165
 
107
166
  /**
@@ -134,6 +193,52 @@ class Dispatch {
134
193
  };
135
194
  }
136
195
 
196
+ /**
197
+ * @description Check if the browser/client is currently online
198
+ * @return {boolean} True if online, false if offline
199
+ * @private
200
+ */
201
+ _isOnline() {
202
+ if(this._forceCache) return false;
203
+ return typeof navigator !== 'undefined' &amp;&amp; navigator.onLine;
204
+ }
205
+
206
+ /**
207
+ * @description Get cached data for a specific request
208
+ * @param {string} url The request URL
209
+ * @param {object} options Request options including method, data, params, and headers
210
+ * @return {Promise&lt;object|null>} Cached data or null if no cache exists
211
+ * @private
212
+ */
213
+ async _getCache(url, options) {
214
+ try {
215
+ return await this._cache.getCache(url, options);
216
+ } catch (error) {
217
+ console.warn('Cache retrieval failed:', error);
218
+ return null;
219
+ }
220
+ }
221
+
222
+ /**
223
+ * @description Set data in cache for a specific request
224
+ * @param {string} url The request URL
225
+ * @param {object} options Request options including method, data, params, and headers
226
+ * @param {object} data The data to cache
227
+ * @return {Promise&lt;void>}
228
+ * @private
229
+ */
230
+ async _setCache(url, options, data) {
231
+ try {
232
+ await this._cache.setCache(url, options, data);
233
+ } catch (error) {
234
+ console.warn('Cache storage failed:', error);
235
+ }
236
+ }
237
+
238
+ errorOffline() {
239
+ this._cache.errorOffline();
240
+ }
241
+
137
242
  /**
138
243
  * Get the URL context
139
244
  * @param url {string} Full url
@@ -159,8 +264,8 @@ class Dispatch {
159
264
 
160
265
  const self = this;
161
266
  const header = session ? self._setHeader(session) : {};
162
- const apiCall = self._client.get(url, header);
163
- return self._returnData(await apiCall);
267
+ const apiCall = await self._client.get(url, header);
268
+ return self._returnData(apiCall);
164
269
  }
165
270
 
166
271
  /**
@@ -207,6 +207,45 @@ class Application {
207
207
  throw ex;
208
208
  }
209
209
  }
210
+
211
+ /**
212
+ * @author Myndware &lt;augusto.pissarra@myndware.com>
213
+ * @description Gets the application and pages to start the cache process
214
+ * @param {object} params
215
+ * @param {object} params.orgId the orgId of this application
216
+ * @param {object} params.appId the application id
217
+ * @param {string} session Session, token JWT
218
+ * @returns {promise}
219
+ * @public
220
+ * @example
221
+ *
222
+ * const API = require('@docbrasil/api-systemmanager');
223
+ * const api = new API();
224
+ * const params = {
225
+ * orgId: '55e4a3bd6be6b45210833f78',
226
+ * appId: '55e4a3bd6be6b45210833fae',
227
+ * };
228
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
229
+ * await api.user.application.getCache(params, session);
230
+ */
231
+ async getCache(params, session) {
232
+ const self = this;
233
+
234
+ try {
235
+ Joi.assert(params, Joi.object().required(), 'Params to get task');
236
+ Joi.assert(params.orgId, Joi.string().required(), 'The organizations id');
237
+ Joi.assert(params.appId, Joi.string().required(), 'The application id');
238
+ Joi.assert(session, Joi.string().required(), 'Session token JWT');
239
+
240
+ const { orgId, appId} = params;
241
+ const apiCall = self._client
242
+ .get(`/organizations/${orgId}/applications/${appId}/cache`, self._setHeader(session));
243
+
244
+ return self._returnData(await apiCall);
245
+ } catch (ex) {
246
+ throw ex;
247
+ }
248
+ }
210
249
  }
211
250
 
212
251
  export default Application;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@docbrasil/api-systemmanager",
3
3
  "description": "Module API System Manager",
4
- "version": "1.1.50",
4
+ "version": "1.1.52",
5
5
  "scripts": {
6
6
  "htmldoc": "rm -rf docs && jsdoc api/** -d docs -t ./node_modules/better-docs",
7
7
  "doc": "rm -rf doc && mkdir doc && jsdoc2md api/**/* api/* > doc/api.md",