@krainovsd/js-helpers 0.14.11 → 0.14.13

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/lib/cjs/index.cjs CHANGED
@@ -736,11 +736,11 @@ function getVisiblePosition({ initialPosition, node, visibleArea, placement = "b
736
736
  ? 0
737
737
  : 10, flex, nested, }) {
738
738
  /** Viewport Variables */
739
- const totalHeight = document.documentElement.scrollHeight;
740
- const totalWidth = document.documentElement.scrollWidth;
741
- const viewport = visibleArea ?? document.documentElement;
742
- const scrollTop = document.documentElement.scrollTop;
743
- const scrollLeft = document.documentElement.scrollLeft;
739
+ const totalHeight = document.body.scrollHeight;
740
+ const totalWidth = document.body.scrollWidth;
741
+ const viewport = visibleArea ?? document.body;
742
+ const scrollTop = document.body.scrollTop;
743
+ const scrollLeft = document.body.scrollLeft;
744
744
  const rect = viewport.getBoundingClientRect();
745
745
  const viewportWidth = viewport.clientWidth;
746
746
  const viewportHeight = viewport.clientHeight;
@@ -1245,15 +1245,15 @@ function execAnimation(element, className, autoCloseDelay = 700) {
1245
1245
  const RESPONSE_DATA_SYMBOL = Symbol("response data");
1246
1246
 
1247
1247
  async function updateAuthToken(options) {
1248
- let token = localStorage.getItem(options.storageTokenName);
1249
- const expires = localStorage.getItem(options.storageTokenExpiresName);
1248
+ let token = localStorage.getItem(options.tokenStorageName);
1249
+ const expires = localStorage.getItem(options.expiresTokenStorageName);
1250
1250
  if (!token || !expires || Date.now() > +expires) {
1251
1251
  token = await (options.tokenRequest ? options.tokenRequest() : getAuthToken(options));
1252
1252
  if (isNull(token)) {
1253
- return void window.location.replace(options.authUrl());
1253
+ return void window.location.replace(typeof options.oauthUrl === "function" ? options.oauthUrl() : options.oauthUrl);
1254
1254
  }
1255
1255
  if (isUndefined(token)) {
1256
- return void window.location.replace(options.errorUrl);
1256
+ return void window.location.replace(typeof options.errorUrl === "function" ? options.errorUrl() : options.errorUrl);
1257
1257
  }
1258
1258
  }
1259
1259
  return token;
@@ -1271,8 +1271,8 @@ async function getAuthToken(options) {
1271
1271
  const result = (await response.json());
1272
1272
  try {
1273
1273
  const { expires, token } = transformData(result, options.pathToToken, options.pathToTokenExpires);
1274
- localStorage.setItem(options.storageTokenExpiresName, expires);
1275
- localStorage.setItem(options.storageTokenName, token);
1274
+ localStorage.setItem(options.expiresTokenStorageName, expires);
1275
+ localStorage.setItem(options.tokenStorageName, token);
1276
1276
  return token;
1277
1277
  }
1278
1278
  catch {
@@ -1299,8 +1299,10 @@ function transformData(data, pathToToken, pathToTokenExpires) {
1299
1299
  }
1300
1300
  async function getAuthTokenNoRefresh(options) {
1301
1301
  let waiting = true;
1302
- const url = new URL(window.origin);
1303
- url.searchParams.append(options.queryIsRefreshTokenName, "true");
1302
+ const url = new URL(typeof options.refreshTokenWindowUrl === "function"
1303
+ ? options.refreshTokenWindowUrl()
1304
+ : (options.refreshTokenWindowUrl ?? window.origin));
1305
+ url.searchParams.append(options.onlyRefreshTokenWindowQueryName, "true");
1304
1306
  let windowInstance = window.open(url.toString(), "_blank", "width=800,height=600,left=100,top=100");
1305
1307
  windowInstance ??= window.open(url.toString());
1306
1308
  if (!windowInstance) {
@@ -1308,7 +1310,7 @@ async function getAuthTokenNoRefresh(options) {
1308
1310
  options.onWindowOpenError();
1309
1311
  return;
1310
1312
  }
1311
- const channel = new BroadcastChannel(options.queryIsRefreshTokenName);
1313
+ const channel = new BroadcastChannel(options.onlyRefreshTokenWindowQueryName);
1312
1314
  channel.onmessage = () => {
1313
1315
  if (waiting) {
1314
1316
  waiting = false;
@@ -1327,41 +1329,50 @@ async function getAuthTokenNoRefresh(options) {
1327
1329
  await waitUntil(() => waiting);
1328
1330
  }
1329
1331
  function updateAuthTokenNoRefresh(options) {
1330
- let expires = localStorage.getItem(options.storageTokenExpiresName);
1332
+ let expires = localStorage.getItem(options.expiresTokenStorageName);
1331
1333
  if (!expires || Number.isNaN(+expires) || Date.now() > +expires)
1332
1334
  expires = null;
1333
- const queries = getQueryValues([options.queryTokenExpiresName, options.queryIsRefreshTokenName]);
1334
- const refreshQuery = queries?.[options.queryIsRefreshTokenName];
1335
- const expiresQuery = queries?.[options.queryTokenExpiresName];
1335
+ const queries = getQueryValues([
1336
+ options.expiresTokenQueryName,
1337
+ options.onlyRefreshTokenWindowQueryName,
1338
+ ]);
1339
+ const refreshQuery = queries?.[options.onlyRefreshTokenWindowQueryName];
1340
+ const expiresQuery = queries?.[options.expiresTokenQueryName];
1341
+ /** Is OnlyRefresh window */
1336
1342
  const isRefresh = isString(refreshQuery)
1337
1343
  ? refreshQuery === "true"
1338
1344
  : isArray(refreshQuery)
1339
1345
  ? refreshQuery[refreshQuery.length - 1] === "true"
1340
1346
  : false;
1347
+ /** Expires token */
1341
1348
  const expiresFromQuery = isString(expiresQuery)
1342
1349
  ? expiresQuery
1343
1350
  : isArray(expiresQuery)
1344
1351
  ? expiresQuery[expiresQuery.length - 1]
1345
1352
  : false;
1353
+ /** Extract expires from query */
1346
1354
  if (!expires && expiresFromQuery) {
1347
1355
  expires = expiresFromQuery;
1348
1356
  if (!expires || Number.isNaN(+expires) || Date.now() > +expires)
1349
1357
  expires = null;
1350
1358
  }
1359
+ /** OAuth flow if not expires */
1351
1360
  if (!expires) {
1352
- window.location.replace(options.authUrl());
1361
+ window.location.replace(typeof options.oauthUrl === "function" ? options.oauthUrl() : options.oauthUrl);
1353
1362
  return null;
1354
1363
  }
1355
- localStorage.setItem(options.storageTokenExpiresName, expires);
1364
+ localStorage.setItem(options.expiresTokenStorageName, expires);
1365
+ /** Close if OnlyRefresh window */
1356
1366
  if (isRefresh) {
1357
- const channel = new BroadcastChannel(options.queryIsRefreshTokenName);
1367
+ const channel = new BroadcastChannel(options.onlyRefreshTokenWindowQueryName);
1358
1368
  channel.postMessage(true);
1359
1369
  channel.close();
1360
1370
  window.close();
1361
1371
  }
1372
+ /** Delete expires query */
1362
1373
  if (expiresFromQuery) {
1363
1374
  const url = new URL(window.location.href);
1364
- url.searchParams.delete(options.queryTokenExpiresName);
1375
+ url.searchParams.delete(options.expiresTokenQueryName);
1365
1376
  window.location.replace(url.toString());
1366
1377
  return null;
1367
1378
  }
@@ -1371,7 +1382,7 @@ function updateAuthTokenNoRefresh(options) {
1371
1382
  async function updateAuthUser(options) {
1372
1383
  const userInfo = await (options.userRequest ? options.userRequest() : getAuthUser(options));
1373
1384
  if (isNull(userInfo)) {
1374
- return void window.location.replace(options.authUrl());
1385
+ return void window.location.replace(options.oauthUrl());
1375
1386
  }
1376
1387
  if (isUndefined(userInfo)) {
1377
1388
  return void window.location.replace(options.errorUrl);
@@ -1401,9 +1412,9 @@ async function getAuthUser(options) {
1401
1412
  let isFetchingAccessToken$1 = false;
1402
1413
  const generateAuthMiddleWare = (options) => async (request) => {
1403
1414
  if (!options.authTokenUrl ||
1404
- !options.authUrl ||
1405
- !options.storageTokenExpiresName ||
1406
- !options.storageTokenName ||
1415
+ !options.oauthUrl ||
1416
+ !options.expiresTokenStorageName ||
1417
+ !options.tokenStorageName ||
1407
1418
  !options.pathToTokenExpires ||
1408
1419
  !options.pathToToken ||
1409
1420
  !options.errorUrl) {
@@ -1420,17 +1431,17 @@ const generateAuthMiddleWare = (options) => async (request) => {
1420
1431
  }
1421
1432
  if (isFetchingAccessToken$1)
1422
1433
  await waitUntil(() => isFetchingAccessToken$1);
1423
- const expires = localStorage.getItem(options.storageTokenExpiresName);
1424
- let token = localStorage.getItem(options.storageTokenName);
1434
+ const expires = localStorage.getItem(options.expiresTokenStorageName);
1435
+ let token = localStorage.getItem(options.tokenStorageName);
1425
1436
  if (!expires || Date.now() > +expires || !token) {
1426
1437
  isFetchingAccessToken$1 = true;
1427
1438
  token = await (options.tokenRequest ? options.tokenRequest() : getAuthToken(options));
1428
1439
  isFetchingAccessToken$1 = false;
1429
1440
  if (isNull(token)) {
1430
- return void window.location.replace(options.authUrl());
1441
+ return void window.location.replace(typeof options.oauthUrl === "function" ? options.oauthUrl() : options.oauthUrl);
1431
1442
  }
1432
1443
  if (isUndefined(token)) {
1433
- return void window.location.replace(options.errorUrl);
1444
+ return void window.location.replace(typeof options.errorUrl === "function" ? options.errorUrl() : options.errorUrl);
1434
1445
  }
1435
1446
  }
1436
1447
  if (!isSameOrigin)
@@ -1442,7 +1453,7 @@ const generateAuthMiddleWare = (options) => async (request) => {
1442
1453
 
1443
1454
  let isFetchingAccessToken = false;
1444
1455
  const generateAuthNoRefreshMiddleWare = (options) => async (request) => {
1445
- if (!options.authUrl || !options.storageTokenExpiresName || !options.errorUrl) {
1456
+ if (!options.oauthUrl || !options.expiresTokenStorageName || !options.errorUrl) {
1446
1457
  throw new Error("Auth middleware hasn't required options");
1447
1458
  }
1448
1459
  const isSameOrigin = !startWith(request.path, "http");
@@ -1456,13 +1467,13 @@ const generateAuthNoRefreshMiddleWare = (options) => async (request) => {
1456
1467
  }
1457
1468
  if (isFetchingAccessToken)
1458
1469
  await waitUntil(() => isFetchingAccessToken);
1459
- const expires = localStorage.getItem(options.storageTokenExpiresName);
1470
+ const expires = localStorage.getItem(options.expiresTokenStorageName);
1460
1471
  if (!expires || Number.isNaN(+expires) || Date.now() > +expires) {
1461
1472
  isFetchingAccessToken = true;
1462
1473
  await (options.tokenRequest ? options.tokenRequest() : getAuthTokenNoRefresh(options));
1463
1474
  isFetchingAccessToken = false;
1464
1475
  }
1465
- const token = options.storageTokenName ? localStorage.getItem(options.storageTokenName) : null;
1476
+ const token = options.tokenStorageName ? localStorage.getItem(options.tokenStorageName) : null;
1466
1477
  if (!isSameOrigin && token)
1467
1478
  request.headers = {
1468
1479
  ...request.headers,