tuijs-util 1.3.7 → 1.3.8
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/package.json +2 -2
- package/src/cjs/index.cjs +41 -19
- package/src/esm/index.js +2 -1
- package/src/esm/lib/util.dom.js +0 -6
- package/src/esm/lib/util.http.js +35 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tuijs-util",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.8",
|
|
4
4
|
"module": "src/esm/index.js",
|
|
5
5
|
"main": "src/cjs/index.cjs",
|
|
6
6
|
"exports": {
|
|
@@ -20,6 +20,6 @@
|
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@rollup/plugin-babel": "^6.0.4",
|
|
22
22
|
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
23
|
-
"rollup": "^4.
|
|
23
|
+
"rollup": "^4.24.0"
|
|
24
24
|
}
|
|
25
25
|
}
|
package/src/cjs/index.cjs
CHANGED
|
@@ -339,12 +339,6 @@ function elmCleanerArray(templateLit) {
|
|
|
339
339
|
}
|
|
340
340
|
}
|
|
341
341
|
|
|
342
|
-
/**
|
|
343
|
-
* @typedef {Array} ImageUrls - A list of image urls
|
|
344
|
-
* @property {string} imageUrl - The url of the desired image
|
|
345
|
-
* @property {Array<string>} imageUrlStrings - An Array of image url strings
|
|
346
|
-
*/
|
|
347
|
-
|
|
348
342
|
/**
|
|
349
343
|
* Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
|
|
350
344
|
* @param {string} url
|
|
@@ -388,15 +382,38 @@ function urlAddHttps(url) {
|
|
|
388
382
|
}
|
|
389
383
|
|
|
390
384
|
/**
|
|
391
|
-
*
|
|
385
|
+
* Sends GET request to specified URL
|
|
392
386
|
* @async
|
|
393
|
-
* @param {string}
|
|
387
|
+
* @param {string} url
|
|
388
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
389
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
390
|
+
*/
|
|
391
|
+
async function reqGet(url) {
|
|
392
|
+
try {
|
|
393
|
+
const res = await fetch(url, {
|
|
394
|
+
method: 'GET'
|
|
395
|
+
});
|
|
396
|
+
if (!res.ok) {
|
|
397
|
+
throw new Error(res);
|
|
398
|
+
}
|
|
399
|
+
return res;
|
|
400
|
+
} catch (er) {
|
|
401
|
+
throw new Error(er.message);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Collects JSON data from a given url
|
|
407
|
+
* @async
|
|
408
|
+
* @param {string} url
|
|
394
409
|
* @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
|
|
395
410
|
* @throws {Error} - Throws Error if an error is detected.
|
|
396
411
|
*/
|
|
397
|
-
async function
|
|
412
|
+
async function reqGetJson(url) {
|
|
398
413
|
try {
|
|
399
|
-
const res = await fetch(
|
|
414
|
+
const res = await fetch(url, {
|
|
415
|
+
method: 'GET'
|
|
416
|
+
});
|
|
400
417
|
if (!res.ok) {
|
|
401
418
|
throw new Error(res);
|
|
402
419
|
}
|
|
@@ -408,13 +425,13 @@ async function reqFileJson(filePath) {
|
|
|
408
425
|
}
|
|
409
426
|
|
|
410
427
|
/**
|
|
411
|
-
*
|
|
428
|
+
* Collects text data from a given url
|
|
412
429
|
* @async
|
|
413
430
|
* @param {string} url
|
|
414
|
-
* @returns {Promise<Object>} - Returns
|
|
431
|
+
* @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
|
|
415
432
|
* @throws {Error} - Throws Error if an error is detected.
|
|
416
433
|
*/
|
|
417
|
-
async function
|
|
434
|
+
async function reqGetText(url) {
|
|
418
435
|
try {
|
|
419
436
|
const res = await fetch(url, {
|
|
420
437
|
method: 'GET'
|
|
@@ -422,7 +439,8 @@ async function reqGet(url) {
|
|
|
422
439
|
if (!res.ok) {
|
|
423
440
|
throw new Error(res);
|
|
424
441
|
}
|
|
425
|
-
|
|
442
|
+
const data = await res.text();
|
|
443
|
+
return data;
|
|
426
444
|
} catch (er) {
|
|
427
445
|
throw new Error(er.message);
|
|
428
446
|
}
|
|
@@ -460,14 +478,17 @@ async function reqPostJson(url, dataJson) {
|
|
|
460
478
|
}
|
|
461
479
|
}
|
|
462
480
|
|
|
463
|
-
// Simple POST request. export function is expecting FormData.
|
|
464
481
|
/**
|
|
465
|
-
*
|
|
482
|
+
* Sends POST request to specified URL, which contains FormData in the body.
|
|
483
|
+
* @param {string} url
|
|
484
|
+
* @param {FormData} dataForm
|
|
485
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
486
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
466
487
|
*/
|
|
467
488
|
async function reqPostForm(url, dataForm) {
|
|
468
489
|
try {
|
|
469
490
|
if (!(dataForm instanceof FormData)) {
|
|
470
|
-
throw `The data provided was not form data
|
|
491
|
+
throw new Error(`The data provided was not form data`);
|
|
471
492
|
}
|
|
472
493
|
const res = await fetch(url, {
|
|
473
494
|
method: 'POST',
|
|
@@ -478,7 +499,7 @@ async function reqPostForm(url, dataForm) {
|
|
|
478
499
|
}
|
|
479
500
|
return res;
|
|
480
501
|
} catch (er) {
|
|
481
|
-
throw new Error(er);
|
|
502
|
+
throw new Error(er.message);
|
|
482
503
|
}
|
|
483
504
|
}
|
|
484
505
|
|
|
@@ -614,8 +635,9 @@ exports.regExNumbers = regExNumbers;
|
|
|
614
635
|
exports.regExSpecial = regExSpecial;
|
|
615
636
|
exports.regExUrl = regExUrl;
|
|
616
637
|
exports.removeChar = removeChar;
|
|
617
|
-
exports.reqFileJson = reqFileJson;
|
|
618
638
|
exports.reqGet = reqGet;
|
|
639
|
+
exports.reqGetJson = reqGetJson;
|
|
640
|
+
exports.reqGetText = reqGetText;
|
|
619
641
|
exports.reqPostForm = reqPostForm;
|
|
620
642
|
exports.reqPostJson = reqPostJson;
|
|
621
643
|
exports.scrollIntoView = scrollIntoView;
|
package/src/esm/index.js
CHANGED
package/src/esm/lib/util.dom.js
CHANGED
|
@@ -78,9 +78,3 @@ export function elmCleanerArray(templateLit) {
|
|
|
78
78
|
throw new Error(er.message);
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* @typedef {Array} ImageUrls - A list of image urls
|
|
84
|
-
* @property {string} imageUrl - The url of the desired image
|
|
85
|
-
* @property {Array<string>} imageUrlStrings - An Array of image url strings
|
|
86
|
-
*/
|
package/src/esm/lib/util.http.js
CHANGED
|
@@ -41,15 +41,34 @@ export function urlAddHttps(url) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
44
|
+
* Sends GET request to specified URL
|
|
45
45
|
* @async
|
|
46
|
-
* @param {string}
|
|
46
|
+
* @param {string} url
|
|
47
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
48
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
49
|
+
*/
|
|
50
|
+
export async function reqGet(url) {
|
|
51
|
+
try {
|
|
52
|
+
const res = await fetch(url, { method: 'GET' });
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
throw new Error(res);
|
|
55
|
+
}
|
|
56
|
+
return res;
|
|
57
|
+
} catch (er) {
|
|
58
|
+
throw new Error(er.message);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Collects JSON data from a given url
|
|
64
|
+
* @async
|
|
65
|
+
* @param {string} url
|
|
47
66
|
* @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
|
|
48
67
|
* @throws {Error} - Throws Error if an error is detected.
|
|
49
68
|
*/
|
|
50
|
-
export async function
|
|
69
|
+
export async function reqGetJson(url) {
|
|
51
70
|
try {
|
|
52
|
-
const res = await fetch(
|
|
71
|
+
const res = await fetch(url, { method: 'GET' });
|
|
53
72
|
if (!res.ok) {
|
|
54
73
|
throw new Error(res);
|
|
55
74
|
}
|
|
@@ -61,19 +80,20 @@ export async function reqFileJson(filePath) {
|
|
|
61
80
|
}
|
|
62
81
|
|
|
63
82
|
/**
|
|
64
|
-
*
|
|
83
|
+
* Collects text data from a given url
|
|
65
84
|
* @async
|
|
66
85
|
* @param {string} url
|
|
67
|
-
* @returns {Promise<Object>} - Returns
|
|
86
|
+
* @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
|
|
68
87
|
* @throws {Error} - Throws Error if an error is detected.
|
|
69
88
|
*/
|
|
70
|
-
export async function
|
|
89
|
+
export async function reqGetText(url) {
|
|
71
90
|
try {
|
|
72
91
|
const res = await fetch(url, { method: 'GET' });
|
|
73
92
|
if (!res.ok) {
|
|
74
93
|
throw new Error(res);
|
|
75
94
|
}
|
|
76
|
-
|
|
95
|
+
const data = await res.text();
|
|
96
|
+
return data;
|
|
77
97
|
} catch (er) {
|
|
78
98
|
throw new Error(er.message);
|
|
79
99
|
}
|
|
@@ -111,14 +131,17 @@ export async function reqPostJson(url, dataJson) {
|
|
|
111
131
|
}
|
|
112
132
|
}
|
|
113
133
|
|
|
114
|
-
// Simple POST request. export function is expecting FormData.
|
|
115
134
|
/**
|
|
116
|
-
*
|
|
135
|
+
* Sends POST request to specified URL, which contains FormData in the body.
|
|
136
|
+
* @param {string} url
|
|
137
|
+
* @param {FormData} dataForm
|
|
138
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
139
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
117
140
|
*/
|
|
118
141
|
export async function reqPostForm(url, dataForm) {
|
|
119
142
|
try {
|
|
120
143
|
if (!(dataForm instanceof FormData)) {
|
|
121
|
-
throw `The data provided was not form data
|
|
144
|
+
throw new Error(`The data provided was not form data`);
|
|
122
145
|
}
|
|
123
146
|
const res = await fetch(url, {
|
|
124
147
|
method: 'POST',
|
|
@@ -129,6 +152,6 @@ export async function reqPostForm(url, dataForm) {
|
|
|
129
152
|
}
|
|
130
153
|
return res;
|
|
131
154
|
} catch (er) {
|
|
132
|
-
throw new Error(er);
|
|
155
|
+
throw new Error(er.message);
|
|
133
156
|
}
|
|
134
157
|
}
|