@docbrasil/api-systemmanager 1.1.82 → 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/api/ai/index.js CHANGED
@@ -15,7 +15,6 @@ class MyndAI {
15
15
 
16
16
  const self = this;
17
17
  self.parent = options.parent;
18
- self._client = self.parent.dispatch.getClient();
19
18
 
20
19
  self.sessions = new AISession(options);
21
20
  }
@@ -92,7 +91,8 @@ class MyndAI {
92
91
  Joi.assert(params, Joi.object().required().error(new Error('params is required')));
93
92
  Joi.assert(params.prompt, Joi.string().required().error(new Error('Provide a prompt')));
94
93
 
95
- const apiCall = self._client
94
+ const client = self.parent.dispatch.getAkamaiClient();
95
+ const apiCall = client
96
96
  .post('/agents/explain', params, self._setHeader(authorization));
97
97
 
98
98
  return self._returnData(await apiCall);
@@ -14,7 +14,6 @@ class AISession {
14
14
 
15
15
  const self = this;
16
16
  self.parent = options.parent;
17
- self._client = self.parent.dispatch.getClient();
18
17
  }
19
18
 
20
19
  /**
@@ -78,7 +77,8 @@ class AISession {
78
77
  Joi.assert(params, Joi.object().required().error(new Error('params is required')));
79
78
  Joi.assert(params.documentId, Joi.string().required().error(new Error('documentId is required')));
80
79
 
81
- const apiCall = self._client
80
+ const client = self.parent.dispatch.getAkamaiClient();
81
+ const apiCall = client
82
82
  .get(`/agents/session/document/${params.documentId}`, self._setHeader(authorization));
83
83
 
84
84
  return self._returnData(await apiCall);
@@ -140,7 +140,8 @@ class AISession {
140
140
 
141
141
  const { documentId, ...payload } = params;
142
142
 
143
- const apiCall = self._client
143
+ const client = self.parent.dispatch.getAkamaiClient();
144
+ const apiCall = client
144
145
  .patch(`/agents/session/document/${documentId}`, payload, self._setHeader(authorization));
145
146
 
146
147
  return self._returnData(await apiCall);
@@ -186,7 +187,8 @@ class AISession {
186
187
  Joi.assert(params, Joi.object().required().error(new Error('params is required')));
187
188
  Joi.assert(params.agentType, Joi.string().required().error(new Error('agentType is required')));
188
189
 
189
- const apiCall = self._client
190
+ const client = self.parent.dispatch.getAkamaiClient();
191
+ const apiCall = client
190
192
  .post('/agents/create', params, self._setHeader(authorization));
191
193
 
192
194
  return self._returnData(await apiCall);
@@ -230,7 +232,8 @@ class AISession {
230
232
 
231
233
  const { sessionId, ...payload } = params;
232
234
 
233
- const apiCall = self._client
235
+ const client = self.parent.dispatch.getAkamaiClient();
236
+ const apiCall = client
234
237
  .post(`/agents/${sessionId}/execute`, payload, self._setHeader(authorization));
235
238
 
236
239
  return self._returnData(await apiCall);
package/api/dispatch.js CHANGED
@@ -198,6 +198,85 @@ class Dispatch {
198
198
  getClient() {
199
199
  return this._client;
200
200
  }
201
+
202
+ /**
203
+ * @description Decode the payload of a JWT token (base64url → JSON).
204
+ * Does NOT verify the signature — used only to extract claims for header building.
205
+ * @param {string} token JWT token string
206
+ * @return {object|null} Decoded payload or null on failure
207
+ * @private
208
+ */
209
+ _decodeJwtPayload(token) {
210
+ try {
211
+ const parts = token.split('.');
212
+ if (parts.length !== 3) return null;
213
+ const base64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
214
+ const json = decodeURIComponent(
215
+ atob(base64).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join('')
216
+ );
217
+ return JSON.parse(json);
218
+ } catch (e) {
219
+ return null;
220
+ }
221
+ }
222
+
223
+ /**
224
+ * @description Create a dedicated Axios client for Akamai routes.
225
+ * In DEV there is no NGiNX to translate the Authorization JWT into the
226
+ * x-api-key / x-user-id / x-organization-id headers that Akamai expects.
227
+ * When an apiKey is supplied the client adds a request interceptor that
228
+ * decodes the JWT and sets those headers automatically.
229
+ * @param {string} url The Akamai base URL (e.g., http://localhost:9008 in DEV).
230
+ * @param {object} [options] Optional configuration
231
+ * @param {string} [options.apiKey] API key for Akamai authentication.
232
+ * When provided, the client will transform the Authorization JWT into
233
+ * x-api-key, x-user-id and x-organization-id headers on every request.
234
+ * @public
235
+ */
236
+ setAkamaiBaseUrl(url, options = {}) {
237
+ Joi.assert(url, Joi.string().required());
238
+
239
+ const self = this;
240
+
241
+ self._akamaiClient = Axios.create({
242
+ baseURL: url,
243
+ withCredentials: true
244
+ });
245
+
246
+ // When an API key is provided, add interceptor to build Akamai headers from the JWT
247
+ if (options.apiKey) {
248
+ const apiKey = options.apiKey;
249
+
250
+ self._akamaiClient.interceptors.request.use((config) => {
251
+ const auth = config.headers && (config.headers.Authorization || config.headers.authorization);
252
+ if (auth) {
253
+ const payload = self._decodeJwtPayload(auth);
254
+ if (payload) {
255
+ config.headers['x-api-key'] = apiKey;
256
+ config.headers['x-user-id'] = payload._id || payload.userId;
257
+ config.headers['x-organization-id'] = payload.orgId || payload.organizationId;
258
+ if (payload.language) {
259
+ config.headers['x-country'] = payload.language;
260
+ }
261
+ // Remove the Authorization header — Akamai doesn't use it
262
+ delete config.headers.Authorization;
263
+ delete config.headers.authorization;
264
+ }
265
+ }
266
+ return config;
267
+ });
268
+ }
269
+ }
270
+
271
+ /**
272
+ * @description Get the Akamai Axios client. Falls back to the default client
273
+ * for backward compatibility (e.g., PROD where NGiNX proxies all routes).
274
+ * @return {AxiosInstance} The Akamai client or default client.
275
+ * @public
276
+ */
277
+ getAkamaiClient() {
278
+ return this._akamaiClient || this._client;
279
+ }
201
280
  }
