ls-pro-common 3.1.0 → 3.1.1

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.
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ declare type Getter<T> = () => T;
3
+ export default function useSyncState<T>(defaultValue: T | Getter<T>): readonly [T, (action: React.SetStateAction<T>) => void, Getter<T>];
4
+ export {};
@@ -0,0 +1,18 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import React from 'react';
3
+ export default function useSyncState(defaultValue) {
4
+ var _React$useState = React.useState(0),
5
+ _React$useState2 = _slicedToArray(_React$useState, 2),
6
+ forceUpdate = _React$useState2[1];
7
+ var stateRef = React.useRef(typeof defaultValue === 'function' ? defaultValue() : defaultValue);
8
+ var setState = React.useCallback(function (action) {
9
+ stateRef.current = typeof action === 'function' ? action(stateRef.current) : action;
10
+ forceUpdate(function (prev) {
11
+ return prev + 1;
12
+ });
13
+ }, []);
14
+ var getState = React.useCallback(function () {
15
+ return stateRef.current;
16
+ }, []);
17
+ return [stateRef.current, setState, getState];
18
+ }
@@ -1,4 +1,11 @@
1
1
  declare const request: import("umi-request").RequestMethod<false>;
2
+ /**
3
+ * 判断URL是否跨域
4
+ *
5
+ * @param url 需要判断的URL
6
+ * @returns 如果是跨域返回true,否则返回false
7
+ */
8
+ export declare const isCrossDomain: (url: string) => boolean;
2
9
  /**
3
10
  * Get请求
4
11
  *
package/es/http/index.js CHANGED
@@ -24,6 +24,33 @@ var browserId = '';
24
24
  } catch (_unused) {}
25
25
  }, 0);
26
26
  })();
27
+ //是否跨域
28
+ /**
29
+ * 判断URL是否跨域
30
+ *
31
+ * @param url 需要判断的URL
32
+ * @returns 如果是跨域返回true,否则返回false
33
+ */
34
+ export var isCrossDomain = function isCrossDomain(url) {
35
+ // 如果URL不是以http(s)://开头,则直接认为是同源
36
+ if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('//')) {
37
+ return false;
38
+ }
39
+ try {
40
+ // 创建URL对象
41
+ var currentUrl = window.location;
42
+ if (url.startsWith('//')) {
43
+ url = currentUrl.protocol + url;
44
+ }
45
+ var targetUrl = new URL(url);
46
+ // 判断协议、域名、端口是否一致
47
+ return targetUrl.protocol !== currentUrl.protocol || targetUrl.hostname !== currentUrl.hostname || targetUrl.port !== currentUrl.port;
48
+ } catch (e) {
49
+ // URL解析失败,返回true表示跨域
50
+ console.warn('URL解析失败:', e);
51
+ return true;
52
+ }
53
+ };
27
54
  /** 请求拦截器,统一添加token */
28
55
  request.interceptors.request.use(function (url, options) {
29
56
  var _options$params;
@@ -57,6 +84,11 @@ request.interceptors.request.use(function (url, options) {
57
84
  options.headers['x-asm-prefer-tag'] = tag; //灰度发版标识
58
85
  }
59
86
 
87
+ var token = getCookie('token');
88
+ if (token && url.indexOf('noToken=1') === -1) {
89
+ //@ts-ignore
90
+ options.headers['token'] = token;
91
+ }
60
92
  url = url.replace('&noToken=1', '').replace('noToken=1', '');
61
93
  var resCode = getUrlQuery('resCode', url) || getUrlQuery('resCode') || getUrlQuery('resourceId') || getResourceProps('resourceId');
62
94
  var param = {
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ declare type Getter<T> = () => T;
3
+ export default function useSyncState<T>(defaultValue: T | Getter<T>): readonly [T, (action: React.SetStateAction<T>) => void, Getter<T>];
4
+ export {};
@@ -0,0 +1,18 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import React from 'react';
3
+ export default function useSyncState(defaultValue) {
4
+ var _React$useState = React.useState(0),
5
+ _React$useState2 = _slicedToArray(_React$useState, 2),
6
+ forceUpdate = _React$useState2[1];
7
+ var stateRef = React.useRef(typeof defaultValue === 'function' ? defaultValue() : defaultValue);
8
+ var setState = React.useCallback(function (action) {
9
+ stateRef.current = typeof action === 'function' ? action(stateRef.current) : action;
10
+ forceUpdate(function (prev) {
11
+ return prev + 1;
12
+ });
13
+ }, []);
14
+ var getState = React.useCallback(function () {
15
+ return stateRef.current;
16
+ }, []);
17
+ return [stateRef.current, setState, getState];
18
+ }
@@ -1,4 +1,11 @@
1
1
  declare const request: import("umi-request").RequestMethod<false>;
2
+ /**
3
+ * 判断URL是否跨域
4
+ *
5
+ * @param url 需要判断的URL
6
+ * @returns 如果是跨域返回true,否则返回false
7
+ */
8
+ export declare const isCrossDomain: (url: string) => boolean;
2
9
  /**
3
10
  * Get请求
4
11
  *
package/lib/http/index.js CHANGED
@@ -24,6 +24,33 @@ var browserId = '';
24
24
  } catch (_unused) {}
25
25
  }, 0);
26
26
  })();
27
+ //是否跨域
28
+ /**
29
+ * 判断URL是否跨域
30
+ *
31
+ * @param url 需要判断的URL
32
+ * @returns 如果是跨域返回true,否则返回false
33
+ */
34
+ export var isCrossDomain = function isCrossDomain(url) {
35
+ // 如果URL不是以http(s)://开头,则直接认为是同源
36
+ if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('//')) {
37
+ return false;
38
+ }
39
+ try {
40
+ // 创建URL对象
41
+ var currentUrl = window.location;
42
+ if (url.startsWith('//')) {
43
+ url = currentUrl.protocol + url;
44
+ }
45
+ var targetUrl = new URL(url);
46
+ // 判断协议、域名、端口是否一致
47
+ return targetUrl.protocol !== currentUrl.protocol || targetUrl.hostname !== currentUrl.hostname || targetUrl.port !== currentUrl.port;
48
+ } catch (e) {
49
+ // URL解析失败,返回true表示跨域
50
+ console.warn('URL解析失败:', e);
51
+ return true;
52
+ }
53
+ };
27
54
  /** 请求拦截器,统一添加token */
28
55
  request.interceptors.request.use(function (url, options) {
29
56
  var _options$params;
@@ -57,6 +84,11 @@ request.interceptors.request.use(function (url, options) {
57
84
  options.headers['x-asm-prefer-tag'] = tag; //灰度发版标识
58
85
  }
59
86
 
87
+ var token = getCookie('token');
88
+ if (token && url.indexOf('noToken=1') === -1) {
89
+ //@ts-ignore
90
+ options.headers['token'] = token;
91
+ }
60
92
  url = url.replace('&noToken=1', '').replace('noToken=1', '');
61
93
  var resCode = getUrlQuery('resCode', url) || getUrlQuery('resCode') || getUrlQuery('resourceId') || getResourceProps('resourceId');
62
94
  var param = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ls-pro-common",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "ls-pro-common",
5
5
  "license": "MIT",
6
6
  "sideEffects": [