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