@krainovsd/js-helpers 0.10.6 → 0.12.0
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/README.md +14 -10
- package/lib/cjs/index.cjs +70 -62
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/esm/constants/environment.js +5 -5
- package/lib/esm/constants/environment.js.map +1 -1
- package/lib/esm/index.js +1 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/lib/api/auth/token.js +29 -28
- package/lib/esm/lib/api/auth/token.js.map +1 -1
- package/lib/esm/lib/api/core.js +17 -31
- package/lib/esm/lib/api/core.js.map +1 -1
- package/lib/esm/lib/browser/get-query-values.js +26 -0
- package/lib/esm/lib/browser/get-query-values.js.map +1 -0
- package/lib/esm/lib/browser/get-visible-position.js +2 -2
- package/lib/esm/lib/browser/get-visible-position.js.map +1 -1
- package/lib/esm/lib/date/date-difference.js +1 -1
- package/lib/esm/lib/date/date-difference.js.map +1 -1
- package/lib/esm/lib/utils/get-caller-function-name.js +1 -1
- package/lib/esm/lib/utils/get-caller-function-name.js.map +1 -1
- package/lib/esm/lib/utils/limit-stream-of-requests.js +2 -0
- package/lib/esm/lib/utils/limit-stream-of-requests.js.map +1 -1
- package/lib/esm/lib/utils/set-by-path.js +0 -1
- package/lib/esm/lib/utils/set-by-path.js.map +1 -1
- package/lib/esm/lib/utils/translit.js +1 -0
- package/lib/esm/lib/utils/translit.js.map +1 -1
- package/lib/index.d.ts +18 -7
- package/package.json +17 -25
package/README.md
CHANGED
|
@@ -4,24 +4,28 @@ The library of helpers for JS in Browser and NodeJS environments.
|
|
|
4
4
|
|
|
5
5
|
## Installing
|
|
6
6
|
|
|
7
|
-
### Package manager
|
|
8
|
-
|
|
9
7
|
Using pnpm:
|
|
10
8
|
```
|
|
11
9
|
pnpm install @krainovsd/js-helpers
|
|
12
10
|
```
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
```
|
|
16
|
-
yarn add @krainovsd/js-helpers
|
|
17
|
-
```
|
|
12
|
+
## Monorepo
|
|
18
13
|
|
|
19
|
-
Using
|
|
20
|
-
```
|
|
21
|
-
|
|
14
|
+
Using in frontend app with monorepo make sure that node-fetch will be excluded from bundle.
|
|
15
|
+
```js
|
|
16
|
+
export default defineConfig({
|
|
17
|
+
base: "/",
|
|
18
|
+
plugins: [
|
|
19
|
+
react(),
|
|
20
|
+
],
|
|
21
|
+
build: {
|
|
22
|
+
rollupOptions: {
|
|
23
|
+
external: ["node-fetch"],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
});
|
|
22
27
|
```
|
|
23
28
|
|
|
24
|
-
|
|
25
29
|
## Usage
|
|
26
30
|
|
|
27
31
|
```js
|
package/lib/cjs/index.cjs
CHANGED
|
@@ -27,18 +27,18 @@ const POST_API_MIDDLEWARES = {
|
|
|
27
27
|
Logger: "logger",
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
/* eslint-disable no-restricted-globals */
|
|
31
30
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
32
31
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
33
32
|
const IS_JEST = typeof process != "undefined" && process.env.JEST_WORKER_ID !== undefined;
|
|
34
|
-
const IS_BUN = typeof process !== "undefined" && process
|
|
35
|
-
const IS_DENO =
|
|
36
|
-
|
|
33
|
+
const IS_BUN = typeof process !== "undefined" && process?.versions?.bun != undefined;
|
|
34
|
+
const IS_DENO =
|
|
35
|
+
// @ts-expect-error
|
|
36
|
+
typeof Deno !== "undefined" &&
|
|
37
37
|
// @ts-expect-error
|
|
38
38
|
typeof Deno.version !== "undefined" &&
|
|
39
39
|
// @ts-expect-error
|
|
40
40
|
typeof Deno.version.deno !== "undefined";
|
|
41
|
-
const IS_NODE = typeof process !== "undefined" && process
|
|
41
|
+
const IS_NODE = typeof process !== "undefined" && process?.versions?.node != undefined;
|
|
42
42
|
const IS_WEB_WORKER = typeof self === "object" &&
|
|
43
43
|
self.constructor &&
|
|
44
44
|
self.constructor.name === "DedicatedWorkerGlobalScope";
|
|
@@ -126,7 +126,7 @@ function getByPath(data, path, defaultValue = undefined) {
|
|
|
126
126
|
return get(data, path, defaultValue);
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
function dateDifference(
|
|
129
|
+
function dateDifference({ firstDate, float = false, secondDate = new Date(), type, }) {
|
|
130
130
|
const first = dayjs(firstDate);
|
|
131
131
|
return first.diff(secondDate, type, float);
|
|
132
132
|
}
|
|
@@ -277,7 +277,6 @@ function setByPath(data, path, value) {
|
|
|
277
277
|
set(data, path, value);
|
|
278
278
|
}
|
|
279
279
|
catch (error) {
|
|
280
|
-
// eslint-disable-next-line no-console
|
|
281
280
|
console.warn(error);
|
|
282
281
|
}
|
|
283
282
|
}
|
|
@@ -416,6 +415,8 @@ function limitStreamOfRequests({ countRequests, maxCountInParallel, promiseGette
|
|
|
416
415
|
if (resultCb)
|
|
417
416
|
resultCb(result);
|
|
418
417
|
currentResponses++;
|
|
418
|
+
if (currentResponses === countRequests)
|
|
419
|
+
return void resolve(results);
|
|
419
420
|
request(promiseGetter(++currentRequests), currentRequests);
|
|
420
421
|
})
|
|
421
422
|
.catch((error) => {
|
|
@@ -456,7 +457,7 @@ function getCallerFunctionName() {
|
|
|
456
457
|
const stack = error.stack.split("\n");
|
|
457
458
|
if (stack[2]) {
|
|
458
459
|
const callerLine = stack[2].trim();
|
|
459
|
-
const match =
|
|
460
|
+
const match = /at (\w+)/.exec(callerLine);
|
|
460
461
|
if (match) {
|
|
461
462
|
return match[1];
|
|
462
463
|
}
|
|
@@ -466,6 +467,7 @@ function getCallerFunctionName() {
|
|
|
466
467
|
return null;
|
|
467
468
|
}
|
|
468
469
|
|
|
470
|
+
/* eslint-disable id-length */
|
|
469
471
|
const translitDict = {
|
|
470
472
|
А: "A",
|
|
471
473
|
Б: "B",
|
|
@@ -699,14 +701,14 @@ function getVisiblePosition({ initialPosition, node, visibleArea, placement = "b
|
|
|
699
701
|
let targetHeight = 0;
|
|
700
702
|
let targetWidth = 0;
|
|
701
703
|
let { top: targetTopPosition, left: targetLeftPosition, height: nodeHeight, width: nodeWidth, } = node.getBoundingClientRect();
|
|
702
|
-
if (initialPosition
|
|
704
|
+
if (initialPosition?.targetNode) {
|
|
703
705
|
const { top: childTop, left: childLeft, height, width, } = initialPosition.targetNode.getBoundingClientRect();
|
|
704
706
|
targetHeight = height;
|
|
705
707
|
targetWidth = width;
|
|
706
708
|
targetTopPosition = childTop;
|
|
707
709
|
targetLeftPosition = childLeft;
|
|
708
710
|
}
|
|
709
|
-
if (initialPosition
|
|
711
|
+
if (initialPosition?.position) {
|
|
710
712
|
if (initialPosition.position.x) {
|
|
711
713
|
targetLeftPosition = initialPosition.position.x;
|
|
712
714
|
}
|
|
@@ -1268,6 +1270,26 @@ function getFlexVisiblePosition({ initialLeft, initialTop, isCompletelyVisibleX,
|
|
|
1268
1270
|
};
|
|
1269
1271
|
}
|
|
1270
1272
|
|
|
1273
|
+
function getQueryValues(keys) {
|
|
1274
|
+
if (!IS_BROWSER && !IS_JEST)
|
|
1275
|
+
return null;
|
|
1276
|
+
const object = {};
|
|
1277
|
+
const queries = window.location.search.substring(1).split("&");
|
|
1278
|
+
for (const query of queries) {
|
|
1279
|
+
const [key, value] = query.split("=");
|
|
1280
|
+
if (keys.includes(key) && value) {
|
|
1281
|
+
const prev = object[key];
|
|
1282
|
+
if (isArray(prev))
|
|
1283
|
+
prev.push(value);
|
|
1284
|
+
else if (isString(prev))
|
|
1285
|
+
object[key] = [prev, value];
|
|
1286
|
+
else
|
|
1287
|
+
object[key] = value;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
return object;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1271
1293
|
const RESPONSE_DATA_SYMBOL = Symbol("response data");
|
|
1272
1294
|
|
|
1273
1295
|
async function updateAuthToken(options) {
|
|
@@ -1358,18 +1380,21 @@ function updateAuthTokenNoRefresh(options) {
|
|
|
1358
1380
|
let expires = localStorage.getItem(options.storageTokenExpiresName);
|
|
1359
1381
|
if (!expires || Number.isNaN(+expires) || Date.now() > +expires)
|
|
1360
1382
|
expires = null;
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1383
|
+
const queries = getQueryValues([options.queryTokenExpiresName, options.queryIsRefreshTokenName]);
|
|
1384
|
+
const refreshQuery = queries?.[options.queryIsRefreshTokenName];
|
|
1385
|
+
const expiresQuery = queries?.[options.queryTokenExpiresName];
|
|
1386
|
+
const isRefresh = isString(refreshQuery)
|
|
1387
|
+
? refreshQuery === "true"
|
|
1388
|
+
: isArray(refreshQuery)
|
|
1389
|
+
? refreshQuery[refreshQuery.length - 1] === "true"
|
|
1390
|
+
: false;
|
|
1391
|
+
const expiresFromQuery = isString(expiresQuery)
|
|
1392
|
+
? expiresQuery
|
|
1393
|
+
: isArray(expiresQuery)
|
|
1394
|
+
? expiresQuery[expiresQuery.length - 1]
|
|
1395
|
+
: false;
|
|
1396
|
+
if (!expires && expiresFromQuery) {
|
|
1397
|
+
expires = expiresFromQuery;
|
|
1373
1398
|
if (!expires || Number.isNaN(+expires) || Date.now() > +expires)
|
|
1374
1399
|
expires = null;
|
|
1375
1400
|
}
|
|
@@ -1378,17 +1403,13 @@ function updateAuthTokenNoRefresh(options) {
|
|
|
1378
1403
|
return null;
|
|
1379
1404
|
}
|
|
1380
1405
|
localStorage.setItem(options.storageTokenExpiresName, expires);
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
channel.postMessage(true);
|
|
1387
|
-
channel.close();
|
|
1388
|
-
window.close();
|
|
1389
|
-
}
|
|
1406
|
+
if (isRefresh) {
|
|
1407
|
+
const channel = new BroadcastChannel(options.queryIsRefreshTokenName);
|
|
1408
|
+
channel.postMessage(true);
|
|
1409
|
+
channel.close();
|
|
1410
|
+
window.close();
|
|
1390
1411
|
}
|
|
1391
|
-
if (
|
|
1412
|
+
if (expiresFromQuery) {
|
|
1392
1413
|
const url = new URL(window.location.href);
|
|
1393
1414
|
url.searchParams.delete(options.queryTokenExpiresName);
|
|
1394
1415
|
window.location.replace(url.toString());
|
|
@@ -1634,14 +1655,16 @@ class ResponseError extends Error {
|
|
|
1634
1655
|
status;
|
|
1635
1656
|
code;
|
|
1636
1657
|
description;
|
|
1637
|
-
|
|
1658
|
+
headers;
|
|
1659
|
+
constructor({ message, status, description, code, headers }) {
|
|
1638
1660
|
super(message);
|
|
1639
1661
|
this.status = status;
|
|
1640
1662
|
this.description = description;
|
|
1641
1663
|
this.code = code;
|
|
1664
|
+
this.headers = headers;
|
|
1642
1665
|
}
|
|
1643
1666
|
}
|
|
1644
|
-
function createRequestClientInstance(options
|
|
1667
|
+
function createRequestClientInstance(options) {
|
|
1645
1668
|
let executeMiddlewares;
|
|
1646
1669
|
let executePostMiddlewares;
|
|
1647
1670
|
function setMiddlewares({ activeMiddlewares = [], middlewareOptions = {}, customMiddlewares = [], activePostMiddlewares = [], postMiddlewaresOptions = {}, customPostMiddlewares = [], } = {}) {
|
|
@@ -1672,34 +1695,17 @@ function createRequestClientInstance(options = {}) {
|
|
|
1672
1695
|
if (isObject(body) || isArray(body))
|
|
1673
1696
|
preparedBody = JSON.stringify(preparedBody);
|
|
1674
1697
|
}
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
body
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
signal: request.signal,
|
|
1687
|
-
});
|
|
1688
|
-
}
|
|
1689
|
-
if (IS_NODE && !IS_JEST) {
|
|
1690
|
-
const nodeFetch = (await import('node-fetch')).default;
|
|
1691
|
-
response = await nodeFetch(url, {
|
|
1692
|
-
method,
|
|
1693
|
-
body: preparedBody,
|
|
1694
|
-
headers: {
|
|
1695
|
-
...(body instanceof FormData || !body
|
|
1696
|
-
? {}
|
|
1697
|
-
: { "Content-Type": "application/json; charset=UTF-8" }),
|
|
1698
|
-
...headers,
|
|
1699
|
-
},
|
|
1700
|
-
signal: request.signal,
|
|
1701
|
-
});
|
|
1702
|
-
}
|
|
1698
|
+
const response = await options.client(url, {
|
|
1699
|
+
method,
|
|
1700
|
+
body: preparedBody,
|
|
1701
|
+
headers: {
|
|
1702
|
+
...(body instanceof FormData || !body
|
|
1703
|
+
? {}
|
|
1704
|
+
: { "Content-Type": "application/json; charset=UTF-8" }),
|
|
1705
|
+
...headers,
|
|
1706
|
+
},
|
|
1707
|
+
signal: request.signal,
|
|
1708
|
+
});
|
|
1703
1709
|
await executePostMiddlewares(response);
|
|
1704
1710
|
if (!response) {
|
|
1705
1711
|
throw new Error("hasn't response");
|
|
@@ -1726,6 +1732,7 @@ function createRequestClientInstance(options = {}) {
|
|
|
1726
1732
|
status: response.status,
|
|
1727
1733
|
message: `HTTP error! Status: ${response.status}`,
|
|
1728
1734
|
description: result,
|
|
1735
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
1729
1736
|
});
|
|
1730
1737
|
}
|
|
1731
1738
|
if (request.downloadFile) {
|
|
@@ -1903,6 +1910,7 @@ exports.getCallerFunctionName = getCallerFunctionName;
|
|
|
1903
1910
|
exports.getColorFormat = getColorFormat;
|
|
1904
1911
|
exports.getDateByRules = getDateByRules;
|
|
1905
1912
|
exports.getFileNameFromHeader = getFileNameFromHeader;
|
|
1913
|
+
exports.getQueryValues = getQueryValues;
|
|
1906
1914
|
exports.getRandomColor = getRandomColor;
|
|
1907
1915
|
exports.getToday = getToday;
|
|
1908
1916
|
exports.getTomorrow = getTomorrow;
|