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