@whitesev/utils 2.2.9 → 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 +336 -60
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +336 -60
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +336 -60
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +336 -60
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +336 -60
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +336 -60
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Httpx.d.ts +2 -3
- package/dist/types/src/Log.d.ts +1 -2
- package/dist/types/src/Utils.d.ts +15 -10
- package/dist/types/src/Vue.d.ts +43 -0
- package/dist/types/src/VueObject.d.ts +28 -28
- package/package.json +2 -1
- package/src/Httpx.ts +117 -74
- package/src/Log.ts +1 -2
- package/src/Utils.ts +26 -25
- package/src/Vue.ts +241 -0
- package/src/VueObject.ts +29 -29
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AnyObject } from "./Utils";
|
|
2
1
|
/**
|
|
3
2
|
* 状态码
|
|
4
3
|
* + https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status
|
|
@@ -15,7 +14,7 @@ export type HttpxResponseCallBackType = "onload" | "onerror" | "ontimeout" | "on
|
|
|
15
14
|
export type HttpxResponseMap = {
|
|
16
15
|
arraybuffer: ArrayBuffer;
|
|
17
16
|
blob: Blob;
|
|
18
|
-
json:
|
|
17
|
+
json: any;
|
|
19
18
|
stream: ReadableStream<string>;
|
|
20
19
|
document: Document;
|
|
21
20
|
undefined: string;
|
|
@@ -965,7 +964,7 @@ export declare interface HttpxDetails {
|
|
|
965
964
|
/**
|
|
966
965
|
* 将该对象添加到响应的属性中,可为空
|
|
967
966
|
*/
|
|
968
|
-
context?:
|
|
967
|
+
context?: any;
|
|
969
968
|
/**
|
|
970
969
|
* 重写mimeType,可为空
|
|
971
970
|
*/
|
package/dist/types/src/Log.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { AnyObject } from "./Utils";
|
|
2
1
|
/** Utils.Log的初始化配置 */
|
|
3
2
|
declare interface UtilsLogOptions {
|
|
4
3
|
/** 是否输出Tag,false的话其它的颜色也不输出,默认为true */
|
|
@@ -83,7 +82,7 @@ declare class Log {
|
|
|
83
82
|
* @example
|
|
84
83
|
* log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
|
|
85
84
|
*/
|
|
86
|
-
table(msg:
|
|
85
|
+
table(msg: any[], color?: string, otherStyle?: string): void;
|
|
87
86
|
/**
|
|
88
87
|
* 配置Log对象的颜色
|
|
89
88
|
* @param paramDetails 配置信息
|
|
@@ -13,6 +13,7 @@ import type { DOMUtils_EventType } from "./Event";
|
|
|
13
13
|
import type { Vue2Object } from "./VueObject";
|
|
14
14
|
import type { UtilsAjaxHookResult } from "./AjaxHookerType";
|
|
15
15
|
import { type UtilsWindowApiOption } from "./WindowApi";
|
|
16
|
+
import { Vue } from "./Vue";
|
|
16
17
|
export declare var unsafeWindow: Window & typeof globalThis;
|
|
17
18
|
export type JSTypeMap = {
|
|
18
19
|
string: string;
|
|
@@ -546,12 +547,12 @@ declare class Utils {
|
|
|
546
547
|
* Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
|
|
547
548
|
*/
|
|
548
549
|
getReactObj(element: HTMLElement | Element): {
|
|
549
|
-
reactFiber?:
|
|
550
|
-
reactProps?:
|
|
551
|
-
reactEvents?:
|
|
552
|
-
reactEventHandlers?:
|
|
553
|
-
reactInternalInstance?:
|
|
554
|
-
reactContainer?:
|
|
550
|
+
reactFiber?: any;
|
|
551
|
+
reactProps?: any;
|
|
552
|
+
reactEvents?: any;
|
|
553
|
+
reactEventHandlers?: any;
|
|
554
|
+
reactInternalInstance?: any;
|
|
555
|
+
reactContainer?: any;
|
|
555
556
|
};
|
|
556
557
|
/**
|
|
557
558
|
* 获取对象上的Symbol属性,如果没设置keyName,那么返回一个对象,对象是所有遍历到的Symbol对象
|
|
@@ -958,7 +959,7 @@ declare class Utils {
|
|
|
958
959
|
* Utils.parseObjectToArray({"工具类":"jsonToArray","return","Array"});
|
|
959
960
|
* > ['jsonToArray', 'Array']
|
|
960
961
|
**/
|
|
961
|
-
parseObjectToArray(target:
|
|
962
|
+
parseObjectToArray(target: any): any;
|
|
962
963
|
/**
|
|
963
964
|
* 自动锁对象,用于循环判断运行的函数,在循环外new后使用,注意,如果函数内部存在异步操作,需要使用await
|
|
964
965
|
* @example
|
|
@@ -1660,7 +1661,7 @@ declare class Utils {
|
|
|
1660
1661
|
* > "test success set"
|
|
1661
1662
|
*
|
|
1662
1663
|
*/
|
|
1663
|
-
waitProperty<T extends any>(checkObj:
|
|
1664
|
+
waitProperty<T extends any>(checkObj: any | (() => any), checkPropertyName: string): Promise<T>;
|
|
1664
1665
|
/**
|
|
1665
1666
|
* 在规定时间内等待对象上的属性出现
|
|
1666
1667
|
* @param checkObj 检查的对象
|
|
@@ -1671,7 +1672,7 @@ declare class Utils {
|
|
|
1671
1672
|
* await Utils.waitPropertyByInterval(window,"test");
|
|
1672
1673
|
* console.log("test success set");
|
|
1673
1674
|
*/
|
|
1674
|
-
waitPropertyByInterval<T extends any>(checkObj:
|
|
1675
|
+
waitPropertyByInterval<T extends any>(checkObj: any | (() => any), checkPropertyName: string | ((obj: any) => boolean), intervalTimer?: number, maxTime?: number): Promise<T>;
|
|
1675
1676
|
/**
|
|
1676
1677
|
* 在规定时间内等待元素上的__vue__属性或者__vue__属性上的某个值出现出现
|
|
1677
1678
|
* @param element 目标元素
|
|
@@ -1707,7 +1708,7 @@ declare class Utils {
|
|
|
1707
1708
|
* console.log(window.test);
|
|
1708
1709
|
* > 111;
|
|
1709
1710
|
*/
|
|
1710
|
-
watchObject(target:
|
|
1711
|
+
watchObject(target: any, propertyName: string, getCallBack: (value: any) => void, setCallBack: (value: any) => void): void;
|
|
1711
1712
|
/**
|
|
1712
1713
|
* 创建一个新的Utils实例
|
|
1713
1714
|
* @param option
|
|
@@ -1742,6 +1743,10 @@ declare class Utils {
|
|
|
1742
1743
|
* Utils.generateUUID()
|
|
1743
1744
|
*/
|
|
1744
1745
|
generateUUID: () => string;
|
|
1746
|
+
/**
|
|
1747
|
+
* 自定义的动态响应对象
|
|
1748
|
+
*/
|
|
1749
|
+
Vue: typeof Vue;
|
|
1745
1750
|
}
|
|
1746
1751
|
declare let utils: Utils;
|
|
1747
1752
|
export { utils as Utils };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
declare class RefImpl {
|
|
2
|
+
_value: any;
|
|
3
|
+
_isRef: boolean;
|
|
4
|
+
_rawValue: any;
|
|
5
|
+
_vue: Vue;
|
|
6
|
+
constructor(vueIns: Vue, rawValue: any);
|
|
7
|
+
get value(): any;
|
|
8
|
+
set value(newValue: any);
|
|
9
|
+
}
|
|
10
|
+
declare class ObjectRefImpl {
|
|
11
|
+
object: any;
|
|
12
|
+
key: any;
|
|
13
|
+
constructor(object: any, key: any);
|
|
14
|
+
get value(): any;
|
|
15
|
+
set value(newValue: any);
|
|
16
|
+
}
|
|
17
|
+
export declare class Vue {
|
|
18
|
+
private reactMap;
|
|
19
|
+
private targetMap;
|
|
20
|
+
private activeEffect;
|
|
21
|
+
constructor();
|
|
22
|
+
/**
|
|
23
|
+
* 生成一个被代理的对象
|
|
24
|
+
* @param target 需要代理的对象
|
|
25
|
+
*/
|
|
26
|
+
reactive<T extends object>(target: T): T;
|
|
27
|
+
/**
|
|
28
|
+
* 观察被reactive的对象值改变
|
|
29
|
+
* @param source 被观察的对象,这里采用函数返回对象
|
|
30
|
+
* @param changeCallBack 值改变的回调
|
|
31
|
+
*/
|
|
32
|
+
watch<T>(source: () => T, changeCallBack: (newValue: T | undefined, oldValue: T | undefined) => void): void;
|
|
33
|
+
toReactive(value: any): any;
|
|
34
|
+
ref(value: any): RefImpl;
|
|
35
|
+
toRef(object: any, key: any): ObjectRefImpl;
|
|
36
|
+
toRefs(object: any): {};
|
|
37
|
+
private trigger;
|
|
38
|
+
private triggerEffect;
|
|
39
|
+
private track;
|
|
40
|
+
private trackEffect;
|
|
41
|
+
private traversal;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
|
@@ -1,49 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
$attrs: AnyObject;
|
|
1
|
+
export declare interface Vue2Object {
|
|
2
|
+
$attrs: any;
|
|
4
3
|
$children: Vue2Object[];
|
|
5
4
|
$createElement: (...args: any[]) => any;
|
|
6
5
|
$el: HTMLElement;
|
|
7
|
-
$listeners:
|
|
8
|
-
$options:
|
|
6
|
+
$listeners: any;
|
|
7
|
+
$options: any;
|
|
9
8
|
$parent: Vue2Object;
|
|
10
|
-
$refs:
|
|
9
|
+
$refs: any;
|
|
11
10
|
$root: Vue2Object;
|
|
12
|
-
$scopedSlots:
|
|
13
|
-
$slots:
|
|
14
|
-
$store:
|
|
15
|
-
$vnode:
|
|
16
|
-
_data:
|
|
11
|
+
$scopedSlots: any;
|
|
12
|
+
$slots: any;
|
|
13
|
+
$store: any;
|
|
14
|
+
$vnode: any;
|
|
15
|
+
_data: any;
|
|
17
16
|
_directInactive: boolean;
|
|
18
|
-
_events:
|
|
17
|
+
_events: any;
|
|
19
18
|
_hasHookEvent: boolean;
|
|
20
19
|
_isBeingDestroyed: boolean;
|
|
21
20
|
_isDestroyed: boolean;
|
|
22
21
|
_isMounted: boolean;
|
|
23
22
|
_isVue: boolean;
|
|
24
|
-
$data:
|
|
23
|
+
$data: any;
|
|
25
24
|
$isServer: boolean;
|
|
26
|
-
$props:
|
|
27
|
-
$route:
|
|
25
|
+
$props: any;
|
|
26
|
+
$route: any & {
|
|
28
27
|
fullPath: string;
|
|
29
28
|
hash: string;
|
|
30
|
-
matched:
|
|
31
|
-
meta:
|
|
29
|
+
matched: any[];
|
|
30
|
+
meta: any;
|
|
32
31
|
name: string;
|
|
33
|
-
params:
|
|
32
|
+
params: any;
|
|
34
33
|
path: string;
|
|
35
|
-
query:
|
|
34
|
+
query: any;
|
|
36
35
|
};
|
|
37
|
-
$router:
|
|
36
|
+
$router: any & {
|
|
38
37
|
afterHooks: Function[];
|
|
39
38
|
app: Vue2Object;
|
|
40
39
|
apps: Vue2Object[];
|
|
41
40
|
beforeHooks: Function[];
|
|
42
41
|
fallback: boolean;
|
|
43
|
-
history:
|
|
42
|
+
history: any & {
|
|
44
43
|
base: string;
|
|
45
|
-
current:
|
|
46
|
-
listeners:
|
|
44
|
+
current: any;
|
|
45
|
+
listeners: any[];
|
|
47
46
|
router: Vue2Object["$router"];
|
|
48
47
|
/**
|
|
49
48
|
*
|
|
@@ -58,16 +57,16 @@ export declare interface Vue2Object extends AnyObject {
|
|
|
58
57
|
* @param data 可选的 HistoryState 以关联该导航记录
|
|
59
58
|
* @returns
|
|
60
59
|
*/
|
|
61
|
-
push: (to: string, data?:
|
|
60
|
+
push: (to: string, data?: any) => void;
|
|
62
61
|
/**
|
|
63
62
|
*
|
|
64
63
|
* @param to 要设置的地址
|
|
65
64
|
* @param data 可选的 HistoryState 以关联该导航记录
|
|
66
65
|
* @returns
|
|
67
66
|
*/
|
|
68
|
-
replace: (to: string, data?:
|
|
67
|
+
replace: (to: string, data?: any) => void;
|
|
69
68
|
};
|
|
70
|
-
matcher:
|
|
69
|
+
matcher: any & {
|
|
71
70
|
addRoute: (...args: any[]) => any;
|
|
72
71
|
addRoutes: (...args: any[]) => any;
|
|
73
72
|
getRoutes: () => any;
|
|
@@ -75,7 +74,7 @@ export declare interface Vue2Object extends AnyObject {
|
|
|
75
74
|
};
|
|
76
75
|
mode: string;
|
|
77
76
|
resolveHooks: ((...args: any[]) => any)[];
|
|
78
|
-
currentRoute:
|
|
77
|
+
currentRoute: any;
|
|
79
78
|
beforeEach: (callback: ((
|
|
80
79
|
/** 即将要进入的目标 路由对象 */
|
|
81
80
|
to: Vue2Object["$route"],
|
|
@@ -99,11 +98,12 @@ export declare interface Vue2Object extends AnyObject {
|
|
|
99
98
|
/** 移除上一个添加的监听 */
|
|
100
99
|
| (() => void)) => void;
|
|
101
100
|
};
|
|
102
|
-
$ssrContext:
|
|
101
|
+
$ssrContext: any;
|
|
103
102
|
$watch: (key: string | string[] | (() => any), handler: (this: any, newVal: any, oldVal: any) => void, options?: {
|
|
104
103
|
immediate?: boolean;
|
|
105
104
|
deep?: boolean;
|
|
106
105
|
}) => void;
|
|
106
|
+
[key: string]: any;
|
|
107
107
|
}
|
|
108
108
|
export declare interface HTMLVue2DivElement extends HTMLDivElement {
|
|
109
109
|
__vue__: Vue2Object;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whitesev/utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "一个常用的工具库",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"typescript": "^5.5.4"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
|
+
"dev": "rollup --config --watch",
|
|
47
48
|
"build": "rollup --config",
|
|
48
49
|
"build:all": "rollup --config"
|
|
49
50
|
}
|
package/src/Httpx.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Utils } from "./Utils";
|
|
2
2
|
import { GenerateUUID } from "./UtilsCommon";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -98,7 +98,7 @@ export type HttpxResponseCallBackType =
|
|
|
98
98
|
export type HttpxResponseMap = {
|
|
99
99
|
arraybuffer: ArrayBuffer;
|
|
100
100
|
blob: Blob;
|
|
101
|
-
json:
|
|
101
|
+
json: any;
|
|
102
102
|
stream: ReadableStream<string>;
|
|
103
103
|
document: Document;
|
|
104
104
|
undefined: string;
|
|
@@ -1062,7 +1062,7 @@ export declare interface HttpxDetails {
|
|
|
1062
1062
|
/**
|
|
1063
1063
|
* 将该对象添加到响应的属性中,可为空
|
|
1064
1064
|
*/
|
|
1065
|
-
context?:
|
|
1065
|
+
context?: any;
|
|
1066
1066
|
/**
|
|
1067
1067
|
* 重写mimeType,可为空
|
|
1068
1068
|
*/
|
|
@@ -1299,7 +1299,7 @@ class Httpx {
|
|
|
1299
1299
|
return uuid;
|
|
1300
1300
|
} else {
|
|
1301
1301
|
console.warn(
|
|
1302
|
-
"HttpxRequestHook.addBeforeRequestCallBack
|
|
1302
|
+
"[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function"
|
|
1303
1303
|
);
|
|
1304
1304
|
}
|
|
1305
1305
|
},
|
|
@@ -1889,66 +1889,93 @@ class Httpx {
|
|
|
1889
1889
|
argumentsList: any
|
|
1890
1890
|
) {
|
|
1891
1891
|
/* X浏览器会因为设置了responseType导致不返回responseText */
|
|
1892
|
-
let
|
|
1892
|
+
let originResponse: HttpxAsyncResultData<HttpxDetails> = argumentsList[0];
|
|
1893
1893
|
/* responseText为空,response不为空的情况 */
|
|
1894
1894
|
if (
|
|
1895
|
-
Utils.isNull(
|
|
1896
|
-
Utils.isNotNull(
|
|
1895
|
+
Utils.isNull(originResponse["responseText"]) &&
|
|
1896
|
+
Utils.isNotNull(originResponse["response"])
|
|
1897
1897
|
) {
|
|
1898
|
-
if (typeof
|
|
1898
|
+
if (typeof originResponse["response"] === "object") {
|
|
1899
1899
|
Utils.tryCatch().run(() => {
|
|
1900
|
-
|
|
1900
|
+
originResponse["responseText"] = JSON.stringify(
|
|
1901
|
+
originResponse["response"]
|
|
1902
|
+
);
|
|
1901
1903
|
});
|
|
1902
1904
|
} else {
|
|
1903
|
-
|
|
1905
|
+
originResponse["responseText"] = originResponse["response"];
|
|
1904
1906
|
}
|
|
1905
1907
|
}
|
|
1906
1908
|
|
|
1907
1909
|
/* response为空,responseText不为空的情况 */
|
|
1908
1910
|
if (
|
|
1909
|
-
|
|
1910
|
-
typeof
|
|
1911
|
-
|
|
1911
|
+
originResponse["response"] == null &&
|
|
1912
|
+
typeof originResponse["responseText"] === "string" &&
|
|
1913
|
+
originResponse["responseText"].trim() !== ""
|
|
1912
1914
|
) {
|
|
1913
|
-
|
|
1915
|
+
/** 原始的请求text */
|
|
1916
|
+
let httpxResponseText = originResponse.responseText;
|
|
1917
|
+
// 自定义个新的response
|
|
1918
|
+
let httpxResponse: any = httpxResponseText;
|
|
1914
1919
|
if (details.responseType === "json") {
|
|
1915
|
-
|
|
1920
|
+
httpxResponse = Utils.toJSON(httpxResponseText);
|
|
1916
1921
|
} else if (details.responseType === "document") {
|
|
1917
1922
|
let parser = new DOMParser();
|
|
1918
|
-
|
|
1919
|
-
|
|
1923
|
+
httpxResponse = parser.parseFromString(
|
|
1924
|
+
httpxResponseText,
|
|
1920
1925
|
"text/html"
|
|
1921
1926
|
);
|
|
1922
1927
|
} else if (details.responseType === "arraybuffer") {
|
|
1923
1928
|
let encoder = new TextEncoder();
|
|
1924
|
-
let arrayBuffer = encoder.encode(
|
|
1925
|
-
|
|
1929
|
+
let arrayBuffer = encoder.encode(httpxResponseText);
|
|
1930
|
+
httpxResponse = arrayBuffer;
|
|
1926
1931
|
} else if (details.responseType === "blob") {
|
|
1927
1932
|
let encoder = new TextEncoder();
|
|
1928
|
-
let arrayBuffer = encoder.encode(
|
|
1929
|
-
|
|
1930
|
-
} else {
|
|
1931
|
-
newResponse = Response["responseText"];
|
|
1933
|
+
let arrayBuffer = encoder.encode(httpxResponseText);
|
|
1934
|
+
httpxResponse = new Blob([arrayBuffer]);
|
|
1932
1935
|
}
|
|
1936
|
+
// 尝试覆盖原response
|
|
1933
1937
|
try {
|
|
1934
|
-
|
|
1938
|
+
let setStatus = Reflect.set(
|
|
1939
|
+
originResponse,
|
|
1940
|
+
"response",
|
|
1941
|
+
httpxResponse
|
|
1942
|
+
);
|
|
1943
|
+
if (!setStatus) {
|
|
1944
|
+
console.warn(
|
|
1945
|
+
"[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse"
|
|
1946
|
+
);
|
|
1947
|
+
try {
|
|
1948
|
+
Reflect.set(originResponse, "httpxResponse", httpxResponse);
|
|
1949
|
+
} catch (error) {
|
|
1950
|
+
console.warn(
|
|
1951
|
+
"[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖"
|
|
1952
|
+
);
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1935
1955
|
} catch (error) {
|
|
1936
|
-
console.warn(
|
|
1956
|
+
console.warn(
|
|
1957
|
+
"[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse"
|
|
1958
|
+
);
|
|
1959
|
+
try {
|
|
1960
|
+
Reflect.set(originResponse, "httpxResponse", httpxResponse);
|
|
1961
|
+
} catch (error) {
|
|
1962
|
+
console.warn(
|
|
1963
|
+
"[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖"
|
|
1964
|
+
);
|
|
1965
|
+
}
|
|
1937
1966
|
}
|
|
1938
1967
|
}
|
|
1939
1968
|
/* Stay扩展中没有finalUrl,对应的是responseURL */
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
(
|
|
1943
|
-
) {
|
|
1944
|
-
Response["finalUrl"] = (Response as any)["responseURL"];
|
|
1969
|
+
let originResponseURL = Reflect.get(originResponse, "responseURL");
|
|
1970
|
+
if (originResponse["finalUrl"] == null && originResponseURL != null) {
|
|
1971
|
+
Reflect.set(originResponse, "finalUrl", originResponseURL);
|
|
1945
1972
|
}
|
|
1946
1973
|
|
|
1947
1974
|
/* 状态码2xx都是成功的 */
|
|
1948
|
-
if (Math.floor(
|
|
1975
|
+
if (Math.floor(originResponse.status / 100) === 2) {
|
|
1949
1976
|
if (
|
|
1950
1977
|
this.context.HttpxResponseHook.successResponseCallBack(
|
|
1951
|
-
|
|
1978
|
+
originResponse,
|
|
1952
1979
|
details
|
|
1953
1980
|
) == null
|
|
1954
1981
|
) {
|
|
@@ -1957,7 +1984,7 @@ class Httpx {
|
|
|
1957
1984
|
}
|
|
1958
1985
|
resolve({
|
|
1959
1986
|
status: true,
|
|
1960
|
-
data:
|
|
1987
|
+
data: originResponse,
|
|
1961
1988
|
details: details,
|
|
1962
1989
|
msg: "请求完毕",
|
|
1963
1990
|
type: "onload",
|
|
@@ -2007,7 +2034,7 @@ class Httpx {
|
|
|
2007
2034
|
*/
|
|
2008
2035
|
request(details: Required<HttpxDetails>) {
|
|
2009
2036
|
if (this.context.#LOG_DETAILS) {
|
|
2010
|
-
console.log("Httpx
|
|
2037
|
+
console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
|
|
2011
2038
|
}
|
|
2012
2039
|
if (
|
|
2013
2040
|
typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
|
|
@@ -2047,37 +2074,43 @@ class Httpx {
|
|
|
2047
2074
|
abortController: AbortController
|
|
2048
2075
|
) {
|
|
2049
2076
|
fetch(details.url, fetchRequestInit)
|
|
2050
|
-
.then(async (
|
|
2051
|
-
/**
|
|
2052
|
-
|
|
2053
|
-
*/
|
|
2054
|
-
let httpxResponse = {
|
|
2077
|
+
.then(async (fetchResponse) => {
|
|
2078
|
+
/** 自定义的response */
|
|
2079
|
+
let httpxResponse: HttpxAsyncResultData = {
|
|
2055
2080
|
isFetch: true,
|
|
2056
|
-
finalUrl:
|
|
2081
|
+
finalUrl: fetchResponse.url,
|
|
2057
2082
|
readyState: 4,
|
|
2058
|
-
|
|
2059
|
-
|
|
2083
|
+
// @ts-ignore
|
|
2084
|
+
status: fetchResponse.status,
|
|
2085
|
+
statusText: fetchResponse.statusText,
|
|
2060
2086
|
response: void 0,
|
|
2061
|
-
responseFetchHeaders:
|
|
2087
|
+
responseFetchHeaders: fetchResponse.headers,
|
|
2062
2088
|
responseHeaders: "",
|
|
2089
|
+
// @ts-ignore
|
|
2063
2090
|
responseText: void 0,
|
|
2064
2091
|
responseType: details.responseType,
|
|
2065
2092
|
responseXML: void 0,
|
|
2066
2093
|
};
|
|
2067
2094
|
Object.assign(httpxResponse, details.context || {});
|
|
2068
2095
|
|
|
2069
|
-
|
|
2096
|
+
// 把headers转为字符串
|
|
2097
|
+
for (const [key, value] of (fetchResponse.headers as any).entries()) {
|
|
2070
2098
|
httpxResponse.responseHeaders += `${key}: ${value}\n`;
|
|
2071
2099
|
}
|
|
2072
2100
|
|
|
2073
|
-
|
|
2101
|
+
/** 请求返回的类型 */
|
|
2102
|
+
const fetchResponseType = fetchResponse.headers.get("Content-Type");
|
|
2103
|
+
|
|
2104
|
+
/* 如果需要stream,且获取到的是stream,那直接返回 */
|
|
2074
2105
|
if (
|
|
2075
2106
|
details.responseType === "stream" ||
|
|
2076
|
-
(
|
|
2077
|
-
|
|
2107
|
+
(fetchResponse.headers.has("Content-Type") &&
|
|
2108
|
+
fetchResponse.headers
|
|
2109
|
+
.get("Content-Type")!
|
|
2110
|
+
.includes("text/event-stream"))
|
|
2078
2111
|
) {
|
|
2079
|
-
(httpxResponse
|
|
2080
|
-
(httpxResponse
|
|
2112
|
+
Reflect.set(httpxResponse, "isStream", true);
|
|
2113
|
+
Reflect.set(httpxResponse, "response", fetchResponse.body);
|
|
2081
2114
|
Reflect.deleteProperty(httpxResponse, "responseText");
|
|
2082
2115
|
Reflect.deleteProperty(httpxResponse, "responseXML");
|
|
2083
2116
|
details.onload(httpxResponse);
|
|
@@ -2085,57 +2118,67 @@ class Httpx {
|
|
|
2085
2118
|
}
|
|
2086
2119
|
|
|
2087
2120
|
/** 响应 */
|
|
2088
|
-
let response = "";
|
|
2121
|
+
let response: any = "";
|
|
2089
2122
|
/** 响应字符串 */
|
|
2090
2123
|
let responseText = "";
|
|
2091
2124
|
/** 响应xml文档 */
|
|
2092
|
-
let responseXML = "";
|
|
2093
|
-
|
|
2094
|
-
let arrayBuffer = await
|
|
2125
|
+
let responseXML: XMLDocument | string = "";
|
|
2126
|
+
/** 先获取二进制数据 */
|
|
2127
|
+
let arrayBuffer = await fetchResponse.arrayBuffer();
|
|
2095
2128
|
|
|
2129
|
+
/** 数据编码 */
|
|
2096
2130
|
let encoding = "utf-8";
|
|
2097
|
-
if (
|
|
2098
|
-
let charsetMatched =
|
|
2131
|
+
if (fetchResponse.headers.has("Content-Type")) {
|
|
2132
|
+
let charsetMatched = fetchResponse.headers
|
|
2099
2133
|
.get("Content-Type")
|
|
2100
2134
|
?.match(/charset=(.+)/);
|
|
2101
2135
|
if (charsetMatched) {
|
|
2102
2136
|
encoding = charsetMatched[1];
|
|
2137
|
+
encoding = encoding.toLowerCase();
|
|
2103
2138
|
}
|
|
2104
2139
|
}
|
|
2140
|
+
// Failed to construct 'TextDecoder': The encoding label provided ('"UTF-8"') is invalid.
|
|
2141
|
+
// 去除引号
|
|
2142
|
+
encoding = encoding.replace(/('|")/gi, "");
|
|
2143
|
+
// 编码
|
|
2105
2144
|
let textDecoder = new TextDecoder(encoding);
|
|
2106
2145
|
responseText = textDecoder.decode(arrayBuffer);
|
|
2107
2146
|
response = responseText;
|
|
2108
2147
|
|
|
2109
2148
|
if (details.responseType === "arraybuffer") {
|
|
2110
|
-
|
|
2149
|
+
// response返回格式是二进制流
|
|
2150
|
+
response = arrayBuffer;
|
|
2111
2151
|
} else if (details.responseType === "blob") {
|
|
2112
|
-
|
|
2152
|
+
// response返回格式是blob
|
|
2153
|
+
response = new Blob([arrayBuffer]);
|
|
2154
|
+
} else if (
|
|
2155
|
+
details.responseType === "json" ||
|
|
2156
|
+
(typeof fetchResponseType === "string" &&
|
|
2157
|
+
fetchResponseType.includes("application/json"))
|
|
2158
|
+
) {
|
|
2159
|
+
// response返回格式是JSON格式
|
|
2160
|
+
response = Utils.toJSON(responseText);
|
|
2113
2161
|
} else if (
|
|
2114
2162
|
details.responseType === "document" ||
|
|
2115
2163
|
details.responseType == null
|
|
2116
2164
|
) {
|
|
2165
|
+
// response返回格式是文档格式
|
|
2117
2166
|
let parser = new DOMParser();
|
|
2118
|
-
|
|
2119
|
-
responseText,
|
|
2120
|
-
"text/html"
|
|
2121
|
-
);
|
|
2122
|
-
} else if (details.responseType === "json") {
|
|
2123
|
-
(response as any) = Utils.toJSON(responseText);
|
|
2167
|
+
response = parser.parseFromString(responseText, "text/html");
|
|
2124
2168
|
}
|
|
2169
|
+
// 转为XML结构
|
|
2125
2170
|
let parser = new DOMParser();
|
|
2126
|
-
|
|
2127
|
-
responseText,
|
|
2128
|
-
"text/xml"
|
|
2129
|
-
);
|
|
2171
|
+
responseXML = parser.parseFromString(responseText, "text/xml");
|
|
2130
2172
|
|
|
2131
|
-
(httpxResponse
|
|
2132
|
-
(httpxResponse
|
|
2133
|
-
(httpxResponse
|
|
2173
|
+
Reflect.set(httpxResponse, "response", response);
|
|
2174
|
+
Reflect.set(httpxResponse, "responseText", responseText);
|
|
2175
|
+
Reflect.set(httpxResponse, "responseXML", responseXML);
|
|
2134
2176
|
|
|
2177
|
+
// 执行回调
|
|
2135
2178
|
details.onload(httpxResponse);
|
|
2136
2179
|
})
|
|
2137
|
-
.catch((
|
|
2138
|
-
if (
|
|
2180
|
+
.catch((error: any) => {
|
|
2181
|
+
if (error.name === "AbortError") {
|
|
2139
2182
|
return;
|
|
2140
2183
|
}
|
|
2141
2184
|
details.onerror({
|
|
@@ -2146,7 +2189,7 @@ class Httpx {
|
|
|
2146
2189
|
statusText: "",
|
|
2147
2190
|
responseHeaders: "",
|
|
2148
2191
|
responseText: "",
|
|
2149
|
-
error:
|
|
2192
|
+
error: error,
|
|
2150
2193
|
});
|
|
2151
2194
|
});
|
|
2152
2195
|
details.onloadstart({
|
|
@@ -2210,7 +2253,7 @@ class Httpx {
|
|
|
2210
2253
|
constructor(__xmlHttpRequest__?: any) {
|
|
2211
2254
|
if (typeof __xmlHttpRequest__ !== "function") {
|
|
2212
2255
|
console.warn(
|
|
2213
|
-
"Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function
|
|
2256
|
+
"[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch"
|
|
2214
2257
|
);
|
|
2215
2258
|
}
|
|
2216
2259
|
this.interceptors.request.context = this as any;
|
package/src/Log.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { AnyObject } from "./Utils";
|
|
2
1
|
|
|
3
2
|
/** Utils.Log的初始化配置 */
|
|
4
3
|
declare interface UtilsLogOptions {
|
|
@@ -245,7 +244,7 @@ class Log {
|
|
|
245
244
|
* @example
|
|
246
245
|
* log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
|
|
247
246
|
*/
|
|
248
|
-
table(msg:
|
|
247
|
+
table(msg: any[], color = this.#details.infoColor, otherStyle = "") {
|
|
249
248
|
if (this.#disable) return;
|
|
250
249
|
this.checkClearConsole();
|
|
251
250
|
let stack = new Error()!.stack!.split("\n");
|