@whitesev/utils 2.7.1 → 2.7.2

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.
Files changed (45) hide show
  1. package/dist/index.amd.js +168 -212
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.cjs.js +168 -212
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.esm.js +168 -212
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/index.iife.js +168 -212
  8. package/dist/index.iife.js.map +1 -1
  9. package/dist/index.system.js +168 -212
  10. package/dist/index.system.js.map +1 -1
  11. package/dist/index.umd.js +168 -212
  12. package/dist/index.umd.js.map +1 -1
  13. package/dist/types/src/ColorConversion.d.ts +3 -8
  14. package/dist/types/src/Dictionary.d.ts +21 -14
  15. package/dist/types/src/GBKEncoder.d.ts +1 -2
  16. package/dist/types/src/Hooks.d.ts +1 -2
  17. package/dist/types/src/Httpx.d.ts +45 -46
  18. package/dist/types/src/LockFunction.d.ts +1 -2
  19. package/dist/types/src/Log.d.ts +1 -2
  20. package/dist/types/src/Progress.d.ts +1 -2
  21. package/dist/types/src/UtilsGMMenu.d.ts +1 -2
  22. package/dist/types/src/WindowApi.d.ts +1 -2
  23. package/dist/types/src/indexedDB.d.ts +1 -2
  24. package/dist/types/src/types/Httpx.d.ts +73 -67
  25. package/dist/types/src/types/env.d.ts +2 -0
  26. package/dist/types/src/types/global.d.ts +3 -0
  27. package/package.json +1 -1
  28. package/src/ColorConversion.ts +14 -25
  29. package/src/DOMUtils.ts +14 -16
  30. package/src/Dictionary.ts +39 -35
  31. package/src/GBKEncoder.ts +8 -12
  32. package/src/Hooks.ts +1 -3
  33. package/src/Httpx.ts +194 -174
  34. package/src/LockFunction.ts +3 -3
  35. package/src/Log.ts +1 -3
  36. package/src/Progress.ts +1 -3
  37. package/src/TryCatch.ts +4 -4
  38. package/src/Utils.ts +27 -43
  39. package/src/UtilsGMMenu.ts +19 -22
  40. package/src/Vue.ts +4 -7
  41. package/src/WindowApi.ts +2 -5
  42. package/src/indexedDB.ts +8 -8
  43. package/src/types/Httpx.d.ts +73 -67
  44. package/src/types/env.d.ts +2 -0
  45. package/src/types/global.d.ts +3 -0
package/src/Httpx.ts CHANGED
@@ -12,7 +12,7 @@ import type {
12
12
  } from "./types/Httpx";
13
13
  import { GenerateUUID } from "./UtilsCommon";
14
14
 
