mitra-interactions-sdk 1.0.60-beta.1 → 1.0.60-beta.11
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.js +119 -79
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +119 -79
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -255,9 +255,9 @@ function getFetch() {
|
|
|
255
255
|
if (typeof globalThis.fetch === "function") return globalThis.fetch;
|
|
256
256
|
throw new Error("fetch n\xE3o dispon\xEDvel. Use Node.js 18+ ou instale node-fetch.");
|
|
257
257
|
}
|
|
258
|
-
function
|
|
259
|
-
const base =
|
|
260
|
-
return `${base}/
|
|
258
|
+
function buildBffAuthEndpoint(gateway, path) {
|
|
259
|
+
const base = gateway.replace(/\/+$/, "");
|
|
260
|
+
return `${base}/auth${path}`;
|
|
261
261
|
}
|
|
262
262
|
function resolveLoginOptions2(options) {
|
|
263
263
|
var _a;
|
|
@@ -283,24 +283,21 @@ function autoConfigureFromLogin2(response, authUrl, projectId) {
|
|
|
283
283
|
...response.integrationURL ? { integrationURL: response.integrationURL } : {}
|
|
284
284
|
});
|
|
285
285
|
}
|
|
286
|
-
async function
|
|
286
|
+
async function postBffAuth(url, body) {
|
|
287
287
|
const fetchFn = getFetch();
|
|
288
288
|
const response = await fetchFn(url, {
|
|
289
289
|
method: "POST",
|
|
290
290
|
headers: { "Content-Type": "application/json" },
|
|
291
291
|
body: JSON.stringify(body)
|
|
292
292
|
});
|
|
293
|
+
const text = await response.text().catch(() => "");
|
|
294
|
+
const { data, isJson } = parseBody(text);
|
|
293
295
|
if (!response.ok) {
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
parsed = text ? JSON.parse(text) : null;
|
|
298
|
-
} catch (e) {
|
|
299
|
-
}
|
|
300
|
-
const msg = (parsed == null ? void 0 : parsed.message) || (parsed == null ? void 0 : parsed.error) || `HTTP ${response.status}`;
|
|
301
|
-
throw { message: msg, status: response.status, details: parsed };
|
|
296
|
+
const fallback = !isJson && text ? `HTTP ${response.status} \u2014 ${text}` : `HTTP ${response.status}`;
|
|
297
|
+
const msg = isJson && ((data == null ? void 0 : data.message) || (data == null ? void 0 : data.error)) || fallback;
|
|
298
|
+
throw { message: msg, status: response.status, details: data };
|
|
302
299
|
}
|
|
303
|
-
return
|
|
300
|
+
return data;
|
|
304
301
|
}
|
|
305
302
|
function normalizeResponse(tokens, authUrl) {
|
|
306
303
|
return {
|
|
@@ -317,25 +314,13 @@ var OAUTH_RESULT_TYPE = "mitra-oauth-result";
|
|
|
317
314
|
var POPUP_W = 480;
|
|
318
315
|
var POPUP_H = 600;
|
|
319
316
|
var POPUP_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
method: "GET",
|
|
324
|
-
headers: { accept: "application/json" }
|
|
325
|
-
});
|
|
326
|
-
if (!response.ok) {
|
|
327
|
-
throw new Error(`SSO discovery falhou (HTTP ${response.status}).`);
|
|
328
|
-
}
|
|
329
|
-
const data = await response.json().catch(() => null);
|
|
330
|
-
const url = data && typeof data.authPageUrl === "string" ? data.authPageUrl : "";
|
|
331
|
-
if (!url) {
|
|
332
|
-
throw new Error("SSO discovery n\xE3o retornou authPageUrl.");
|
|
333
|
-
}
|
|
334
|
-
return url;
|
|
317
|
+
var SSO_AUTH_PAGE_URL = "https://d2by6yrxhbu2rl.cloudfront.net/sdk-auth.html";
|
|
318
|
+
async function fetchSsoAuthPageUrl(_baseURL) {
|
|
319
|
+
return SSO_AUTH_PAGE_URL;
|
|
335
320
|
}
|
|
336
321
|
function buildStartUrl(authPageUrl, provider, state, appId, apiUrl) {
|
|
337
322
|
const sep = authPageUrl.includes("?") ? "&" : "?";
|
|
338
|
-
return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl }).toString();
|
|
323
|
+
return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl, origin: window.location.origin }).toString();
|
|
339
324
|
}
|
|
340
325
|
function getOriginFromUrl2(url) {
|
|
341
326
|
try {
|
|
@@ -392,11 +377,11 @@ function openOAuthPopup(providerUrl, expectedOrigin, expectedState) {
|
|
|
392
377
|
reject(new Error(data.error || "Erro na autentica\xE7\xE3o OAuth."));
|
|
393
378
|
return;
|
|
394
379
|
}
|
|
395
|
-
if (!data.
|
|
396
|
-
reject(new Error("Resposta OAuth sem
|
|
380
|
+
if (!data.token) {
|
|
381
|
+
reject(new Error("Resposta OAuth sem token."));
|
|
397
382
|
return;
|
|
398
383
|
}
|
|
399
|
-
resolve({
|
|
384
|
+
resolve({ token: data.token });
|
|
400
385
|
}
|
|
401
386
|
function cleanup() {
|
|
402
387
|
clearTimeout(timer);
|
|
@@ -423,15 +408,9 @@ async function loginWithProvider(provider, options) {
|
|
|
423
408
|
const appId = String(projectId);
|
|
424
409
|
const authPageUrl = await fetchSsoAuthPageUrl(authUrl);
|
|
425
410
|
const pageOrigin = getOriginFromUrl2(authPageUrl);
|
|
426
|
-
const redirectUri = authPageUrl;
|
|
427
411
|
const startUrl = buildStartUrl(authPageUrl, provider, state, appId, authUrl);
|
|
428
|
-
const {
|
|
429
|
-
const
|
|
430
|
-
code,
|
|
431
|
-
redirectUri,
|
|
432
|
-
appId
|
|
433
|
-
});
|
|
434
|
-
const response = normalizeResponse(tokens, authUrl);
|
|
412
|
+
const { token } = await openOAuthPopup(startUrl, pageOrigin, state);
|
|
413
|
+
const response = normalizeResponse(token, authUrl);
|
|
435
414
|
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
436
415
|
return response;
|
|
437
416
|
} finally {
|
|
@@ -456,25 +435,30 @@ async function loginMitra2(method, options) {
|
|
|
456
435
|
}
|
|
457
436
|
async function emailLoginMitra2(options) {
|
|
458
437
|
const { authUrl, projectId } = resolveLoginOptions2(options);
|
|
459
|
-
const
|
|
438
|
+
const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/login"), {
|
|
439
|
+
projectId: String(projectId),
|
|
460
440
|
email: options.email,
|
|
461
|
-
password: options.password
|
|
462
|
-
appId: String(projectId)
|
|
441
|
+
password: options.password
|
|
463
442
|
});
|
|
464
|
-
const response =
|
|
443
|
+
const response = {
|
|
444
|
+
token: data.token,
|
|
445
|
+
// baseURL = authUrl (o gateway configurado). O BFF não devolve baseURL/integrationURL;
|
|
446
|
+
// a base é sempre o gateway, resolvido no lado do SDK.
|
|
447
|
+
baseURL: authUrl,
|
|
448
|
+
// Guarda o refreshToken do login — é ele que vai no body do refresh depois.
|
|
449
|
+
...data.refreshToken ? { refreshToken: data.refreshToken } : {}
|
|
450
|
+
};
|
|
465
451
|
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
466
452
|
return response;
|
|
467
453
|
}
|
|
468
454
|
async function emailSignupMitra2(options) {
|
|
469
455
|
const { authUrl, projectId } = resolveLoginOptions2(options);
|
|
470
|
-
|
|
456
|
+
await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/signup"), {
|
|
457
|
+
projectId: String(projectId),
|
|
471
458
|
name: options.name,
|
|
472
459
|
email: options.email,
|
|
473
|
-
password: options.password
|
|
474
|
-
appId: String(projectId)
|
|
460
|
+
password: options.password
|
|
475
461
|
});
|
|
476
|
-
const response = normalizeResponse(tokens, authUrl);
|
|
477
|
-
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
478
462
|
}
|
|
479
463
|
async function emailVerifyCodeMitra2(_options) {
|
|
480
464
|
throw new Error(NOT_SUPPORTED_VERIFY);
|
|
@@ -490,10 +474,15 @@ async function refreshTokenSilently2(authUrl, _projectId) {
|
|
|
490
474
|
const config = isConfigured() ? getConfig() : null;
|
|
491
475
|
const refreshToken = config == null ? void 0 : config.refreshToken;
|
|
492
476
|
if (!refreshToken) {
|
|
493
|
-
throw new Error("refreshToken n\xE3o dispon\xEDvel para renova\xE7\xE3o
|
|
477
|
+
throw new Error("refreshToken n\xE3o dispon\xEDvel para renova\xE7\xE3o. Fa\xE7a login novamente.");
|
|
494
478
|
}
|
|
495
|
-
const
|
|
496
|
-
return
|
|
479
|
+
const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/refresh-token"), { refreshToken });
|
|
480
|
+
return {
|
|
481
|
+
token: data.token,
|
|
482
|
+
// Refresh não muda a base — mantém o authUrl (gateway).
|
|
483
|
+
baseURL: authUrl,
|
|
484
|
+
...data.refreshToken ? { refreshToken: data.refreshToken } : {}
|
|
485
|
+
};
|
|
497
486
|
} finally {
|
|
498
487
|
_refreshPromise2 = null;
|
|
499
488
|
}
|
|
@@ -713,28 +702,30 @@ function buildUrl(endpoint, params) {
|
|
|
713
702
|
async function tryRefreshToken() {
|
|
714
703
|
var _a, _b;
|
|
715
704
|
const config = getConfig();
|
|
716
|
-
if (!config.token || config.projectId == null) return false;
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
705
|
+
if (!config.token || config.projectId == null || !config.baseURL) return false;
|
|
706
|
+
if (!isNewWorld()) {
|
|
707
|
+
try {
|
|
708
|
+
const baseURL = config.baseURL.replace(/\/+$/, "");
|
|
709
|
+
const resp = await getFetch3()(`${baseURL}/mitraspace/project/refreshedToken/${config.projectId}`, {
|
|
710
|
+
method: "GET",
|
|
711
|
+
headers: { "Authorization": formatToken(config.token) }
|
|
712
|
+
});
|
|
713
|
+
if (resp.ok) {
|
|
714
|
+
const data = await resp.json();
|
|
715
|
+
if (data.token) {
|
|
716
|
+
const newToken = `Bearer ${stripBearer(data.token)}`;
|
|
717
|
+
const session = {
|
|
718
|
+
token: newToken,
|
|
719
|
+
baseURL: ((_a = data.merge) == null ? void 0 : _a.backURL) || config.baseURL,
|
|
720
|
+
...((_b = data.merge) == null ? void 0 : _b.integrationURL) ? { integrationURL: data.merge.integrationURL } : {}
|
|
721
|
+
};
|
|
722
|
+
updateGlobalToken(session.token);
|
|
723
|
+
if (config.onTokenRefresh) config.onTokenRefresh(session);
|
|
724
|
+
return true;
|
|
725
|
+
}
|
|
735
726
|
}
|
|
727
|
+
} catch (e) {
|
|
736
728
|
}
|
|
737
|
-
} catch (e) {
|
|
738
729
|
}
|
|
739
730
|
if (!config.authUrl || typeof window === "undefined") return false;
|
|
740
731
|
try {
|
|
@@ -748,10 +739,11 @@ async function tryRefreshToken() {
|
|
|
748
739
|
}
|
|
749
740
|
async function handleResponse(response) {
|
|
750
741
|
const text = await response.text();
|
|
751
|
-
const data
|
|
742
|
+
const { data, isJson } = parseBody(text);
|
|
752
743
|
if (!response.ok) {
|
|
753
|
-
const
|
|
754
|
-
const
|
|
744
|
+
const fallback = !isJson && text ? `HTTP ${response.status} \u2014 ${text}` : `HTTP ${response.status}`;
|
|
745
|
+
const base = isJson && ((data == null ? void 0 : data.message) || (data == null ? void 0 : data.error)) || fallback;
|
|
746
|
+
const msg = isJson && (data == null ? void 0 : data.hint) ? `${base} \u2014 ${data.hint}` : base;
|
|
755
747
|
throw {
|
|
756
748
|
message: msg,
|
|
757
749
|
status: response.status,
|
|
@@ -763,7 +755,8 @@ async function handleResponse(response) {
|
|
|
763
755
|
async function fetchWithRefresh(url, init) {
|
|
764
756
|
const fetchFn = getFetch3();
|
|
765
757
|
const response = await fetchFn(url, init);
|
|
766
|
-
|
|
758
|
+
const expiredStatus = isNewWorld() ? 401 : 403;
|
|
759
|
+
if (response.status === expiredStatus) {
|
|
767
760
|
const refreshed = await tryRefreshToken();
|
|
768
761
|
if (refreshed) {
|
|
769
762
|
const newHeaders = { ...buildHeaders() };
|
|
@@ -2010,10 +2003,11 @@ function createMitraInstance(initialConfig) {
|
|
|
2010
2003
|
}
|
|
2011
2004
|
async function handleResponse2(response) {
|
|
2012
2005
|
const text = await response.text();
|
|
2013
|
-
const data
|
|
2006
|
+
const { data, isJson } = parseBody(text);
|
|
2014
2007
|
if (!response.ok) {
|
|
2015
|
-
const
|
|
2016
|
-
const
|
|
2008
|
+
const fallback = !isJson && text ? `HTTP ${response.status} \u2014 ${text}` : `HTTP ${response.status}`;
|
|
2009
|
+
const base = isJson && ((data == null ? void 0 : data.message) || (data == null ? void 0 : data.error)) || fallback;
|
|
2010
|
+
const msg = isJson && (data == null ? void 0 : data.hint) ? `${base} \u2014 ${data.hint}` : base;
|
|
2017
2011
|
throw { message: msg, status: response.status, details: data };
|
|
2018
2012
|
}
|
|
2019
2013
|
return data;
|
|
@@ -2359,6 +2353,45 @@ function stripBearer(token) {
|
|
|
2359
2353
|
}
|
|
2360
2354
|
return t;
|
|
2361
2355
|
}
|
|
2356
|
+
function parseBody(text) {
|
|
2357
|
+
if (!text) return { data: null, isJson: true };
|
|
2358
|
+
try {
|
|
2359
|
+
return { data: JSON.parse(text), isJson: true };
|
|
2360
|
+
} catch (e) {
|
|
2361
|
+
return { data: text, isJson: false };
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
var SESSION_STORE_KEY = "mitra-session";
|
|
2365
|
+
function safeLocalStorage() {
|
|
2366
|
+
try {
|
|
2367
|
+
return typeof localStorage !== "undefined" ? localStorage : null;
|
|
2368
|
+
} catch (e) {
|
|
2369
|
+
return null;
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
function persistRefreshToken(refreshToken) {
|
|
2373
|
+
const ls = safeLocalStorage();
|
|
2374
|
+
if (!ls) return;
|
|
2375
|
+
try {
|
|
2376
|
+
const raw = ls.getItem(SESSION_STORE_KEY);
|
|
2377
|
+
const obj = raw ? JSON.parse(raw) : {};
|
|
2378
|
+
obj.refreshToken = refreshToken;
|
|
2379
|
+
ls.setItem(SESSION_STORE_KEY, JSON.stringify(obj));
|
|
2380
|
+
} catch (e) {
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
function readPersistedRefreshToken() {
|
|
2384
|
+
const ls = safeLocalStorage();
|
|
2385
|
+
if (!ls) return void 0;
|
|
2386
|
+
try {
|
|
2387
|
+
const raw = ls.getItem(SESSION_STORE_KEY);
|
|
2388
|
+
if (!raw) return void 0;
|
|
2389
|
+
const obj = JSON.parse(raw);
|
|
2390
|
+
return typeof (obj == null ? void 0 : obj.refreshToken) === "string" && obj.refreshToken ? obj.refreshToken : void 0;
|
|
2391
|
+
} catch (e) {
|
|
2392
|
+
return void 0;
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2362
2395
|
function readInjectedEnv() {
|
|
2363
2396
|
if (typeof window === "undefined") return {};
|
|
2364
2397
|
const env = window.__mitraEnv;
|
|
@@ -2376,6 +2409,12 @@ function configureSdkMitra(config = {}) {
|
|
|
2376
2409
|
throw new Error("baseURL \xE9 obrigat\xF3rio. Passe em configureSdkMitra({ baseURL }), fa\xE7a login para receber via sdk-auth, ou rode em uma app publicada pelo Mitra (window.__mitraEnv).");
|
|
2377
2410
|
}
|
|
2378
2411
|
if (merged.token != null) merged.token = stripBearer(merged.token);
|
|
2412
|
+
if (!merged.refreshToken) {
|
|
2413
|
+
const persisted = readPersistedRefreshToken();
|
|
2414
|
+
if (persisted) merged.refreshToken = persisted;
|
|
2415
|
+
} else {
|
|
2416
|
+
persistRefreshToken(merged.refreshToken);
|
|
2417
|
+
}
|
|
2379
2418
|
globalConfig = {
|
|
2380
2419
|
...merged,
|
|
2381
2420
|
baseURL: merged.baseURL.replace(/\/+$/, "")
|
|
@@ -2402,6 +2441,7 @@ function updateGlobalToken(token, refreshToken) {
|
|
|
2402
2441
|
token: stripBearer(token),
|
|
2403
2442
|
...refreshToken !== void 0 ? { refreshToken } : {}
|
|
2404
2443
|
};
|
|
2444
|
+
if (refreshToken) persistRefreshToken(refreshToken);
|
|
2405
2445
|
}
|
|
2406
2446
|
}
|
|
2407
2447
|
function resolveProjectId2(projectId) {
|