@whitesev/utils 2.3.0 → 2.3.1
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/index.amd.js +92 -56
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +92 -56
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +92 -56
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +92 -56
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +92 -56
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +92 -56
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Httpx.d.ts +1273 -1273
- package/dist/types/src/Log.d.ts +96 -96
- package/dist/types/src/Utils.d.ts +1752 -1752
- package/dist/types/src/VueObject.d.ts +110 -110
- package/package.json +1 -1
- package/src/Httpx.ts +114 -71
package/dist/index.umd.js
CHANGED
|
@@ -1583,7 +1583,7 @@
|
|
|
1583
1583
|
return uuid;
|
|
1584
1584
|
}
|
|
1585
1585
|
else {
|
|
1586
|
-
console.warn("HttpxRequestHook.addBeforeRequestCallBack
|
|
1586
|
+
console.warn("[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function");
|
|
1587
1587
|
}
|
|
1588
1588
|
},
|
|
1589
1589
|
/**
|
|
@@ -2062,65 +2062,81 @@
|
|
|
2062
2062
|
*/
|
|
2063
2063
|
onLoad(details, resolve, reject, argumentsList) {
|
|
2064
2064
|
/* X浏览器会因为设置了responseType导致不返回responseText */
|
|
2065
|
-
let
|
|
2065
|
+
let originResponse = argumentsList[0];
|
|
2066
2066
|
/* responseText为空,response不为空的情况 */
|
|
2067
|
-
if (utils.isNull(
|
|
2068
|
-
utils.isNotNull(
|
|
2069
|
-
if (typeof
|
|
2067
|
+
if (utils.isNull(originResponse["responseText"]) &&
|
|
2068
|
+
utils.isNotNull(originResponse["response"])) {
|
|
2069
|
+
if (typeof originResponse["response"] === "object") {
|
|
2070
2070
|
utils.tryCatch().run(() => {
|
|
2071
|
-
|
|
2071
|
+
originResponse["responseText"] = JSON.stringify(originResponse["response"]);
|
|
2072
2072
|
});
|
|
2073
2073
|
}
|
|
2074
2074
|
else {
|
|
2075
|
-
|
|
2075
|
+
originResponse["responseText"] = originResponse["response"];
|
|
2076
2076
|
}
|
|
2077
2077
|
}
|
|
2078
2078
|
/* response为空,responseText不为空的情况 */
|
|
2079
|
-
if (
|
|
2080
|
-
typeof
|
|
2081
|
-
|
|
2082
|
-
|
|
2079
|
+
if (originResponse["response"] == null &&
|
|
2080
|
+
typeof originResponse["responseText"] === "string" &&
|
|
2081
|
+
originResponse["responseText"].trim() !== "") {
|
|
2082
|
+
/** 原始的请求text */
|
|
2083
|
+
let httpxResponseText = originResponse.responseText;
|
|
2084
|
+
// 自定义个新的response
|
|
2085
|
+
let httpxResponse = httpxResponseText;
|
|
2083
2086
|
if (details.responseType === "json") {
|
|
2084
|
-
|
|
2087
|
+
httpxResponse = utils.toJSON(httpxResponseText);
|
|
2085
2088
|
}
|
|
2086
2089
|
else if (details.responseType === "document") {
|
|
2087
2090
|
let parser = new DOMParser();
|
|
2088
|
-
|
|
2091
|
+
httpxResponse = parser.parseFromString(httpxResponseText, "text/html");
|
|
2089
2092
|
}
|
|
2090
2093
|
else if (details.responseType === "arraybuffer") {
|
|
2091
2094
|
let encoder = new TextEncoder();
|
|
2092
|
-
let arrayBuffer = encoder.encode(
|
|
2093
|
-
|
|
2095
|
+
let arrayBuffer = encoder.encode(httpxResponseText);
|
|
2096
|
+
httpxResponse = arrayBuffer;
|
|
2094
2097
|
}
|
|
2095
2098
|
else if (details.responseType === "blob") {
|
|
2096
2099
|
let encoder = new TextEncoder();
|
|
2097
|
-
let arrayBuffer = encoder.encode(
|
|
2098
|
-
|
|
2099
|
-
}
|
|
2100
|
-
else {
|
|
2101
|
-
newResponse = Response["responseText"];
|
|
2100
|
+
let arrayBuffer = encoder.encode(httpxResponseText);
|
|
2101
|
+
httpxResponse = new Blob([arrayBuffer]);
|
|
2102
2102
|
}
|
|
2103
|
+
// 尝试覆盖原response
|
|
2103
2104
|
try {
|
|
2104
|
-
|
|
2105
|
+
let setStatus = Reflect.set(originResponse, "response", httpxResponse);
|
|
2106
|
+
if (!setStatus) {
|
|
2107
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse");
|
|
2108
|
+
try {
|
|
2109
|
+
Reflect.set(originResponse, "httpxResponse", httpxResponse);
|
|
2110
|
+
}
|
|
2111
|
+
catch (error) {
|
|
2112
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2105
2115
|
}
|
|
2106
2116
|
catch (error) {
|
|
2107
|
-
console.warn("response
|
|
2117
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse");
|
|
2118
|
+
try {
|
|
2119
|
+
Reflect.set(originResponse, "httpxResponse", httpxResponse);
|
|
2120
|
+
}
|
|
2121
|
+
catch (error) {
|
|
2122
|
+
console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
|
|
2123
|
+
}
|
|
2108
2124
|
}
|
|
2109
2125
|
}
|
|
2110
2126
|
/* Stay扩展中没有finalUrl,对应的是responseURL */
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2127
|
+
let originResponseURL = Reflect.get(originResponse, "responseURL");
|
|
2128
|
+
if (originResponse["finalUrl"] == null && originResponseURL != null) {
|
|
2129
|
+
Reflect.set(originResponse, "finalUrl", originResponseURL);
|
|
2114
2130
|
}
|
|
2115
2131
|
/* 状态码2xx都是成功的 */
|
|
2116
|
-
if (Math.floor(
|
|
2117
|
-
if (this.context.HttpxResponseHook.successResponseCallBack(
|
|
2132
|
+
if (Math.floor(originResponse.status / 100) === 2) {
|
|
2133
|
+
if (this.context.HttpxResponseHook.successResponseCallBack(originResponse, details) == null) {
|
|
2118
2134
|
// reject(new TypeError("response is intercept with onloada"));
|
|
2119
2135
|
return;
|
|
2120
2136
|
}
|
|
2121
2137
|
resolve({
|
|
2122
2138
|
status: true,
|
|
2123
|
-
data:
|
|
2139
|
+
data: originResponse,
|
|
2124
2140
|
details: details,
|
|
2125
2141
|
msg: "请求完毕",
|
|
2126
2142
|
type: "onload",
|
|
@@ -2165,7 +2181,7 @@
|
|
|
2165
2181
|
*/
|
|
2166
2182
|
request(details) {
|
|
2167
2183
|
if (this.context.#LOG_DETAILS) {
|
|
2168
|
-
console.log("Httpx
|
|
2184
|
+
console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
|
|
2169
2185
|
}
|
|
2170
2186
|
if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
|
|
2171
2187
|
"function") {
|
|
@@ -2198,33 +2214,38 @@
|
|
|
2198
2214
|
*/
|
|
2199
2215
|
fetch(details, fetchRequestInit, abortController) {
|
|
2200
2216
|
fetch(details.url, fetchRequestInit)
|
|
2201
|
-
.then(async (
|
|
2202
|
-
/**
|
|
2203
|
-
* @type {HttpxAsyncResultData}
|
|
2204
|
-
*/
|
|
2217
|
+
.then(async (fetchResponse) => {
|
|
2218
|
+
/** 自定义的response */
|
|
2205
2219
|
let httpxResponse = {
|
|
2206
2220
|
isFetch: true,
|
|
2207
|
-
finalUrl:
|
|
2221
|
+
finalUrl: fetchResponse.url,
|
|
2208
2222
|
readyState: 4,
|
|
2209
|
-
|
|
2210
|
-
|
|
2223
|
+
// @ts-ignore
|
|
2224
|
+
status: fetchResponse.status,
|
|
2225
|
+
statusText: fetchResponse.statusText,
|
|
2211
2226
|
response: void 0,
|
|
2212
|
-
responseFetchHeaders:
|
|
2227
|
+
responseFetchHeaders: fetchResponse.headers,
|
|
2213
2228
|
responseHeaders: "",
|
|
2229
|
+
// @ts-ignore
|
|
2214
2230
|
responseText: void 0,
|
|
2215
2231
|
responseType: details.responseType,
|
|
2216
2232
|
responseXML: void 0,
|
|
2217
2233
|
};
|
|
2218
2234
|
Object.assign(httpxResponse, details.context || {});
|
|
2219
|
-
|
|
2235
|
+
// 把headers转为字符串
|
|
2236
|
+
for (const [key, value] of fetchResponse.headers.entries()) {
|
|
2220
2237
|
httpxResponse.responseHeaders += `${key}: ${value}\n`;
|
|
2221
2238
|
}
|
|
2222
|
-
|
|
2239
|
+
/** 请求返回的类型 */
|
|
2240
|
+
const fetchResponseType = fetchResponse.headers.get("Content-Type");
|
|
2241
|
+
/* 如果需要stream,且获取到的是stream,那直接返回 */
|
|
2223
2242
|
if (details.responseType === "stream" ||
|
|
2224
|
-
(
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2243
|
+
(fetchResponse.headers.has("Content-Type") &&
|
|
2244
|
+
fetchResponse.headers
|
|
2245
|
+
.get("Content-Type")
|
|
2246
|
+
.includes("text/event-stream"))) {
|
|
2247
|
+
Reflect.set(httpxResponse, "isStream", true);
|
|
2248
|
+
Reflect.set(httpxResponse, "response", fetchResponse.body);
|
|
2228
2249
|
Reflect.deleteProperty(httpxResponse, "responseText");
|
|
2229
2250
|
Reflect.deleteProperty(httpxResponse, "responseXML");
|
|
2230
2251
|
details.onload(httpxResponse);
|
|
@@ -2236,42 +2257,57 @@
|
|
|
2236
2257
|
let responseText = "";
|
|
2237
2258
|
/** 响应xml文档 */
|
|
2238
2259
|
let responseXML = "";
|
|
2239
|
-
|
|
2260
|
+
/** 先获取二进制数据 */
|
|
2261
|
+
let arrayBuffer = await fetchResponse.arrayBuffer();
|
|
2262
|
+
/** 数据编码 */
|
|
2240
2263
|
let encoding = "utf-8";
|
|
2241
|
-
if (
|
|
2242
|
-
let charsetMatched =
|
|
2264
|
+
if (fetchResponse.headers.has("Content-Type")) {
|
|
2265
|
+
let charsetMatched = fetchResponse.headers
|
|
2243
2266
|
.get("Content-Type")
|
|
2244
2267
|
?.match(/charset=(.+)/);
|
|
2245
2268
|
if (charsetMatched) {
|
|
2246
2269
|
encoding = charsetMatched[1];
|
|
2270
|
+
encoding = encoding.toLowerCase();
|
|
2247
2271
|
}
|
|
2248
2272
|
}
|
|
2273
|
+
// Failed to construct 'TextDecoder': The encoding label provided ('"UTF-8"') is invalid.
|
|
2274
|
+
// 去除引号
|
|
2275
|
+
encoding = encoding.replace(/('|")/gi, "");
|
|
2276
|
+
// 编码
|
|
2249
2277
|
let textDecoder = new TextDecoder(encoding);
|
|
2250
2278
|
responseText = textDecoder.decode(arrayBuffer);
|
|
2251
2279
|
response = responseText;
|
|
2252
2280
|
if (details.responseType === "arraybuffer") {
|
|
2281
|
+
// response返回格式是二进制流
|
|
2253
2282
|
response = arrayBuffer;
|
|
2254
2283
|
}
|
|
2255
2284
|
else if (details.responseType === "blob") {
|
|
2285
|
+
// response返回格式是blob
|
|
2256
2286
|
response = new Blob([arrayBuffer]);
|
|
2257
2287
|
}
|
|
2288
|
+
else if (details.responseType === "json" ||
|
|
2289
|
+
(typeof fetchResponseType === "string" &&
|
|
2290
|
+
fetchResponseType.includes("application/json"))) {
|
|
2291
|
+
// response返回格式是JSON格式
|
|
2292
|
+
response = utils.toJSON(responseText);
|
|
2293
|
+
}
|
|
2258
2294
|
else if (details.responseType === "document" ||
|
|
2259
2295
|
details.responseType == null) {
|
|
2296
|
+
// response返回格式是文档格式
|
|
2260
2297
|
let parser = new DOMParser();
|
|
2261
2298
|
response = parser.parseFromString(responseText, "text/html");
|
|
2262
2299
|
}
|
|
2263
|
-
|
|
2264
|
-
response = utils.toJSON(responseText);
|
|
2265
|
-
}
|
|
2300
|
+
// 转为XML结构
|
|
2266
2301
|
let parser = new DOMParser();
|
|
2267
2302
|
responseXML = parser.parseFromString(responseText, "text/xml");
|
|
2268
|
-
httpxResponse
|
|
2269
|
-
httpxResponse
|
|
2270
|
-
httpxResponse
|
|
2303
|
+
Reflect.set(httpxResponse, "response", response);
|
|
2304
|
+
Reflect.set(httpxResponse, "responseText", responseText);
|
|
2305
|
+
Reflect.set(httpxResponse, "responseXML", responseXML);
|
|
2306
|
+
// 执行回调
|
|
2271
2307
|
details.onload(httpxResponse);
|
|
2272
2308
|
})
|
|
2273
|
-
.catch((
|
|
2274
|
-
if (
|
|
2309
|
+
.catch((error) => {
|
|
2310
|
+
if (error.name === "AbortError") {
|
|
2275
2311
|
return;
|
|
2276
2312
|
}
|
|
2277
2313
|
details.onerror({
|
|
@@ -2282,7 +2318,7 @@
|
|
|
2282
2318
|
statusText: "",
|
|
2283
2319
|
responseHeaders: "",
|
|
2284
2320
|
responseText: "",
|
|
2285
|
-
error:
|
|
2321
|
+
error: error,
|
|
2286
2322
|
});
|
|
2287
2323
|
});
|
|
2288
2324
|
details.onloadstart({
|
|
@@ -2345,7 +2381,7 @@
|
|
|
2345
2381
|
*/
|
|
2346
2382
|
constructor(__xmlHttpRequest__) {
|
|
2347
2383
|
if (typeof __xmlHttpRequest__ !== "function") {
|
|
2348
|
-
console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function
|
|
2384
|
+
console.warn("[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch");
|
|
2349
2385
|
}
|
|
2350
2386
|
this.interceptors.request.context = this;
|
|
2351
2387
|
this.interceptors.response.context = this;
|