15
- class Httpx {
15
+ export class Httpx {
16
16
  private GM_Api = {
17
17
  xmlHttpRequest: null as any,
18
18
  };
@@ -243,7 +243,9 @@ class Httpx {
243
243
  * 对请求的参数进行合并处理
244
244
  */
245
245
  handleBeforeRequestOptionArgs(...args: (HttpxRequestOption | string)[]) {
246
- let option: HttpxRequestOption = {};
246
+ let option: HttpxRequestOption = {
247
+ url: void 0 as any as string,
248
+ };
247
249
  if (typeof args[0] === "string") {
248
250
  /* 传入的是url,转为配置 */
249
251
  let url = args[0];
@@ -266,7 +268,7 @@ class Httpx {
266
268
  * @param method 当前请求方法,默认get
267
269
  * @param userRequestOption 用户的请求配置
268
270
  * @param resolve promise回调
269
- * @param reject 抛出错误回调
271
+ * @param reject promise抛出错误回调
270
272
  */
271
273
  getRequestOption(
272
274
  method: HttpxMethod,
@@ -354,7 +356,7 @@ class Httpx {
354
356
  userRequestOption.password ||
355
357
  this.context.#defaultRequestOption.password,
356
358
  onabort(...args) {
357
- that.context.HttpxCallBack.onAbort(
359
+ that.context.HttpxResponseCallBack.onAbort(
358
360
  userRequestOption as Required<HttpxRequestOption>,
359
361
  resolve,
360
362
  reject,
@@ -362,7 +364,7 @@ class Httpx {
362
364
  );
363
365
  },
364
366
  onerror(...args) {
365
- that.context.HttpxCallBack.onError(
367
+ that.context.HttpxResponseCallBack.onError(
366
368
  userRequestOption as Required<HttpxRequestOption>,
367
369
  resolve,
368
370
  reject,
@@ -370,25 +372,25 @@ class Httpx {
370
372
  );
371
373
  },
372
374
  onloadstart(...args) {
373
- that.context.HttpxCallBack.onLoadStart(
375
+ that.context.HttpxResponseCallBack.onLoadStart(
374
376
  userRequestOption as Required<HttpxRequestOption>,
375
377
  args
376
378
  );
377
379
  },
378
380
  onprogress(...args) {
379
- that.context.HttpxCallBack.onProgress(
381
+ that.context.HttpxResponseCallBack.onProgress(
380
382
  userRequestOption as Required<HttpxRequestOption>,
381
383
  args
382
384
  );
383
385
  },
384
386
  onreadystatechange(...args) {
385
- that.context.HttpxCallBack.onReadyStateChange(
387
+ that.context.HttpxResponseCallBack.onReadyStateChange(
386
388
  userRequestOption as Required<HttpxRequestOption>,
387
389
  args
388
390
  );
389
391
  },
390
392
  ontimeout(...args) {
391
- that.context.HttpxCallBack.onTimeout(
393
+ that.context.HttpxResponseCallBack.onTimeout(
392
394
  userRequestOption as Required<HttpxRequestOption>,
393
395
  resolve,
394
396
  reject,
@@ -396,7 +398,7 @@ class Httpx {
396
398
  );
397
399
  },
398
400
  onload(...args) {
399
- that.context.HttpxCallBack.onLoad(
401
+ that.context.HttpxResponseCallBack.onLoad(
400
402
  userRequestOption as Required<HttpxRequestOption>,
401
403
  resolve,
402
404
  reject,
@@ -545,8 +547,9 @@ class Httpx {
545
547
  } else if (typeof requestOption.data === "object") {
546
548
  isHandler = true;
547
549
  // URLSearchParams参数可以转普通的string:string,包括FormData
548
- // @ts-ignore
549
- let searchParams = new URLSearchParams(requestOption.data);
550
+ let searchParams = new URLSearchParams(
551
+ requestOption.data as Record<string, string>
552
+ );
550
553
  urlSearch = searchParams.toString();
551
554
  }
552
555
  if (isHandler) {
@@ -600,8 +603,7 @@ class Httpx {
600
603
  // application/x-www-form-urlencoded
601
604
  if (typeof requestOption.data === "object") {
602
605
  requestOption.data = new URLSearchParams(
603
- // @ts-ignore
604
- requestOption.data
606
+ requestOption.data as Record<string, string>
605
607
  ).toString();
606
608
  }
607
609
  } else if (ContentType.includes("multipart/form-data")) {
@@ -621,7 +623,7 @@ class Httpx {
621
623
  },
622
624
  /**
623
625
  * 处理发送请求的配置,去除值为undefined、空function的值
624
- * @param option
626
+ * @param option 请求配置
625
627
  */
626
628
  removeRequestNullOption(
627
629
  option: Required<HttpxRequestOption>
@@ -637,13 +639,13 @@ class Httpx {
637
639
  }
638
640
  });
639
641
  if (CommonUtil.isNull(option.url)) {
640
- throw new TypeError(`Utils.Httpx 参数 url不符合要求: ${option.url}`);
642
+ throw new TypeError(`Utils.Httpx 参数url不能为空:${option.url}`);
641
643
  }
642
644
  return option;
643
645
  },
644
646
  /**
645
647
  * 处理fetch的配置
646
- * @param option
648
+ * @param option 请求配置
647
649
  */
648
650
  handleFetchOption(option: Required<HttpxRequestOption>) {
649
651
  /**
@@ -698,13 +700,13 @@ class Httpx {
698
700
  };
699
701
  },
700
702
  };
701
- private HttpxCallBack = {
703
+ private HttpxResponseCallBack = {
702
704
  context: this,
703
705
  /**
704
706
  * onabort请求被取消-触发
705
707
  * @param details 配置
706
- * @param resolve 回调
707
- * @param reject 抛出错误
708
+ * @param resolve promise回调
709
+ * @param reject promise抛出错误回调
708
710
  * @param argsResult 返回的参数列表
709
711
  */
710
712
  async onAbort(
@@ -714,10 +716,12 @@ class Httpx {
714
716
  argsResult: any
715
717
  ) {
716
718
  // console.log(argsResult);
717
- if ("onabort" in details) {
719
+ if (typeof details?.onabort === "function") {
718
720
  details.onabort.apply(this, argsResult);
719
- } else if ("onabort" in this.context.#defaultRequestOption) {
720
- this.context.#defaultRequestOption!.onabort!.apply(this, argsResult);
721
+ } else if (
722
+ typeof this.context.#defaultRequestOption?.onabort === "function"
723
+ ) {
724
+ this.context.#defaultRequestOption.onabort.apply(this, argsResult);
721
725
  }
722
726
  let response = argsResult;
723
727
  if (response.length) {
@@ -726,12 +730,12 @@ class Httpx {
726
730
  if (
727
731
  (await this.context.HttpxResponseHook.errorResponseCallBack({
728
732
  type: "onabort",
729
- error: new TypeError("request canceled"),
733
+ error: new Error("request canceled"),
730
734
  response: null,
731
735
  details: details,
732
736
  })) == null
733
737
  ) {
734
- // reject(new TypeError("response is intercept with onabort"));
738
+ // reject(new Error("response is intercept with onabort"));
735
739
  return;
736
740
  }
737
741
  resolve({
@@ -744,66 +748,74 @@ class Httpx {
744
748
  });
745
749
  },
746
750
  /**
747
- * onerror请求异常-触发
751
+ * ontimeout请求超时-触发
748
752
  * @param details 配置
749
753
  * @param resolve 回调
750
754
  * @param reject 抛出错误
751
755
  * @param argsResult 返回的参数列表
752
756
  */
753
- async onError(
757
+ async onTimeout(
754
758
  details: Required<HttpxRequestOption>,
755
759
  resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
756
760
  reject: (...args: any[]) => void,
757
761
  argsResult: any
758
762
  ) {
759
763
  // console.log(argsResult);
760
- if ("onerror" in details) {
761
- details.onerror.apply(this, argsResult);
762
- } else if ("onerror" in this.context.#defaultRequestOption) {
763
- this.context.#defaultRequestOption!.onerror!.apply(this, argsResult);
764
+ if (typeof details?.ontimeout === "function") {
765
+ // 执行配置中的ontime回调
766
+ details.ontimeout.apply(this, argsResult);
767
+ } else if (
768
+ typeof this.context.#defaultRequestOption?.ontimeout === "function"
769
+ ) {
770
+ // 执行默认配置的ontime回调
771
+ this.context.#defaultRequestOption.ontimeout.apply(this, argsResult);
764
772
  }
773
+ // 获取响应结果
765
774
  let response = argsResult;
766
775
  if (response.length) {
767
776
  response = response[0];
768
777
  }
778
+ // 执行错误回调的钩子
769
779
  if (
770
780
  (await this.context.HttpxResponseHook.errorResponseCallBack({
771
- type: "onerror",
772
- error: new TypeError("request error"),
781
+ type: "ontimeout",
782
+ error: new Error("request timeout"),
773
783
  response: response,
774
784
  details: details,
775
785
  })) == null
776
786
  ) {
777
- // reject(new TypeError("response is intercept with onerror"));
787
+ // reject(new Error("response is intercept with ontimeout"));
778
788
  return;
779
789
  }
780
790
  resolve({
781
791
  data: response,
782
792
  details: details,
783
- msg: "请求异常",
793
+ msg: "请求超时",
784
794
  status: false,
785
- statusCode: response["status"],
786
- type: "onerror",
795
+ statusCode: 0,
796
+ type: "ontimeout",
787
797
  });
788
798
  },
789
799
  /**
790
- * ontimeout请求超时-触发
800
+ * onerror请求异常-触发
791
801
  * @param details 配置
792
802
  * @param resolve 回调
793
803
  * @param reject 抛出错误
794
804
  * @param argsResult 返回的参数列表
795
805
  */
796
- async onTimeout(
806
+ async onError(
797
807
  details: Required<HttpxRequestOption>,
798
808
  resolve: (resultOption: HttpxResponse<HttpxRequestOption>) => void,
799
809
  reject: (...args: any[]) => void,
800
810
  argsResult: any
801
811
  ) {
802
812
  // console.log(argsResult);
803
- if ("ontimeout" in details) {
804
- details.ontimeout.apply(this, argsResult);
805
- } else if ("ontimeout" in this.context.#defaultRequestOption) {
806
- this.context.#defaultRequestOption!.ontimeout!.apply(this, argsResult);
813
+ if (typeof details?.onerror === "function") {
814
+ details.onerror.apply(this, argsResult);
815
+ } else if (
816
+ typeof this.context.#defaultRequestOption?.onerror === "function"
817
+ ) {
818
+ this.context.#defaultRequestOption.onerror.apply(this, argsResult);
807
819
  }
808
820
  let response = argsResult;
809
821
  if (response.length) {
@@ -811,41 +823,24 @@ class Httpx {
811
823
  }
812
824
  if (
813
825
  (await this.context.HttpxResponseHook.errorResponseCallBack({
814
- type: "ontimeout",
815
- error: new TypeError("request timeout"),
816
- response: (argsResult || [null])[0],
826
+ type: "onerror",
827
+ error: new Error("request error"),
828
+ response: response,
817
829
  details: details,
818
830
  })) == null
819
831
  ) {
820
- // reject(new TypeError("response is intercept with ontimeout"));
832
+ // reject(new Error("response is intercept with onerror"));
821
833
  return;
822
834
  }
823
835
  resolve({
824
836
  data: response,
825
837
  details: details,
826
- msg: "请求超时",
838
+ msg: "请求异常",
827
839
  status: false,
828
- statusCode: 0,
829
- type: "ontimeout",
840
+ statusCode: response["status"],
841
+ type: "onerror",
830
842
  });
831
843
  },
832
-
833
- /**
834
- * onloadstart请求开始-触发
835
- * @param details 配置
836
- * @param argsResult 返回的参数列表
837
- */
838
- onLoadStart(details: Required<HttpxRequestOption>, argsResult: any[]) {
839
- // console.log(argsResult);
840
- if ("onloadstart" in details) {
841
- details.onloadstart.apply(this, argsResult);
842
- } else if ("onloadstart" in this.context.#defaultRequestOption) {
843
- this.context.#defaultRequestOption!.onloadstart!.apply(
844
- this,
845
- argsResult
846
- );
847
- }
848
- },
849
844
  /**
850
845
  * onload加载完毕-触发
851
846
  * @param details 请求的配置
@@ -951,7 +946,7 @@ class Httpx {
951
946
  details
952
947
  )) == null
953
948
  ) {
954
- // reject(new TypeError("response is intercept with onloada"));
949
+ // reject(new Error("response is intercept with onloada"));
955
950
  return;
956
951
  }
957
952
  resolve({
@@ -963,7 +958,7 @@ class Httpx {
963
958
  type: "onload",
964
959
  });
965
960
  } else {
966
- this.context.HttpxCallBack.onError(
961
+ this.context.HttpxResponseCallBack.onError(
967
962
  details,
968
963
  resolve,
969
964
  reject,
@@ -972,16 +967,18 @@ class Httpx {
972
967
  }
973
968
  },
974
969
  /**
975
- * onprogress上传进度-触发
970
+ * onloadstart请求开始-触发
976
971
  * @param details 配置
977
972
  * @param argsResult 返回的参数列表
978
973
  */
979
- onProgress(details: Required<HttpxRequestOption>, argsResult: any[]) {
974
+ onLoadStart(details: Required<HttpxRequestOption>, argsResult: any[]) {
980
975
  // console.log(argsResult);
981
- if ("onprogress" in details) {
982
- details.onprogress.apply(this, argsResult);
983
- } else if ("onprogress" in this.context.#defaultRequestOption) {
984
- this.context.#defaultRequestOption!.onprogress!.apply(this, argsResult);
976
+ if (typeof details?.onloadstart === "function") {
977
+ details.onloadstart.apply(this, argsResult);
978
+ } else if (
979
+ typeof this.context.#defaultRequestOption?.onloadstart === "function"
980
+ ) {
981
+ this.context.#defaultRequestOption.onloadstart.apply(this, argsResult);
985
982
  }
986
983
  },
987
984
  /**
@@ -994,15 +991,33 @@ class Httpx {
994
991
  argsResult: any[]
995
992
  ) {
996
993
  // console.log(argsResult);
997
- if ("onreadystatechange" in details) {
994
+ if (typeof details?.onreadystatechange === "function") {
998
995
  details.onreadystatechange.apply(this, argsResult);
999
- } else if ("onreadystatechange" in this.context.#defaultRequestOption) {
1000
- this.context.#defaultRequestOption!.onreadystatechange!.apply(
996
+ } else if (
997
+ typeof this.context.#defaultRequestOption?.onreadystatechange ===
998
+ "function"
999
+ ) {
1000
+ this.context.#defaultRequestOption.onreadystatechange.apply(
1001
1001
  this,
1002
1002
  argsResult
1003
1003
  );
1004
1004
  }
1005
1005
  },
1006
+ /**
1007
+ * onprogress上传进度-触发
1008
+ * @param details 配置
1009
+ * @param argsResult 返回的参数列表
1010
+ */
1011
+ onProgress(details: Required<HttpxRequestOption>, argsResult: any[]) {
1012
+ // console.log(argsResult);
1013
+ if (typeof details?.onprogress === "function") {
1014
+ details.onprogress.apply(this, argsResult);
1015
+ } else if (
1016
+ typeof this.context.#defaultRequestOption?.onprogress === "function"
1017
+ ) {
1018
+ this.context.#defaultRequestOption.onprogress.apply(this, argsResult);
1019
+ }
1020
+ },
1006
1021
  };
1007
1022
  private HttpxRequest = {
1008
1023
  context: this,
@@ -1064,15 +1079,12 @@ class Httpx {
1064
1079
  isFetch: true,
1065
1080
  finalUrl: fetchResponse.url,
1066
1081
  readyState: 4,
1067
- // @ts-ignore
1068
1082
  status: fetchResponse.status,
1069
1083
  statusText: fetchResponse.statusText,
1070
- // @ts-ignore
1071
- response: void 0,
1084
+ response: "",
1072
1085
  responseFetchHeaders: fetchResponse.headers,
1073
1086
  responseHeaders: "",
1074
- // @ts-ignore
1075
- responseText: void 0,
1087
+ responseText: "",
1076
1088
  responseType: option.responseType,
1077
1089
  responseXML: void 0,
1078
1090
  };
@@ -1105,7 +1117,7 @@ class Httpx {
1105
1117
  /** 响应 */
1106
1118
  let response: any = "";
1107
1119
  /** 响应字符串 */
1108
- let responseText = "";
1120
+ let responseText: string = "";
1109
1121
  /** 响应xml文档 */
1110
1122
  let responseXML: XMLDocument | string = "";
1111
1123
  /** 先获取二进制数据 */
@@ -1155,9 +1167,9 @@ class Httpx {
1155
1167
  let parser = new DOMParser();
1156
1168
  responseXML = parser.parseFromString(responseText, "text/xml");
1157
1169
 
1158
- Reflect.set(httpxResponse, "response", response);
1159
- Reflect.set(httpxResponse, "responseText", responseText);
1160
- Reflect.set(httpxResponse, "responseXML", responseXML);
1170
+ httpxResponse.response = response;
1171
+ httpxResponse.responseText = responseText;
1172
+ httpxResponse.responseXML = responseXML;
1161
1173
 
1162
1174
  // 执行回调
1163
1175
  option.onload(httpxResponse);
@@ -1197,7 +1209,7 @@ class Httpx {
1197
1209
  * 默认配置
1198
1210
  */
1199
1211
  #defaultRequestOption = <HttpxRequestOption>{
1200
- url: void 0,
1212
+ url: void 0 as undefined | string,
1201
1213
  timeout: 5000,
1202
1214
  async: false,
1203
1215
  responseType: void 0,
@@ -1357,13 +1369,6 @@ class Httpx {
1357
1369
  setXMLHttpRequest(httpRequest: Function) {
1358
1370
  this.GM_Api.xmlHttpRequest = httpRequest;
1359
1371
  }
1360
- /**
1361
- * GET 请求
1362
- * @param url 网址
1363
- */
1364
- get<T extends HttpxRequestOption>(
1365
- url: string
1366
- ): HttpxPromise<HttpxResponse<T>>;
1367
1372
  /**
1368
1373
  * GET 请求
1369
1374
  * @param details 配置
@@ -1371,16 +1376,25 @@ class Httpx {
1371
1376
  get<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1372
1377
  /**
1373
1378
  * GET 请求
1374
- * @param url 网址
1379
+ * @param url 请求的url
1375
1380
  * @param details 配置
1376
1381
  */
1377
- get<T extends HttpxRequestOption>(
1382
+ get<T extends Omit<HttpxRequestOption, "url">>(
1378
1383
  url: string,
1379
- details: T
1380
- ): HttpxPromise<HttpxResponse<T>>;
1384
+ details?: T
1385
+ ): HttpxPromise<
1386
+ HttpxResponse<
1387
+ T & {
1388
+ /**
1389
+ * 请求的url
1390
+ */
1391
+ url: string;
1392
+ }
1393
+ >
1394
+ >;
1381
1395
  /**
1382
1396
  * GET 请求
1383
- * @param url 网址
1397
+ * @param url 请求的url
1384
1398
  * @param details 配置
1385
1399
  */
1386
1400
  get(
@@ -1398,24 +1412,26 @@ class Httpx {
1398
1412
  * @param details 配置
1399
1413
  */
1400
1414
  post<T extends HttpxRequestOption>(
1401
- details: T
1415
+ details?: T
1402
1416
  ): HttpxPromise<HttpxResponse<T>>;
1403
1417
  /**
1404
1418
  * POST 请求
1405
- * @param url 网址
1406
- */
1407
- post<T extends HttpxRequestOption>(
1408
- url: string
1409
- ): HttpxPromise<HttpxResponse<T>>;
1410
- /**
1411
- * POST 请求
1412
- * @param url 网址
1419
+ * @param url 请求的url
1413
1420
  * @param details 配置
1414
1421
  */
1415
- post<T extends HttpxRequestOption>(
1422
+ post<T extends Omit<HttpxRequestOption, "url">>(
1416
1423
  url: string,
1417
- details: T
1418
- ): HttpxPromise<HttpxResponse<T>>;
1424
+ details?: T
1425
+ ): HttpxPromise<
1426
+ HttpxResponse<
1427
+ T & {
1428
+ /**
1429
+ * 请求的url
1430
+ */
1431
+ url: string;
1432
+ }
1433
+ >
1434
+ >;
1419
1435
  /**
1420
1436
  * POST 请求
1421
1437
  */
@@ -1436,20 +1452,22 @@ class Httpx {
1436
1452
  ): HttpxPromise<HttpxResponse<T>>;
1437
1453
  /**
1438
1454
  * HEAD 请求
1439
- * @param url 网址
1440
- */
1441
- head<T extends HttpxRequestOption>(
1442
- url: string
1443
- ): HttpxPromise<HttpxResponse<T>>;
1444
- /**
1445
- * HEAD 请求
1446
- * @param url 网址
1455
+ * @param url 请求的url
1447
1456
  * @param details 配置
1448
1457
  */
1449
- head<T extends HttpxRequestOption>(
1458
+ head<T extends Omit<HttpxRequestOption, "url">>(
1450
1459
  url: string,
1451
- details: T
1452
- ): HttpxPromise<HttpxResponse<T>>;
1460
+ details?: T
1461
+ ): HttpxPromise<
1462
+ HttpxResponse<
1463
+ T & {
1464
+ /**
1465
+ * 请求的url
1466
+ */
1467
+ url: string;
1468
+ }
1469
+ >
1470
+ >;
1453
1471
  /**
1454
1472
  * HEAD 请求
1455
1473
  */
@@ -1472,20 +1490,22 @@ class Httpx {
1472
1490
  ): HttpxPromise<HttpxResponse<T>>;
1473
1491
  /**
1474
1492
  * OPTIONS 请求
1475
- * @param url 网址
1476
- */
1477
- options<T extends HttpxRequestOption>(
1478
- url: string
1479
- ): HttpxPromise<HttpxResponse<T>>;
1480
- /**
1481
- * OPTIONS 请求
1482
- * @param url 网址
1493
+ * @param url 请求的url
1483
1494
  * @param details 配置
1484
1495
  */
1485
- options<T extends HttpxRequestOption>(
1496
+ options<T extends Omit<HttpxRequestOption, "url">>(
1486
1497
  url: string,
1487
- details: T
1488
- ): HttpxPromise<HttpxResponse<T>>;
1498
+ details?: T
1499
+ ): HttpxPromise<
1500
+ HttpxResponse<
1501
+ T & {
1502
+ /**
1503
+ * 请求的url
1504
+ */
1505
+ url: string;
1506
+ }
1507
+ >
1508
+ >;
1489
1509
  /**
1490
1510
  * OPTIONS 请求
1491
1511
  */
@@ -1499,7 +1519,6 @@ class Httpx {
1499
1519
  Reflect.deleteProperty(option, "onprogress");
1500
1520
  });
1501
1521
  }
1502
-
1503
1522
  /**
1504
1523
  * DELETE 请求
1505
1524
  * @param details 配置
@@ -1509,20 +1528,22 @@ class Httpx {
1509
1528
  ): HttpxPromise<HttpxResponse<T>>;
1510
1529
  /**
1511
1530
  * DELETE 请求
1512
- * @param url 网址
1513
- */
1514
- delete<T extends HttpxRequestOption>(
1515
- url: string
1516
- ): HttpxPromise<HttpxResponse<T>>;
1517
- /**
1518
- * DELETE 请求
1519
- * @param url 网址
1531
+ * @param url 请求的url
1520
1532
  * @param details 配置
1521
1533
  */
1522
- delete<T extends HttpxRequestOption>(
1534
+ delete<T extends Omit<HttpxRequestOption, "url">>(
1523
1535
  url: string,
1524
- details: T
1525
- ): HttpxPromise<HttpxResponse<T>>;
1536
+ details?: T
1537
+ ): HttpxPromise<
1538
+ HttpxResponse<
1539
+ T & {
1540
+ /**
1541
+ * 请求的url
1542
+ */
1543
+ url: string;
1544
+ }
1545
+ >
1546
+ >;
1526
1547
  /**
1527
1548
  * DELETE 请求
1528
1549
  */
@@ -1536,7 +1557,6 @@ class Httpx {
1536
1557
  Reflect.deleteProperty(option, "onprogress");
1537
1558
  });
1538
1559
  }
1539
-
1540
1560
  /**
1541
1561
  * PUT 请求
1542
1562
  * @param details 配置
@@ -1544,20 +1564,22 @@ class Httpx {
1544
1564
  put<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1545
1565
  /**
1546
1566
  * PUT 请求
1547
- * @param url 网址
1548
- */
1549
- put<T extends HttpxRequestOption>(
1550
- url: string
1551
- ): HttpxPromise<HttpxResponse<T>>;
1552
- /**
1553
- * PUT 请求
1554
- * @param url 网址
1567
+ * @param url 请求的url
1555
1568
  * @param details 配置
1556
1569
  */
1557
- put<T extends HttpxRequestOption>(
1570
+ put<T extends Omit<HttpxRequestOption, "url">>(
1558
1571
  url: string,
1559
- details: T
1560
- ): HttpxPromise<HttpxResponse<T>>;
1572
+ details?: T
1573
+ ): HttpxPromise<
1574
+ HttpxResponse<
1575
+ T & {
1576
+ /**
1577
+ * 请求的url
1578
+ */
1579
+ url: string;
1580
+ }
1581
+ >
1582
+ >;
1561
1583
  /**
1562
1584
  * PUT 请求
1563
1585
  */
@@ -1569,7 +1591,6 @@ class Httpx {
1569
1591
  userRequestOption.method = "PUT";
1570
1592
  return this.request(userRequestOption);
1571
1593
  }
1572
-
1573
1594
  /**
1574
1595
  * 发送请求
1575
1596
  * @param details 配置
@@ -1585,20 +1606,23 @@ class Httpx {
1585
1606
  let abortFn: Function | null = null;
1586
1607
  let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(
1587
1608
  async (resolve, reject) => {
1588
- let requestOption = this.HttpxRequestOption.getRequestOption(
1589
- useRequestOption.method!,
1590
- useRequestOption,
1591
- resolve,
1592
- reject
1609
+ let requestOption = <Required<T>>(
1610
+ this.HttpxRequestOption.getRequestOption(
1611
+ useRequestOption.method!,
1612
+ useRequestOption,
1613
+ resolve,
1614
+ reject
1615
+ )
1593
1616
  );
1594
1617
  if (typeof beforeRequestOption === "function") {
1595
- // @ts-ignore
1596
1618
  beforeRequestOption(requestOption);
1597
1619
  }
1598
- // @ts-ignore
1599
- requestOption =
1600
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
1601
- const requestResult = await this.HttpxRequest.request(requestOption);
1620
+ requestOption = this.HttpxRequestOption.removeRequestNullOption(
1621
+ <Required<HttpxRequestOption>>requestOption
1622
+ ) as Required<T>;
1623
+ const requestResult = await this.HttpxRequest.request(
1624
+ <Required<HttpxRequestOption>>requestOption
1625
+ );
1602
1626
  if (
1603
1627
  requestResult != null &&
1604
1628
  typeof requestResult.abort === "function"
@@ -1606,16 +1630,12 @@ class Httpx {
1606
1630
  abortFn = requestResult.abort;
1607
1631
  }
1608
1632
  }
1609
- );
1610
- // @ts-ignore
1633
+ ) as HttpxPromise<HttpxResponse<T>>;
1611
1634
  promise.abort = () => {
1612
1635
  if (typeof abortFn === "function") {
1613
1636
  abortFn();
1614
1637
  }
1615
1638
  };
1616
- // @ts-ignore
1617
1639
  return promise;
1618
1640
  }
1619
1641
  }
1620
-
1621
- export { Httpx };