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