@whitesev/utils 1.5.1 → 1.5.3

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.
@@ -1139,7 +1139,7 @@ declare class Httpx {
1139
1139
  * 添加拦截器
1140
1140
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
1141
1141
  */
1142
- use(fn: <T = Required<HttpxDetails>>(details: T) => void | T): string | undefined;
1142
+ use(fn: <T extends Required<HttpxDetails>>(details: T) => void | T): string | undefined;
1143
1143
  /**
1144
1144
  * 移除拦截器
1145
1145
  * @param id 通过use返回的id
@@ -1162,7 +1162,7 @@ declare class Httpx {
1162
1162
  * @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
1163
1163
  * + 超出 2xx 范围的状态码都会触发该函数
1164
1164
  */
1165
- use(successFn?: (<T_1 = HttpxAsyncResultData<HttpxDetails>>(response: HttpxAsyncResultData) => void | T_1) | undefined, errorFn?: (<T_2 = HttpxHookErrorData>(data: T_2) => void | T_2) | undefined): string | undefined;
1165
+ use(successFn?: (<T_1 extends HttpxAsyncResultData<HttpxDetails>>(response: HttpxAsyncResultData) => void | T_1) | undefined, errorFn?: (<T_2 extends HttpxHookErrorData>(data: T_2) => void | T_2) | undefined): string | undefined;
1166
1166
  /**
1167
1167
  * 移除拦截器
1168
1168
  * @param id 通过use返回的id
@@ -847,6 +847,7 @@ declare class Utils {
847
847
  * Utils.isNotNull("123");
848
848
  * > true
849
849
  */
850
+ isNotNull<T>(value: T | null): value is T;
850
851
  isNotNull(...args: any[]): boolean;
851
852
  /**
852
853
  * 判断对象或数据是否为空
@@ -893,6 +894,7 @@ declare class Utils {
893
894
  Utils.isNull(false,[123]);
894
895
  > false
895
896
  **/
897
+ isNull<T>(value: T | null): value is null;
896
898
  isNull(...args: any[]): boolean;
897
899
  /**
898
900
  * 判断浏览器主题是否是暗黑|深色模式
@@ -1732,11 +1734,23 @@ declare class Utils {
1732
1734
  /**
1733
1735
  * 将对象转换为FormData
1734
1736
  * @param data 待转换的对象
1735
- * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
1737
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent),默认false
1738
+ * @param valueAutoParseToStr 是否对值强制使用JSON.stringify()转换,默认false
1739
+ * @example
1740
+ * Utils.toFormData({
1741
+ * test: "1",
1742
+ * 666: 666,
1743
+ * })
1736
1744
  */
1737
1745
  toFormData(data: {
1738
1746
  [key: string]: string | Blob | File | number;
1739
- }, isEncode?: boolean): FormData;
1747
+ }, isEncode?: boolean, valueAutoParseToStr?: boolean): FormData;
1748
+ /**
1749
+ * 生成uuid
1750
+ * @example
1751
+ * Utils.generateUUID()
1752
+ */
1753
+ generateUUID(): string;
1740
1754
  }
1741
1755
  declare let utils: Utils;
1742
1756
  export { utils as Utils };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