202
281
 
203
282
  export default Dispatch;
package/dist/bundle.cjs CHANGED
@@ -210,6 +210,85 @@ class Dispatch {
210
210
  getClient() {
211
211
  return this._client;
212
212
  }
213
+
214
+ /**
215
+ * @description Decode the payload of a JWT token (base64url → JSON).
216
+ * Does NOT verify the signature — used only to extract claims for header building.
217
+ * @param {string} token JWT token string
218
+ * @return {object|null} Decoded payload or null on failure
219
+ * @private
220
+ */
221
+ _decodeJwtPayload(token) {
222
+ try {
223
+ const parts = token.split('.');
224
+ if (parts.length !== 3) return null;
225
+ const base64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
226
+ const json = decodeURIComponent(
227
+ atob(base64).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join('')
228
+ );
229
+ return JSON.parse(json);
230
+ } catch (e) {
231
+ return null;
232
+ }
233
+ }
234
+
235
+ /**
236
+ * @description Create a dedicated Axios client for Akamai routes.
237
+ * In DEV there is no NGiNX to translate the Authorization JWT into the
238
+ * x-api-key / x-user-id / x-organization-id headers that Akamai expects.
239
+ * When an apiKey is supplied the client adds a request interceptor that
240
+ * decodes the JWT and sets those headers automatically.
241
+ * @param {string} url The Akamai base URL (e.g., http://localhost:9008 in DEV).
242
+ * @param {object} [options] Optional configuration
243
+ * @param {string} [options.apiKey] API key for Akamai authentication.
244
+ * When provided, the client will transform the Authorization JWT into
245
+ * x-api-key, x-user-id and x-organization-id headers on every request.
246
+ * @public
247
+ */
248
+ setAkamaiBaseUrl(url, options = {}) {
249
+ Joi__default["default"].assert(url, Joi__default["default"].string().required());
250
+
251
+ const self = this;
252
+
253
+ self._akamaiClient = Axios__default["default"].create({
254
+ baseURL: url,
255
+ withCredentials: true
256
+ });
257
+
258
+ // When an API key is provided, add interceptor to build Akamai headers from the JWT
259
+ if (options.apiKey) {
260
+ const apiKey = options.apiKey;
261
+
262
+ self._akamaiClient.interceptors.request.use((config) => {
263
+ const auth = config.headers && (config.headers.Authorization || config.headers.authorization);
264
+ if (auth) {
265
+ const payload = self._decodeJwtPayload(auth);
266
+ if (payload) {
267
+ config.headers['x-api-key'] = apiKey;
268
+ config.headers['x-user-id'] = payload._id || payload.userId;
269
+ config.headers['x-organization-id'] = payload.orgId || payload.organizationId;
270
+ if (payload.language) {
271
+ config.headers['x-country'] = payload.language;
272
+ }
273
+ // Remove the Authorization header — Akamai doesn't use it
274
+ delete config.headers.Authorization;
275
+ delete config.headers.authorization;
276
+ }
277
+ }
278
+ return config;
279
+ });
280
+ }
281
+ }
282
+
283
+ /**
284
+ * @description Get the Akamai Axios client. Falls back to the default client
285
+ * for backward compatibility (e.g., PROD where NGiNX proxies all routes).
286
+ * @return {AxiosInstance} The Akamai client or default client.
287
+ * @public
288
+ */
289
+ getAkamaiClient() {
290
+ return this._akamaiClient || this._client;
291
+ }
213
292
  }
214
293
 
