@whitesev/utils 2.4.1 → 2.4.3
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/LICENSE +674 -0
- package/dist/index.amd.js +66 -58
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +66 -58
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +66 -58
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +66 -58
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +66 -58
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +66 -58
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Httpx.d.ts +4 -4
- package/dist/types/src/types/Httpx.d.ts +7 -0
- package/package.json +1 -1
- package/src/Httpx.ts +92 -83
- package/src/Utils.ts +1 -1
- package/src/types/Httpx.d.ts +7 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { HttpxHookErrorData, HttpxRequestOption, HttpxRequestOptionConfig, HttpxResponse, HttpxResponseData } from "./types/Httpx";
|
|
1
|
+
import type { HttpxHookErrorData, HttpxRequestOption, HttpxRequestOptionConfig, HttpxResponse, HttpxResponseData, HttpxPromise } from "./types/Httpx";
|
|
2
2
|
declare class Httpx {
|
|
3
3
|
#private;
|
|
4
4
|
private GM_Api;
|
|
5
5
|
private HttpxRequestHook;
|
|
6
6
|
private HttpxResponseHook;
|
|
7
|
-
private
|
|
7
|
+
private HttpxRequestOption;
|
|
8
8
|
private HttpxCallBack;
|
|
9
9
|
private HttpxRequest;
|
|
10
10
|
/**
|
|
@@ -30,7 +30,7 @@ declare class Httpx {
|
|
|
30
30
|
* 添加拦截器
|
|
31
31
|
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
32
32
|
*/
|
|
33
|
-
use(fn: <T extends Required<HttpxRequestOption>>(details: T) => void | T): string | undefined;
|
|
33
|
+
use(fn: <T extends Required<HttpxRequestOption>>(details: T) => void | T | Promise<void | T>): string | undefined;
|
|
34
34
|
/**
|
|
35
35
|
* 移除拦截器
|
|
36
36
|
* @param id 通过use返回的id
|
|
@@ -53,7 +53,7 @@ declare class Httpx {
|
|
|
53
53
|
* @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
|
|
54
54
|
* + 超出 2xx 范围的状态码都会触发该函数
|
|
55
55
|
*/
|
|
56
|
-
use(successFn?: <T extends HttpxResponseData<HttpxRequestOption>>(response: T, details: HttpxRequestOption) => void | T, errorFn?: <T extends HttpxHookErrorData>(data: T) => void | T): string | undefined;
|
|
56
|
+
use(successFn?: <T extends HttpxResponseData<HttpxRequestOption>>(response: T, details: HttpxRequestOption) => void | T, errorFn?: <T extends HttpxHookErrorData>(data: T) => void | T | Promise<void | T>): string | undefined;
|
|
57
57
|
/**
|
|
58
58
|
* 移除拦截器
|
|
59
59
|
* @param id 通过use返回的id
|
|
@@ -1314,3 +1314,10 @@ export declare interface HttpxHookErrorData {
|
|
|
1314
1314
|
/** 请求的配置 */
|
|
1315
1315
|
details: HttpxRequestOption;
|
|
1316
1316
|
}
|
|
1317
|
+
|
|
1318
|
+
/**
|
|
1319
|
+
* httpx的自定义Promise,包含额外函数
|
|
1320
|
+
*/
|
|
1321
|
+
export declare interface HttpxPromise<T> extends Promise<T> {
|
|
1322
|
+
abort: () => void;
|
|
1323
|
+
}
|
package/package.json
CHANGED
package/src/Httpx.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
HttpxRequestOptionConfig,
|
|
7
7
|
HttpxResponse,
|
|
8
8
|
HttpxResponseData,
|
|
9
|
+
HttpxPromise,
|
|
9
10
|
} from "./types/Httpx";
|
|
10
11
|
import { Utils } from "./Utils";
|
|
11
12
|
import { GenerateUUID } from "./UtilsCommon";
|
|
@@ -22,7 +23,7 @@ class Httpx {
|
|
|
22
23
|
configList: <
|
|
23
24
|
{
|
|
24
25
|
id: string;
|
|
25
|
-
fn: Function
|
|
26
|
+
fn: Function | Promise<Function>;
|
|
26
27
|
}[]
|
|
27
28
|
>[],
|
|
28
29
|
},
|
|
@@ -32,7 +33,7 @@ class Httpx {
|
|
|
32
33
|
* @param details 当前的请求配置
|
|
33
34
|
* @private
|
|
34
35
|
*/
|
|
35
|
-
beforeRequestCallBack(details: HttpxRequestOption) {
|
|
36
|
+
async beforeRequestCallBack(details: HttpxRequestOption) {
|
|
36
37
|
if (typeof details.allowInterceptConfig === "boolean") {
|
|
37
38
|
if (!details.allowInterceptConfig) {
|
|
38
39
|
// 不允许拦截
|
|
@@ -57,7 +58,7 @@ class Httpx {
|
|
|
57
58
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
58
59
|
let item = this.$config.configList[index];
|
|
59
60
|
if (typeof item.fn === "function") {
|
|
60
|
-
let result = item.fn(details);
|
|
61
|
+
let result = await item.fn(details);
|
|
61
62
|
if (result == null) {
|
|
62
63
|
return;
|
|
63
64
|
}
|
|
@@ -113,8 +114,8 @@ class Httpx {
|
|
|
113
114
|
configList: <
|
|
114
115
|
{
|
|
115
116
|
id: string;
|
|
116
|
-
successFn?: Function
|
|
117
|
-
errorFn?: Function
|
|
117
|
+
successFn?: Function | Promise<Function>;
|
|
118
|
+
errorFn?: Function | Promise<Function>;
|
|
118
119
|
}[]
|
|
119
120
|
>[],
|
|
120
121
|
},
|
|
@@ -123,7 +124,7 @@ class Httpx {
|
|
|
123
124
|
* @param response 响应
|
|
124
125
|
* @param details 请求的配置
|
|
125
126
|
*/
|
|
126
|
-
successResponseCallBack(
|
|
127
|
+
async successResponseCallBack(
|
|
127
128
|
response: HttpxResponseData<HttpxRequestOption>,
|
|
128
129
|
details: HttpxRequestOption
|
|
129
130
|
) {
|
|
@@ -152,7 +153,8 @@ class Httpx {
|
|
|
152
153
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
153
154
|
let item = this.$config.configList[index];
|
|
154
155
|
if (typeof item.successFn === "function") {
|
|
155
|
-
|
|
156
|
+
let result = await item.successFn(response, details);
|
|
157
|
+
if (result == null) {
|
|
156
158
|
return;
|
|
157
159
|
}
|
|
158
160
|
}
|
|
@@ -165,9 +167,7 @@ class Httpx {
|
|
|
165
167
|
* @returns
|
|
166
168
|
* 返回null|undefined就是拦截掉了
|
|
167
169
|
*/
|
|
168
|
-
errorResponseCallBack<T extends HttpxHookErrorData>(
|
|
169
|
-
data: T
|
|
170
|
-
): T | null | undefined {
|
|
170
|
+
async errorResponseCallBack<T extends HttpxHookErrorData>(data: T) {
|
|
171
171
|
if (typeof data.details.allowInterceptConfig === "boolean") {
|
|
172
172
|
if (!data.details.allowInterceptConfig) {
|
|
173
173
|
// 不允许拦截
|
|
@@ -193,7 +193,8 @@ class Httpx {
|
|
|
193
193
|
for (let index = 0; index < this.$config.configList.length; index++) {
|
|
194
194
|
let item = this.$config.configList[index];
|
|
195
195
|
if (typeof item.errorFn === "function") {
|
|
196
|
-
|
|
196
|
+
let result = await item.errorFn(data);
|
|
197
|
+
if (result == null) {
|
|
197
198
|
return;
|
|
198
199
|
}
|
|
199
200
|
}
|
|
@@ -235,7 +236,7 @@ class Httpx {
|
|
|
235
236
|
this.$config.configList = [];
|
|
236
237
|
},
|
|
237
238
|
};
|
|
238
|
-
private
|
|
239
|
+
private HttpxRequestOption = {
|
|
239
240
|
context: this,
|
|
240
241
|
/**
|
|
241
242
|
* 根据传入的参数处理获取details配置
|
|
@@ -669,7 +670,7 @@ class Httpx {
|
|
|
669
670
|
* @param reject 抛出错误
|
|
670
671
|
* @param argsResult 返回的参数列表
|
|
671
672
|
*/
|
|
672
|
-
onAbort(
|
|
673
|
+
async onAbort(
|
|
673
674
|
details: Required<HttpxRequestOption>,
|
|
674
675
|
resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
|
|
675
676
|
reject: (...args: any[]) => void,
|
|
@@ -686,12 +687,12 @@ class Httpx {
|
|
|
686
687
|
response = response[0];
|
|
687
688
|
}
|
|
688
689
|
if (
|
|
689
|
-
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
690
|
+
(await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
690
691
|
type: "onabort",
|
|
691
692
|
error: new TypeError("request canceled"),
|
|
692
693
|
response: null,
|
|
693
694
|
details: details,
|
|
694
|
-
}) == null
|
|
695
|
+
})) == null
|
|
695
696
|
) {
|
|
696
697
|
// reject(new TypeError("response is intercept with onabort"));
|
|
697
698
|
return;
|
|
@@ -705,7 +706,6 @@ class Httpx {
|
|
|
705
706
|
type: "onabort",
|
|
706
707
|
});
|
|
707
708
|
},
|
|
708
|
-
|
|
709
709
|
/**
|
|
710
710
|
* onerror请求异常-触发
|
|
711
711
|
* @param details 配置
|
|
@@ -713,7 +713,7 @@ class Httpx {
|
|
|
713
713
|
* @param reject 抛出错误
|
|
714
714
|
* @param argsResult 返回的参数列表
|
|
715
715
|
*/
|
|
716
|
-
onError(
|
|
716
|
+
async onError(
|
|
717
717
|
details: Required<HttpxRequestOption>,
|
|
718
718
|
resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
|
|
719
719
|
reject: (...args: any[]) => void,
|
|
@@ -730,12 +730,12 @@ class Httpx {
|
|
|
730
730
|
response = response[0];
|
|
731
731
|
}
|
|
732
732
|
if (
|
|
733
|
-
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
733
|
+
(await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
734
734
|
type: "onerror",
|
|
735
735
|
error: new TypeError("request error"),
|
|
736
736
|
response: response,
|
|
737
737
|
details: details,
|
|
738
|
-
}) == null
|
|
738
|
+
})) == null
|
|
739
739
|
) {
|
|
740
740
|
// reject(new TypeError("response is intercept with onerror"));
|
|
741
741
|
return;
|
|
@@ -756,7 +756,7 @@ class Httpx {
|
|
|
756
756
|
* @param reject 抛出错误
|
|
757
757
|
* @param argsResult 返回的参数列表
|
|
758
758
|
*/
|
|
759
|
-
onTimeout(
|
|
759
|
+
async onTimeout(
|
|
760
760
|
details: Required<HttpxRequestOption>,
|
|
761
761
|
resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
|
|
762
762
|
reject: (...args: any[]) => void,
|
|
@@ -773,12 +773,12 @@ class Httpx {
|
|
|
773
773
|
response = response[0];
|
|
774
774
|
}
|
|
775
775
|
if (
|
|
776
|
-
this.context.HttpxResponseHook.errorResponseCallBack({
|
|
776
|
+
(await this.context.HttpxResponseHook.errorResponseCallBack({
|
|
777
777
|
type: "ontimeout",
|
|
778
778
|
error: new TypeError("request timeout"),
|
|
779
779
|
response: (argsResult || [null])[0],
|
|
780
780
|
details: details,
|
|
781
|
-
}) == null
|
|
781
|
+
})) == null
|
|
782
782
|
) {
|
|
783
783
|
// reject(new TypeError("response is intercept with ontimeout"));
|
|
784
784
|
return;
|
|
@@ -813,7 +813,7 @@ class Httpx {
|
|
|
813
813
|
* @param reject 抛出错误
|
|
814
814
|
* @param argsResult 返回的参数列表
|
|
815
815
|
*/
|
|
816
|
-
onLoad(
|
|
816
|
+
async onLoad(
|
|
817
817
|
details: Required<HttpxRequestOption>,
|
|
818
818
|
resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
|
|
819
819
|
reject: (...args: any[]) => void,
|
|
@@ -906,10 +906,10 @@ class Httpx {
|
|
|
906
906
|
/* 状态码2xx都是成功的 */
|
|
907
907
|
if (Math.floor(originResponse.status / 100) === 2) {
|
|
908
908
|
if (
|
|
909
|
-
this.context.HttpxResponseHook.successResponseCallBack(
|
|
909
|
+
(await this.context.HttpxResponseHook.successResponseCallBack(
|
|
910
910
|
originResponse,
|
|
911
911
|
details
|
|
912
|
-
) == null
|
|
912
|
+
)) == null
|
|
913
913
|
) {
|
|
914
914
|
// reject(new TypeError("response is intercept with onloada"));
|
|
915
915
|
return;
|
|
@@ -970,7 +970,7 @@ class Httpx {
|
|
|
970
970
|
* 发送请求
|
|
971
971
|
* @param details
|
|
972
972
|
*/
|
|
973
|
-
request(details: Required<HttpxRequestOption>) {
|
|
973
|
+
async request(details: Required<HttpxRequestOption>) {
|
|
974
974
|
if (this.context.#LOG_DETAILS) {
|
|
975
975
|
console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
|
|
976
976
|
}
|
|
@@ -979,7 +979,7 @@ class Httpx {
|
|
|
979
979
|
"function"
|
|
980
980
|
) {
|
|
981
981
|
let hookResult =
|
|
982
|
-
this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
982
|
+
await this.context.HttpxRequestHook.beforeRequestCallBack(details);
|
|
983
983
|
if (hookResult == null) {
|
|
984
984
|
return;
|
|
985
985
|
}
|
|
@@ -990,7 +990,7 @@ class Httpx {
|
|
|
990
990
|
fetchOption: fetchOption,
|
|
991
991
|
fetchRequestOption: fetchRequestOption,
|
|
992
992
|
abortController,
|
|
993
|
-
} = this.context.
|
|
993
|
+
} = this.context.HttpxRequestOption.handleFetchOption(details);
|
|
994
994
|
return this.fetch(fetchOption, fetchRequestOption, abortController);
|
|
995
995
|
} else {
|
|
996
996
|
// 使用GM_xmlHttpRequest请求
|
|
@@ -1225,7 +1225,7 @@ class Httpx {
|
|
|
1225
1225
|
/**
|
|
1226
1226
|
* 拦截器
|
|
1227
1227
|
*/
|
|
1228
|
-
|
|
1228
|
+
interceptors = {
|
|
1229
1229
|
/**
|
|
1230
1230
|
* 请求拦截器
|
|
1231
1231
|
*/
|
|
@@ -1236,7 +1236,9 @@ class Httpx {
|
|
|
1236
1236
|
* @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
|
|
1237
1237
|
*/
|
|
1238
1238
|
use(
|
|
1239
|
-
fn: <T extends Required<HttpxRequestOption>>(
|
|
1239
|
+
fn: <T extends Required<HttpxRequestOption>>(
|
|
1240
|
+
details: T
|
|
1241
|
+
) => void | T | Promise<void | T>
|
|
1240
1242
|
) {
|
|
1241
1243
|
if (typeof fn !== "function") {
|
|
1242
1244
|
console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
|
|
@@ -1275,7 +1277,9 @@ class Httpx {
|
|
|
1275
1277
|
response: T,
|
|
1276
1278
|
details: HttpxRequestOption
|
|
1277
1279
|
) => void | T,
|
|
1278
|
-
errorFn?: <T extends HttpxHookErrorData>(
|
|
1280
|
+
errorFn?: <T extends HttpxHookErrorData>(
|
|
1281
|
+
data: T
|
|
1282
|
+
) => void | T | Promise<void | T>
|
|
1279
1283
|
) {
|
|
1280
1284
|
if (typeof successFn !== "function" && typeof errorFn !== "function") {
|
|
1281
1285
|
console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
|
|
@@ -1309,14 +1313,14 @@ class Httpx {
|
|
|
1309
1313
|
* GET 请求
|
|
1310
1314
|
* @param url 网址
|
|
1311
1315
|
*/
|
|
1312
|
-
|
|
1316
|
+
get<T extends HttpxRequestOption>(
|
|
1313
1317
|
url: string // @ts-ignore
|
|
1314
1318
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1315
1319
|
/**
|
|
1316
1320
|
* GET 请求
|
|
1317
1321
|
* @param details 配置
|
|
1318
1322
|
*/
|
|
1319
|
-
|
|
1323
|
+
get<T extends HttpxRequestOption>(
|
|
1320
1324
|
details: T // @ts-ignore
|
|
1321
1325
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1322
1326
|
/**
|
|
@@ -1324,7 +1328,7 @@ class Httpx {
|
|
|
1324
1328
|
* @param url 网址
|
|
1325
1329
|
* @param details 配置
|
|
1326
1330
|
*/
|
|
1327
|
-
|
|
1331
|
+
get<T extends HttpxRequestOption>(
|
|
1328
1332
|
url: string,
|
|
1329
1333
|
details: T // @ts-ignore
|
|
1330
1334
|
): HttpxPromise<HttpxResponse<T>>;
|
|
@@ -1333,24 +1337,24 @@ class Httpx {
|
|
|
1333
1337
|
* @param url 网址
|
|
1334
1338
|
* @param details 配置
|
|
1335
1339
|
*/
|
|
1336
|
-
|
|
1340
|
+
get(
|
|
1337
1341
|
...args: (string | HttpxRequestOption)[] // @ts-ignore
|
|
1338
1342
|
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1339
|
-
let userRequestOption = this.
|
|
1343
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
|
|
1340
1344
|
...args
|
|
1341
1345
|
);
|
|
1342
1346
|
let abortFn: Function | null = null;
|
|
1343
1347
|
let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(
|
|
1344
|
-
(resolve, reject) => {
|
|
1345
|
-
let requestOption = this.
|
|
1348
|
+
async (resolve, reject) => {
|
|
1349
|
+
let requestOption = this.HttpxRequestOption.getRequestOption(
|
|
1346
1350
|
"GET",
|
|
1347
1351
|
userRequestOption,
|
|
1348
1352
|
resolve,
|
|
1349
1353
|
reject
|
|
1350
1354
|
);
|
|
1351
1355
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
1352
|
-
this.
|
|
1353
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
1356
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
1357
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
1354
1358
|
if (
|
|
1355
1359
|
requestResult != null &&
|
|
1356
1360
|
typeof requestResult.abort === "function"
|
|
@@ -1365,20 +1369,21 @@ class Httpx {
|
|
|
1365
1369
|
abortFn();
|
|
1366
1370
|
}
|
|
1367
1371
|
};
|
|
1372
|
+
// @ts-ignore
|
|
1368
1373
|
return promise;
|
|
1369
1374
|
}
|
|
1370
1375
|
/**
|
|
1371
1376
|
* POST 请求
|
|
1372
1377
|
* @param details 配置
|
|
1373
1378
|
*/
|
|
1374
|
-
|
|
1379
|
+
post<T extends HttpxRequestOption>(
|
|
1375
1380
|
details: T // @ts-ignore
|
|
1376
1381
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1377
1382
|
/**
|
|
1378
1383
|
* POST 请求
|
|
1379
1384
|
* @param url 网址
|
|
1380
1385
|
*/
|
|
1381
|
-
|
|
1386
|
+
post<T extends HttpxRequestOption>(
|
|
1382
1387
|
url: string
|
|
1383
1388
|
): // @ts-ignore
|
|
1384
1389
|
HttpxPromise<HttpxResponse<T>>;
|
|
@@ -1387,23 +1392,23 @@ class Httpx {
|
|
|
1387
1392
|
* @param url 网址
|
|
1388
1393
|
* @param details 配置
|
|
1389
1394
|
*/
|
|
1390
|
-
|
|
1395
|
+
post<T extends HttpxRequestOption>(
|
|
1391
1396
|
url: string,
|
|
1392
1397
|
details: T // @ts-ignore
|
|
1393
1398
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1394
1399
|
/**
|
|
1395
1400
|
* POST 请求
|
|
1396
1401
|
*/
|
|
1397
|
-
|
|
1402
|
+
post(
|
|
1398
1403
|
...args: (HttpxRequestOption | string)[] // @ts-ignore
|
|
1399
1404
|
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1400
|
-
let userRequestOption = this.
|
|
1405
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
|
|
1401
1406
|
...args
|
|
1402
1407
|
);
|
|
1403
1408
|
let abortFn: Function | null = null;
|
|
1404
1409
|
let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
|
|
1405
|
-
(resolve, reject) => {
|
|
1406
|
-
let requestOption = this.
|
|
1410
|
+
async (resolve, reject) => {
|
|
1411
|
+
let requestOption = this.HttpxRequestOption.getRequestOption(
|
|
1407
1412
|
"POST",
|
|
1408
1413
|
userRequestOption,
|
|
1409
1414
|
resolve,
|
|
@@ -1411,8 +1416,8 @@ class Httpx {
|
|
|
1411
1416
|
);
|
|
1412
1417
|
// @ts-ignore
|
|
1413
1418
|
requestOption =
|
|
1414
|
-
this.
|
|
1415
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
1419
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
1420
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
1416
1421
|
if (
|
|
1417
1422
|
requestResult != null &&
|
|
1418
1423
|
typeof requestResult.abort === "function"
|
|
@@ -1427,20 +1432,21 @@ class Httpx {
|
|
|
1427
1432
|
abortFn();
|
|
1428
1433
|
}
|
|
1429
1434
|
};
|
|
1435
|
+
// @ts-ignore
|
|
1430
1436
|
return promise;
|
|
1431
1437
|
}
|
|
1432
1438
|
/**
|
|
1433
1439
|
* HEAD 请求
|
|
1434
1440
|
* @param details 配置
|
|
1435
1441
|
*/
|
|
1436
|
-
|
|
1442
|
+
head<T extends HttpxRequestOption>(
|
|
1437
1443
|
details: T // @ts-ignore
|
|
1438
1444
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1439
1445
|
/**
|
|
1440
1446
|
* HEAD 请求
|
|
1441
1447
|
* @param url 网址
|
|
1442
1448
|
*/
|
|
1443
|
-
|
|
1449
|
+
head<T extends HttpxRequestOption>(
|
|
1444
1450
|
url: string // @ts-ignore
|
|
1445
1451
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1446
1452
|
/**
|
|
@@ -1448,23 +1454,23 @@ class Httpx {
|
|
|
1448
1454
|
* @param url 网址
|
|
1449
1455
|
* @param details 配置
|
|
1450
1456
|
*/
|
|
1451
|
-
|
|
1457
|
+
head<T extends HttpxRequestOption>(
|
|
1452
1458
|
url: string,
|
|
1453
1459
|
details: T // @ts-ignore
|
|
1454
1460
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1455
1461
|
/**
|
|
1456
1462
|
* HEAD 请求
|
|
1457
1463
|
*/
|
|
1458
|
-
|
|
1464
|
+
head(
|
|
1459
1465
|
...args: (HttpxRequestOption | string)[] // @ts-ignore
|
|
1460
1466
|
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1461
|
-
let userRequestOption = this.
|
|
1467
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
|
|
1462
1468
|
...args
|
|
1463
1469
|
);
|
|
1464
1470
|
let abortFn: Function | null = null;
|
|
1465
1471
|
let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
|
|
1466
|
-
(resolve, reject) => {
|
|
1467
|
-
let requestOption = this.
|
|
1472
|
+
async (resolve, reject) => {
|
|
1473
|
+
let requestOption = this.HttpxRequestOption.getRequestOption(
|
|
1468
1474
|
"HEAD",
|
|
1469
1475
|
userRequestOption,
|
|
1470
1476
|
resolve,
|
|
@@ -1473,8 +1479,8 @@ class Httpx {
|
|
|
1473
1479
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
1474
1480
|
// @ts-ignore
|
|
1475
1481
|
requestOption =
|
|
1476
|
-
this.
|
|
1477
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
1482
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
1483
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
1478
1484
|
if (
|
|
1479
1485
|
requestResult != null &&
|
|
1480
1486
|
typeof requestResult.abort === "function"
|
|
@@ -1490,9 +1496,9 @@ class Httpx {
|
|
|
1490
1496
|
abortFn();
|
|
1491
1497
|
}
|
|
1492
1498
|
};
|
|
1499
|
+
// @ts-ignore
|
|
1493
1500
|
return promise;
|
|
1494
1501
|
}
|
|
1495
|
-
|
|
1496
1502
|
/**
|
|
1497
1503
|
* OPTIONS 请求
|
|
1498
1504
|
* @param details 配置
|
|
@@ -1519,16 +1525,16 @@ class Httpx {
|
|
|
1519
1525
|
/**
|
|
1520
1526
|
* OPTIONS 请求
|
|
1521
1527
|
*/
|
|
1522
|
-
|
|
1528
|
+
options(
|
|
1523
1529
|
...args: (HttpxRequestOption | string)[] // @ts-ignore
|
|
1524
1530
|
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1525
|
-
let userRequestOption = this.
|
|
1531
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
|
|
1526
1532
|
...args
|
|
1527
1533
|
);
|
|
1528
1534
|
let abortFn: Function | null = null;
|
|
1529
1535
|
let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
|
|
1530
|
-
(resolve, reject) => {
|
|
1531
|
-
let requestOption = this.
|
|
1536
|
+
async (resolve, reject) => {
|
|
1537
|
+
let requestOption = this.HttpxRequestOption.getRequestOption(
|
|
1532
1538
|
"OPTIONS",
|
|
1533
1539
|
userRequestOption,
|
|
1534
1540
|
resolve,
|
|
@@ -1537,8 +1543,8 @@ class Httpx {
|
|
|
1537
1543
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
1538
1544
|
// @ts-ignore
|
|
1539
1545
|
requestOption =
|
|
1540
|
-
this.
|
|
1541
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
1546
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
1547
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
1542
1548
|
if (
|
|
1543
1549
|
requestResult != null &&
|
|
1544
1550
|
typeof requestResult.abort === "function"
|
|
@@ -1553,6 +1559,7 @@ class Httpx {
|
|
|
1553
1559
|
abortFn();
|
|
1554
1560
|
}
|
|
1555
1561
|
};
|
|
1562
|
+
// @ts-ignore
|
|
1556
1563
|
return promise;
|
|
1557
1564
|
}
|
|
1558
1565
|
|
|
@@ -1560,14 +1567,14 @@ class Httpx {
|
|
|
1560
1567
|
* DELETE 请求
|
|
1561
1568
|
* @param details 配置
|
|
1562
1569
|
*/
|
|
1563
|
-
|
|
1570
|
+
delete<T extends HttpxRequestOption>(
|
|
1564
1571
|
details: T // @ts-ignore
|
|
1565
1572
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1566
1573
|
/**
|
|
1567
1574
|
* DELETE 请求
|
|
1568
1575
|
* @param url 网址
|
|
1569
1576
|
*/
|
|
1570
|
-
|
|
1577
|
+
delete<T extends HttpxRequestOption>(
|
|
1571
1578
|
url: string // @ts-ignore
|
|
1572
1579
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1573
1580
|
/**
|
|
@@ -1575,23 +1582,23 @@ class Httpx {
|
|
|
1575
1582
|
* @param url 网址
|
|
1576
1583
|
* @param details 配置
|
|
1577
1584
|
*/
|
|
1578
|
-
|
|
1585
|
+
delete<T extends HttpxRequestOption>(
|
|
1579
1586
|
url: string,
|
|
1580
1587
|
details: T // @ts-ignore
|
|
1581
1588
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1582
1589
|
/**
|
|
1583
1590
|
* DELETE 请求
|
|
1584
1591
|
*/
|
|
1585
|
-
|
|
1592
|
+
delete(
|
|
1586
1593
|
...args: (HttpxRequestOption | string)[] // @ts-ignore
|
|
1587
1594
|
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1588
|
-
let userRequestOption = this.
|
|
1595
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
|
|
1589
1596
|
...args
|
|
1590
1597
|
);
|
|
1591
1598
|
let abortFn: Function | null = null;
|
|
1592
1599
|
let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
|
|
1593
|
-
(resolve, reject) => {
|
|
1594
|
-
let requestOption = this.
|
|
1600
|
+
async (resolve, reject) => {
|
|
1601
|
+
let requestOption = this.HttpxRequestOption.getRequestOption(
|
|
1595
1602
|
"DELETE",
|
|
1596
1603
|
userRequestOption,
|
|
1597
1604
|
resolve,
|
|
@@ -1600,8 +1607,8 @@ class Httpx {
|
|
|
1600
1607
|
Reflect.deleteProperty(requestOption, "onprogress");
|
|
1601
1608
|
// @ts-ignore
|
|
1602
1609
|
requestOption =
|
|
1603
|
-
this.
|
|
1604
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
1610
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
1611
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
1605
1612
|
if (
|
|
1606
1613
|
requestResult != null &&
|
|
1607
1614
|
typeof requestResult.abort === "function"
|
|
@@ -1617,6 +1624,7 @@ class Httpx {
|
|
|
1617
1624
|
abortFn();
|
|
1618
1625
|
}
|
|
1619
1626
|
};
|
|
1627
|
+
// @ts-ignore
|
|
1620
1628
|
return promise;
|
|
1621
1629
|
}
|
|
1622
1630
|
|
|
@@ -1624,14 +1632,14 @@ class Httpx {
|
|
|
1624
1632
|
* PUT 请求
|
|
1625
1633
|
* @param details 配置
|
|
1626
1634
|
*/
|
|
1627
|
-
|
|
1635
|
+
put<T extends HttpxRequestOption>(
|
|
1628
1636
|
details: T // @ts-ignore
|
|
1629
1637
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1630
1638
|
/**
|
|
1631
1639
|
* PUT 请求
|
|
1632
1640
|
* @param url 网址
|
|
1633
1641
|
*/
|
|
1634
|
-
|
|
1642
|
+
put<T extends HttpxRequestOption>(
|
|
1635
1643
|
url: string // @ts-ignore
|
|
1636
1644
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1637
1645
|
/**
|
|
@@ -1639,23 +1647,23 @@ class Httpx {
|
|
|
1639
1647
|
* @param url 网址
|
|
1640
1648
|
* @param details 配置
|
|
1641
1649
|
*/
|
|
1642
|
-
|
|
1650
|
+
put<T extends HttpxRequestOption>(
|
|
1643
1651
|
url: string,
|
|
1644
1652
|
details: T // @ts-ignore
|
|
1645
1653
|
): HttpxPromise<HttpxResponse<T>>;
|
|
1646
1654
|
/**
|
|
1647
1655
|
* PUT 请求
|
|
1648
1656
|
*/
|
|
1649
|
-
|
|
1657
|
+
put(
|
|
1650
1658
|
...args: (HttpxRequestOption | string)[] // @ts-ignore
|
|
1651
1659
|
): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
|
|
1652
|
-
let userRequestOption = this.
|
|
1660
|
+
let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
|
|
1653
1661
|
...args
|
|
1654
1662
|
);
|
|
1655
1663
|
let abortFn: Function | null = null;
|
|
1656
1664
|
let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
|
|
1657
|
-
(resolve, reject) => {
|
|
1658
|
-
let requestOption = this.
|
|
1665
|
+
async (resolve, reject) => {
|
|
1666
|
+
let requestOption = this.HttpxRequestOption.getRequestOption(
|
|
1659
1667
|
"PUT",
|
|
1660
1668
|
userRequestOption,
|
|
1661
1669
|
resolve,
|
|
@@ -1663,8 +1671,8 @@ class Httpx {
|
|
|
1663
1671
|
);
|
|
1664
1672
|
// @ts-ignore
|
|
1665
1673
|
requestOption =
|
|
1666
|
-
this.
|
|
1667
|
-
const requestResult = this.HttpxRequest.request(requestOption);
|
|
1674
|
+
this.HttpxRequestOption.removeRequestNullOption(requestOption);
|
|
1675
|
+
const requestResult = await this.HttpxRequest.request(requestOption);
|
|
1668
1676
|
if (
|
|
1669
1677
|
requestResult != null &&
|
|
1670
1678
|
typeof requestResult.abort === "function"
|
|
@@ -1679,6 +1687,7 @@ class Httpx {
|
|
|
1679
1687
|
abortFn();
|
|
1680
1688
|
}
|
|
1681
1689
|
};
|
|
1690
|
+
// @ts-ignore
|
|
1682
1691
|
return promise;
|
|
1683
1692
|
}
|
|
1684
1693
|
}
|
package/src/Utils.ts
CHANGED
package/src/types/Httpx.d.ts
CHANGED
|
@@ -1314,3 +1314,10 @@ export declare interface HttpxHookErrorData {
|
|
|
1314
1314
|
/** 请求的配置 */
|
|
1315
1315
|
details: HttpxRequestOption;
|
|
1316
1316
|
}
|
|
1317
|
+
|
|
1318
|
+
/**
|
|
1319
|
+
* httpx的自定义Promise,包含额外函数
|
|
1320
|
+
*/
|
|
1321
|
+
export declare interface HttpxPromise<T> extends Promise<T> {
|
|
1322
|
+
abort: () => void;
|
|
1323
|
+
}
|