@whitesev/utils 2.6.5 → 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,9 +239,9 @@ 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
247
  /* 传入的是url,转为配置 */
@@ -249,13 +249,14 @@ class Httpx {
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
257
  /* 传入的是配置 */
258
- option = args[0];
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,11 +1378,10 @@ 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 useRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1353
- ...args
1354
- );
1383
+ let useRequestOption =
1384
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1355
1385
  useRequestOption.method = "GET";
1356
1386
  return this.request(useRequestOption, (option) => {
1357
1387
  Reflect.deleteProperty(option, "onprogress");
@@ -1362,7 +1392,7 @@ class Httpx {
1362
1392
  * @param details 配置
1363
1393
  */
1364
1394
  post<T extends HttpxRequestOption>(
1365
- details: T // @ts-ignore
1395
+ details: T
1366
1396
  ): HttpxPromise<HttpxResponse<T>>;
1367
1397
  /**
1368
1398
  * POST 请求
@@ -1370,8 +1400,7 @@ class Httpx {
1370
1400
  */
1371
1401
  post<T extends HttpxRequestOption>(
1372
1402
  url: string
1373
- ): // @ts-ignore
1374
- HttpxPromise<HttpxResponse<T>>;
1403
+ ): HttpxPromise<HttpxResponse<T>>;
1375
1404
  /**
1376
1405
  * POST 请求
1377
1406
  * @param url 网址
@@ -1379,17 +1408,16 @@ class Httpx {
1379
1408
  */
1380
1409
  post<T extends HttpxRequestOption>(
1381
1410
  url: string,
1382
- details: T // @ts-ignore
1411
+ details: T
1383
1412
  ): HttpxPromise<HttpxResponse<T>>;
1384
1413
  /**
1385
1414
  * POST 请求
1386
1415
  */
1387
1416
  post(
1388
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1417
+ ...args: (HttpxRequestOption | string)[]
1389
1418
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1390
- let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1391
- ...args
1392
- );
1419
+ let useRequestOption =
1420
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1393
1421
  useRequestOption.method = "POST";
1394
1422
  return this.request(useRequestOption);
1395
1423
  }
@@ -1398,14 +1426,14 @@ class Httpx {
1398
1426
  * @param details 配置
1399
1427
  */
1400
1428
  head<T extends HttpxRequestOption>(
1401
- details: T // @ts-ignore
1429
+ details: T
1402
1430
  ): HttpxPromise<HttpxResponse<T>>;
1403
1431
  /**
1404
1432
  * HEAD 请求
1405
1433
  * @param url 网址
1406
1434
  */
1407
1435
  head<T extends HttpxRequestOption>(
1408
- url: string // @ts-ignore
1436
+ url: string
1409
1437
  ): HttpxPromise<HttpxResponse<T>>;
1410
1438
  /**
1411
1439
  * HEAD 请求
@@ -1414,17 +1442,16 @@ class Httpx {
1414
1442
  */
1415
1443
  head<T extends HttpxRequestOption>(
1416
1444
  url: string,
1417
- details: T // @ts-ignore
1445
+ details: T
1418
1446
  ): HttpxPromise<HttpxResponse<T>>;
1419
1447
  /**
1420
1448
  * HEAD 请求
1421
1449
  */
1422
1450
  head(
1423
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1451
+ ...args: (HttpxRequestOption | string)[]
1424
1452
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1425
- let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1426
- ...args
1427
- );
1453
+ let useRequestOption =
1454
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1428
1455
  useRequestOption.method = "HEAD";
1429
1456
  return this.request(useRequestOption, (option) => {
1430
1457
  Reflect.deleteProperty(option, "onprogress");
@@ -1435,14 +1462,14 @@ class Httpx {
1435
1462
  * @param details 配置
1436
1463
  */
1437
1464
  options<T extends HttpxRequestOption>(
1438
- details: T // @ts-ignore
1465
+ details: T
1439
1466
  ): HttpxPromise<HttpxResponse<T>>;
1440
1467
  /**
1441
1468
  * OPTIONS 请求
1442
1469
  * @param url 网址
1443
1470
  */
1444
1471
  options<T extends HttpxRequestOption>(
1445
- url: string // @ts-ignore
1472
+ url: string
1446
1473
  ): HttpxPromise<HttpxResponse<T>>;
1447
1474
  /**
1448
1475
  * OPTIONS 请求
@@ -1451,17 +1478,16 @@ class Httpx {
1451
1478
  */
1452
1479
  options<T extends HttpxRequestOption>(
1453
1480
  url: string,
1454
- details: T // @ts-ignore
1481
+ details: T
1455
1482
  ): HttpxPromise<HttpxResponse<T>>;
1456
1483
  /**
1457
1484
  * OPTIONS 请求
1458
1485
  */
1459
1486
  options(
1460
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1487
+ ...args: (HttpxRequestOption | string)[]
1461
1488
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1462
- let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1463
- ...args
1464
- );
1489
+ let useRequestOption =
1490
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1465
1491
  useRequestOption.method = "OPTIONS";
1466
1492
  return this.request(useRequestOption, (option) => {
1467
1493
  Reflect.deleteProperty(option, "onprogress");
@@ -1473,14 +1499,14 @@ class Httpx {
1473
1499
  * @param details 配置
1474
1500
  */
1475
1501
  delete<T extends HttpxRequestOption>(
1476
- details: T // @ts-ignore
1502
+ details: T
1477
1503
  ): HttpxPromise<HttpxResponse<T>>;
1478
1504
  /**
1479
1505
  * DELETE 请求
1480
1506
  * @param url 网址
1481
1507
  */
1482
1508
  delete<T extends HttpxRequestOption>(
1483
- url: string // @ts-ignore
1509
+ url: string
1484
1510
  ): HttpxPromise<HttpxResponse<T>>;
1485
1511
  /**
1486
1512
  * DELETE 请求
@@ -1489,17 +1515,16 @@ class Httpx {
1489
1515
  */
1490
1516
  delete<T extends HttpxRequestOption>(
1491
1517
  url: string,
1492
- details: T // @ts-ignore
1518
+ details: T
1493
1519
  ): HttpxPromise<HttpxResponse<T>>;
1494
1520
  /**
1495
1521
  * DELETE 请求
1496
1522
  */
1497
1523
  delete(
1498
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1524
+ ...args: (HttpxRequestOption | string)[]
1499
1525
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1500
- let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1501
- ...args
1502
- );
1526
+ let useRequestOption =
1527
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1503
1528
  useRequestOption.method = "DELETE";
1504
1529
  return this.request(useRequestOption, (option) => {
1505
1530
  Reflect.deleteProperty(option, "onprogress");
@@ -1510,15 +1535,13 @@ class Httpx {
1510
1535
  * PUT 请求
1511
1536
  * @param details 配置
1512
1537
  */
1513
- put<T extends HttpxRequestOption>(
1514
- details: T // @ts-ignore
1515
- ): HttpxPromise<HttpxResponse<T>>;
1538
+ put<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1516
1539
  /**
1517
1540
  * PUT 请求
1518
1541
  * @param url 网址
1519
1542
  */
1520
1543
  put<T extends HttpxRequestOption>(
1521
- url: string // @ts-ignore
1544
+ url: string
1522
1545
  ): HttpxPromise<HttpxResponse<T>>;
1523
1546
  /**
1524
1547
  * PUT 请求
@@ -1527,17 +1550,16 @@ class Httpx {
1527
1550
  */
1528
1551
  put<T extends HttpxRequestOption>(
1529
1552
  url: string,
1530
- details: T // @ts-ignore
1553
+ details: T
1531
1554
  ): HttpxPromise<HttpxResponse<T>>;
1532
1555
  /**
1533
1556
  * PUT 请求
1534
1557
  */
1535
1558
  put(
1536
- ...args: (HttpxRequestOption | string)[] // @ts-ignore
1559
+ ...args: (HttpxRequestOption | string)[]
1537
1560
  ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1538
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(
1539
- ...args
1540
- );
1561
+ let userRequestOption =
1562
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1541
1563
  userRequestOption.method = "PUT";
1542
1564
  return this.request(userRequestOption);
1543
1565
  }
@@ -1552,7 +1574,7 @@ class Httpx {
1552
1574
  beforeRequestOption?: (option: Required<T>) => void
1553
1575
  ): HttpxPromise<HttpxResponse<T>> {
1554
1576
  let useRequestOption =
1555
- this.HttpxRequestOption.handleBeforeRequestOption(details);
1577
+ this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
1556
1578
  /** 取消请求 */
1557
1579
  let abortFn: Function | null = null;
1558
1580
  let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(
package/src/Utils.ts CHANGED
@@ -5197,6 +5197,22 @@ class Utils {
5197
5197
  }
5198
5198
  return new URL(text);
5199
5199
  }
5200
+ /**
5201
+ * 覆盖对象中的函数this指向
5202
+ * @param target 需要覆盖的对象
5203
+ * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
5204
+ */
5205
+ coverObjectFunctionThis(target: any, objectThis?: any) {
5206
+ if (typeof target !== "object" || target === null) {
5207
+ throw new Error("target must be object");
5208
+ }
5209
+ objectThis = objectThis || target;
5210
+ Object.keys(target).forEach((key) => {
5211
+ if (typeof target[key] === "function") {
5212
+ target[key] = target[key].bind(objectThis);
5213
+ }
5214
+ });
5215
+ }
5200
5216
  /**
5201
5217
  * 生成uuid
5202
5218
  * @example
@@ -1210,12 +1210,7 @@ export declare interface HttpxRequestOption {
1210
1210
  /**
1211
1211
  * 自定义的配置请求
1212
1212
  */
1213
- export declare interface HttpxRequestOptionConfig extends HttpxRequestOption {
1214
- /**
1215
- * (可选)是否输出请求配置
1216
- */
1217
- logDetails?: boolean;
1218
- }
1213
+ export declare interface HttpxRequestOptionConfig extends HttpxInitOption {}
1219
1214
  /**
1220
1215
  * 响应的数据的data
1221
1216
  */
@@ -1334,3 +1329,26 @@ export declare interface HttpxHookErrorData {
1334
1329
  export declare interface HttpxPromise<T> extends Promise<T> {
1335
1330
  abort: () => void;
1336
1331
  }
1332
+
1333
+ /**
1334
+ * httpx的初始化配置
1335
+ */
1336
+ export declare interface HttpxInitOption extends HttpxRequestOption {
1337
+ /**
1338
+ * 实例化,可传入GM_xmlhttpRequest,未传入则使用window.fetch
1339
+ */
1340
+ xmlHttpRequest?: Function;
1341
+ /**
1342
+ * `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。
1343
+ */
1344
+ baseURL?: string | undefined;
1345
+ /**
1346
+ * 重试次数
1347
+ * @default 0
1348
+ */
1349
+ retry?: number;
1350
+ /**
1351
+ * (可选)是否输出请求配置
1352
+ */
1353
+ logDetails?: boolean;
1354
+ }