@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.umd.js CHANGED
@@ -2287,24 +2287,25 @@
2287
2287
  HttpxRequestOption = {
2288
2288
  context: this,
2289
2289
  /**
2290
- * 根据传入的参数处理获取details配置
2290
+ * 对请求的参数进行合并处理
2291
2291
  */
2292
- handleBeforeRequestOption(...args) {
2292
+ handleBeforeRequestOptionArgs(...args) {
2293
2293
  let option = {};
2294
2294
  if (typeof args[0] === "string") {
2295
- /* 传入的是url,details? */
2295
+ /* 传入的是url,转为配置 */
2296
2296
  let url = args[0];
2297
2297
  option.url = url;
2298
2298
  if (typeof args[1] === "object") {
2299
2299
  /* 处理第二个参数details */
2300
- let details = args[1];
2301
- option = details;
2300
+ let optionArg = args[1];
2301
+ utils.assign(option, optionArg, true);
2302
2302
  option.url = url;
2303
2303
  }
2304
2304
  }
2305
2305
  else {
2306
- /* 传入的是details */
2307
- option = args[0];
2306
+ /* 传入的是配置 */
2307
+ let optionArg = args[0];
2308
+ utils.assign(option, optionArg, true);
2308
2309
  }
2309
2310
  return option;
2310
2311
  },
@@ -2317,41 +2318,59 @@
2317
2318
  */
2318
2319
  getRequestOption(method, userRequestOption, resolve, reject) {
2319
2320
  let that = this;
2321
+ let url = userRequestOption.url || this.context.#defaultRequestOption.url;
2322
+ if (typeof url === "string") {
2323
+ // 去除左右空格
2324
+ url = url.trim();
2325
+ if (url.startsWith("http://") || url.startsWith("https://")) ;
2326
+ else {
2327
+ if (typeof this.context.#defaultInitOption.baseURL === "string") {
2328
+ // 设置了基础域
2329
+ url = this.context.#defaultInitOption.baseURL + url;
2330
+ }
2331
+ }
2332
+ }
2320
2333
  let requestOption = {
2321
- url: userRequestOption.url || this.context.#defaultDetails.url,
2334
+ url: url,
2322
2335
  method: (method || "GET").toString().toUpperCase().trim(),
2323
- timeout: userRequestOption.timeout || this.context.#defaultDetails.timeout,
2336
+ timeout: userRequestOption.timeout ||
2337
+ this.context.#defaultRequestOption.timeout,
2324
2338
  responseType: userRequestOption.responseType ||
2325
- this.context.#defaultDetails.responseType,
2339
+ this.context.#defaultRequestOption.responseType,
2326
2340
  /* 对象使用深拷贝 */
2327
- headers: utils.deepClone(this.context.#defaultDetails.headers),
2328
- data: userRequestOption.data || this.context.#defaultDetails.data,
2329
- redirect: userRequestOption.redirect || this.context.#defaultDetails.redirect,
2330
- cookie: userRequestOption.cookie || this.context.#defaultDetails.cookie,
2341
+ headers: utils.deepClone(this.context.#defaultRequestOption.headers),
2342
+ data: userRequestOption.data || this.context.#defaultRequestOption.data,
2343
+ redirect: userRequestOption.redirect ||
2344
+ this.context.#defaultRequestOption.redirect,
2345
+ cookie: userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
2331
2346
  cookiePartition: userRequestOption.cookiePartition ||
2332
- this.context.#defaultDetails.cookiePartition,
2333
- binary: userRequestOption.binary || this.context.#defaultDetails.binary,
2334
- nocache: userRequestOption.nocache || this.context.#defaultDetails.nocache,
2347
+ this.context.#defaultRequestOption.cookiePartition,
2348
+ binary: userRequestOption.binary || this.context.#defaultRequestOption.binary,
2349
+ nocache: userRequestOption.nocache ||
2350
+ this.context.#defaultRequestOption.nocache,
2335
2351
  revalidate: userRequestOption.revalidate ||
2336
- this.context.#defaultDetails.revalidate,
2352
+ this.context.#defaultRequestOption.revalidate,
2337
2353
  /* 对象使用深拷贝 */
2338
- context: utils.deepClone(userRequestOption.context || this.context.#defaultDetails.context),
2354
+ context: utils.deepClone(userRequestOption.context ||
2355
+ this.context.#defaultRequestOption.context),
2339
2356
  overrideMimeType: userRequestOption.overrideMimeType ||
2340
- this.context.#defaultDetails.overrideMimeType,
2341
- anonymous: userRequestOption.anonymous || this.context.#defaultDetails.anonymous,
2342
- fetch: userRequestOption.fetch || this.context.#defaultDetails.fetch,
2357
+ this.context.#defaultRequestOption.overrideMimeType,
2358
+ anonymous: userRequestOption.anonymous ||
2359
+ this.context.#defaultRequestOption.anonymous,
2360
+ fetch: userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
2343
2361
  /* 对象使用深拷贝 */
2344
- fetchInit: utils.deepClone(this.context.#defaultDetails.fetchInit),
2362
+ fetchInit: utils.deepClone(this.context.#defaultRequestOption.fetchInit),
2345
2363
  allowInterceptConfig: {
2346
- beforeRequest: this.context.#defaultDetails
2364
+ beforeRequest: this.context.#defaultRequestOption
2347
2365
  .allowInterceptConfig.beforeRequest,
2348
- afterResponseSuccess: this.context.#defaultDetails
2366
+ afterResponseSuccess: this.context.#defaultRequestOption
2349
2367
  .allowInterceptConfig.afterResponseSuccess,
2350
- afterResponseError: this.context.#defaultDetails
2368
+ afterResponseError: this.context.#defaultRequestOption
2351
2369
  .allowInterceptConfig.afterResponseError,
2352
2370
  },
2353
- user: userRequestOption.user || this.context.#defaultDetails.user,
2354
- password: userRequestOption.password || this.context.#defaultDetails.password,
2371
+ user: userRequestOption.user || this.context.#defaultRequestOption.user,
2372
+ password: userRequestOption.password ||
2373
+ this.context.#defaultRequestOption.password,
2355
2374
  onabort(...args) {
2356
2375
  that.context.HttpxCallBack.onAbort(userRequestOption, resolve, reject, args);
2357
2376
  },
@@ -2645,8 +2664,8 @@
2645
2664
  if ("onabort" in details) {
2646
2665
  details.onabort.apply(this, argsResult);
2647
2666
  }
2648
- else if ("onabort" in this.context.#defaultDetails) {
2649
- this.context.#defaultDetails.onabort.apply(this, argsResult);
2667
+ else if ("onabort" in this.context.#defaultRequestOption) {
2668
+ this.context.#defaultRequestOption.onabort.apply(this, argsResult);
2650
2669
  }
2651
2670
  let response = argsResult;
2652
2671
  if (response.length) {
@@ -2682,8 +2701,8 @@
2682
2701
  if ("onerror" in details) {
2683
2702
  details.onerror.apply(this, argsResult);
2684
2703
  }
2685
- else if ("onerror" in this.context.#defaultDetails) {
2686
- this.context.#defaultDetails.onerror.apply(this, argsResult);
2704
+ else if ("onerror" in this.context.#defaultRequestOption) {
2705
+ this.context.#defaultRequestOption.onerror.apply(this, argsResult);
2687
2706
  }
2688
2707
  let response = argsResult;
2689
2708
  if (response.length) {
@@ -2719,8 +2738,8 @@
2719
2738
  if ("ontimeout" in details) {
2720
2739
  details.ontimeout.apply(this, argsResult);
2721
2740
  }
2722
- else if ("ontimeout" in this.context.#defaultDetails) {
2723
- this.context.#defaultDetails.ontimeout.apply(this, argsResult);
2741
+ else if ("ontimeout" in this.context.#defaultRequestOption) {
2742
+ this.context.#defaultRequestOption.ontimeout.apply(this, argsResult);
2724
2743
  }
2725
2744
  let response = argsResult;
2726
2745
  if (response.length) {
@@ -2754,8 +2773,8 @@
2754
2773
  if ("onloadstart" in details) {
2755
2774
  details.onloadstart.apply(this, argsResult);
2756
2775
  }
2757
- else if ("onloadstart" in this.context.#defaultDetails) {
2758
- this.context.#defaultDetails.onloadstart.apply(this, argsResult);
2776
+ else if ("onloadstart" in this.context.#defaultRequestOption) {
2777
+ this.context.#defaultRequestOption.onloadstart.apply(this, argsResult);
2759
2778
  }
2760
2779
  },
2761
2780
  /**
@@ -2863,8 +2882,8 @@
2863
2882
  if ("onprogress" in details) {
2864
2883
  details.onprogress.apply(this, argsResult);
2865
2884
  }
2866
- else if ("onprogress" in this.context.#defaultDetails) {
2867
- this.context.#defaultDetails.onprogress.apply(this, argsResult);
2885
+ else if ("onprogress" in this.context.#defaultRequestOption) {
2886
+ this.context.#defaultRequestOption.onprogress.apply(this, argsResult);
2868
2887
  }
2869
2888
  },
2870
2889
  /**
@@ -2877,8 +2896,8 @@
2877
2896
  if ("onreadystatechange" in details) {
2878
2897
  details.onreadystatechange.apply(this, argsResult);
2879
2898
  }
2880
- else if ("onreadystatechange" in this.context.#defaultDetails) {
2881
- this.context.#defaultDetails.onreadystatechange.apply(this, argsResult);
2899
+ else if ("onreadystatechange" in this.context.#defaultRequestOption) {
2900
+ this.context.#defaultRequestOption.onreadystatechange.apply(this, argsResult);
2882
2901
  }
2883
2902
  },
2884
2903
  };
@@ -2889,7 +2908,7 @@
2889
2908
  * @param details
2890
2909
  */
2891
2910
  async request(details) {
2892
- if (this.context.#LOG_DETAILS) {
2911
+ if (this.context.#defaultInitOption.logDetails) {
2893
2912
  console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
2894
2913
  }
2895
2914
  if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
@@ -3051,7 +3070,7 @@
3051
3070
  /**
3052
3071
  * 默认配置
3053
3072
  */
3054
- #defaultDetails = {
3073
+ #defaultRequestOption = {
3055
3074
  url: undefined,
3056
3075
  timeout: 5000,
3057
3076
  async: false,
@@ -3083,31 +3102,39 @@
3083
3102
  onreadystatechange() { },
3084
3103
  onprogress() { },
3085
3104
  };
3105
+ #defaultInitOption = {
3106
+ /**
3107
+ * `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。
3108
+ */
3109
+ baseURL: undefined,
3110
+ /**
3111
+ * 当前使用请求时,输出请求的配置,一般用于DEBUG|DEV
3112
+ */
3113
+ logDetails: false,
3114
+ };
3086
3115
  /**
3087
- * 当前使用请求时,输出请求的配置
3088
- */
3089
- #LOG_DETAILS = false;
3090
- /**
3091
- * 实例化,可传入GM_xmlhttpRequest,未传入则使用window.fetch
3092
- * @param xmlHttpRequest
3116
+ * 实例化
3117
+ * @param option 初始化配置
3093
3118
  */
3094
- constructor(xmlHttpRequest) {
3095
- if (typeof xmlHttpRequest !== "function") {
3119
+ constructor(option = {}) {
3120
+ if (typeof option.xmlHttpRequest !== "function") {
3096
3121
  console.warn("[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch");
3097
3122
  }
3123
+ utils.coverObjectFunctionThis(this);
3098
3124
  this.interceptors.request.context = this;
3099
3125
  this.interceptors.response.context = this;
3100
- this.GM_Api.xmlHttpRequest = xmlHttpRequest;
3126
+ this.config(option);
3101
3127
  }
3102
3128
  /**
3103
3129
  * 覆盖当前配置
3104
- * @param details
3130
+ * @param option
3105
3131
  */
3106
- config(details = {}) {
3107
- if ("logDetails" in details && typeof details["logDetails"] === "boolean") {
3108
- this.#LOG_DETAILS = details["logDetails"];
3132
+ config(option = {}) {
3133
+ if (typeof option.xmlHttpRequest === "function") {
3134
+ this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
3109
3135
  }
3110
- this.#defaultDetails = utils.assign(this.#defaultDetails, details);
3136
+ this.#defaultRequestOption = utils.assign(this.#defaultRequestOption, option);
3137
+ this.#defaultInitOption = utils.assign(this.#defaultInitOption, option);
3111
3138
  }
3112
3139
  /**
3113
3140
  * 拦截器
@@ -3189,149 +3216,74 @@
3189
3216
  * @param url 网址
3190
3217
  * @param details 配置
3191
3218
  */
3192
- get(...args // @ts-ignore
3193
- ) {
3194
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3195
- let abortFn = null;
3196
- let promise = new globalThis.Promise(async (resolve, reject) => {
3197
- let requestOption = this.HttpxRequestOption.getRequestOption("GET", userRequestOption, resolve, reject);
3198
- Reflect.deleteProperty(requestOption, "onprogress");
3199
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3200
- const requestResult = await this.HttpxRequest.request(requestOption);
3201
- if (requestResult != null &&
3202
- typeof requestResult.abort === "function") {
3203
- abortFn = requestResult.abort;
3204
- }
3219
+ get(...args) {
3220
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3221
+ useRequestOption.method = "GET";
3222
+ return this.request(useRequestOption, (option) => {
3223
+ Reflect.deleteProperty(option, "onprogress");
3205
3224
  });
3206
- // @ts-ignore
3207
- promise.abort = () => {
3208
- if (typeof abortFn === "function") {
3209
- abortFn();
3210
- }
3211
- };
3212
- // @ts-ignore
3213
- return promise;
3214
3225
  }
3215
3226
  /**
3216
3227
  * POST 请求
3217
3228
  */
3218
- post(...args // @ts-ignore
3219
- ) {
3220
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3221
- let abortFn = null;
3222
- let promise = new Promise(async (resolve, reject) => {
3223
- let requestOption = this.HttpxRequestOption.getRequestOption("POST", userRequestOption, resolve, reject);
3224
- // @ts-ignore
3225
- requestOption =
3226
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3227
- const requestResult = await this.HttpxRequest.request(requestOption);
3228
- if (requestResult != null &&
3229
- typeof requestResult.abort === "function") {
3230
- abortFn = requestResult.abort;
3231
- }
3232
- });
3233
- // @ts-ignore
3234
- promise.abort = () => {
3235
- if (typeof abortFn === "function") {
3236
- abortFn();
3237
- }
3238
- };
3239
- // @ts-ignore
3240
- return promise;
3229
+ post(...args) {
3230
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3231
+ useRequestOption.method = "POST";
3232
+ return this.request(useRequestOption);
3241
3233
  }
3242
3234
  /**
3243
3235
  * HEAD 请求
3244
3236
  */
3245
- head(...args // @ts-ignore
3246
- ) {
3247
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3248
- let abortFn = null;
3249
- let promise = new Promise(async (resolve, reject) => {
3250
- let requestOption = this.HttpxRequestOption.getRequestOption("HEAD", userRequestOption, resolve, reject);
3251
- Reflect.deleteProperty(requestOption, "onprogress");
3252
- // @ts-ignore
3253
- requestOption =
3254
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3255
- const requestResult = await this.HttpxRequest.request(requestOption);
3256
- if (requestResult != null &&
3257
- typeof requestResult.abort === "function") {
3258
- abortFn = requestResult.abort;
3259
- }
3237
+ head(...args) {
3238
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3239
+ useRequestOption.method = "HEAD";
3240
+ return this.request(useRequestOption, (option) => {
3241
+ Reflect.deleteProperty(option, "onprogress");
3260
3242
  });
3261
- // @ts-ignore
3262
- promise.abort = () => {
3263
- if (typeof abortFn === "function") {
3264
- abortFn();
3265
- }
3266
- };
3267
- // @ts-ignore
3268
- return promise;
3269
3243
  }
3270
3244
  /**
3271
3245
  * OPTIONS 请求
3272
3246
  */
3273
- options(...args // @ts-ignore
3274
- ) {
3275
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3276
- let abortFn = null;
3277
- let promise = new Promise(async (resolve, reject) => {
3278
- let requestOption = this.HttpxRequestOption.getRequestOption("OPTIONS", userRequestOption, resolve, reject);
3279
- Reflect.deleteProperty(requestOption, "onprogress");
3280
- // @ts-ignore
3281
- requestOption =
3282
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3283
- const requestResult = await this.HttpxRequest.request(requestOption);
3284
- if (requestResult != null &&
3285
- typeof requestResult.abort === "function") {
3286
- abortFn = requestResult.abort;
3287
- }
3247
+ options(...args) {
3248
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3249
+ useRequestOption.method = "OPTIONS";
3250
+ return this.request(useRequestOption, (option) => {
3251
+ Reflect.deleteProperty(option, "onprogress");
3288
3252
  });
3289
- // @ts-ignore
3290
- promise.abort = () => {
3291
- if (typeof abortFn === "function") {
3292
- abortFn();
3293
- }
3294
- };
3295
- // @ts-ignore
3296
- return promise;
3297
3253
  }
3298
3254
  /**
3299
3255
  * DELETE 请求
3300
3256
  */
3301
- delete(...args // @ts-ignore
3302
- ) {
3303
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3304
- let abortFn = null;
3305
- let promise = new Promise(async (resolve, reject) => {
3306
- let requestOption = this.HttpxRequestOption.getRequestOption("DELETE", userRequestOption, resolve, reject);
3307
- Reflect.deleteProperty(requestOption, "onprogress");
3308
- // @ts-ignore
3309
- requestOption =
3310
- this.HttpxRequestOption.removeRequestNullOption(requestOption);
3311
- const requestResult = await this.HttpxRequest.request(requestOption);
3312
- if (requestResult != null &&
3313
- typeof requestResult.abort === "function") {
3314
- abortFn = requestResult.abort;
3315
- }
3257
+ delete(...args) {
3258
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3259
+ useRequestOption.method = "DELETE";
3260
+ return this.request(useRequestOption, (option) => {
3261
+ Reflect.deleteProperty(option, "onprogress");
3316
3262
  });
3317
- // @ts-ignore
3318
- promise.abort = () => {
3319
- if (typeof abortFn === "function") {
3320
- abortFn();
3321
- }
3322
- };
3323
- // @ts-ignore
3324
- return promise;
3325
3263
  }
3326
3264
  /**
3327
3265
  * PUT 请求
3328
3266
  */
3329
- put(...args // @ts-ignore
3330
- ) {
3331
- let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOption(...args);
3267
+ put(...args) {
3268
+ let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
3269
+ userRequestOption.method = "PUT";
3270
+ return this.request(userRequestOption);
3271
+ }
3272
+ /**
3273
+ * 发送请求
3274
+ * @param details 配置
3275
+ * @param beforeRequestOption 处理请求前的配置
3276
+ */
3277
+ request(details, beforeRequestOption) {
3278
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
3279
+ /** 取消请求 */
3332
3280
  let abortFn = null;
3333
- let promise = new Promise(async (resolve, reject) => {
3334
- let requestOption = this.HttpxRequestOption.getRequestOption("PUT", userRequestOption, resolve, reject);
3281
+ let promise = new globalThis.Promise(async (resolve, reject) => {
3282
+ let requestOption = this.HttpxRequestOption.getRequestOption(useRequestOption.method, useRequestOption, resolve, reject);
3283
+ if (typeof beforeRequestOption === "function") {
3284
+ // @ts-ignore
3285
+ beforeRequestOption(requestOption);
3286
+ }
3335
3287
  // @ts-ignore
3336
3288
  requestOption =
3337
3289
  this.HttpxRequestOption.removeRequestNullOption(requestOption);
@@ -5059,7 +5011,7 @@
5059
5011
  this.windowApi = new WindowApi(option);
5060
5012
  }
5061
5013
  /** 版本号 */
5062
- version = "2025.3.25";
5014
+ version = "2025.4.11";
5063
5015
  addStyle(cssText) {
5064
5016
  if (typeof cssText !== "string") {
5065
5017
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -8228,6 +8180,22 @@
8228
8180
  }
8229
8181
  return new URL(text);
8230
8182
  }
8183
+ /**
8184
+ * 覆盖对象中的函数this指向
8185
+ * @param target 需要覆盖的对象
8186
+ * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
8187
+ */
8188
+ coverObjectFunctionThis(target, objectThis) {
8189
+ if (typeof target !== "object" || target === null) {
8190
+ throw new Error("target must be object");
8191
+ }
8192
+ objectThis = objectThis || target;
8193
+ Object.keys(target).forEach((key) => {
8194
+ if (typeof target[key] === "function") {
8195
+ target[key] = target[key].bind(objectThis);
8196
+ }
8197
+ });
8198
+ }
8231
8199
  /**
8232
8200
  * 生成uuid
8233
8201
  * @example