215
294
  /**
@@ -16129,7 +16208,6 @@ class AISession {
16129
16208
 
16130
16209
  const self = this;
16131
16210
  self.parent = options.parent;
16132
- self._client = self.parent.dispatch.getClient();
16133
16211
  }
16134
16212
 
16135
16213
  /**
@@ -16193,7 +16271,8 @@ class AISession {
16193
16271
  Joi__default["default"].assert(params, Joi__default["default"].object().required().error(new Error('params is required')));
16194
16272
  Joi__default["default"].assert(params.documentId, Joi__default["default"].string().required().error(new Error('documentId is required')));
16195
16273
 
16196
- const apiCall = self._client
16274
+ const client = self.parent.dispatch.getAkamaiClient();
16275
+ const apiCall = client
16197
16276
  .get(`/agents/session/document/${params.documentId}`, self._setHeader(authorization));
16198
16277
 
16199
16278
  return self._returnData(await apiCall);
@@ -16255,7 +16334,8 @@ class AISession {
16255
16334
 
16256
16335
  const { documentId, ...payload } = params;
16257
16336
 
16258
- const apiCall = self._client
16337
+ const client = self.parent.dispatch.getAkamaiClient();
16338
+ const apiCall = client
16259
16339
  .patch(`/agents/session/document/${documentId}`, payload, self._setHeader(authorization));
16260
16340
 
16261
16341
  return self._returnData(await apiCall);
@@ -16301,7 +16381,8 @@ class AISession {
16301
16381
  Joi__default["default"].assert(params, Joi__default["default"].object().required().error(new Error('params is required')));
16302
16382
  Joi__default["default"].assert(params.agentType, Joi__default["default"].string().required().error(new Error('agentType is required')));
16303
16383
 
16304
- const apiCall = self._client
16384
+ const client = self.parent.dispatch.getAkamaiClient();
16385
+ const apiCall = client
16305
16386
  .post('/agents/create', params, self._setHeader(authorization));
16306
16387
 
16307
16388
  return self._returnData(await apiCall);
@@ -16345,7 +16426,8 @@ class AISession {
16345
16426
 
16346
16427
  const { sessionId, ...payload } = params;
16347
16428
 
16348
- const apiCall = self._client
16429
+ const client = self.parent.dispatch.getAkamaiClient();
16430
+ const apiCall = client
16349
16431
  .post(`/agents/${sessionId}/execute`, payload, self._setHeader(authorization));
16350
16432
 
16351
16433
  return self._returnData(await apiCall);
@@ -16367,7 +16449,6 @@ class MyndAI {
16367
16449
 
16368
16450
  const self = this;
16369
16451
  self.parent = options.parent;
16370
- self._client = self.parent.dispatch.getClient();
16371
16452
 
16372
16453
  self.sessions = new AISession(options);
16373
16454
  }
@@ -16444,7 +16525,8 @@ class MyndAI {
16444
16525
  Joi__default["default"].assert(params, Joi__default["default"].object().required().error(new Error('params is required')));
16445
16526
  Joi__default["default"].assert(params.prompt, Joi__default["default"].string().required().error(new Error('Provide a prompt')));
16446
16527
 
16447
- const apiCall = self._client
16528
+ const client = self.parent.dispatch.getAkamaiClient();
16529
+ const apiCall = client
16448
16530
  .post('/agents/explain', params, self._setHeader(authorization));
16449
16531
 
16450
16532
  return self._returnData(await apiCall);
@@ -16511,6 +16593,7 @@ class API {
16511
16593
  }
16512
16594
  },
16513
16595
  uri: 'http://localhost:8080',
16596
+ akamaiUri: null,
16514
16597
  attemptsRetry: 3,
16515
16598
  httpStatusToRetry: [401],
16516
16599
  debug: {success: true, error: true}
@@ -16526,6 +16609,23 @@ class API {
16526
16609
  self.admin = new Admin({parent: self});
16527
16610
  self.external = new External({parent: self});
16528
16611
  self.ai = new MyndAI({parent: self});
16612
+
16613
+ // If akamaiUri was provided in options, configure the Akamai client
16614
+ if (self.options.akamaiUri) {
16615
+ self.dispatch.setAkamaiBaseUrl(self.options.akamaiUri, { apiKey: self.options.akamaiApiKey });
16616
+ }
16617
+ }
16618
+
16619
+ /**
16620
+ * @description Set the Akamai base URL for agent/AI routes.
16621
+ * @param {string} url The Akamai base URL.
16622
+ * @param {object} [options] Optional configuration
16623
+ * @param {string} [options.apiKey] API key for Akamai authentication (required in DEV,
16624
+ * where there is no NGiNX to translate Authorization JWT into x-* headers).
16625
+ * @public
16626
+ */
16627
+ setAkamaiBaseUrl(url, options = {}) {
16628
+ this.dispatch.setAkamaiBaseUrl(url, options);
16529
16629
  }
16530
16630
  }
16531
16631