@base-web-kits/base-tools-web 1.1.6 → 1.1.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/dist/base-tools-web.umd.global.js +337 -264
- package/dist/base-tools-web.umd.global.js.map +1 -1
- package/dist/index.cjs +339 -267
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +342 -267
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,158 +1,218 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
9
16
|
}
|
|
10
|
-
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
11
21
|
return new Promise((resolve, reject) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/web/clipboard/index.ts
|
|
42
|
+
function copyText(text) {
|
|
43
|
+
return __async(this, null, function* () {
|
|
44
|
+
if (typeof text !== "string") text = String(text != null ? text : "");
|
|
45
|
+
if (navigator.clipboard && typeof navigator.clipboard.writeText === "function") {
|
|
46
|
+
try {
|
|
47
|
+
yield navigator.clipboard.writeText(text);
|
|
48
|
+
return;
|
|
49
|
+
} catch (e) {
|
|
31
50
|
}
|
|
32
|
-
} catch (e) {
|
|
33
|
-
reject(e);
|
|
34
51
|
}
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
try {
|
|
54
|
+
const textarea = document.createElement("textarea");
|
|
55
|
+
textarea.value = text;
|
|
56
|
+
textarea.setAttribute("readonly", "");
|
|
57
|
+
textarea.style.position = "fixed";
|
|
58
|
+
textarea.style.top = "0";
|
|
59
|
+
textarea.style.right = "-9999px";
|
|
60
|
+
textarea.style.opacity = "0";
|
|
61
|
+
textarea.style.pointerEvents = "none";
|
|
62
|
+
document.body.appendChild(textarea);
|
|
63
|
+
textarea.focus();
|
|
64
|
+
textarea.select();
|
|
65
|
+
textarea.setSelectionRange(0, textarea.value.length);
|
|
66
|
+
const ok = document.execCommand("copy");
|
|
67
|
+
document.body.removeChild(textarea);
|
|
68
|
+
if (ok) {
|
|
69
|
+
resolve();
|
|
70
|
+
} else {
|
|
71
|
+
reject(new Error("Copy failed: clipboard unavailable"));
|
|
72
|
+
}
|
|
73
|
+
} catch (e) {
|
|
74
|
+
reject(e);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
35
77
|
});
|
|
36
78
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (canWriteClipboard()) {
|
|
51
|
-
const { html: html2, text } = nodeToHtmlText(node);
|
|
52
|
-
await writeClipboard({
|
|
53
|
-
"text/html": new Blob([html2], { type: "text/html" }),
|
|
54
|
-
"text/plain": new Blob([text], { type: "text/plain" })
|
|
55
|
-
});
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const { html } = nodeToHtmlText(node);
|
|
59
|
-
return execCopyFromHtml(html);
|
|
60
|
-
}
|
|
61
|
-
async function copyImage(image) {
|
|
62
|
-
const blob = await toImageBlob(image);
|
|
63
|
-
if (!blob) throw new Error("Unsupported image source");
|
|
64
|
-
if (canWriteClipboard()) {
|
|
65
|
-
const type = blob.type || "image/png";
|
|
66
|
-
await writeClipboard({ [type]: blob });
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
throw new Error("Clipboard image write not supported");
|
|
70
|
-
}
|
|
71
|
-
async function copyUrl(url) {
|
|
72
|
-
const s = String(url ?? "");
|
|
73
|
-
if (canWriteClipboard()) {
|
|
74
|
-
await writeClipboard({
|
|
75
|
-
"text/uri-list": new Blob([s], { type: "text/uri-list" }),
|
|
76
|
-
"text/plain": new Blob([s], { type: "text/plain" })
|
|
77
|
-
});
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
await copyText(s);
|
|
79
|
+
function copyHtml(html) {
|
|
80
|
+
return __async(this, null, function* () {
|
|
81
|
+
const s = String(html != null ? html : "");
|
|
82
|
+
if (canWriteClipboard()) {
|
|
83
|
+
const plain = htmlToText(s);
|
|
84
|
+
yield writeClipboard({
|
|
85
|
+
"text/html": new Blob([s], { type: "text/html" }),
|
|
86
|
+
"text/plain": new Blob([plain], { type: "text/plain" })
|
|
87
|
+
});
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
return execCopyFromHtml(s);
|
|
91
|
+
});
|
|
81
92
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
93
|
+
function copyNode(node) {
|
|
94
|
+
return __async(this, null, function* () {
|
|
95
|
+
if (canWriteClipboard()) {
|
|
96
|
+
const { html: html2, text } = nodeToHtmlText(node);
|
|
97
|
+
yield writeClipboard({
|
|
98
|
+
"text/html": new Blob([html2], { type: "text/html" }),
|
|
99
|
+
"text/plain": new Blob([text], { type: "text/plain" })
|
|
100
|
+
});
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const { html } = nodeToHtmlText(node);
|
|
104
|
+
return execCopyFromHtml(html);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function copyImage(image) {
|
|
108
|
+
return __async(this, null, function* () {
|
|
109
|
+
const blob = yield toImageBlob(image);
|
|
110
|
+
if (!blob) throw new Error("Unsupported image source");
|
|
111
|
+
if (canWriteClipboard()) {
|
|
112
|
+
const type = blob.type || "image/png";
|
|
113
|
+
yield writeClipboard({ [type]: blob });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
throw new Error("Clipboard image write not supported");
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function copyUrl(url) {
|
|
120
|
+
return __async(this, null, function* () {
|
|
121
|
+
const s = String(url != null ? url : "");
|
|
122
|
+
if (canWriteClipboard()) {
|
|
123
|
+
yield writeClipboard({
|
|
124
|
+
"text/uri-list": new Blob([s], { type: "text/uri-list" }),
|
|
125
|
+
"text/plain": new Blob([s], { type: "text/plain" })
|
|
126
|
+
});
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
yield copyText(s);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
function copyBlob(blob) {
|
|
133
|
+
return __async(this, null, function* () {
|
|
134
|
+
if (canWriteClipboard()) {
|
|
135
|
+
const type = blob.type || "application/octet-stream";
|
|
136
|
+
yield writeClipboard({ [type]: blob });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
throw new Error("Clipboard blob write not supported");
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function copyRtf(rtf) {
|
|
143
|
+
return __async(this, null, function* () {
|
|
144
|
+
const s = String(rtf != null ? rtf : "");
|
|
145
|
+
if (canWriteClipboard()) {
|
|
146
|
+
const plain = s.replace(/\\par[\s]?/g, "\n").replace(/\{[^}]*\}/g, "").replace(/\\[a-zA-Z]+[0-9'-]*/g, "").replace(/\r?\n/g, "\n").trim();
|
|
147
|
+
yield writeClipboard({
|
|
148
|
+
"text/rtf": new Blob([s], { type: "text/rtf" }),
|
|
149
|
+
"text/plain": new Blob([plain], { type: "text/plain" })
|
|
150
|
+
});
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
yield copyText(s);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function copyTable(rows) {
|
|
157
|
+
return __async(this, null, function* () {
|
|
158
|
+
const data = Array.isArray(rows) ? rows : [];
|
|
159
|
+
const escapeHtml = (t) => t.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
160
|
+
const html = (() => {
|
|
161
|
+
const trs = data.map((r) => `<tr>${r.map((c) => `<td>${escapeHtml(String(c))}</td>`).join("")}</tr>`).join("");
|
|
162
|
+
return `<table>${trs}</table>`;
|
|
163
|
+
})();
|
|
164
|
+
const tsv = data.map((r) => r.map((c) => String(c)).join(" ")).join("\n");
|
|
165
|
+
const csv = data.map(
|
|
166
|
+
(r) => r.map((c) => {
|
|
167
|
+
const s = String(c);
|
|
168
|
+
const needQuote = /[",\n]/.test(s);
|
|
169
|
+
const escaped = s.replace(/"/g, '""');
|
|
170
|
+
return needQuote ? `"${escaped}"` : escaped;
|
|
171
|
+
}).join(",")
|
|
172
|
+
).join("\n");
|
|
173
|
+
if (canWriteClipboard()) {
|
|
174
|
+
yield writeClipboard({
|
|
175
|
+
"text/html": new Blob([html], { type: "text/html" }),
|
|
176
|
+
"text/tab-separated-values": new Blob([tsv], { type: "text/tab-separated-values" }),
|
|
177
|
+
"text/csv": new Blob([csv], { type: "text/csv" }),
|
|
178
|
+
"text/plain": new Blob([tsv], { type: "text/plain" })
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
yield copyText(tsv);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
function toImageBlob(image) {
|
|
186
|
+
return __async(this, null, function* () {
|
|
187
|
+
if (image instanceof Blob) return image;
|
|
188
|
+
if (image instanceof HTMLCanvasElement)
|
|
189
|
+
return yield new Promise((resolve, reject) => {
|
|
190
|
+
image.toBlob(
|
|
191
|
+
(b) => b ? resolve(b) : reject(new Error("Canvas toBlob failed")),
|
|
192
|
+
"image/png"
|
|
193
|
+
);
|
|
194
|
+
});
|
|
195
|
+
const isBitmap = typeof ImageBitmap !== "undefined" && image instanceof ImageBitmap;
|
|
196
|
+
if (isBitmap) {
|
|
197
|
+
const cnv = document.createElement("canvas");
|
|
198
|
+
cnv.width = image.width;
|
|
199
|
+
cnv.height = image.height;
|
|
200
|
+
const ctx = cnv.getContext("2d");
|
|
201
|
+
ctx == null ? void 0 : ctx.drawImage(image, 0, 0);
|
|
202
|
+
return yield new Promise((resolve, reject) => {
|
|
203
|
+
cnv.toBlob((b) => b ? resolve(b) : reject(new Error("Canvas toBlob failed")), "image/png");
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
});
|
|
150
208
|
}
|
|
151
209
|
function canWriteClipboard() {
|
|
152
210
|
return !!(navigator.clipboard && typeof navigator.clipboard.write === "function" && typeof ClipboardItem !== "undefined");
|
|
153
211
|
}
|
|
154
|
-
|
|
155
|
-
|
|
212
|
+
function writeClipboard(items) {
|
|
213
|
+
return __async(this, null, function* () {
|
|
214
|
+
yield navigator.clipboard.write([new ClipboardItem(items)]);
|
|
215
|
+
});
|
|
156
216
|
}
|
|
157
217
|
function htmlToText(html) {
|
|
158
218
|
const div = document.createElement("div");
|
|
@@ -160,9 +220,10 @@ function htmlToText(html) {
|
|
|
160
220
|
return div.textContent || "";
|
|
161
221
|
}
|
|
162
222
|
function nodeToHtmlText(node) {
|
|
223
|
+
var _a;
|
|
163
224
|
const container = document.createElement("div");
|
|
164
225
|
container.appendChild(node.cloneNode(true));
|
|
165
|
-
const html = node instanceof Element ? node.outerHTML
|
|
226
|
+
const html = node instanceof Element ? (_a = node.outerHTML) != null ? _a : container.innerHTML : container.innerHTML;
|
|
166
227
|
const text = container.textContent || "";
|
|
167
228
|
return { html, text };
|
|
168
229
|
}
|
|
@@ -181,11 +242,11 @@ function execCopyFromHtml(html) {
|
|
|
181
242
|
const selection = window.getSelection();
|
|
182
243
|
const range = document.createRange();
|
|
183
244
|
range.selectNodeContents(div);
|
|
184
|
-
selection
|
|
185
|
-
selection
|
|
245
|
+
selection == null ? void 0 : selection.removeAllRanges();
|
|
246
|
+
selection == null ? void 0 : selection.addRange(range);
|
|
186
247
|
const ok = document.execCommand("copy");
|
|
187
248
|
document.body.removeChild(div);
|
|
188
|
-
selection
|
|
249
|
+
selection == null ? void 0 : selection.removeAllRanges();
|
|
189
250
|
if (ok) {
|
|
190
251
|
resolve();
|
|
191
252
|
} else {
|
|
@@ -214,10 +275,11 @@ function setCookie(name, value, days) {
|
|
|
214
275
|
document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}`;
|
|
215
276
|
}
|
|
216
277
|
function getCookie(name) {
|
|
278
|
+
var _a;
|
|
217
279
|
const value = `; ${document.cookie}`;
|
|
218
280
|
const parts = value.split(`; ${name}=`);
|
|
219
281
|
if (parts.length === 2) {
|
|
220
|
-
const v = parts.pop()
|
|
282
|
+
const v = (_a = parts.pop()) == null ? void 0 : _a.split(";").shift();
|
|
221
283
|
return v ? decodeURIComponent(v) : null;
|
|
222
284
|
}
|
|
223
285
|
return null;
|
|
@@ -361,80 +423,87 @@ function unlockBodyScroll() {
|
|
|
361
423
|
}
|
|
362
424
|
|
|
363
425
|
// src/web/network/load.ts
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
} else if (url.includes(";base64,")) {
|
|
373
|
-
blobUrl = url;
|
|
374
|
-
} else {
|
|
375
|
-
if (fileName) {
|
|
376
|
-
const res = await fetch(url);
|
|
377
|
-
if (!res.ok) throw new Error(`fetch error ${res.status}\uFF1A${url}`);
|
|
378
|
-
const blob = await res.blob();
|
|
379
|
-
blobUrl = URL.createObjectURL(blob);
|
|
426
|
+
function download(url, fileName = "") {
|
|
427
|
+
return __async(this, null, function* () {
|
|
428
|
+
if (!url) return;
|
|
429
|
+
let blobUrl = "";
|
|
430
|
+
let needRevoke = false;
|
|
431
|
+
try {
|
|
432
|
+
if (url instanceof Blob) {
|
|
433
|
+
blobUrl = URL.createObjectURL(url);
|
|
380
434
|
needRevoke = true;
|
|
381
|
-
} else {
|
|
435
|
+
} else if (url.includes(";base64,")) {
|
|
382
436
|
blobUrl = url;
|
|
437
|
+
} else {
|
|
438
|
+
if (fileName) {
|
|
439
|
+
const res = yield fetch(url);
|
|
440
|
+
if (!res.ok) throw new Error(`fetch error ${res.status}\uFF1A${url}`);
|
|
441
|
+
const blob = yield res.blob();
|
|
442
|
+
blobUrl = URL.createObjectURL(blob);
|
|
443
|
+
needRevoke = true;
|
|
444
|
+
} else {
|
|
445
|
+
blobUrl = url;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const a = document.createElement("a");
|
|
449
|
+
a.href = blobUrl;
|
|
450
|
+
a.download = fileName;
|
|
451
|
+
document.body.appendChild(a);
|
|
452
|
+
a.click();
|
|
453
|
+
document.body.removeChild(a);
|
|
454
|
+
} finally {
|
|
455
|
+
if (needRevoke) {
|
|
456
|
+
setTimeout(() => URL.revokeObjectURL(blobUrl), 100);
|
|
383
457
|
}
|
|
384
458
|
}
|
|
385
|
-
|
|
386
|
-
a.href = blobUrl;
|
|
387
|
-
a.download = fileName;
|
|
388
|
-
document.body.appendChild(a);
|
|
389
|
-
a.click();
|
|
390
|
-
document.body.removeChild(a);
|
|
391
|
-
} finally {
|
|
392
|
-
if (needRevoke) {
|
|
393
|
-
setTimeout(() => URL.revokeObjectURL(blobUrl), 100);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
459
|
+
});
|
|
396
460
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
461
|
+
function parseAxiosBlob(res) {
|
|
462
|
+
return __async(this, null, function* () {
|
|
463
|
+
const { data, headers, status, statusText, config } = res;
|
|
464
|
+
if (status < 200 || status >= 300) throw new Error(`${status}\uFF0C${statusText}\uFF1A${config.url}`);
|
|
465
|
+
if (data.type.includes("application/json")) {
|
|
466
|
+
const txt = yield data.text();
|
|
467
|
+
throw JSON.parse(txt);
|
|
468
|
+
}
|
|
469
|
+
const fileName = getDispositionFileName(headers["content-disposition"]);
|
|
470
|
+
return { blob: data, fileName };
|
|
471
|
+
});
|
|
406
472
|
}
|
|
407
473
|
function getDispositionFileName(disposition) {
|
|
474
|
+
var _a;
|
|
408
475
|
if (!disposition) return "";
|
|
409
476
|
const rfc5987 = /filename\*\s*=\s*([^']*)''([^;]*)/i.exec(disposition);
|
|
410
|
-
if (rfc5987
|
|
477
|
+
if (rfc5987 == null ? void 0 : rfc5987[2]) {
|
|
411
478
|
try {
|
|
412
479
|
return decodeURIComponent(rfc5987[2].trim()).replace(/[\r\n]+/g, "");
|
|
413
|
-
} catch {
|
|
480
|
+
} catch (e) {
|
|
414
481
|
return rfc5987[2].trim().replace(/[\r\n]+/g, "");
|
|
415
482
|
}
|
|
416
483
|
}
|
|
417
484
|
const old = /filename\s*=\s*(?:"([^"]*)"|([^";]*))(?=;|$)/i.exec(disposition);
|
|
418
|
-
if (old) return (old[1]
|
|
485
|
+
if (old) return ((_a = old[1]) != null ? _a : old[2]).trim().replace(/[\r\n]+/g, "");
|
|
419
486
|
return "";
|
|
420
487
|
}
|
|
421
|
-
|
|
422
|
-
return
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
488
|
+
function loadJs(src, attrs) {
|
|
489
|
+
return __async(this, null, function* () {
|
|
490
|
+
return new Promise((resolve, reject) => {
|
|
491
|
+
if (hasJs(src)) return resolve();
|
|
492
|
+
const script = document.createElement("script");
|
|
493
|
+
script.type = "text/javascript";
|
|
494
|
+
script.src = src;
|
|
495
|
+
if (attrs) {
|
|
496
|
+
const keys = Object.keys(attrs);
|
|
497
|
+
keys.forEach((key) => {
|
|
498
|
+
const v = attrs[key];
|
|
499
|
+
if (v === null || v === void 0 || v === false) return;
|
|
500
|
+
script.setAttribute(key, typeof v === "boolean" ? "" : v);
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
script.onload = () => resolve();
|
|
504
|
+
script.onerror = (e) => reject(e);
|
|
505
|
+
document.head.appendChild(script);
|
|
506
|
+
});
|
|
438
507
|
});
|
|
439
508
|
}
|
|
440
509
|
function hasJs(src) {
|
|
@@ -445,23 +514,25 @@ function hasJs(src) {
|
|
|
445
514
|
return src2 && new URL(src2, document.baseURI).href === target;
|
|
446
515
|
});
|
|
447
516
|
}
|
|
448
|
-
|
|
449
|
-
return
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
517
|
+
function loadCss(href, attrs) {
|
|
518
|
+
return __async(this, null, function* () {
|
|
519
|
+
return new Promise((resolve, reject) => {
|
|
520
|
+
if (hasCss(href)) return resolve();
|
|
521
|
+
const link = document.createElement("link");
|
|
522
|
+
link.rel = "stylesheet";
|
|
523
|
+
link.href = href;
|
|
524
|
+
if (attrs) {
|
|
525
|
+
const keys = Object.keys(attrs);
|
|
526
|
+
keys.forEach((key) => {
|
|
527
|
+
const v = attrs[key];
|
|
528
|
+
if (v === null || v === void 0) return;
|
|
529
|
+
link.setAttribute(key, String(v));
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
link.onload = () => resolve();
|
|
533
|
+
link.onerror = (e) => reject(e);
|
|
534
|
+
document.head.appendChild(link);
|
|
535
|
+
});
|
|
465
536
|
});
|
|
466
537
|
}
|
|
467
538
|
function hasCss(href) {
|
|
@@ -505,7 +576,8 @@ function request(config) {
|
|
|
505
576
|
}
|
|
506
577
|
};
|
|
507
578
|
const promise = new Promise((resolve, reject) => {
|
|
508
|
-
const execute =
|
|
579
|
+
const execute = () => __async(null, null, function* () {
|
|
580
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
509
581
|
const {
|
|
510
582
|
url,
|
|
511
583
|
data,
|
|
@@ -551,7 +623,7 @@ function request(config) {
|
|
|
551
623
|
fillBody = fillData;
|
|
552
624
|
}
|
|
553
625
|
}
|
|
554
|
-
const logConfig = {
|
|
626
|
+
const logConfig = __spreadProps(__spreadValues({}, config), { data: fillData, header: fillHeader, url: fillUrl });
|
|
555
627
|
const startTime = Date.now();
|
|
556
628
|
const isCache = cacheTime && cacheTime > 0;
|
|
557
629
|
const cacheKey = isCache ? JSON.stringify({ url: fillUrl, data: fillData }) : "";
|
|
@@ -570,32 +642,32 @@ function request(config) {
|
|
|
570
642
|
}
|
|
571
643
|
}
|
|
572
644
|
const appConfig2 = getBaseToolsConfig();
|
|
573
|
-
if (showLoading) appConfig2.showLoading
|
|
645
|
+
if (showLoading) (_a = appConfig2.showLoading) == null ? void 0 : _a.call(appConfig2);
|
|
574
646
|
let isTimeout = false;
|
|
575
647
|
const timeoutId = setTimeout(() => {
|
|
576
648
|
isTimeout = true;
|
|
577
649
|
controller.abort();
|
|
578
650
|
}, timeout);
|
|
579
651
|
try {
|
|
580
|
-
const response =
|
|
652
|
+
const response = yield fetch(fillUrl, {
|
|
581
653
|
method,
|
|
582
654
|
headers: fillHeader,
|
|
583
655
|
body: fillBody,
|
|
584
656
|
signal
|
|
585
657
|
});
|
|
586
658
|
if (!response.ok) {
|
|
587
|
-
if (showLoading) appConfig2.hideLoading
|
|
659
|
+
if (showLoading) (_b = appConfig2.hideLoading) == null ? void 0 : _b.call(appConfig2);
|
|
588
660
|
throw new Error(`HTTP Error ${response.status}: ${response.statusText}`);
|
|
589
661
|
}
|
|
590
662
|
if (enableChunked) {
|
|
591
|
-
if (showLoading) appConfig2.hideLoading
|
|
592
|
-
const res2 =
|
|
663
|
+
if (showLoading) (_c = appConfig2.hideLoading) == null ? void 0 : _c.call(appConfig2);
|
|
664
|
+
const res2 = yield handleStreamResponse(response, chunkCallback);
|
|
593
665
|
logRequestInfo({ status: "success", config: logConfig, startTime, res: res2 });
|
|
594
666
|
resolve(res2);
|
|
595
667
|
return;
|
|
596
668
|
}
|
|
597
|
-
const resData =
|
|
598
|
-
if (showLoading) appConfig2.hideLoading
|
|
669
|
+
const resData = yield parseResponse(response, responseType);
|
|
670
|
+
if (showLoading) (_d = appConfig2.hideLoading) == null ? void 0 : _d.call(appConfig2);
|
|
599
671
|
const res = responseInterceptor ? responseInterceptor(resData) : resData;
|
|
600
672
|
const code = getObjectValue(res, codeKey);
|
|
601
673
|
const scode = successKey ? getObjectValue(res, successKey) : code;
|
|
@@ -608,28 +680,28 @@ function request(config) {
|
|
|
608
680
|
resolve(getResult(res, resKey));
|
|
609
681
|
} else if (isRelogin) {
|
|
610
682
|
reject(res);
|
|
611
|
-
appConfig2.toLogin
|
|
683
|
+
(_e = appConfig2.toLogin) == null ? void 0 : _e.call(appConfig2);
|
|
612
684
|
} else {
|
|
613
|
-
if (toastError && msg) appConfig2.toast
|
|
685
|
+
if (toastError && msg) (_f = appConfig2.toast) == null ? void 0 : _f.call(appConfig2, { status: "fail", msg });
|
|
614
686
|
reject(res);
|
|
615
687
|
}
|
|
616
688
|
} catch (e) {
|
|
617
689
|
const status = "fail";
|
|
618
690
|
const isAbortError = e instanceof DOMException && e.name === "AbortError";
|
|
619
691
|
if (isAbortError && isTimeout) {
|
|
620
|
-
if (toastError) appConfig2.toast
|
|
692
|
+
if (toastError) (_g = appConfig2.toast) == null ? void 0 : _g.call(appConfig2, { status, msg: "\u8BF7\u6C42\u8D85\u65F6" });
|
|
621
693
|
const timeoutError = new Error("Request Timeout");
|
|
622
694
|
logRequestInfo({ status, config: logConfig, startTime, e: timeoutError });
|
|
623
695
|
reject(timeoutError);
|
|
624
696
|
return;
|
|
625
697
|
}
|
|
626
|
-
if (!isAbortError && toastError) appConfig2.toast
|
|
698
|
+
if (!isAbortError && toastError) (_h = appConfig2.toast) == null ? void 0 : _h.call(appConfig2, { status, msg: "\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25" });
|
|
627
699
|
logRequestInfo({ status, config: logConfig, startTime, e });
|
|
628
700
|
reject(e);
|
|
629
701
|
} finally {
|
|
630
702
|
if (timeoutId) clearTimeout(timeoutId);
|
|
631
703
|
}
|
|
632
|
-
};
|
|
704
|
+
});
|
|
633
705
|
execute();
|
|
634
706
|
});
|
|
635
707
|
promise.task = task;
|
|
@@ -643,7 +715,7 @@ function logRequestInfo(options) {
|
|
|
643
715
|
const { url, data, header, method, extraLog } = config;
|
|
644
716
|
const endTime = Date.now();
|
|
645
717
|
const fmt = "YYYY-MM-DD HH:mm:ss.SSS";
|
|
646
|
-
const info = {
|
|
718
|
+
const info = __spreadValues({
|
|
647
719
|
name: "request",
|
|
648
720
|
status,
|
|
649
721
|
url,
|
|
@@ -653,9 +725,8 @@ function logRequestInfo(options) {
|
|
|
653
725
|
fromCache,
|
|
654
726
|
startTime: toDayjs(startTime).format(fmt),
|
|
655
727
|
endTime: toDayjs(endTime).format(fmt),
|
|
656
|
-
duration: endTime - startTime
|
|
657
|
-
|
|
658
|
-
};
|
|
728
|
+
duration: endTime - startTime
|
|
729
|
+
}, extraLog);
|
|
659
730
|
if (status === "success") {
|
|
660
731
|
info.res = cloneDeep(res);
|
|
661
732
|
log("info", info);
|
|
@@ -677,33 +748,37 @@ function checkCache(cacheKey) {
|
|
|
677
748
|
}
|
|
678
749
|
return cached.res;
|
|
679
750
|
}
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
chunkCallback
|
|
751
|
+
function handleStreamResponse(response, chunkCallback) {
|
|
752
|
+
return __async(this, null, function* () {
|
|
753
|
+
if (!response.body) throw new Error("Response body is null");
|
|
754
|
+
const reader = response.body.getReader();
|
|
755
|
+
while (true) {
|
|
756
|
+
const { done, value } = yield reader.read();
|
|
757
|
+
if (done) break;
|
|
758
|
+
if (chunkCallback && value) {
|
|
759
|
+
chunkCallback({ data: value.buffer });
|
|
760
|
+
}
|
|
688
761
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
762
|
+
return "Stream Finished";
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
function parseResponse(response, responseType) {
|
|
766
|
+
return __async(this, null, function* () {
|
|
767
|
+
let resData;
|
|
768
|
+
if (responseType === "arraybuffer") {
|
|
769
|
+
resData = yield response.arrayBuffer();
|
|
770
|
+
} else if (responseType === "text") {
|
|
771
|
+
resData = yield response.text();
|
|
772
|
+
} else {
|
|
773
|
+
const text = yield response.text();
|
|
774
|
+
try {
|
|
775
|
+
resData = JSON.parse(text);
|
|
776
|
+
} catch (e) {
|
|
777
|
+
resData = text;
|
|
778
|
+
}
|
|
704
779
|
}
|
|
705
|
-
|
|
706
|
-
|
|
780
|
+
return resData;
|
|
781
|
+
});
|
|
707
782
|
}
|
|
708
783
|
function toSearchParams(data) {
|
|
709
784
|
const params = new URLSearchParams();
|
|
@@ -775,7 +850,7 @@ function getLocalStorage(key) {
|
|
|
775
850
|
return parsed[WK.val];
|
|
776
851
|
}
|
|
777
852
|
return parsed;
|
|
778
|
-
} catch {
|
|
853
|
+
} catch (e) {
|
|
779
854
|
return raw;
|
|
780
855
|
}
|
|
781
856
|
}
|