@krainovsd/js-helpers 0.14.13 → 0.15.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/lib/cjs/index.cjs +432 -245
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/esm/constants/api.js +1 -2
- package/lib/esm/constants/api.js.map +1 -1
- package/lib/esm/index.js +16 -4
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/lib/api/core.js +2 -2
- package/lib/esm/lib/api/middlewares/index.js +4 -10
- package/lib/esm/lib/api/middlewares/index.js.map +1 -1
- package/lib/esm/lib/api/middlewares/{auth-no-refresh-middleware.js → oauth-middleware.js} +13 -7
- package/lib/esm/lib/api/middlewares/oauth-middleware.js.map +1 -0
- package/lib/esm/lib/api/{auth → oauth}/token.js +4 -61
- package/lib/esm/lib/api/oauth/token.js.map +1 -0
- package/lib/esm/lib/api/oauth/user.js.map +1 -0
- package/lib/esm/lib/browser/copy-to-clipboard.js +37 -0
- package/lib/esm/lib/browser/copy-to-clipboard.js.map +1 -0
- package/lib/esm/lib/browser/download-file.js +1 -1
- package/lib/esm/lib/browser/extract-queries.js +21 -0
- package/lib/esm/lib/browser/extract-queries.js.map +1 -0
- package/lib/esm/lib/lodash/clone-deep.js +62 -0
- package/lib/esm/lib/lodash/clone-deep.js.map +1 -0
- package/lib/esm/lib/lodash/debounce.js +32 -0
- package/lib/esm/lib/lodash/debounce.js.map +1 -0
- package/lib/esm/lib/lodash/difference-by.js +18 -0
- package/lib/esm/lib/lodash/difference-by.js.map +1 -0
- package/lib/esm/lib/lodash/difference-with.js +18 -0
- package/lib/esm/lib/lodash/difference-with.js.map +1 -0
- package/lib/esm/lib/lodash/difference.js +18 -0
- package/lib/esm/lib/lodash/difference.js.map +1 -0
- package/lib/esm/lib/lodash/get-by-path.js.map +1 -0
- package/lib/esm/lib/lodash/is-equal.js +46 -0
- package/lib/esm/lib/lodash/is-equal.js.map +1 -0
- package/lib/esm/lib/lodash/set-by-path.js.map +1 -0
- package/lib/esm/lib/lodash/throttle.js +22 -0
- package/lib/esm/lib/lodash/throttle.js.map +1 -0
- package/lib/esm/lib/lodash/type.js +10 -0
- package/lib/esm/lib/lodash/type.js.map +1 -0
- package/lib/esm/lib/typings/is-primitive.js +7 -0
- package/lib/esm/lib/typings/is-primitive.js.map +1 -0
- package/lib/esm/lib/utils/declination.js +14 -0
- package/lib/esm/lib/utils/declination.js.map +1 -0
- package/lib/esm/lib/utils/sync-object-values.js +2 -2
- package/lib/esm/lib/utils/sync-object-values.js.map +1 -1
- package/lib/index.d.ts +47 -26
- package/package.json +1 -1
- package/lib/esm/lib/api/auth/token.js.map +0 -1
- package/lib/esm/lib/api/auth/user.js.map +0 -1
- package/lib/esm/lib/api/middlewares/auth-middleware.js +0 -57
- package/lib/esm/lib/api/middlewares/auth-middleware.js.map +0 -1
- package/lib/esm/lib/api/middlewares/auth-no-refresh-middleware.js.map +0 -1
- package/lib/esm/lib/utils/get-by-path.js.map +0 -1
- package/lib/esm/lib/utils/set-by-path.js.map +0 -1
- /package/lib/esm/lib/api/{auth → oauth}/user.js +0 -0
- /package/lib/esm/lib/{utils → lodash}/get-by-path.js +0 -0
- /package/lib/esm/lib/{utils → lodash}/set-by-path.js +0 -0
package/lib/cjs/index.cjs
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const get = require('lodash/get');
|
|
6
5
|
const dayjs = require('dayjs');
|
|
7
6
|
const todayPlugin = require('dayjs/plugin/isToday');
|
|
8
7
|
const tomorrowPlugin = require('dayjs/plugin/isTomorrow');
|
|
9
8
|
const yesterdayPlugin = require('dayjs/plugin/isYesterday');
|
|
9
|
+
const get = require('lodash/get');
|
|
10
10
|
const set = require('lodash/set');
|
|
11
11
|
|
|
12
12
|
const DATE_TYPES = {
|
|
@@ -19,8 +19,7 @@ const DATE_TYPES = {
|
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
const API_MIDDLEWARES = {
|
|
22
|
-
|
|
23
|
-
AuthNoRefresh: "authNoRefresh",
|
|
22
|
+
Oauth: "oauth",
|
|
24
23
|
Logger: "logger",
|
|
25
24
|
};
|
|
26
25
|
const POST_API_MIDDLEWARES = {
|
|
@@ -71,61 +70,6 @@ const waitUntil = (condition, checkInterval = 100) => {
|
|
|
71
70
|
});
|
|
72
71
|
};
|
|
73
72
|
|
|
74
|
-
function checkType(value, condition) {
|
|
75
|
-
if (condition) {
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function isArray(value) {
|
|
82
|
-
return Boolean(value && Array.isArray(value));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function isBoolean(value) {
|
|
86
|
-
return typeof value === "boolean";
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function isNumber(value) {
|
|
90
|
-
return typeof value === "number" && !Number.isNaN(value);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function isString(value) {
|
|
94
|
-
return typeof value === "string";
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function isId(value) {
|
|
98
|
-
return isNumber(value) || isString(value);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function isNull(value) {
|
|
102
|
-
return value === null;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function isNullable(value) {
|
|
106
|
-
return typeof value === "undefined" || value === null;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function isObject(value) {
|
|
110
|
-
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function isUndefined(value) {
|
|
114
|
-
return typeof value === "undefined";
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function isDate(value) {
|
|
118
|
-
if (value instanceof Date)
|
|
119
|
-
return !Number.isNaN(value.valueOf());
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function getByPath(data, path, defaultValue) {
|
|
124
|
-
if ((!isObject(data) && !isArray(data)) || !isString(path))
|
|
125
|
-
return defaultValue;
|
|
126
|
-
return get(data, path, defaultValue);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
73
|
function dateDifference({ firstDate, float = false, secondDate = new Date(), type, }) {
|
|
130
74
|
const first = dayjs(firstDate);
|
|
131
75
|
return first.diff(secondDate, type, float);
|
|
@@ -222,6 +166,60 @@ function isYesterday(date) {
|
|
|
222
166
|
}
|
|
223
167
|
}
|
|
224
168
|
|
|
169
|
+
function checkType(value, condition) {
|
|
170
|
+
if (condition) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function isArray(value) {
|
|
177
|
+
return Boolean(value && Array.isArray(value));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function isBoolean(value) {
|
|
181
|
+
return typeof value === "boolean";
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isNumber(value) {
|
|
185
|
+
return typeof value === "number" && !Number.isNaN(value);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function isString(value) {
|
|
189
|
+
return typeof value === "string";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function isId(value) {
|
|
193
|
+
return isNumber(value) || isString(value);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function isNull(value) {
|
|
197
|
+
return value === null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function isNullable(value) {
|
|
201
|
+
return typeof value === "undefined" || value === null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isObject(value) {
|
|
205
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function isUndefined(value) {
|
|
209
|
+
return typeof value === "undefined";
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function isDate(value) {
|
|
213
|
+
if (value instanceof Date)
|
|
214
|
+
return !Number.isNaN(value.valueOf());
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function isPrimitive(value) {
|
|
219
|
+
const type = typeof value;
|
|
220
|
+
return value == undefined || (type != "object" && type != "function");
|
|
221
|
+
}
|
|
222
|
+
|
|
225
223
|
function dateFormat(date, format) {
|
|
226
224
|
const correctDate = new Date(date);
|
|
227
225
|
if (!isDate(correctDate))
|
|
@@ -274,17 +272,6 @@ function fieldViewFormat(field, type = "string") {
|
|
|
274
272
|
}
|
|
275
273
|
}
|
|
276
274
|
|
|
277
|
-
function setByPath(data, path, value) {
|
|
278
|
-
try {
|
|
279
|
-
if (!isObject(data) && !isArray(data))
|
|
280
|
-
throw new Error("bad data");
|
|
281
|
-
set(data, path, value);
|
|
282
|
-
}
|
|
283
|
-
catch (error) {
|
|
284
|
-
console.warn(error);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
275
|
function jsonParse(json) {
|
|
289
276
|
if (!isString(json))
|
|
290
277
|
return null;
|
|
@@ -356,6 +343,23 @@ function arrayToMapByKey(array, key) {
|
|
|
356
343
|
return map;
|
|
357
344
|
}
|
|
358
345
|
|
|
346
|
+
function getByPath(data, path, defaultValue) {
|
|
347
|
+
if ((!isObject(data) && !isArray(data)) || !isString(path))
|
|
348
|
+
return defaultValue;
|
|
349
|
+
return get(data, path, defaultValue);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function setByPath(data, path, value) {
|
|
353
|
+
try {
|
|
354
|
+
if (!isObject(data) && !isArray(data))
|
|
355
|
+
throw new Error("bad data");
|
|
356
|
+
set(data, path, value);
|
|
357
|
+
}
|
|
358
|
+
catch (error) {
|
|
359
|
+
console.warn(error);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
359
363
|
function syncObjectValues(oldObj, newObj, exception = []) {
|
|
360
364
|
for (const field in newObj) {
|
|
361
365
|
if (exception.includes(field))
|
|
@@ -667,6 +671,18 @@ function createLocalIdGenerator() {
|
|
|
667
671
|
};
|
|
668
672
|
}
|
|
669
673
|
|
|
674
|
+
function declination(value, words) {
|
|
675
|
+
value = Math.abs(value) % 100;
|
|
676
|
+
const num = value % 10;
|
|
677
|
+
if (value > 10 && value < 20)
|
|
678
|
+
return words[2];
|
|
679
|
+
if (num > 1 && num < 5)
|
|
680
|
+
return words[1];
|
|
681
|
+
if (num == 1)
|
|
682
|
+
return words[0];
|
|
683
|
+
return words[2];
|
|
684
|
+
}
|
|
685
|
+
|
|
670
686
|
function downloadFile({ data, fileName, mimeType }) {
|
|
671
687
|
if (!IS_BROWSER && !IS_JEST)
|
|
672
688
|
return null;
|
|
@@ -1242,62 +1258,129 @@ function execAnimation(element, className, autoCloseDelay = 700) {
|
|
|
1242
1258
|
});
|
|
1243
1259
|
}
|
|
1244
1260
|
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
if (!token || !expires || Date.now() > +expires) {
|
|
1251
|
-
token = await (options.tokenRequest ? options.tokenRequest() : getAuthToken(options));
|
|
1252
|
-
if (isNull(token)) {
|
|
1253
|
-
return void window.location.replace(typeof options.oauthUrl === "function" ? options.oauthUrl() : options.oauthUrl);
|
|
1254
|
-
}
|
|
1255
|
-
if (isUndefined(token)) {
|
|
1256
|
-
return void window.location.replace(typeof options.errorUrl === "function" ? options.errorUrl() : options.errorUrl);
|
|
1257
|
-
}
|
|
1258
|
-
}
|
|
1259
|
-
return token;
|
|
1260
|
-
}
|
|
1261
|
-
async function getAuthToken(options) {
|
|
1262
|
-
let status = 0;
|
|
1263
|
-
try {
|
|
1264
|
-
const response = await fetch(options.authTokenUrl, {
|
|
1265
|
-
method: "GET",
|
|
1266
|
-
});
|
|
1267
|
-
status = response.status;
|
|
1268
|
-
if (!response.ok) {
|
|
1269
|
-
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
1261
|
+
function copyToClipboard(textToCopy) {
|
|
1262
|
+
return new Promise((resolve) => {
|
|
1263
|
+
if (!IS_BROWSER && !IS_JEST) {
|
|
1264
|
+
resolve(false);
|
|
1265
|
+
return;
|
|
1270
1266
|
}
|
|
1271
|
-
const result = (await response.json());
|
|
1272
1267
|
try {
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1268
|
+
if (navigator.clipboard && window.isSecureContext) {
|
|
1269
|
+
navigator.clipboard
|
|
1270
|
+
.writeText(textToCopy)
|
|
1271
|
+
.then(() => resolve(true))
|
|
1272
|
+
.catch(() => resolve(false));
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1275
|
+
const textArea = document.createElement("textarea");
|
|
1276
|
+
textArea.value = textToCopy;
|
|
1277
|
+
textArea.style.position = "fixed";
|
|
1278
|
+
textArea.style.left = "-999999px";
|
|
1279
|
+
textArea.style.top = "-999999px";
|
|
1280
|
+
document.body.appendChild(textArea);
|
|
1281
|
+
textArea.focus();
|
|
1282
|
+
textArea.select();
|
|
1283
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated
|
|
1284
|
+
const result = document.execCommand("copy");
|
|
1285
|
+
textArea.remove();
|
|
1286
|
+
resolve(result);
|
|
1277
1287
|
}
|
|
1278
1288
|
catch {
|
|
1279
|
-
|
|
1289
|
+
resolve(false);
|
|
1280
1290
|
}
|
|
1281
|
-
}
|
|
1282
|
-
catch {
|
|
1283
|
-
if (status >= 500)
|
|
1284
|
-
return undefined;
|
|
1285
|
-
return null;
|
|
1286
|
-
}
|
|
1291
|
+
});
|
|
1287
1292
|
}
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1293
|
+
|
|
1294
|
+
function extractQueries() {
|
|
1295
|
+
const queryMap = {};
|
|
1296
|
+
if (!IS_BROWSER && !IS_JEST)
|
|
1297
|
+
return queryMap;
|
|
1298
|
+
const queryString = document.location.search;
|
|
1299
|
+
queryString
|
|
1300
|
+
.replace(/^\?/, "")
|
|
1301
|
+
.split("&")
|
|
1302
|
+
.forEach((query) => {
|
|
1303
|
+
const [key, value] = query.split("=", 2);
|
|
1304
|
+
queryMap[key] ??= [];
|
|
1305
|
+
const prev = queryMap[key];
|
|
1306
|
+
prev.push(value);
|
|
1307
|
+
});
|
|
1308
|
+
return queryMap;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
const RESPONSE_DATA_SYMBOL = Symbol("response data");
|
|
1312
|
+
|
|
1313
|
+
function generateConsoleMiddleware(options = {}) {
|
|
1314
|
+
return (request) => {
|
|
1315
|
+
return new Promise((resolve) => {
|
|
1316
|
+
if ((options.filter &&
|
|
1317
|
+
!options.filter(request)) ||
|
|
1318
|
+
(options.filterHeaders && !options.filterHeaders(request.headers)) ||
|
|
1319
|
+
(options.filterMethod && !options.filterMethod(request.method)) ||
|
|
1320
|
+
(options.filterParams && !options.filterParams(request.params)) ||
|
|
1321
|
+
(options.filterPath && !options.filterPath(request.path))) {
|
|
1322
|
+
resolve(true);
|
|
1323
|
+
return;
|
|
1324
|
+
}
|
|
1325
|
+
// eslint-disable-next-line no-console
|
|
1326
|
+
console.log(request);
|
|
1327
|
+
resolve(true);
|
|
1328
|
+
});
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
function generateConsolePostMiddleware(options = {}) {
|
|
1333
|
+
return (response) => {
|
|
1334
|
+
return new Promise((resolve) => {
|
|
1335
|
+
void (async function logger() {
|
|
1336
|
+
try {
|
|
1337
|
+
if (!response ||
|
|
1338
|
+
(options.filter && !options.filter(response)) ||
|
|
1339
|
+
(options.filterStatus && !options.filterStatus(response.status)) ||
|
|
1340
|
+
(options.filterUrl && !options.filterUrl(response.url)) ||
|
|
1341
|
+
(options.filterHeaders && !options.filterHeaders(response.headers))) {
|
|
1342
|
+
resolve(true);
|
|
1343
|
+
return;
|
|
1344
|
+
}
|
|
1345
|
+
const contentType = response.headers.get("content-type");
|
|
1346
|
+
let result;
|
|
1347
|
+
if (contentType?.includes?.("text")) {
|
|
1348
|
+
result = await response.text();
|
|
1349
|
+
}
|
|
1350
|
+
else if (contentType?.includes?.("json")) {
|
|
1351
|
+
result = await response.json();
|
|
1352
|
+
}
|
|
1353
|
+
else {
|
|
1354
|
+
result = await response.blob();
|
|
1355
|
+
}
|
|
1356
|
+
Object.defineProperty(response, RESPONSE_DATA_SYMBOL, {
|
|
1357
|
+
value: result,
|
|
1358
|
+
writable: false,
|
|
1359
|
+
enumerable: false,
|
|
1360
|
+
configurable: true,
|
|
1361
|
+
});
|
|
1362
|
+
console.log({
|
|
1363
|
+
url: response.url,
|
|
1364
|
+
status: response.status,
|
|
1365
|
+
headers: response.headers,
|
|
1366
|
+
body: result,
|
|
1367
|
+
});
|
|
1368
|
+
resolve(true);
|
|
1369
|
+
}
|
|
1370
|
+
catch {
|
|
1371
|
+
if (response) {
|
|
1372
|
+
console.log({ url: response.url, status: response.status, headers: response.headers });
|
|
1373
|
+
}
|
|
1374
|
+
resolve(true);
|
|
1375
|
+
}
|
|
1376
|
+
})().finally(() => {
|
|
1377
|
+
resolve(true);
|
|
1378
|
+
});
|
|
1379
|
+
});
|
|
1298
1380
|
};
|
|
1299
1381
|
}
|
|
1300
|
-
|
|
1382
|
+
|
|
1383
|
+
async function getOauthTokenFromOtherWindow(options) {
|
|
1301
1384
|
let waiting = true;
|
|
1302
1385
|
const url = new URL(typeof options.refreshTokenWindowUrl === "function"
|
|
1303
1386
|
? options.refreshTokenWindowUrl()
|
|
@@ -1328,7 +1411,7 @@ async function getAuthTokenNoRefresh(options) {
|
|
|
1328
1411
|
}, 15000);
|
|
1329
1412
|
await waitUntil(() => waiting);
|
|
1330
1413
|
}
|
|
1331
|
-
function
|
|
1414
|
+
function getOauthToken(options) {
|
|
1332
1415
|
let expires = localStorage.getItem(options.expiresTokenStorageName);
|
|
1333
1416
|
if (!expires || Number.isNaN(+expires) || Date.now() > +expires)
|
|
1334
1417
|
expires = null;
|
|
@@ -1409,50 +1492,8 @@ async function getAuthUser(options) {
|
|
|
1409
1492
|
}
|
|
1410
1493
|
}
|
|
1411
1494
|
|
|
1412
|
-
let isFetchingAccessToken$1 = false;
|
|
1413
|
-
const generateAuthMiddleWare = (options) => async (request) => {
|
|
1414
|
-
if (!options.authTokenUrl ||
|
|
1415
|
-
!options.oauthUrl ||
|
|
1416
|
-
!options.expiresTokenStorageName ||
|
|
1417
|
-
!options.tokenStorageName ||
|
|
1418
|
-
!options.pathToTokenExpires ||
|
|
1419
|
-
!options.pathToToken ||
|
|
1420
|
-
!options.errorUrl) {
|
|
1421
|
-
throw new Error("Auth middleware hasn't required options");
|
|
1422
|
-
}
|
|
1423
|
-
const isSameOrigin = !startWith(request.path, "http");
|
|
1424
|
-
if (request.token) {
|
|
1425
|
-
if (!isSameOrigin)
|
|
1426
|
-
request.headers = {
|
|
1427
|
-
...request.headers,
|
|
1428
|
-
Authorization: `Bearer ${request.token}`,
|
|
1429
|
-
};
|
|
1430
|
-
return;
|
|
1431
|
-
}
|
|
1432
|
-
if (isFetchingAccessToken$1)
|
|
1433
|
-
await waitUntil(() => isFetchingAccessToken$1);
|
|
1434
|
-
const expires = localStorage.getItem(options.expiresTokenStorageName);
|
|
1435
|
-
let token = localStorage.getItem(options.tokenStorageName);
|
|
1436
|
-
if (!expires || Date.now() > +expires || !token) {
|
|
1437
|
-
isFetchingAccessToken$1 = true;
|
|
1438
|
-
token = await (options.tokenRequest ? options.tokenRequest() : getAuthToken(options));
|
|
1439
|
-
isFetchingAccessToken$1 = false;
|
|
1440
|
-
if (isNull(token)) {
|
|
1441
|
-
return void window.location.replace(typeof options.oauthUrl === "function" ? options.oauthUrl() : options.oauthUrl);
|
|
1442
|
-
}
|
|
1443
|
-
if (isUndefined(token)) {
|
|
1444
|
-
return void window.location.replace(typeof options.errorUrl === "function" ? options.errorUrl() : options.errorUrl);
|
|
1445
|
-
}
|
|
1446
|
-
}
|
|
1447
|
-
if (!isSameOrigin)
|
|
1448
|
-
request.headers = {
|
|
1449
|
-
...request.headers,
|
|
1450
|
-
Authorization: `Bearer ${token}`,
|
|
1451
|
-
};
|
|
1452
|
-
};
|
|
1453
|
-
|
|
1454
1495
|
let isFetchingAccessToken = false;
|
|
1455
|
-
const
|
|
1496
|
+
const generateOauthMiddleware = (options) => async (request) => {
|
|
1456
1497
|
if (!options.oauthUrl || !options.expiresTokenStorageName || !options.errorUrl) {
|
|
1457
1498
|
throw new Error("Auth middleware hasn't required options");
|
|
1458
1499
|
}
|
|
@@ -1468,12 +1509,18 @@ const generateAuthNoRefreshMiddleWare = (options) => async (request) => {
|
|
|
1468
1509
|
if (isFetchingAccessToken)
|
|
1469
1510
|
await waitUntil(() => isFetchingAccessToken);
|
|
1470
1511
|
const expires = localStorage.getItem(options.expiresTokenStorageName);
|
|
1512
|
+
let token;
|
|
1471
1513
|
if (!expires || Number.isNaN(+expires) || Date.now() > +expires) {
|
|
1472
1514
|
isFetchingAccessToken = true;
|
|
1473
|
-
await (options
|
|
1515
|
+
await getOauthTokenFromOtherWindow(options);
|
|
1516
|
+
if (options.tokenRequest) {
|
|
1517
|
+
token = await options.tokenRequest();
|
|
1518
|
+
if (token != undefined && options.tokenStorageName) {
|
|
1519
|
+
localStorage.setItem(options.tokenStorageName, token);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1474
1522
|
isFetchingAccessToken = false;
|
|
1475
1523
|
}
|
|
1476
|
-
const token = options.tokenStorageName ? localStorage.getItem(options.tokenStorageName) : null;
|
|
1477
1524
|
if (!isSameOrigin && token)
|
|
1478
1525
|
request.headers = {
|
|
1479
1526
|
...request.headers,
|
|
@@ -1481,88 +1528,13 @@ const generateAuthNoRefreshMiddleWare = (options) => async (request) => {
|
|
|
1481
1528
|
};
|
|
1482
1529
|
};
|
|
1483
1530
|
|
|
1484
|
-
function generateConsoleMiddleware(options = {}) {
|
|
1485
|
-
return (request) => {
|
|
1486
|
-
return new Promise((resolve) => {
|
|
1487
|
-
if ((options.filter &&
|
|
1488
|
-
!options.filter(request)) ||
|
|
1489
|
-
(options.filterHeaders && !options.filterHeaders(request.headers)) ||
|
|
1490
|
-
(options.filterMethod && !options.filterMethod(request.method)) ||
|
|
1491
|
-
(options.filterParams && !options.filterParams(request.params)) ||
|
|
1492
|
-
(options.filterPath && !options.filterPath(request.path))) {
|
|
1493
|
-
resolve(true);
|
|
1494
|
-
return;
|
|
1495
|
-
}
|
|
1496
|
-
// eslint-disable-next-line no-console
|
|
1497
|
-
console.log(request);
|
|
1498
|
-
resolve(true);
|
|
1499
|
-
});
|
|
1500
|
-
};
|
|
1501
|
-
}
|
|
1502
|
-
|
|
1503
|
-
function generateConsolePostMiddleware(options = {}) {
|
|
1504
|
-
return (response) => {
|
|
1505
|
-
return new Promise((resolve) => {
|
|
1506
|
-
void (async function logger() {
|
|
1507
|
-
try {
|
|
1508
|
-
if (!response ||
|
|
1509
|
-
(options.filter && !options.filter(response)) ||
|
|
1510
|
-
(options.filterStatus && !options.filterStatus(response.status)) ||
|
|
1511
|
-
(options.filterUrl && !options.filterUrl(response.url)) ||
|
|
1512
|
-
(options.filterHeaders && !options.filterHeaders(response.headers))) {
|
|
1513
|
-
resolve(true);
|
|
1514
|
-
return;
|
|
1515
|
-
}
|
|
1516
|
-
const contentType = response.headers.get("content-type");
|
|
1517
|
-
let result;
|
|
1518
|
-
if (contentType?.includes?.("text")) {
|
|
1519
|
-
result = await response.text();
|
|
1520
|
-
}
|
|
1521
|
-
else if (contentType?.includes?.("json")) {
|
|
1522
|
-
result = await response.json();
|
|
1523
|
-
}
|
|
1524
|
-
else {
|
|
1525
|
-
result = await response.blob();
|
|
1526
|
-
}
|
|
1527
|
-
Object.defineProperty(response, RESPONSE_DATA_SYMBOL, {
|
|
1528
|
-
value: result,
|
|
1529
|
-
writable: false,
|
|
1530
|
-
enumerable: false,
|
|
1531
|
-
configurable: true,
|
|
1532
|
-
});
|
|
1533
|
-
console.log({
|
|
1534
|
-
url: response.url,
|
|
1535
|
-
status: response.status,
|
|
1536
|
-
headers: response.headers,
|
|
1537
|
-
body: result,
|
|
1538
|
-
});
|
|
1539
|
-
resolve(true);
|
|
1540
|
-
}
|
|
1541
|
-
catch {
|
|
1542
|
-
if (response) {
|
|
1543
|
-
console.log({ url: response.url, status: response.status, headers: response.headers });
|
|
1544
|
-
}
|
|
1545
|
-
resolve(true);
|
|
1546
|
-
}
|
|
1547
|
-
})().finally(() => {
|
|
1548
|
-
resolve(true);
|
|
1549
|
-
});
|
|
1550
|
-
});
|
|
1551
|
-
};
|
|
1552
|
-
}
|
|
1553
|
-
|
|
1554
1531
|
function generateMiddlewares(activeMiddlewares, middlewareOptions, customMiddlewares) {
|
|
1555
1532
|
const selectedMiddlewares = customMiddlewares;
|
|
1556
1533
|
for (const key of activeMiddlewares) {
|
|
1557
1534
|
switch (key) {
|
|
1558
|
-
case API_MIDDLEWARES.
|
|
1559
|
-
if (middlewareOptions.
|
|
1560
|
-
selectedMiddlewares.push(
|
|
1561
|
-
continue;
|
|
1562
|
-
}
|
|
1563
|
-
case API_MIDDLEWARES.AuthNoRefresh: {
|
|
1564
|
-
if (middlewareOptions.authNoRefresh && (IS_BROWSER || IS_JEST))
|
|
1565
|
-
selectedMiddlewares.push(generateAuthNoRefreshMiddleWare(middlewareOptions.authNoRefresh));
|
|
1535
|
+
case API_MIDDLEWARES.Oauth: {
|
|
1536
|
+
if (middlewareOptions.oauth && (IS_BROWSER || IS_JEST))
|
|
1537
|
+
selectedMiddlewares.push(generateOauthMiddleware(middlewareOptions.oauth));
|
|
1566
1538
|
continue;
|
|
1567
1539
|
}
|
|
1568
1540
|
case API_MIDDLEWARES.Logger: {
|
|
@@ -1865,6 +1837,211 @@ function getRandomColor() {
|
|
|
1865
1837
|
return `#${randomColor.padStart(6, "0")}`;
|
|
1866
1838
|
}
|
|
1867
1839
|
|
|
1840
|
+
function type(value) {
|
|
1841
|
+
return value === null
|
|
1842
|
+
? "Null"
|
|
1843
|
+
: value === undefined
|
|
1844
|
+
? "Undefined"
|
|
1845
|
+
: Object.prototype.toString.call(value).slice(8, -1);
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
1849
|
+
function cloneDeep(value, map) {
|
|
1850
|
+
map ??= new Map();
|
|
1851
|
+
// this avoids the slower switch with a quick if decision removing some milliseconds in each run.
|
|
1852
|
+
if (isPrimitive(value)) {
|
|
1853
|
+
return value;
|
|
1854
|
+
}
|
|
1855
|
+
function copy(copiedValue) {
|
|
1856
|
+
// Check for circular and same references on the object graph and return its corresponding clone.
|
|
1857
|
+
const cachedCopy = map.get(value);
|
|
1858
|
+
if (cachedCopy) {
|
|
1859
|
+
return cachedCopy;
|
|
1860
|
+
}
|
|
1861
|
+
map.set(value, copiedValue);
|
|
1862
|
+
for (const key in value) {
|
|
1863
|
+
if (Object.hasOwn(value, key)) {
|
|
1864
|
+
copiedValue[key] = cloneDeep(value[key], map);
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
return copiedValue;
|
|
1868
|
+
}
|
|
1869
|
+
switch (type(value)) {
|
|
1870
|
+
case "Object":
|
|
1871
|
+
return copy(Object.create(Object.getPrototypeOf(value)));
|
|
1872
|
+
case "Array":
|
|
1873
|
+
return copy(Array(value.length));
|
|
1874
|
+
case "Date":
|
|
1875
|
+
return new Date(value.valueOf());
|
|
1876
|
+
case "RegExp":
|
|
1877
|
+
return cloneRegExp(value);
|
|
1878
|
+
case "Int8Array":
|
|
1879
|
+
case "Uint8Array":
|
|
1880
|
+
case "Uint8ClampedArray":
|
|
1881
|
+
case "Int16Array":
|
|
1882
|
+
case "Uint16Array":
|
|
1883
|
+
case "Int32Array":
|
|
1884
|
+
case "Uint32Array":
|
|
1885
|
+
case "Float32Array":
|
|
1886
|
+
case "Float64Array":
|
|
1887
|
+
case "BigInt64Array":
|
|
1888
|
+
case "BigUint64Array":
|
|
1889
|
+
return value.slice();
|
|
1890
|
+
default:
|
|
1891
|
+
return value;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
function cloneRegExp(pattern) {
|
|
1895
|
+
return new RegExp(pattern.source, pattern.flags
|
|
1896
|
+
? pattern.flags
|
|
1897
|
+
: (pattern.global ? "g" : "") +
|
|
1898
|
+
(pattern.ignoreCase ? "i" : "") +
|
|
1899
|
+
(pattern.multiline ? "m" : "") +
|
|
1900
|
+
(pattern.sticky ? "y" : "") +
|
|
1901
|
+
(pattern.unicode ? "u" : "") +
|
|
1902
|
+
(pattern.dotAll ? "s" : ""));
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1906
|
+
function debounce({ delay }, func) {
|
|
1907
|
+
let timer;
|
|
1908
|
+
let active = true;
|
|
1909
|
+
const debounced = (...args) => {
|
|
1910
|
+
if (active) {
|
|
1911
|
+
clearTimeout(timer);
|
|
1912
|
+
timer = setTimeout(() => {
|
|
1913
|
+
if (active) {
|
|
1914
|
+
func(...args);
|
|
1915
|
+
}
|
|
1916
|
+
timer = undefined;
|
|
1917
|
+
}, delay);
|
|
1918
|
+
}
|
|
1919
|
+
else {
|
|
1920
|
+
func(...args);
|
|
1921
|
+
}
|
|
1922
|
+
};
|
|
1923
|
+
debounced.isPending = () => {
|
|
1924
|
+
return timer !== undefined;
|
|
1925
|
+
};
|
|
1926
|
+
debounced.cancel = () => {
|
|
1927
|
+
active = false;
|
|
1928
|
+
};
|
|
1929
|
+
debounced.flush = (...args) => {
|
|
1930
|
+
func(...args);
|
|
1931
|
+
};
|
|
1932
|
+
return debounced;
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
function difference(first, second) {
|
|
1936
|
+
let idx = 0;
|
|
1937
|
+
const out = [];
|
|
1938
|
+
const toFilterOut = new Set();
|
|
1939
|
+
for (let i = 0; i < second.length; i += 1) {
|
|
1940
|
+
toFilterOut.add(second[i]);
|
|
1941
|
+
}
|
|
1942
|
+
while (idx < first.length) {
|
|
1943
|
+
if (!toFilterOut.has(first[idx])) {
|
|
1944
|
+
out[out.length] = first[idx];
|
|
1945
|
+
}
|
|
1946
|
+
idx += 1;
|
|
1947
|
+
}
|
|
1948
|
+
return out;
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
function differenceBy(first, second, key) {
|
|
1952
|
+
let idx = 0;
|
|
1953
|
+
const out = [];
|
|
1954
|
+
const toFilterOut = new Set();
|
|
1955
|
+
for (let i = 0; i < second.length; i += 1) {
|
|
1956
|
+
toFilterOut.add(second[i][key]);
|
|
1957
|
+
}
|
|
1958
|
+
while (idx < first.length) {
|
|
1959
|
+
if (!toFilterOut.has(first[idx][key])) {
|
|
1960
|
+
out.push(first[idx]);
|
|
1961
|
+
}
|
|
1962
|
+
idx += 1;
|
|
1963
|
+
}
|
|
1964
|
+
return out;
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
function differenceWith(first, second, extractPrimitive) {
|
|
1968
|
+
let idx = 0;
|
|
1969
|
+
const out = [];
|
|
1970
|
+
const toFilterOut = new Set();
|
|
1971
|
+
for (let i = 0; i < second.length; i += 1) {
|
|
1972
|
+
toFilterOut.add(extractPrimitive(second[i]));
|
|
1973
|
+
}
|
|
1974
|
+
while (idx < first.length) {
|
|
1975
|
+
if (!toFilterOut.has(extractPrimitive(first[idx]))) {
|
|
1976
|
+
out.push(first[idx]);
|
|
1977
|
+
}
|
|
1978
|
+
idx += 1;
|
|
1979
|
+
}
|
|
1980
|
+
return out;
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
function isEqual(aVal, bVal) {
|
|
1984
|
+
if (aVal === bVal)
|
|
1985
|
+
return true;
|
|
1986
|
+
if (aVal == undefined ||
|
|
1987
|
+
bVal == undefined ||
|
|
1988
|
+
typeof aVal !== "object" ||
|
|
1989
|
+
typeof bVal !== "object") {
|
|
1990
|
+
return aVal === bVal;
|
|
1991
|
+
}
|
|
1992
|
+
if (aVal instanceof Date && bVal instanceof Date) {
|
|
1993
|
+
return aVal.getTime() === bVal.getTime();
|
|
1994
|
+
}
|
|
1995
|
+
if (aVal instanceof RegExp && bVal instanceof RegExp) {
|
|
1996
|
+
return aVal.toString() === bVal.toString();
|
|
1997
|
+
}
|
|
1998
|
+
if (aVal.constructor !== bVal.constructor) {
|
|
1999
|
+
return false;
|
|
2000
|
+
}
|
|
2001
|
+
if (Array.isArray(aVal) && Array.isArray(bVal)) {
|
|
2002
|
+
if (aVal.length !== bVal.length)
|
|
2003
|
+
return false;
|
|
2004
|
+
for (let i = 0; i < aVal.length; i++) {
|
|
2005
|
+
if (!isEqual(aVal[i], bVal[i]))
|
|
2006
|
+
return false;
|
|
2007
|
+
}
|
|
2008
|
+
return true;
|
|
2009
|
+
}
|
|
2010
|
+
if (isObject(aVal) && isObject(bVal)) {
|
|
2011
|
+
const keysA = Object.keys(aVal);
|
|
2012
|
+
const keysB = Object.keys(bVal);
|
|
2013
|
+
if (keysA.length !== keysB.length)
|
|
2014
|
+
return false;
|
|
2015
|
+
for (const key of keysA) {
|
|
2016
|
+
if (!keysB.includes(key))
|
|
2017
|
+
return false;
|
|
2018
|
+
if (!isEqual(aVal[key], bVal[key]))
|
|
2019
|
+
return false;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
return true;
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2026
|
+
function throttle({ interval }, func) {
|
|
2027
|
+
let ready = true;
|
|
2028
|
+
let timer;
|
|
2029
|
+
const throttled = (...args) => {
|
|
2030
|
+
if (!ready)
|
|
2031
|
+
return;
|
|
2032
|
+
func(...args);
|
|
2033
|
+
ready = false;
|
|
2034
|
+
timer = setTimeout(() => {
|
|
2035
|
+
ready = true;
|
|
2036
|
+
timer = undefined;
|
|
2037
|
+
}, interval);
|
|
2038
|
+
};
|
|
2039
|
+
throttled.isThrottled = () => {
|
|
2040
|
+
return timer !== undefined;
|
|
2041
|
+
};
|
|
2042
|
+
return throttled;
|
|
2043
|
+
}
|
|
2044
|
+
|
|
1868
2045
|
exports.API_MIDDLEWARES = API_MIDDLEWARES;
|
|
1869
2046
|
exports.COLOR_FORMATS = COLOR_FORMATS;
|
|
1870
2047
|
exports.DATE_TYPES = DATE_TYPES;
|
|
@@ -1880,24 +2057,32 @@ exports.ResponseError = ResponseError;
|
|
|
1880
2057
|
exports.arrayToMapByKey = arrayToMapByKey;
|
|
1881
2058
|
exports.buildQueryString = buildQueryString;
|
|
1882
2059
|
exports.checkType = checkType;
|
|
2060
|
+
exports.cloneDeep = cloneDeep;
|
|
2061
|
+
exports.copyToClipboard = copyToClipboard;
|
|
1883
2062
|
exports.createGlobalId = createGlobalId;
|
|
1884
2063
|
exports.createLocalIdGenerator = createLocalIdGenerator;
|
|
1885
2064
|
exports.createRequestClientInstance = createRequestClientInstance;
|
|
1886
2065
|
exports.createURLWithParams = createURLWithParams;
|
|
1887
2066
|
exports.dateDifference = dateDifference;
|
|
1888
2067
|
exports.dateFormat = dateFormat;
|
|
2068
|
+
exports.debounce = debounce;
|
|
2069
|
+
exports.declination = declination;
|
|
2070
|
+
exports.difference = difference;
|
|
2071
|
+
exports.differenceBy = differenceBy;
|
|
2072
|
+
exports.differenceWith = differenceWith;
|
|
1889
2073
|
exports.downloadFile = downloadFile;
|
|
1890
2074
|
exports.downloadJson = downloadJson;
|
|
1891
2075
|
exports.execAnimation = execAnimation;
|
|
2076
|
+
exports.extractQueries = extractQueries;
|
|
1892
2077
|
exports.fieldViewFormat = fieldViewFormat;
|
|
1893
|
-
exports.getAuthToken = getAuthToken;
|
|
1894
|
-
exports.getAuthTokenNoRefresh = getAuthTokenNoRefresh;
|
|
1895
2078
|
exports.getAuthUser = getAuthUser;
|
|
1896
2079
|
exports.getByPath = getByPath;
|
|
1897
2080
|
exports.getCallerFunctionName = getCallerFunctionName;
|
|
1898
2081
|
exports.getColorFormat = getColorFormat;
|
|
1899
2082
|
exports.getDateByRules = getDateByRules;
|
|
1900
2083
|
exports.getFileNameFromHeader = getFileNameFromHeader;
|
|
2084
|
+
exports.getOauthToken = getOauthToken;
|
|
2085
|
+
exports.getOauthTokenFromOtherWindow = getOauthTokenFromOtherWindow;
|
|
1901
2086
|
exports.getQueryValues = getQueryValues;
|
|
1902
2087
|
exports.getRandomColor = getRandomColor;
|
|
1903
2088
|
exports.getToday = getToday;
|
|
@@ -1907,11 +2092,13 @@ exports.getYesterday = getYesterday;
|
|
|
1907
2092
|
exports.isArray = isArray;
|
|
1908
2093
|
exports.isBoolean = isBoolean;
|
|
1909
2094
|
exports.isDate = isDate;
|
|
2095
|
+
exports.isEqual = isEqual;
|
|
1910
2096
|
exports.isId = isId;
|
|
1911
2097
|
exports.isNull = isNull;
|
|
1912
2098
|
exports.isNullable = isNullable;
|
|
1913
2099
|
exports.isNumber = isNumber;
|
|
1914
2100
|
exports.isObject = isObject;
|
|
2101
|
+
exports.isPrimitive = isPrimitive;
|
|
1915
2102
|
exports.isString = isString;
|
|
1916
2103
|
exports.isToday = isToday;
|
|
1917
2104
|
exports.isTomorrow = isTomorrow;
|
|
@@ -1928,6 +2115,7 @@ exports.speedTest = speedTest;
|
|
|
1928
2115
|
exports.startWith = startWith;
|
|
1929
2116
|
exports.syncObjectValues = syncObjectValues;
|
|
1930
2117
|
exports.takeOpacityColors = takeOpacityColors;
|
|
2118
|
+
exports.throttle = throttle;
|
|
1931
2119
|
exports.transformHEXtoRGB = transformHEXtoRGB;
|
|
1932
2120
|
exports.transformRGBAtoRGB = transformRGBAtoRGB;
|
|
1933
2121
|
exports.transformRGBtoRGBA = transformRGBtoRGBA;
|
|
@@ -1936,8 +2124,7 @@ exports.transformToDayjs = transformToDayjs;
|
|
|
1936
2124
|
exports.transformToNumber = transformToNumber;
|
|
1937
2125
|
exports.translit = translit;
|
|
1938
2126
|
exports.trimUrl = trimUrl;
|
|
1939
|
-
exports.
|
|
1940
|
-
exports.updateAuthTokenNoRefresh = updateAuthTokenNoRefresh;
|
|
2127
|
+
exports.type = type;
|
|
1941
2128
|
exports.updateAuthUser = updateAuthUser;
|
|
1942
2129
|
exports.wait = wait;
|
|
1943
2130
|
exports.waitUntil = waitUntil;
|