mitra-interactions-sdk 1.0.70 → 1.0.72

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/dist/index.d.mts CHANGED
@@ -417,6 +417,19 @@ interface CallIntegrationOptions {
417
417
  params?: Record<string, unknown>;
418
418
  /** Body da requisição (para POST/PUT) */
419
419
  body?: Record<string, unknown>;
420
+ /**
421
+ * Headers só desta chamada. Use apenas quando a API externa exigir um header específico.
422
+ *
423
+ * Se a API exigir corpo `application/x-www-form-urlencoded` (comum em endpoints OAuth como
424
+ * introspect, revoke e token), passe `{ 'Content-Type': 'application/x-www-form-urlencoded' }`
425
+ * e monte o `body` como objeto plano de campos texto, número ou booleano: objeto ou lista dentro
426
+ * do `body` é recusado com 400. Sem esse header, o corpo vai em JSON.
427
+ *
428
+ * A autenticação da integração continua sendo aplicada; um header com o mesmo nome de um da
429
+ * integração (ex.: `Authorization`) substitui o da integração nesta chamada. Valores são texto
430
+ * literal. Quando omitido, nenhum header é enviado.
431
+ */
432
+ headers?: Record<string, string>;
420
433
  }
421
434
  interface CallIntegrationResponse {
422
435
  /** HTTP status retornado pelo serviço externo */
@@ -1332,6 +1345,11 @@ declare function listIntegrationsMitra(options?: ListIntegrationsOptions): Promi
1332
1345
  * POST /interactions/integrations/call
1333
1346
  * Chama uma API externa via serviço de integrações
1334
1347
  * Mapeia connection → integrationSlug no body
1348
+ *
1349
+ * `headers` é opcional e vale só para esta chamada. Com
1350
+ * `headers: { 'Content-Type': 'application/x-www-form-urlencoded' }` o `body` sai como formulário
1351
+ * (`campo=valor&campo=valor`), o que endpoints OAuth como introspect, revoke e token exigem; o
1352
+ * `body` precisa então ser um objeto plano. Sem esse header, o corpo vai em JSON.
1335
1353
  */
1336
1354
  declare function callIntegrationMitra(options: CallIntegrationOptions): Promise<CallIntegrationResponse>;
1337
1355
  /**
package/dist/index.d.ts CHANGED
@@ -417,6 +417,19 @@ interface CallIntegrationOptions {
417
417
  params?: Record<string, unknown>;
418
418
  /** Body da requisição (para POST/PUT) */
419
419
  body?: Record<string, unknown>;
420
+ /**
421
+ * Headers só desta chamada. Use apenas quando a API externa exigir um header específico.
422
+ *
423
+ * Se a API exigir corpo `application/x-www-form-urlencoded` (comum em endpoints OAuth como
424
+ * introspect, revoke e token), passe `{ 'Content-Type': 'application/x-www-form-urlencoded' }`
425
+ * e monte o `body` como objeto plano de campos texto, número ou booleano: objeto ou lista dentro
426
+ * do `body` é recusado com 400. Sem esse header, o corpo vai em JSON.
427
+ *
428
+ * A autenticação da integração continua sendo aplicada; um header com o mesmo nome de um da
429
+ * integração (ex.: `Authorization`) substitui o da integração nesta chamada. Valores são texto
430
+ * literal. Quando omitido, nenhum header é enviado.
431
+ */
432
+ headers?: Record<string, string>;
420
433
  }