package/src/Httpx.ts CHANGED
@@ -2082,7 +2082,7 @@ class Httpx {
2082
2082
  * 添加拦截器
2083
2083
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
2084
2084
  */
2085
- use(fn: <T = Required<HttpxDetails>>(details: T) => void | T) {
2085
+ use(fn: <T extends Required<HttpxDetails>>(details: T) => void | T) {
2086
2086
  if (typeof fn !== "function") {
2087
2087
  console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
2088
2088
  return;
@@ -2116,10 +2116,10 @@ class Httpx {
2116
2116
  * + 超出 2xx 范围的状态码都会触发该函数
2117
2117
  */
2118
2118
  use(
2119
- successFn?: <T = HttpxAsyncResultData>(
2119
+ successFn?: <T extends HttpxAsyncResultData>(
2120
2120
  response: HttpxAsyncResultData
2121
2121
  ) => void | T,
2122
- errorFn?: <T = HttpxHookErrorData>(data: T) => void | T
2122
+ errorFn?: <T extends HttpxHookErrorData>(data: T) => void | T
2123
2123
  ) {
2124
2124
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
2125
2125
  console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
package/src/Utils.ts CHANGED
@@ -1323,7 +1323,7 @@ class Utils {
1323
1323
  ];
1324
1324
  let androidVersion = UtilsContext.getRandomValue(12, 14);
1325
1325
  let randomMobile = UtilsContext.getRandomValue(mobileNameList);
1326
- let chromeVersion1 = UtilsContext.getRandomValue(115, 125);
1326
+ let chromeVersion1 = UtilsContext.getRandomValue(115, 127);
1327
1327
  let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
1328
1328
  let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
1329
1329
  let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
@@ -1417,7 +1417,7 @@ class Utils {
1417
1417
  **/
1418
1418
  getRandomPCUA(): string {
1419
1419
  let UtilsContext = this;
1420
- let chromeVersion1 = UtilsContext.getRandomValue(115, 126);
1420
+ let chromeVersion1 = UtilsContext.getRandomValue(115, 127);
1421
1421
  let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
1422
1422
  let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
1423
1423
  let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
@@ -2019,6 +2019,7 @@ class Utils {
2019
2019
  * Utils.isNotNull("123");
2020
2020
  * > true
2021
2021
  */
2022
+ isNotNull<T>(value: T | null): value is T;
2022
2023
  isNotNull(...args: any[]): boolean;
2023
2024
  isNotNull(...args: any[]): boolean {
2024
2025
  let UtilsContext = this;
@@ -2069,6 +2070,7 @@ class Utils {
2069
2070
  Utils.isNull(false,[123]);
2070
2071
  > false
2071
2072
  **/
2073
+ isNull<T>(value: T | null): value is null;
2072
2074
  isNull(...args: any[]): boolean;
2073
2075
  isNull(...args: any[]): boolean {
2074
2076
  let result = true;
@@ -4834,15 +4836,25 @@ class Utils {
4834
4836
  /**
4835
4837
  * 将对象转换为FormData
4836
4838
  * @param data 待转换的对象
4837
- * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
4839
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent),默认false
4840
+ * @param valueAutoParseToStr 是否对值强制使用JSON.stringify()转换,默认false
4841
+ * @example
4842
+ * Utils.toFormData({
4843
+ * test: "1",
4844
+ * 666: 666,
4845
+ * })
4838
4846
  */
4839
4847
  toFormData(
4840
4848
  data: { [key: string]: string | Blob | File | number },
4841
- isEncode: boolean = false
4849
+ isEncode: boolean = false,
4850
+ valueAutoParseToStr: boolean = false
4842
4851
  ) {
4843
4852
  const formData = new FormData();
4844
4853
  Object.keys(data).forEach((key) => {
4845
4854
  let value = data[key];
4855
+ if (valueAutoParseToStr) {
4856
+ value = JSON.stringify(value);
4857
+ }
4846
4858
  if (typeof value === "number") {
4847
4859
  value = value.toString();
4848
4860
  }
@@ -4857,6 +4869,26 @@ class Utils {
4857
4869
  });
4858
4870
  return formData;
4859
4871
  }
4872
+ /**
4873
+ * 生成uuid
4874
+ * @example
4875
+ * Utils.generateUUID()
4876
+ */
4877
+ generateUUID() {
4878
+ if (typeof globalThis?.crypto?.randomUUID === "function") {
4879
+ return globalThis.crypto.randomUUID();
4880
+ } else {
4881
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
4882
+ /[xy]/g,
4883
+ function (charStr) {
4884
+ var randomValue = (Math.random() * 16) | 0,
4885
+ randomCharValue =
4886
+ charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
4887
+ return randomCharValue.toString(16);
4888
+ }
4889
+ );
4890
+ }
4891
+ }
4860
4892
  }
4861
4893
 
4862
4894
  let utils = new Utils();