@docbrasil/api-systemmanager 1.1.84 → 1.1.85

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
@@ -199,38 +199,18 @@ class Dispatch {
199
199
  return this._client;
200
200
  }
201
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
202
  /**
224
203
  * @description Create a dedicated Axios client for Akamai routes.
225
204
  * In DEV there is no NGiNX to translate the Authorization JWT into the
226
205
  * 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.
206
+ * When a headerBuilder function is supplied, the client adds a request
207
+ * interceptor that replaces the Authorization header with the x-* headers
208
+ * returned by the function.
229
209
  * @param {string} url The Akamai base URL (e.g., http://localhost:9008 in DEV).
230
210
  * @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.
211
+ * @param {function} [options.headerBuilder] A function that returns an object
212
+ * with the Akamai headers (x-api-key, x-user-id, x-organization-id, x-country).
213
+ * Called at request time so it can read live application state (e.g., auth store).
234
214
  * @public
235
215
  */
236
216
  setAkamaiBaseUrl(url, options = {}) {
@@ -243,25 +223,15 @@ class Dispatch {
243
223
  withCredentials: true
244
224
  });
245
225
 
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
-
226
+ // When a headerBuilder is provided, add interceptor to replace Authorization with Akamai headers
227
+ if (typeof options.headerBuilder === 'function') {
250
228
  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
- }
229
+ const headers = options.headerBuilder();
230
+ if (headers) {
231
+ Object.assign(config.headers, headers);
232
+ // Remove the Authorization header — Akamai doesn't use it
233
+ delete config.headers.Authorization;
234
+ delete config.headers.authorization;
265
235
  }
266
236
  return config;
267
237
  });
package/dist/bundle.cjs CHANGED
@@ -211,38 +211,18 @@ class Dispatch {
211
211
  return this._client;
212
212
  }
213
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
214
  /**
236
215
  * @description Create a dedicated Axios client for Akamai routes.
237
216
  * In DEV there is no NGiNX to translate the Authorization JWT into the
238
217
  * 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.
218
+ * When a headerBuilder function is supplied, the client adds a request
219
+ * interceptor that replaces the Authorization header with the x-* headers
220
+ * returned by the function.
241
221
  * @param {string} url The Akamai base URL (e.g., http://localhost:9008 in DEV).
242
222
  * @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.
223
+ * @param {function} [options.headerBuilder] A function that returns an object
224
+ * with the Akamai headers (x-api-key, x-user-id, x-organization-id, x-country).
225
+ * Called at request time so it can read live application state (e.g., auth store).
246
226
  * @public
247
227
  */
248
228
  setAkamaiBaseUrl(url, options = {}) {
@@ -255,25 +235,15 @@ class Dispatch {
255
235
  withCredentials: true
256
236
  });
257
237
 
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
-
238
+ // When a headerBuilder is provided, add interceptor to replace Authorization with Akamai headers
239
+ if (typeof options.headerBuilder === 'function') {
262
240
  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
- }
241
+ const headers = options.headerBuilder();
242
+ if (headers) {
243
+ Object.assign(config.headers, headers);
244
+ // Remove the Authorization header — Akamai doesn't use it
245
+ delete config.headers.Authorization;
246
+ delete config.headers.authorization;
277
247
  }
278
248
  return config;
279
249
  });
@@ -16612,7 +16582,7 @@ class API {
16612
16582
 
16613
16583
  // If akamaiUri was provided in options, configure the Akamai client
16614
16584
  if (self.options.akamaiUri) {
16615
- self.dispatch.setAkamaiBaseUrl(self.options.akamaiUri, { apiKey: self.options.akamaiApiKey });
16585
+ self.dispatch.setAkamaiBaseUrl(self.options.akamaiUri, { headerBuilder: self.options.akamaiHeaderBuilder });
16616
16586
  }
16617
16587
  }
16618
16588
 
@@ -16620,8 +16590,8 @@ class API {
16620
16590
  * @description Set the Akamai base URL for agent/AI routes.
16621
16591
  * @param {string} url The Akamai base URL.
16622
16592
  * @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).
16593
+ * @param {function} [options.headerBuilder] Function returning Akamai headers.
16594
+ * Called at request time can read live app state (e.g., auth store).
16625
16595
  * @public
16626
16596
  */
16627
16597
  setAkamaiBaseUrl(url, options = {}) {