421
434
  interface CallIntegrationResponse {
422
435
  /** HTTP status retornado pelo serviço externo */
@@ -1332,6 +1345,11 @@ declare function listIntegrationsMitra(options?: ListIntegrationsOptions): Promi
1332
1345
  * POST /interactions/integrations/call
1333
1346
  * Chama uma API externa via serviço de integrações
1334
1347
  * Mapeia connection → integrationSlug no body
1348
+ *
1349
+ * `headers` é opcional e vale só para esta chamada. Com
1350
+ * `headers: { 'Content-Type': 'application/x-www-form-urlencoded' }` o `body` sai como formulário
1351
+ * (`campo=valor&campo=valor`), o que endpoints OAuth como introspect, revoke e token exigem; o
1352
+ * `body` precisa então ser um objeto plano. Sem esse header, o corpo vai em JSON.
1335
1353
  */
1336
1354
  declare function callIntegrationMitra(options: CallIntegrationOptions): Promise<CallIntegrationResponse>;
1337
1355
  /**
package/dist/index.js CHANGED
@@ -50,12 +50,15 @@ function openAuthPopup(url, expectedOrigin) {
50
50
  }
51
51
  function autoConfigureFromLogin(response, authUrl, projectId) {
52
52
  if (!response.baseURL) return;
53
+ const previous = isConfigured() ? getConfig() : void 0;
53
54
  configureSdkMitra({
54
55
  baseURL: response.baseURL,
55
56
  token: response.token,
56
57
  authUrl,
57
58
  projectId,
58
- ...response.integrationURL ? { integrationURL: response.integrationURL } : {}
59
+ ...response.integrationURL ? { integrationURL: response.integrationURL } : {},
60
+ ...(previous == null ? void 0 : previous.onTokenRefresh) ? { onTokenRefresh: previous.onTokenRefresh } : {},
61
+ ...(previous == null ? void 0 : previous.onSessionExpired) ? { onSessionExpired: previous.onSessionExpired } : {}
59
62
  });
60
63
  }
61
64
  function buildAuthUrl(authUrl, method, projectId, opts) {
@@ -393,7 +396,8 @@ function withServerFunctionProof(headers, proof = readServerFunctionProof()) {
393
396
  }
394
397
 
395
398
  // src/session-watch.ts
396
- var RENEW_BEFORE_MS = 6e4;
399
+ var RENEW_BEFORE_MIN_MS = 6e4;
400
+ var RENEW_BEFORE_MAX_MS = 10 * 6e4;
397
401
  var RETRY_MS = 1e4;
398
402
  var MAX_TIMEOUT_MS = 2147483647;
399
403
  var refresher = null;
@@ -410,6 +414,14 @@ function limitedSessionExpiry(token) {
410
414
  if (!payload || typeof payload.authTime !== "number" || typeof payload.exp !== "number") return null;
411
415
  return payload.exp * 1e3;
412
416
  }
417
+ function renewBeforeMs() {
418
+ var _a;
419
+ if (!isConfigured()) return RENEW_BEFORE_MIN_MS;
420
+ const payload = decodeJwtPayload((_a = getConfig().token) != null ? _a : "");
421
+ const life = payload && typeof payload.exp === "number" && typeof payload.iat === "number" ? (payload.exp - payload.iat) * 1e3 : 0;
422
+ if (life <= 0) return RENEW_BEFORE_MIN_MS;
423
+ return Math.min(RENEW_BEFORE_MAX_MS, Math.max(RENEW_BEFORE_MIN_MS, Math.floor(life / 3)));
424
+ }
413
425
  function currentExpiry() {
414
426
  if (typeof window === "undefined" || !isConfigured()) return null;
415
427
  const config = getConfig();
@@ -432,7 +444,7 @@ function watchSession() {
432
444
  const expiry = currentExpiry();
433
445
  if (expiry == null) return;
434
446
  listenForReturn();
435
- let delay = Math.max(expiry - Date.now() - RENEW_BEFORE_MS, 0);
447
+ let delay = Math.max(expiry - Date.now() - renewBeforeMs(), 0);
436
448
  if (expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3) {
437
449
  delay = Math.max(expiry - Date.now() + 1e3, RETRY_MS);
438
450
  }
@@ -459,7 +471,7 @@ function onReturn() {
459
471
  const expiry = currentExpiry();
460
472
  if (expiry == null) return;
461
473
  const atSessionEnd = expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3;
462
- if (expiry - Date.now() <= RENEW_BEFORE_MS && (!atSessionEnd || Date.now() >= expiry)) {
474
+ if (expiry - Date.now() <= renewBeforeMs() && (!atSessionEnd || Date.now() >= expiry)) {
463
475
  void renewNow();
464
476
  } else {
465
477
  watchSession();
@@ -823,6 +835,9 @@ async function callIntegrationMitra(options) {
823
835
  if (options.body !== void 0) {
824
836
  body.body = options.body;
825
837
  }
838
+ if (options.headers !== void 0) {
839
+ body.headers = options.headers;
840
+ }
826
841
  return http.post("/interactions/integrations/call", body);
827
842
  }
828
843
  async function stopServerFunctionExecutionMitra(options) {
@@ -2240,6 +2255,7 @@ function createMitraInstance(initialConfig) {
2240
2255
  if (options.endpoint !== void 0) body.endpoint = options.endpoint;
2241
2256
  if (options.params !== void 0) body.params = options.params;
2242
2257
  if (options.body !== void 0) body.body = options.body;
2258
+ if (options.headers !== void 0) body.headers = options.headers;
2243
2259
  return request("POST", "/interactions/integrations/call", { body });
2244
2260
  },
2245
2261
  // Dynamic Schema CRUD