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