@docbrasil/api-systemmanager 1.1.83 → 1.1.84

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
@@ -5403,7 +5403,7 @@ Api dispatch manager
5403
5403
  * [.errorOffline()](#Dispatch+errorOffline)
5404
5404
  * [.getContext(url, [session])](#Dispatch+getContext) ⇒ <code>Promise.&lt;object&gt;</code>
5405
5405
  * [.getClient()](#Dispatch+getClient) ⇒ <code>AxiosInstance</code>
5406
- * [.setAkamaiBaseUrl(url)](#Dispatch+setAkamaiBaseUrl)
5406
+ * [.setAkamaiBaseUrl(url, [options])](#Dispatch+setAkamaiBaseUrl)
5407
5407
  * [.getAkamaiClient()](#Dispatch+getAkamaiClient) ⇒ <code>AxiosInstance</code>
5408
5408
 
5409
5409
  <a name="Dispatch+errorOffline"></a>
@@ -5436,8 +5436,12 @@ Get the Axios client.
5436
5436
  **Access**: public
5437
5437
  <a name="Dispatch+setAkamaiBaseUrl"></a>
5438
5438
 
5439
- ### dispatch.setAkamaiBaseUrl(url)
5439
+ ### dispatch.setAkamaiBaseUrl(url, [options])
5440
5440
  Create a dedicated Axios client for Akamai routes.
5441
+ In DEV there is no NGiNX to translate the Authorization JWT into the
5442
+ x-api-key / x-user-id / x-organization-id headers that Akamai expects.
5443
+ When an apiKey is supplied the client adds a request interceptor that
5444
+ decodes the JWT and sets those headers automatically.
5441
5445
 
5442
5446
  **Kind**: instance method of [<code>Dispatch</code>](#Dispatch)
5443
5447
  **Access**: public
@@ -5445,6 +5449,8 @@ Create a dedicated Axios client for Akamai routes.
5445
5449
  | Param | Type | Description |
5446
5450
  | --- | --- | --- |
5447
5451
  | url | <code>string</code> | The Akamai base URL (e.g., http://localhost:9008 in DEV). |
5452
+ | [options] | <code>object</code> | Optional configuration |
5453
+ | [options.apiKey] | <code>string</code> | API key for Akamai authentication. When provided, the client will transform the Authorization JWT into x-api-key, x-user-id and x-organization-id headers on every request. |
5448
5454
 
5449
5455
  <a name="Dispatch+getAkamaiClient"></a>
5450
5456
 
@@ -387,7 +387,7 @@ for backward compatibility (e.g., PROD where NGiNX proxies all routes).
387
387
  <p class="tag-source">
388
388
  <a href="dispatch.js.html" class="button">View Source</a>
389
389
  <span>
390
- <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line222">line 222</a>
390
+ <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line277">line 277</a>
391
391
  </span>
392
392
  </p>
393
393
 
@@ -808,7 +808,7 @@ for backward compatibility (e.g., PROD where NGiNX proxies all routes).
808
808
 
809
809
  <span class="code-name">
810
810
 
811
- setAkamaiBaseUrl<span class="signature">(url)</span><span class="type-signature"></span>
811
+ setAkamaiBaseUrl<span class="signature">(url, options<span class="signature-attributes">opt</span>)</span><span class="type-signature"></span>
812
812
 
813
813
  </span>
814
814
  </h4>
@@ -818,6 +818,10 @@ for backward compatibility (e.g., PROD where NGiNX proxies all routes).
818
818
 
819
819
  <div class="description">
820
820
  Create a dedicated Axios client for Akamai routes.
821
+ In DEV there is no NGiNX to translate the Authorization JWT into the
822
+ x-api-key / x-user-id / x-organization-id headers that Akamai expects.
823
+ When an apiKey is supplied the client adds a request interceptor that
824
+ decodes the JWT and sets those headers automatically.
821
825
  </div>
822
826
 
823
827
 
@@ -842,6 +846,8 @@ for backward compatibility (e.g., PROD where NGiNX proxies all routes).
842
846
  <th>Type</th>
843
847
 
844
848
 
849
+ <th>Attributes</th>
850
+
845
851
 
846
852
 
847
853
 
@@ -868,6 +874,14 @@ for backward compatibility (e.g., PROD where NGiNX proxies all routes).
868
874
  </td>
869
875
 
870
876
 
877
+ <td class="attributes">
878
+
879
+
880
+
881
+
882
+
883
+ </td>
884
+
871
885
 
872
886
 
873
887
 
@@ -876,6 +890,79 @@ for backward compatibility (e.g., PROD where NGiNX proxies all routes).
876
890
 
877
891
 
878
892
 
893
+
894
+
895
+ <tr class="deep-level-0">
896
+
897
+ <td class="name"><code>options</code></td>
898
+
899
+
900
+ <td class="type">
901
+
902
+
903
+ <code class="param-type">object</code>
904
+
905
+
906
+
907
+ </td>
908
+
909
+
910
+ <td class="attributes">
911
+
912
+ &lt;optional><br>
913
+
914
+
915
+
916
+
917
+
918
+ </td>
919
+
920
+
921
+
922
+
923
+ <td class="description last">Optional configuration</td>
924
+ </tr>
925
+
926
+
927
+
928
+
929
+ <tr class="deep-level-1">
930
+
931
+ <td class="name"><code>apiKey</code></td>
932
+
933
+
934
+ <td class="type">
935
+
936
+
937
+ <code class="param-type">string</code>
938
+
939
+
940
+
941
+ </td>
942
+
943
+
944
+ <td class="attributes">
945
+
946
+ &lt;optional><br>
947
+
948
+
949
+
950
+
951
+
952
+ </td>
953
+
954
+
955
+
956
+
957
+ <td class="description last">API key for Akamai authentication.
958
+ When provided, the client will transform the Authorization JWT into
959
+ x-api-key, x-user-id and x-organization-id headers on every request.</td>
960
+ </tr>
961
+
962
+
963
+
964
+
965
+
879
966
  </tbody>
880
967
  </table>
881
968
  </div>
@@ -919,7 +1006,7 @@ for backward compatibility (e.g., PROD where NGiNX proxies all routes).
919
1006
  <p class="tag-source">
920
1007
  <a href="dispatch.js.html" class="button">View Source</a>
921
1008
  <span>
922
- <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line207">line 207</a>
1009
+ <a href="dispatch.js.html">dispatch.js</a>, <a href="dispatch.js.html#line236">line 236</a>
923
1010
  </span>
924
1011
  </p>
925
1012
 
@@ -286,18 +286,73 @@ class Dispatch {
286
286
  return this._client;
287
287
  }
288
288
 
289
+ /**
290
+ * @description Decode the payload of a JWT token (base64url → JSON).
291
+ * Does NOT verify the signature — used only to extract claims for header building.
292
+ * @param {string} token JWT token string
293
+ * @return {object|null} Decoded payload or null on failure
294
+ * @private
295
+ */
296
+ _decodeJwtPayload(token) {
297
+ try {
298
+ const parts = token.split('.');
299
+ if (parts.length !== 3) return null;
300
+ const base64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
301
+ const json = decodeURIComponent(
302
+ atob(base64).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join('')
303
+ );
304
+ return JSON.parse(json);
305
+ } catch (e) {
306
+ return null;
307
+ }
308
+ }
309
+
289
310
  /**
290
311
  * @description Create a dedicated Axios client for Akamai routes.
312
+ * In DEV there is no NGiNX to translate the Authorization JWT into the
313
+ * x-api-key / x-user-id / x-organization-id headers that Akamai expects.
314
+ * When an apiKey is supplied the client adds a request interceptor that
315
+ * decodes the JWT and sets those headers automatically.
291
316
  * @param {string} url The Akamai base URL (e.g., http://localhost:9008 in DEV).
317
+ * @param {object} [options] Optional configuration
318
+ * @param {string} [options.apiKey] API key for Akamai authentication.
319
+ * When provided, the client will transform the Authorization JWT into
320
+ * x-api-key, x-user-id and x-organization-id headers on every request.
292
321
  * @public
293
322
  */
294
- setAkamaiBaseUrl(url) {
323
+ setAkamaiBaseUrl(url, options = {}) {
295
324
  Joi.assert(url, Joi.string().required());
296
325
 
297
- this._akamaiClient = Axios.create({
326
+ const self = this;
327
+
328
+ self._akamaiClient = Axios.create({
298
329
  baseURL: url,
299
330
  withCredentials: true
300
331
  });
332
+
333
+ // When an API key is provided, add interceptor to build Akamai headers from the JWT
334
+ if (options.apiKey) {
335
+ const apiKey = options.apiKey;
336
+
337
+ self._akamaiClient.interceptors.request.use((config) => {
338
+ const auth = config.headers &amp;&amp; (config.headers.Authorization || config.headers.authorization);
339
+ if (auth) {
340
+ const payload = self._decodeJwtPayload(auth);
341
+ if (payload) {
342
+ config.headers['x-api-key'] = apiKey;
343
+ config.headers['x-user-id'] = payload._id || payload.userId;
344
+ config.headers['x-organization-id'] = payload.orgId || payload.organizationId;
345
+ if (payload.language) {
346
+ config.headers['x-country'] = payload.language;
347
+ }
348
+ // Remove the Authorization header — Akamai doesn't use it
349
+ delete config.headers.Authorization;
350
+ delete config.headers.authorization;
351
+ }
352
+ }
353
+ return config;
354
+ });
355
+ }
301
356
  }
302
357
 
303
358
  /**
package/index.js CHANGED
@@ -87,17 +87,20 @@ class API {
87
87
 
88
88
  // If akamaiUri was provided in options, configure the Akamai client
89
89
  if (self.options.akamaiUri) {
90
- self.dispatch.setAkamaiBaseUrl(self.options.akamaiUri);
90
+ self.dispatch.setAkamaiBaseUrl(self.options.akamaiUri, { apiKey: self.options.akamaiApiKey });
91
91
  }
92
92
  }
93
93
 
94
94
  /**
95
95
  * @description Set the Akamai base URL for agent/AI routes.
96
96
  * @param {string} url The Akamai base URL.
97
+ * @param {object} [options] Optional configuration
98
+ * @param {string} [options.apiKey] API key for Akamai authentication (required in DEV,
99
+ * where there is no NGiNX to translate Authorization JWT into x-* headers).
97
100
  * @public
98
101
  */
99
- setAkamaiBaseUrl(url) {
100
- this.dispatch.setAkamaiBaseUrl(url);
102
+ setAkamaiBaseUrl(url, options = {}) {
103
+ this.dispatch.setAkamaiBaseUrl(url, options);
101
104
  }
102
105
  }
103
106
 
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.83",
4
+ "version": "1.1.84",
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",