@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/dist/index.esm.js CHANGED
@@ -2281,24 +2281,25 @@ class Httpx {
2281
2281
  HttpxRequestOption = {
2282
2282
  context: this,
2283
2283
  /**
2284
- * 根据传入的参数处理获取details配置
2284
+ * 对请求的参数进行合并处理
2285
2285
  */
2286
- handleBeforeRequestOption(...args) {
2286
+ handleBeforeRequestOptionArgs(...args) {
2287
2287
  let option = {};
2288
2288
  if (typeof args[0] === "string") {
2289
- /* 传入的是url,details? */
2289
+ /* 传入的是url,转为配置 */
2290
2290
  let url = args[0];
2291
2291
  option.url = url;
2292
2292
  if (typeof args[1] === "object") {
2293
2293
  /* 处理第二个参数details */
2294
- let details = args[1];
2295
- option = details;
2294
+ let optionArg = args[1];
2295
+ utils.assign(option, optionArg, true);
2296
2296
  option.url = url;
2297
2297
  }
2298
2298
  }
2299
2299
  else {
2300
- /* 传入的是details */
2301
- option = args[0];
2300
+ /* 传入的是配置 */
2301
+ let optionArg = args[0];
2302
+ utils.assign(option, optionArg, true);
2302
2303
  }
2303
2304
  return option;
2304
2305
  },
@@ -2311,41 +2312,59 @@ class Httpx {
2311
2312
  */
2312
2313
  getRequestOption(method, userRequestOption, resolve, reject) {
2313
2314
  let that = this;
2315
+ let url = userRequestOption.url || this.context.#defaultRequestOption.url;
2316
+ if (typeof url === "string") {
2317
+ // 去除左右空格
2318
+ url = url.trim();
2319
+ if (url.startsWith("http://") || url.startsWith("https://")) ;
2320
+ else {
2321
+ if (typeof this.context.#defaultInitOption.baseURL === "string") {
2322
+ // 设置了基础域
2323
+ url = this.context.#defaultInitOption.baseURL + url;
2324
+ }
2325
+ }
2326
+ }
2314
2327
  let requestOption = {
2315
- url: userRequestOption.url || this.context.#defaultDetails.url,
2328
+ url: url,
2316
2329
  method: (method || "GET").toString().toUpperCase().trim(),
2317
- timeout: userRequestOption.timeout || this.context.#defaultDetails.timeout,
2330
+ timeout: userRequestOption.timeout ||
2331
+ this.context.#defaultRequestOption.timeout,
2318
2332
  responseType: userRequestOption.responseType ||
2319
- this.context.#defaultDetails.responseType,
2333
+ this.context.#defaultRequestOption.responseType,
2320
2334
  /* 对象使用深拷贝 */
2321
- headers: utils.deepClone(this.context.#defaultDetails.headers),
2322
- data: userRequestOption.data || this.context.#defaultDetails.data,
2323
- redirect: userRequestOption.redirect || this.context.#defaultDetails.redirect,
2324
- cookie: userRequestOption.cookie || this.context.#defaultDetails.cookie,
2335
+ headers: utils.deepClone(this.context.#defaultRequestOption.headers),
2336
+ data: userRequestOption.data || this.context.#defaultRequestOption.data,
2337
+ redirect: userRequestOption.redirect ||
2338
+ this.context.#defaultRequestOption.redirect,
2339
+ cookie: userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
2325
2340
  cookiePartition: userRequestOption.cookiePartition ||
2326
- this.context.#defaultDetails.cookiePartition,
2327
- binary: userRequestOption.binary || this.context.#defaultDetails.binary,
2328
- nocache: userRequestOption.nocache || this.context.#defaultDetails.nocache,
2341
+ this.context.#defaultRequestOption.cookiePartition,
2342
+ binary: userRequestOption.binary || this.context.#defaultRequestOption.binary,
2343
+ nocache: userRequestOption.nocache ||
2344
+ this.context.#defaultRequestOption.nocache,
2329
2345
  revalidate: userRequestOption.revalidate ||
2330
- this.context.#defaultDetails.revalidate,
2346
+ this.context.#defaultRequestOption.revalidate,
2331
2347
  /* 对象使用深拷贝 */
2332
- context: utils.deepClone(userRequestOption.context || this.context.#defaultDetails.context),
2348
+ context: utils.deepClone(userRequestOption.context ||
2349
+ this.context.#defaultRequestOption.context),
2333
2350
  overrideMimeType: userRequestOption.overrideMimeType ||
2334
- this.context.#defaultDetails.overrideMimeType,
2335
- anonymous: userRequestOption.anonymous || this.context.#defaultDetails.anonymous,
2336
- fetch: userRequestOption.fetch || this.context.#defaultDetails.fetch,
2351
+ this.context.#defaultRequestOption.overrideMimeType,
2352
+ anonymous: userRequestOption.anonymous ||
2353
+ this.context.#defaultRequestOption.anonymous,
2354
+ fetch: userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
2337
2355
  /* 对象使用深拷贝 */
2338
- fetchInit: utils.deepClone(this.context.#defaultDetails.fetchInit),
2356
+ fetchInit: utils.deepClone(this.context.#defaultRequestOption.fetchInit),
2339
2357
  allowInterceptConfig: {
2340
- beforeRequest: this.context.#defaultDetails
2358
+ beforeRequest: this.context.#defaultRequestOption
2341
2359
  .allowInterceptConfig.beforeRequest,
2342
- afterResponseSuccess: this.context.#defaultDetails
2360
+ afterResponseSuccess: this.context.#defaultRequestOption
2343
2361
  .allowInterceptConfig.afterResponseSuccess,
2344
- afterResponseError: this.context.#defaultDetails
2362
+ afterResponseError: this.context.#defaultRequestOption
2345
2363
  .allowInterceptConfig.afterResponseError,
2346
2364
  },
2347
- user: userRequestOption.user || this.context.#defaultDetails.user,
2348
- password: userRequestOption.password || this.context.#defaultDetails.password,
2365
+ user: userRequestOption.user || this.context.#defaultRequestOption.user,
2366
+ password: userRequestOption.password ||
2367
+ this.context.#defaultRequestOption.password,
2349
2368
  onabort(...args) {
2350
2369
  that.context.HttpxCallBack.onAbort(userRequestOption, resolve, reject, args);
2351
2370
  },
@@ -2639,8 +2658,8 @@ class Httpx {
2639
2658
  if ("onabort" in details) {
2640
2659
  details.onabort.apply(this, argsResult);
2641
2660
  }
2642
- else if ("onabort" in this.context.#defaultDetails) {
2643
- this.context.#defaultDetails.onabort.apply(this, argsResult);
2661
+ else if ("onabort" in this.context.#defaultRequestOption) {
2662
+ this.context.#defaultRequestOption.onabort.apply(this, argsResult);
2644
2663
  }
2645
2664
  let response = argsResult;
2646
2665
  if (response.length) {
@@ -2676,8 +2695,8 @@ class Httpx {
2676
2695
  if ("onerror" in details) {
2677
2696
  details.onerror.apply(this, argsResult);
2678
2697
  }
2679
- else if ("onerror" in this.context.#defaultDetails) {
2680
- this.context.#defaultDetails.onerror.apply(this, argsResult);
2698
+ else if ("onerror" in this.context.#defaultRequestOption) {
2699
+ this.context.#defaultRequestOption.onerror.apply(this, argsResult);
2681
2700
  }
2682
2701
  let response = argsResult;
2683
2702
  if (response.length) {
@@ -2713,8 +2732,8 @@ class Httpx {
2713
2732
  if ("ontimeout" in details) {
2714
2733
  details.ontimeout.apply(this, argsResult);
2715
2734
  }
2716
- else if ("ontimeout" in this.context.#defaultDetails) {
2717
- this.context.#defaultDetails.ontimeout.apply(this, argsResult);
2735
+ else if ("ontimeout" in this.context.#defaultRequestOption) {
2736
+ this.context.#defaultRequestOption.ontimeout.apply(this, argsResult);
2718
2737
  }
2719
2738
  let response = argsResult;
2720
2739
  if (response.length) {
@@ -2748,8 +2767,8 @@ class Httpx {
2748
2767
  if ("onloadstart" in details) {
2749
2768
  details.onloadstart.apply(this, argsResult);
2750
2769
  }
2751
- else if ("onloadstart" in this.context.#defaultDetails) {
2752
- this.context.#defaultDetails.onloadstart.apply(this, argsResult);
2770
+ else if ("onloadstart" in this.context.#defaultRequestOption) {
2771
+ this.context.#defaultRequestOption.onloadstart.apply(this, argsResult);
2753
2772
  }
2754
2773
  },
2755
2774
  /**
@@ -2857,8 +2876,8 @@ class Httpx {
2857
2876
  if ("onprogress" in details) {
2858
2877
  details.onprogress.apply(this, argsResult);
2859
2878
  }
2860
- else if ("onprogress" in this.context.#defaultDetails) {
2861
- this.context.#defaultDetails.onprogress.apply(this, argsResult);
2879
+ else if ("onprogress" in this.context.#defaultRequestOption) {
2880
+ this.context.#defaultRequestOption.onprogress.apply(this, argsResult);
2862
2881
  }
2863
2882
  },
2864
2883
  /**
@@ -2871,8 +2890,8 @@ class Httpx {
2871
2890
  if ("onreadystatechange" in details) {
2872
2891
  details.onreadystatechange.apply(this, argsResult);
2873
2892
  }
2874
- else if ("onreadystatechange" in this.context.#defaultDetails) {
2875
- this.context.#defaultDetails.onreadystatechange.apply(this, argsResult);
2893
+ else if ("onreadystatechange" in this.context.#defaultRequestOption) {
2894
+ this.context.#defaultRequestOption.onreadystatechange.apply(this, argsResult);
2876
2895
  }
2877
2896
  },
2878
2897
  };
@@ -2883,7 +2902,7 @@ class Httpx {
2883
2902
  * @param details
2884
2903
  */
2885
2904
  async request(details) {
2886
- if (this.context.#LOG_DETAILS) {
2905
+ if (this.context.#defaultInitOption.logDetails) {
2887
2906
  console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
2888
2907
  }
2889
2908
  if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
@@ -3045,7 +3064,7 @@ class Httpx {
3045
3064
  /**
3046
3065
  * 默认配置
3047
3066
  */
3048
- #defaultDetails = {
3067
+ #defaultRequestOption = {
3049
3068
  url: undefined,
3050
3069
  timeout: 5000,
3051
3070
  async: false,
@@ -3077,31 +3096,39 @@ class Httpx {
3077
3096
  onreadystatechange() { },
3078
3097
  onprogress() { },
3079
3098
  };
3099
+ #defaultInitOption = {
3100
+ /**
3101
+ * `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。
3102
+ */
3103
+ baseURL: undefined,
3104
+ /**
3105
+ * 当前使用请求时,输出请求的配置,一般用于DEBUG|DEV
3106
+ */
3107
+ logDetails: false,
3108
+ };
3080
3109
  /**
3081
- * 当前使用请求时,输出请求的配置
3082
- */
3083
- #LOG_DETAILS = false;
3084
- /**
3085
- * 实例化,可传入GM_xmlhttpRequest,未传入则使用window.fetch
3086
- * @param xmlHttpRequest
3110
+ * 实例化
3111
+ * @param option 初始化配置
3087
3112
  */
3088
- constructor(xmlHttpRequest) {
3089
- if (typeof xmlHttpRequest !== "function") {
3113
+ constructor(option = {}) {
3114
+ if (typeof option.xmlHttpRequest !== "function") {
3090
3115
  console.warn("[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch");
3091
3116
  }
3117
+ utils.coverObjectFunctionThis(this);
3092
3118
  this.interceptors.request.context = this;
3093
3119
  this.interceptors.response.context = this;
3094
- this.GM_Api.xmlHttpRequest = xmlHttpRequest;
3120
+ this.config(option);
3095
3121
  }
3096
3122
  /**
3097
3123
  * 覆盖当前配置
3098
- * @param details
3124
+ * @param option
3099
3125
  */
3100
- config(details = {}) {
3101
- if ("logDetails" in details && typeof details["logDetails"] === "boolean") {
3102
- this.#LOG_DETAILS = details["logDetails"];
3126
+ config(option = {}) {
3127
+ if (typeof option.xmlHttpRequest === "function") {
3128
+ this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
3103
3129
  }
3104
- this.#defaultDetails = utils.assign(this.#defaultDetails, details);
3130
+ this.#defaultRequestOption = utils.assign(this.#defaultRequestOption, option);
3131
+ this.#defaultInitOption = utils.assign(this.#defaultInitOption, option);
3105
3132
  }
3106
3133
  /**
3107
3134
  * 拦截器
@@ -3183,149 +3210,74 @@ class Httpx {
3183
3210
  * @param url 网址
3184
3211
  * @param details 配置
3185
3212
  */
3186
- get(...args // @ts-ignore
3187
- ) {
3188
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3189
- let abortFn = null;
3190
- let promise = new globalThis.Promise(async (resolve, reject) => {
3191
- let requestOption = this.HttpxRequestOption.getRequestOption("GET", userRequestOption, resolve, reject);
3192
- Reflect.deleteProperty(requestOption, "onprogress");
3193
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3194
- const requestResult = await this.HttpxRequest.request(requestOption);
3195
- if (requestResult != null &&
3196
- typeof requestResult.abort === "function") {
3197
- abortFn = requestResult.abort;
3198
- }
3213
+ get(...args) {
3214
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3215
+ useRequestOption.method = "GET";
3216
+ return this.request(useRequestOption, (option) => {
3217
+ Reflect.deleteProperty(option, "onprogress");
3199
3218
  });
3200
- // @ts-ignore
3201
- promise.abort = () => {
3202
- if (typeof abortFn === "function") {
3203
- abortFn();
3204
- }
3205
- };
3206
- // @ts-ignore
3207
- return promise;
3208
3219
  }
3209
3220
  /**
3210
3221
  * POST 请求
3211
3222
  */
3212
- post(...args // @ts-ignore
3213
- ) {
3214
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3215
- let abortFn = null;
3216
- let promise = new Promise(async (resolve, reject) => {
3217
- let requestOption = this.HttpxRequestOption.getRequestOption("POST", userRequestOption, resolve, reject);
3218
- // @ts-ignore
3219
- requestOption =
3220
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3221
- const requestResult = await this.HttpxRequest.request(requestOption);
3222
- if (requestResult != null &&
3223
- typeof requestResult.abort === "function") {
3224
- abortFn = requestResult.abort;
3225
- }
3226
- });
3227
- // @ts-ignore
3228
- promise.abort = () => {
3229
- if (typeof abortFn === "function") {
3230
- abortFn();
3231
- }
3232
- };
3233
- // @ts-ignore
3234
- return promise;
3223
+ post(...args) {
3224
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3225
+ useRequestOption.method = "POST";
3226
+ return this.request(useRequestOption);
3235
3227
  }
3236
3228
  /**
3237
3229
  * HEAD 请求
3238
3230
  */
3239
- head(...args // @ts-ignore
3240
- ) {
3241
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3242
- let abortFn = null;
3243
- let promise = new Promise(async (resolve, reject) => {
3244
- let requestOption = this.HttpxRequestOption.getRequestOption("HEAD", userRequestOption, resolve, reject);
3245
- Reflect.deleteProperty(requestOption, "onprogress");
3246
- // @ts-ignore
3247
- requestOption =
3248
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3249
- const requestResult = await this.HttpxRequest.request(requestOption);
3250
- if (requestResult != null &&
3251
- typeof requestResult.abort === "function") {
3252
- abortFn = requestResult.abort;
3253
- }
3231
+ head(...args) {
3232
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3233
+ useRequestOption.method = "HEAD";
3234
+ return this.request(useRequestOption, (option) => {
3235
+ Reflect.deleteProperty(option, "onprogress");
3254
3236
  });
3255
- // @ts-ignore
3256
- promise.abort = () => {
3257
- if (typeof abortFn === "function") {
3258
- abortFn();
3259
- }
3260
- };
3261
- // @ts-ignore
3262
- return promise;
3263
3237
  }
3264
3238
  /**
3265
3239
  * OPTIONS 请求
3266
3240
  */
3267
- options(...args // @ts-ignore
3268
- ) {
3269
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3270
- let abortFn = null;
3271
- let promise = new Promise(async (resolve, reject) => {
3272
- let requestOption = this.HttpxRequestOption.getRequestOption("OPTIONS", userRequestOption, resolve, reject);
3273
- Reflect.deleteProperty(requestOption, "onprogress");
3274
- // @ts-ignore
3275
- requestOption =
3276
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3277
- const requestResult = await this.HttpxRequest.request(requestOption);
3278
- if (requestResult != null &&
3279
- typeof requestResult.abort === "function") {
3280
- abortFn = requestResult.abort;
3281
- }
3241
+ options(...args) {
3242
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3243
+ useRequestOption.method = "OPTIONS";
3244
+ return this.request(useRequestOption, (option) => {
3245
+ Reflect.deleteProperty(option, "onprogress");
3282
3246
  });
3283
- // @ts-ignore
3284
- promise.abort = () => {
3285
- if (typeof abortFn === "function") {
3286
- abortFn();
3287
- }
3288
- };
3289
- // @ts-ignore
3290
- return promise;
3291
3247
  }
3292
3248
  /**
3293
3249
  * DELETE 请求
3294
3250
  */
3295
- delete(...args // @ts-ignore
3296
- ) {
3297
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3298
- let abortFn = null;
3299
- let promise = new Promise(async (resolve, reject) => {
3300
- let requestOption = this.HttpxRequestOption.getRequestOption("DELETE", userRequestOption, resolve, reject);
3301
- Reflect.deleteProperty(requestOption, "onprogress");
3302
- // @ts-ignore
3303
- requestOption =
3304
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3305
- const requestResult = await this.HttpxRequest.request(requestOption);
3306
- if (requestResult != null &&
3307
- typeof requestResult.abort === "function") {
3308
- abortFn = requestResult.abort;
3309
- }
3251
+ delete(...args) {
3252
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3253
+ useRequestOption.method = "DELETE";
3254
+ return this.request(useRequestOption, (option) => {
3255
+ Reflect.deleteProperty(option, "onprogress");
3310
3256
  });
3311
- // @ts-ignore
3312
- promise.abort = () => {
3313
- if (typeof abortFn === "function") {
3314
- abortFn();
3315
- }
3316
- };
3317
- // @ts-ignore
3318
- return promise;
3319
3257
  }
3320
3258
  /**
3321
3259
  * PUT 请求
3322
3260
  */
3323
- put(...args // @ts-ignore
3324
- ) {
3325
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3261
+ put(...args) {
3262
+ let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3263
+ userRequestOption.method = "PUT";
3264
+ return this.request(userRequestOption);
3265
+ }
3266
+ /**
3267
+ * 发送请求
3268
+ * @param details 配置
3269
+ * @param beforeRequestOption 处理请求前的配置
3270
+ */
3271
+ request(details, beforeRequestOption) {
3272
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
3273
+ /** 取消请求 */
3326
3274
  let abortFn = null;
3327
- let promise = new Promise(async (resolve, reject) => {
3328
- let requestOption = this.HttpxRequestOption.getRequestOption("PUT", userRequestOption, resolve, reject);
3275
+ let promise = new globalThis.Promise(async (resolve, reject) => {
3276
+ let requestOption = this.HttpxRequestOption.getRequestOption(useRequestOption.method, useRequestOption, resolve, reject);
3277
+ if (typeof beforeRequestOption === "function") {
3278
+ // @ts-ignore
3279
+ beforeRequestOption(requestOption);
3280
+ }
3329
3281
  // @ts-ignore
3330
3282
  requestOption =
3331
3283
  this.HttpxRequestOption.removeRequestNullOption(requestOption);
@@ -5053,7 +5005,7 @@ class Utils {
5053
5005
  this.windowApi = new WindowApi(option);
5054
5006
  }
5055
5007
  /** 版本号 */
5056
- version = "2025.3.25";
5008
+ version = "2025.4.11";
5057
5009
  addStyle(cssText) {
5058
5010
  if (typeof cssText !== "string") {
5059
5011
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -8222,6 +8174,22 @@ class Utils {
8222
8174
  }
8223
8175
  return new URL(text);
8224
8176
  }
8177
+ /**
8178
+ * 覆盖对象中的函数this指向
8179
+ * @param target 需要覆盖的对象
8180
+ * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
8181
+ */
8182
+ coverObjectFunctionThis(target, objectThis) {
8183
+ if (typeof target !== "object" || target === null) {
8184
+ throw new Error("target must be object");
8185
+ }
8186
+ objectThis = objectThis || target;
8187
+ Object.keys(target).forEach((key) => {
8188
+ if (typeof target[key] === "function") {
8189
+ target[key] = target[key].bind(objectThis);
8190
+ }
8191
+ });
8192
+ }
8225
8193
  /**
8226
8194
  * 生成uuid
8227
8195
  * @example