@whitesev/utils 2.6.4 → 2.6.6

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/src/Httpx.ts CHANGED
@@ -3,10 +3,10 @@ import type {
3
3
  HttpxHookErrorData,
4
4
  HttpxMethod,
5
5
  HttpxRequestOption,
6
- HttpxRequestOptionConfig,
7
6
  HttpxResponse,
8
7
  HttpxResponseData,
9
8
  HttpxPromise,
9
+ HttpxInitOption,
10
10
  } from "./types/Httpx";
11
11
  import { Utils } from "./Utils";
12
12
  import { GenerateUUID } from "./UtilsCommon";
@@ -239,23 +239,24 @@ class Httpx {
239
239
  private HttpxRequestOption = {
240
240
  context: this,
241
241
  /**
242
- * 根据传入的参数处理获取details配置
242
+ * 对请求的参数进行合并处理
243
243
  */
244
- handleBeforeRequestOption(...args: (HttpxRequestOption | string)[]) {
244
+ handleBeforeRequestOptionArgs(...args: (HttpxRequestOption | string)[]) {
245
245
  let option: HttpxRequestOption = {};
246
246
  if (typeof args[0] === "string") {
247
- /* 传入的是url,details? */
247
+ /* 传入的是url,转为配置 */
248
248
  let url = args[0];
249
249
  option.url = url;
250
250
  if (typeof args[1] === "object") {
251
251
  /* 处理第二个参数details */
252
- let details = args[1];
253
- option = details;
252
+ let optionArg = args[1];
253
+ Utils.assign(option, optionArg, true);
254
254
  option.url = url;
255
255
  }
256
256
  } else {
257
- /* 传入的是details */
258
- option = args[0];
257
+ /* 传入的是配置 */
258
+ let optionArg = args[0];
259
+ Utils.assign(option, optionArg, true);
259
260
  }
260
261
  return option;
261
262
  },
@@ -273,58 +274,82 @@ class Httpx {
273
274
  reject: (...args: any[]) => void
274
275
  ) {
275
276
  let that = this;
277
+ let url = userRequestOption.url || this.context.#defaultRequestOption.url;
278
+ if (typeof url === "string") {
279
+ // 去除左右空格
280
+ url = url.trim();
281
+ if (url.startsWith("http://") || url.startsWith("https://")) {
282
+ // 标准的http请求
283
+ } else {
284
+ if (typeof this.context.#defaultInitOption.baseURL === "string") {
285
+ // 设置了基础域
286
+ url = this.context.#defaultInitOption.baseURL + url;
287
+ }
288
+ }
289
+ }
276
290
  let requestOption = <Required<HttpxRequestOption>>{
277
- url: userRequestOption.url || this.context.#defaultDetails.url,
291
+ url: url,
278
292
  method: (method || "GET").toString().toUpperCase().trim(),
279
293
  timeout:
280
- userRequestOption.timeout || this.context.#defaultDetails.timeout,
294
+ userRequestOption.timeout ||
295
+ this.context.#defaultRequestOption.timeout,
281
296
  responseType:
282
297
  userRequestOption.responseType ||
283
- this.context.#defaultDetails.responseType,
298
+ this.context.#defaultRequestOption.responseType,
284
299
  /* 对象使用深拷贝 */
285
- headers: Utils.deepClone(this.context.#defaultDetails.headers),
286
- data: userRequestOption.data || this.context.#defaultDetails.data,
300
+ headers: Utils.deepClone(this.context.#defaultRequestOption.headers),
301
+ data: userRequestOption.data || this.context.#defaultRequestOption.data,
287
302
  redirect:
288
- userRequestOption.redirect || this.context.#defaultDetails.redirect,
289
- cookie: userRequestOption.cookie || this.context.#defaultDetails.cookie,
303
+ userRequestOption.redirect ||
304
+ this.context.#defaultRequestOption.redirect,
305
+ cookie:
306
+ userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
290
307
  cookiePartition:
291
308
  userRequestOption.cookiePartition ||
292
- this.context.#defaultDetails.cookiePartition,
293
- binary: userRequestOption.binary || this.context.#defaultDetails.binary,
309
+ this.context.#defaultRequestOption.cookiePartition,
310
+ binary:
311
+ userRequestOption.binary || this.context.#defaultRequestOption.binary,
294
312
  nocache:
295
- userRequestOption.nocache || this.context.#defaultDetails.nocache,
313
+ userRequestOption.nocache ||
314
+ this.context.#defaultRequestOption.nocache,
296
315
  revalidate:
297
316
  userRequestOption.revalidate ||
298
- this.context.#defaultDetails.revalidate,
317
+ this.context.#defaultRequestOption.revalidate,
299
318
  /* 对象使用深拷贝 */
300
319
  context: Utils.deepClone(
301
- userRequestOption.context || this.context.#defaultDetails.context
320
+ userRequestOption.context ||
321
+ this.context.#defaultRequestOption.context
302
322
  ),
303
323
  overrideMimeType:
304
324
  userRequestOption.overrideMimeType ||
305
- this.context.#defaultDetails.overrideMimeType,
325
+ this.context.#defaultRequestOption.overrideMimeType,
306
326
  anonymous:
307
- userRequestOption.anonymous || this.context.#defaultDetails.anonymous,
308
- fetch: userRequestOption.fetch || this.context.#defaultDetails.fetch,
327
+ userRequestOption.anonymous ||
328
+ this.context.#defaultRequestOption.anonymous,
329
+ fetch:
330
+ userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
309
331
  /* 对象使用深拷贝 */
310
- fetchInit: Utils.deepClone(this.context.#defaultDetails.fetchInit),
332
+ fetchInit: Utils.deepClone(
333
+ this.context.#defaultRequestOption.fetchInit
334
+ ),
311
335
  allowInterceptConfig: {
312
336
  beforeRequest: (
313
- this.context.#defaultDetails
337
+ this.context.#defaultRequestOption
314
338
  .allowInterceptConfig as HttpxAllowInterceptConfig
315
339
  ).beforeRequest,
316
340
  afterResponseSuccess: (
317
- this.context.#defaultDetails
341
+ this.context.#defaultRequestOption
318
342
  .allowInterceptConfig as HttpxAllowInterceptConfig
319
343
  ).afterResponseSuccess,
320
344
  afterResponseError: (
321
- this.context.#defaultDetails
345
+ this.context.#defaultRequestOption
322
346
  .allowInterceptConfig as HttpxAllowInterceptConfig
323
347
  ).afterResponseError,
324
348
  },
325
- user: userRequestOption.user || this.context.#defaultDetails.user,
349
+ user: userRequestOption.user || this.context.#defaultRequestOption.user,
326
350
  password:
327
- userRequestOption.password || this.context.#defaultDetails.password,
351
+ userRequestOption.password ||
352
+ this.context.#defaultRequestOption.password,
328
353
  onabort(...args) {
329
354
  that.context.HttpxCallBack.onAbort(
330
355
  userRequestOption as Required<HttpxRequestOption>,
@@ -688,8 +713,8 @@ class Httpx {
688
713
  // console.log(argsResult);
689
714
  if ("onabort" in details) {
690
715
  details.onabort.apply(this, argsResult);
691
- } else if ("onabort" in this.context.#defaultDetails) {
692
- this.context.#defaultDetails!.onabort!.apply(this, argsResult);
716
+ } else if ("onabort" in this.context.#defaultRequestOption) {
717
+ this.context.#defaultRequestOption!.onabort!.apply(this, argsResult);
693
718
  }
694
719
  let response = argsResult;
695
720
  if (response.length) {
@@ -731,8 +756,8 @@ class Httpx {
731
756
  // console.log(argsResult);
732
757
  if ("onerror" in details) {
733
758
  details.onerror.apply(this, argsResult);
734
- } else if ("onerror" in this.context.#defaultDetails) {
735
- this.context.#defaultDetails!.onerror!.apply(this, argsResult);
759
+ } else if ("onerror" in this.context.#defaultRequestOption) {
760
+ this.context.#defaultRequestOption!.onerror!.apply(this, argsResult);
736
761
  }
737
762
  let response = argsResult;
738
763
  if (response.length) {
@@ -774,8 +799,8 @@ class Httpx {
774
799
  // console.log(argsResult);
775
800
  if ("ontimeout" in details) {
776
801
  details.ontimeout.apply(this, argsResult);
777
- } else if ("ontimeout" in this.context.#defaultDetails) {
778
- this.context.#defaultDetails!.ontimeout!.apply(this, argsResult);
802
+ } else if ("ontimeout" in this.context.#defaultRequestOption) {
803
+ this.context.#defaultRequestOption!.ontimeout!.apply(this, argsResult);
779
804
  }
780
805
  let response = argsResult;
781
806
  if (response.length) {
@@ -811,8 +836,11 @@ class Httpx {
811
836
  // console.log(argsResult);
812
837
  if ("onloadstart" in details) {
813
838
  details.onloadstart.apply(this, argsResult);
814
- } else if ("onloadstart" in this.context.#defaultDetails) {
815
- this.context.#defaultDetails!.onloadstart!.apply(this, argsResult);
839
+ } else if ("onloadstart" in this.context.#defaultRequestOption) {
840
+ this.context.#defaultRequestOption!.onloadstart!.apply(
841
+ this,
842
+ argsResult
843
+ );
816
844
  }
817
845
  },
818
846
  /**
@@ -949,8 +977,8 @@ class Httpx {
949
977
  // console.log(argsResult);
950
978
  if ("onprogress" in details) {
951
979
  details.onprogress.apply(this, argsResult);
952
- } else if ("onprogress" in this.context.#defaultDetails) {
953
- this.context.#defaultDetails!.onprogress!.apply(this, argsResult);
980
+ } else if ("onprogress" in this.context.#defaultRequestOption) {
981
+ this.context.#defaultRequestOption!.onprogress!.apply(this, argsResult);
954
982
  }
955
983
  },
956
984
  /**
@@ -965,8 +993,8 @@ class Httpx {
965
993
  // console.log(argsResult);
966
994
  if ("onreadystatechange" in details) {
967
995
  details.onreadystatechange.apply(this, argsResult);
968
- } else if ("onreadystatechange" in this.context.#defaultDetails) {
969
- this.context.#defaultDetails!.onreadystatechange!.apply(
996
+ } else if ("onreadystatechange" in this.context.#defaultRequestOption) {
997
+ this.context.#defaultRequestOption!.onreadystatechange!.apply(
970
998
  this,
971
999
  argsResult
972
1000
  );
@@ -980,7 +1008,7 @@ class Httpx {
980
1008
  * @param details
981
1009
  */
982
1010
  async request(details: Required<HttpxRequestOption>) {
983
- if (this.context.#LOG_DETAILS) {
1011
+ if (this.context.#defaultInitOption.logDetails) {
984
1012
  console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
985
1013
  }
986
1014
  if (
@@ -1165,7 +1193,7 @@ class Httpx {
1165
1193
  /**
1166
1194
  * 默认配置
1167
1195
  */
1168
- #defaultDetails = <HttpxRequestOption>{
1196
+ #defaultRequestOption = <HttpxRequestOption>{
1169
1197
  url: void 0,
1170
1198
  timeout: 5000,
1171
1199
  async: false,
@@ -1197,39 +1225,44 @@ class Httpx {
1197
1225
  onreadystatechange() {},
1198
1226
  onprogress() {},
1199
1227
  };
1228
+ #defaultInitOption = {
1229
+ /**
1230
+ * `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。
1231
+ */
1232
+ baseURL: void 0 as undefined | string,
1233
+ /**
1234
+ * 当前使用请求时,输出请求的配置,一般用于DEBUG|DEV
1235
+ */
1236
+ logDetails: false,
1237
+ };
1200
1238
  /**
1201
- * 当前使用请求时,输出请求的配置
1202
- */
1203
- #LOG_DETAILS = false;
1204
- /**
1205
- * 实例化,可传入GM_xmlhttpRequest,未传入则使用window.fetch
1206
- * @param xmlHttpRequest
1239
+ * 实例化
1240
+ * @param option 初始化配置
1207
1241
  */
1208
- constructor(xmlHttpRequest?: Function) {
1209
- if (typeof xmlHttpRequest !== "function") {
1242
+ constructor(option: Partial<HttpxInitOption> = {}) {
1243
+ if (typeof option.xmlHttpRequest !== "function") {
1210
1244
  console.warn(
1211
1245
  "[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch"
1212
1246
  );
1213
1247
  }
1248
+ Utils.coverObjectFunctionThis(this);
1214
1249
  this.interceptors.request.context = this;
1215
1250
  this.interceptors.response.context = this;
1216
- this.GM_Api.xmlHttpRequest = xmlHttpRequest;
1251
+ this.config(option);
1217
1252
  }
1218
-
1219
- /**
1220
- * 覆盖全局配置
1221
- * @param details 配置
1222
- */
1223
- config(details?: Partial<HttpxRequestOptionConfig>): void;
1224
1253
  /**
1225
1254
  * 覆盖当前配置
1226
- * @param details
1255
+ * @param option
1227
1256
  */
1228
- config(details: HttpxRequestOptionConfig = {}) {
1229
- if ("logDetails" in details && typeof details["logDetails"] === "boolean") {
1230
- this.#LOG_DETAILS = details["logDetails"];
1257
+ config(option: Partial<HttpxInitOption> = {}) {
1258
+ if (typeof option.xmlHttpRequest === "function") {
1259
+ this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
1231
1260
  }
1232
- this.#defaultDetails = Utils.assign(this.#defaultDetails, details);
1261
+ this.#defaultRequestOption = Utils.assign(
1262
+ this.#defaultRequestOption,
1263
+ option
1264
+ );
1265
+ this.#defaultInitOption = Utils.assign(this.#defaultInitOption, option);
1233
1266
  }
1234
1267
  /**
1235
1268
  * 拦截器
@@ -1323,15 +1356,13 @@ class Httpx {
1323
1356
  * @param url 网址
1324
1357
  */
1325
1358
  get<T extends HttpxRequestOption>(
1326
- url: string // @ts-ignore
1359
+ url: string
1327
1360
  ): HttpxPromise<HttpxResponse<T>>;
1328
1361
  /**
1329
1362
  * GET 请求
1330
1363
  * @param details 配置
1331
1364
  */
1332
- get<T extends HttpxRequestOption>(
1333
- details: T // @ts-ignore
1334
- ): HttpxPromise<HttpxResponse<T>>;
1365
+ get<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1335
1366
  /**
1336
1367
  * GET 请求
1337
1368
  * @param url 网址
@@ -1339,7 +1370,7 @@ class Httpx {
1339
1370
  */
1340
1371
  get<T extends HttpxRequestOption>(
1341
1372
  url: string,
1342
- details: T // @ts-ignore
1373
+ details: T
1343
1374
  ): HttpxPromise<HttpxResponse<T>>;
1344
1375
  /**
1345
1376
  * GET 请求
@@ -1347,46 +1378,21 @@ class Httpx {
1347
1378
  * @param details 配置
1348
1379
  */
1349
1380
  get(
1350
- ...args: (string | HttpxRequestOption)[] // @ts-ignore
1381
+ ...args: (string | HttpxRequestOption)[]
1351
1382
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1352
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1353
- ...args
1354
- );
1355
- let abortFn: Function | null = null;
1356
- let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(
1357
- async (resolve, reject) => {
1358
- let requestOption = this.HttpxRequestOption.getRequestOption(
1359
- "GET",
1360
- userRequestOption,
1361
- resolve,
1362
- reject
1363
- );
1364
- Reflect.deleteProperty(requestOption, "onprogress");
1365
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
1366
- const requestResult = await this.HttpxRequest.request(requestOption);
1367
- if (
1368
- requestResult != null &&
1369
- typeof requestResult.abort === "function"
1370
- ) {
1371
- abortFn = requestResult.abort;
1372
- }
1373
- }
1374
- );
1375
- // @ts-ignore
1376
- promise.abort = () => {
1377
- if (typeof abortFn === "function") {
1378
- abortFn();
1379
- }
1380
- };
1381
- // @ts-ignore
1382
- return promise;
1383
+ let useRequestOption =
1384
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1385
+ useRequestOption.method = "GET";
1386
+ return this.request(useRequestOption, (option) => {
1387
+ Reflect.deleteProperty(option, "onprogress");
1388
+ });
1383
1389
  }
1384
1390
  /**
1385
1391
  * POST 请求
1386
1392
  * @param details 配置
1387
1393
  */
1388
1394
  post<T extends HttpxRequestOption>(
1389
- details: T // @ts-ignore
1395
+ details: T
1390
1396
  ): HttpxPromise<HttpxResponse<T>>;
1391
1397
  /**
1392
1398
  * POST 请求
@@ -1394,8 +1400,7 @@ class Httpx {
1394
1400
  */
1395
1401
  post<T extends HttpxRequestOption>(
1396
1402
  url: string
1397
- ): // @ts-ignore
1398
- HttpxPromise<HttpxResponse<T>>;
1403
+ ): HttpxPromise<HttpxResponse<T>>;
1399
1404
  /**
1400
1405
  * POST 请求
1401
1406
  * @param url 网址
@@ -1403,60 +1408,32 @@ class Httpx {
1403
1408
  */
1404
1409
  post<T extends HttpxRequestOption>(
1405
1410
  url: string,
1406
- details: T // @ts-ignore
1411
+ details: T
1407
1412
  ): HttpxPromise<HttpxResponse<T>>;
1408
1413
  /**
1409
1414
  * POST 请求
1410
1415
  */
1411
1416
  post(
1412
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1417
+ ...args: (HttpxRequestOption | string)[]
1413
1418
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1414
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1415
- ...args
1416
- );
1417
- let abortFn: Function | null = null;
1418
- let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
1419
- async (resolve, reject) => {
1420
- let requestOption = this.HttpxRequestOption.getRequestOption(
1421
- "POST",
1422
- userRequestOption,
1423
- resolve,
1424
- reject
1425
- );
1426
- // @ts-ignore
1427
- requestOption =
1428
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
1429
- const requestResult = await this.HttpxRequest.request(requestOption);
1430
- if (
1431
- requestResult != null &&
1432
- typeof requestResult.abort === "function"
1433
- ) {
1434
- abortFn = requestResult.abort;
1435
- }
1436
- }
1437
- );
1438
- // @ts-ignore
1439
- promise.abort = () => {
1440
- if (typeof abortFn === "function") {
1441
- abortFn();
1442
- }
1443
- };
1444
- // @ts-ignore
1445
- return promise;
1419
+ let useRequestOption =
1420
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1421
+ useRequestOption.method = "POST";
1422
+ return this.request(useRequestOption);
1446
1423
  }
1447
1424
  /**
1448
1425
  * HEAD 请求
1449
1426
  * @param details 配置
1450
1427
  */
1451
1428
  head<T extends HttpxRequestOption>(
1452
- details: T // @ts-ignore
1429
+ details: T
1453
1430
  ): HttpxPromise<HttpxResponse<T>>;
1454
1431
  /**
1455
1432
  * HEAD 请求
1456
1433
  * @param url 网址
1457
1434
  */
1458
1435
  head<T extends HttpxRequestOption>(
1459
- url: string // @ts-ignore
1436
+ url: string
1460
1437
  ): HttpxPromise<HttpxResponse<T>>;
1461
1438
  /**
1462
1439
  * HEAD 请求
@@ -1465,62 +1442,34 @@ class Httpx {
1465
1442
  */
1466
1443
  head<T extends HttpxRequestOption>(
1467
1444
  url: string,
1468
- details: T // @ts-ignore
1445
+ details: T
1469
1446
  ): HttpxPromise<HttpxResponse<T>>;
1470
1447
  /**
1471
1448
  * HEAD 请求
1472
1449
  */
1473
1450
  head(
1474
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1451
+ ...args: (HttpxRequestOption | string)[]
1475
1452
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1476
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1477
- ...args
1478
- );
1479
- let abortFn: Function | null = null;
1480
- let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
1481
- async (resolve, reject) => {
1482
- let requestOption = this.HttpxRequestOption.getRequestOption(
1483
- "HEAD",
1484
- userRequestOption,
1485
- resolve,
1486
- reject
1487
- );
1488
- Reflect.deleteProperty(requestOption, "onprogress");
1489
- // @ts-ignore
1490
- requestOption =
1491
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
1492
- const requestResult = await this.HttpxRequest.request(requestOption);
1493
- if (
1494
- requestResult != null &&
1495
- typeof requestResult.abort === "function"
1496
- ) {
1497
- abortFn = requestResult.abort;
1498
- }
1499
- }
1500
- );
1501
-
1502
- // @ts-ignore
1503
- promise.abort = () => {
1504
- if (typeof abortFn === "function") {
1505
- abortFn();
1506
- }
1507
- };
1508
- // @ts-ignore
1509
- return promise;
1453
+ let useRequestOption =
1454
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1455
+ useRequestOption.method = "HEAD";
1456
+ return this.request(useRequestOption, (option) => {
1457
+ Reflect.deleteProperty(option, "onprogress");
1458
+ });
1510
1459
  }
1511
1460
  /**
1512
1461
  * OPTIONS 请求
1513
1462
  * @param details 配置
1514
1463
  */
1515
1464
  options<T extends HttpxRequestOption>(
1516
- details: T // @ts-ignore
1465
+ details: T
1517
1466
  ): HttpxPromise<HttpxResponse<T>>;
1518
1467
  /**
1519
1468
  * OPTIONS 请求
1520
1469
  * @param url 网址
1521
1470
  */
1522
1471
  options<T extends HttpxRequestOption>(
1523
- url: string // @ts-ignore
1472
+ url: string
1524
1473
  ): HttpxPromise<HttpxResponse<T>>;
1525
1474
  /**
1526
1475
  * OPTIONS 请求
@@ -1529,47 +1478,20 @@ class Httpx {
1529
1478
  */
1530
1479
  options<T extends HttpxRequestOption>(
1531
1480
  url: string,
1532
- details: T // @ts-ignore
1481
+ details: T
1533
1482
  ): HttpxPromise<HttpxResponse<T>>;
1534
1483
  /**
1535
1484
  * OPTIONS 请求
1536
1485
  */
1537
1486
  options(
1538
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1487
+ ...args: (HttpxRequestOption | string)[]
1539
1488
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1540
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1541
- ...args
1542
- );
1543
- let abortFn: Function | null = null;
1544
- let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
1545
- async (resolve, reject) => {
1546
- let requestOption = this.HttpxRequestOption.getRequestOption(
1547
- "OPTIONS",
1548
- userRequestOption,
1549
- resolve,
1550
- reject
1551
- );
1552
- Reflect.deleteProperty(requestOption, "onprogress");
1553
- // @ts-ignore
1554
- requestOption =
1555
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
1556
- const requestResult = await this.HttpxRequest.request(requestOption);
1557
- if (
1558
- requestResult != null &&
1559
- typeof requestResult.abort === "function"
1560
- ) {
1561
- abortFn = requestResult.abort;
1562
- }
1563
- }
1564
- );
1565
- // @ts-ignore
1566
- promise.abort = () => {
1567
- if (typeof abortFn === "function") {
1568
- abortFn();
1569
- }
1570
- };
1571
- // @ts-ignore
1572
- return promise;
1489
+ let useRequestOption =
1490
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1491
+ useRequestOption.method = "OPTIONS";
1492
+ return this.request(useRequestOption, (option) => {
1493
+ Reflect.deleteProperty(option, "onprogress");
1494
+ });
1573
1495
  }
1574
1496
 
1575
1497
  /**
@@ -1577,14 +1499,14 @@ class Httpx {
1577
1499
  * @param details 配置
1578
1500
  */
1579
1501
  delete<T extends HttpxRequestOption>(
1580
- details: T // @ts-ignore
1502
+ details: T
1581
1503
  ): HttpxPromise<HttpxResponse<T>>;
1582
1504
  /**
1583
1505
  * DELETE 请求
1584
1506
  * @param url 网址
1585
1507
  */
1586
1508
  delete<T extends HttpxRequestOption>(
1587
- url: string // @ts-ignore
1509
+ url: string
1588
1510
  ): HttpxPromise<HttpxResponse<T>>;
1589
1511
  /**
1590
1512
  * DELETE 请求
@@ -1593,63 +1515,33 @@ class Httpx {
1593
1515
  */
1594
1516
  delete<T extends HttpxRequestOption>(
1595
1517
  url: string,
1596
- details: T // @ts-ignore
1518
+ details: T
1597
1519
  ): HttpxPromise<HttpxResponse<T>>;
1598
1520
  /**
1599
1521
  * DELETE 请求
1600
1522
  */
1601
1523
  delete(
1602
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1524
+ ...args: (HttpxRequestOption | string)[]
1603
1525
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1604
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1605
- ...args
1606
- );
1607
- let abortFn: Function | null = null;
1608
- let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
1609
- async (resolve, reject) => {
1610
- let requestOption = this.HttpxRequestOption.getRequestOption(
1611
- "DELETE",
1612
- userRequestOption,
1613
- resolve,
1614
- reject
1615
- );
1616
- Reflect.deleteProperty(requestOption, "onprogress");
1617
- // @ts-ignore
1618
- requestOption =
1619
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
1620
- const requestResult = await this.HttpxRequest.request(requestOption);
1621
- if (
1622
- requestResult != null &&
1623
- typeof requestResult.abort === "function"
1624
- ) {
1625
- abortFn = requestResult.abort;
1626
- }
1627
- }
1628
- );
1629
-
1630
- // @ts-ignore
1631
- promise.abort = () => {
1632
- if (typeof abortFn === "function") {
1633
- abortFn();
1634
- }
1635
- };
1636
- // @ts-ignore
1637
- return promise;
1526
+ let useRequestOption =
1527
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1528
+ useRequestOption.method = "DELETE";
1529
+ return this.request(useRequestOption, (option) => {
1530
+ Reflect.deleteProperty(option, "onprogress");
1531
+ });
1638
1532
  }
1639
1533
 
1640
1534
  /**
1641
1535
  * PUT 请求
1642
1536
  * @param details 配置
1643
1537
  */
1644
- put<T extends HttpxRequestOption>(
1645
- details: T // @ts-ignore
1646
- ): HttpxPromise<HttpxResponse<T>>;
1538
+ put<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1647
1539
  /**
1648
1540
  * PUT 请求
1649
1541
  * @param url 网址
1650
1542
  */
1651
1543
  put<T extends HttpxRequestOption>(
1652
- url: string // @ts-ignore
1544
+ url: string
1653
1545
  ): HttpxPromise<HttpxResponse<T>>;
1654
1546
  /**
1655
1547
  * PUT 请求
@@ -1658,26 +1550,45 @@ class Httpx {
1658
1550
  */
1659
1551
  put<T extends HttpxRequestOption>(
1660
1552
  url: string,
1661
- details: T // @ts-ignore
1553
+ details: T
1662
1554
  ): HttpxPromise<HttpxResponse<T>>;
1663
1555
  /**
1664
1556
  * PUT 请求
1665
1557
  */
1666
1558
  put(
1667
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1559
+ ...args: (HttpxRequestOption | string)[]
1668
1560
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1669
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1670
- ...args
1671
- );
1561
+ let userRequestOption =
1562
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1563
+ userRequestOption.method = "PUT";
1564
+ return this.request(userRequestOption);
1565
+ }
1566
+
1567
+ /**
1568
+ * 发送请求
1569
+ * @param details 配置
1570
+ * @param beforeRequestOption 处理请求前的配置
1571
+ */
1572
+ request<T extends HttpxRequestOption>(
1573
+ details: T,
1574
+ beforeRequestOption?: (option: Required<T>) => void
1575
+ ): HttpxPromise<HttpxResponse<T>> {
1576
+ let useRequestOption =
1577
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
1578
+ /** 取消请求 */
1672
1579
  let abortFn: Function | null = null;
1673
- let promise = new Promise<HttpxResponse<HttpxRequestOption>>(
1580
+ let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(
1674
1581
  async (resolve, reject) => {
1675
1582
  let requestOption = this.HttpxRequestOption.getRequestOption(
1676
- "PUT",
1677
- userRequestOption,
1583
+ useRequestOption.method!,
1584
+ useRequestOption,
1678
1585
  resolve,
1679
1586
  reject
1680
1587
  );
1588
+ if (typeof beforeRequestOption === "function") {
1589
+ // @ts-ignore
1590
+ beforeRequestOption(requestOption);
1591
+ }
1681
1592
  // @ts-ignore
1682
1593
  requestOption =
1683
1594
  this.HttpxRequestOption.removeRequestNullOption(requestOption);