@whitesev/utils 2.5.3 → 2.5.4
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.amd.js +46 -36
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +46 -36
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +46 -36
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +46 -36
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +46 -36
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +46 -36
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/UtilsGMCookie.d.ts +8 -8
- package/dist/types/src/types/UtilsGMCookie.d.ts +54 -16
- package/dist/types/src/types/global.d.ts +3 -0
- package/package.json +3 -2
- package/src/Utils.ts +1 -1
- package/src/UtilsGMCookie.ts +44 -42
- package/src/types/UtilsGMCookie.d.ts +54 -16
- package/src/types/global.d.ts +3 -0
|
@@ -9,28 +9,28 @@ declare class UtilsGMCookie {
|
|
|
9
9
|
get(cookieName: string): UtilsGMCookieResult | undefined;
|
|
10
10
|
/**
|
|
11
11
|
* 获取多组Cookie
|
|
12
|
-
* @param
|
|
12
|
+
* @param option 配置
|
|
13
13
|
* @param callback 获取操作后的回调
|
|
14
14
|
* + cookies object[]
|
|
15
15
|
* + error string|undefined
|
|
16
16
|
**/
|
|
17
|
-
list(
|
|
17
|
+
list(option: UtilsGMCookieListOptions, callback?: (data: UtilsGMCookieResult[], error?: Error) => void): void;
|
|
18
18
|
/**
|
|
19
19
|
* 获取多组Cookie
|
|
20
|
-
* @param
|
|
20
|
+
* @param option 配置
|
|
21
21
|
**/
|
|
22
|
-
getList(
|
|
22
|
+
getList(option: UtilsGMCookieListOptions): UtilsGMCookieResult[];
|
|
23
23
|
/**
|
|
24
24
|
* 设置Cookie
|
|
25
|
-
* @param
|
|
25
|
+
* @param option 配置
|
|
26
26
|
* @param callback 设置操作后的回调(成功/失败)
|
|
27
27
|
*/
|
|
28
|
-
set(
|
|
28
|
+
set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void): void;
|
|
29
29
|
/**
|
|
30
30
|
* 删除Cookie
|
|
31
|
-
* @param
|
|
31
|
+
* @param option 配置
|
|
32
32
|
* @param callback 删除操作后的回调(成功/失败)
|
|
33
33
|
*/
|
|
34
|
-
delete(
|
|
34
|
+
delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void): void;
|
|
35
35
|
}
|
|
36
36
|
export { UtilsGMCookie };
|
|
@@ -1,52 +1,90 @@
|
|
|
1
1
|
export interface UtilsGMCookieResult {
|
|
2
2
|
/** 为 window.location.hostname */
|
|
3
3
|
domain: string;
|
|
4
|
+
/** 过期时间 */
|
|
4
5
|
expirationDate: null;
|
|
5
6
|
hostOnly: true;
|
|
6
7
|
httpOnly: false;
|
|
8
|
+
/** Cookie名 */
|
|
7
9
|
name: string;
|
|
10
|
+
/** Cookie的路径 */
|
|
8
11
|
path: "/";
|
|
12
|
+
/** Cookie是否同源策略 */
|
|
9
13
|
sameSite: "unspecified";
|
|
10
14
|
secure: true;
|
|
11
15
|
session: false;
|
|
16
|
+
/** Cookie值 */
|
|
12
17
|
value: string;
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
export interface UtilsGMCookieListOptions {
|
|
16
21
|
/** 默认为当前的url */
|
|
17
|
-
url
|
|
18
|
-
/**
|
|
19
|
-
|
|
22
|
+
url?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Cookie所在域
|
|
25
|
+
* @default window.location.hostname
|
|
26
|
+
*/
|
|
27
|
+
domain?: string;
|
|
20
28
|
/** 需要检索的Cookie的名字 */
|
|
21
29
|
name: string | RegExp;
|
|
22
|
-
/**
|
|
23
|
-
|
|
30
|
+
/**
|
|
31
|
+
* 需要检索的Cookie的路径
|
|
32
|
+
* @default "/"
|
|
33
|
+
*/
|
|
34
|
+
path?: string;
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
export interface UtilsGMCookieSetOptions {
|
|
27
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* 默认为当前的url
|
|
40
|
+
*/
|
|
28
41
|
url?: string;
|
|
29
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Cookie所在域
|
|
44
|
+
* @default window.location.hostname
|
|
45
|
+
*/
|
|
30
46
|
domain?: string;
|
|
31
47
|
/** 需要检索的Cookie的名字 */
|
|
32
|
-
name
|
|
33
|
-
/**
|
|
48
|
+
name: string;
|
|
49
|
+
/**
|
|
50
|
+
* 需要检索的Cookie的路径
|
|
51
|
+
* @default "/"
|
|
52
|
+
*/
|
|
34
53
|
path?: string;
|
|
35
|
-
/** 值 */
|
|
36
|
-
value
|
|
37
|
-
/**
|
|
54
|
+
/** Cookie值 */
|
|
55
|
+
value: string | number;
|
|
56
|
+
/**
|
|
57
|
+
* 确保Cookie只在通过安全协议(如HTTPS)的情况下传输
|
|
58
|
+
* @default true
|
|
59
|
+
*/
|
|
38
60
|
secure?: boolean;
|
|
39
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* 是否防止JavaScript代码访问Cookie
|
|
63
|
+
* @default false
|
|
64
|
+
*/
|
|
40
65
|
httpOnly?: boolean;
|
|
41
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* Cookie过期时间的时间戳,默认为30天
|
|
68
|
+
* @default Math.floor(Date.now()) + 60 * 60 * 24 * 30
|
|
69
|
+
*/
|
|
42
70
|
expirationDate?: number;
|
|
43
71
|
}
|
|
44
72
|
|
|
45
73
|
export interface UtilsGMCookieDeleteOptions {
|
|
46
|
-
/** 默认为当前的url */
|
|
47
|
-
url: string;
|
|
48
74
|
/** 需要检索的Cookie的名字 */
|
|
49
75
|
name: string;
|
|
76
|
+
/** 默认为当前的url */
|
|
77
|
+
url?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Cookie的路径
|
|
80
|
+
* @default "/"
|
|
81
|
+
*/
|
|
82
|
+
path?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Cookie所在域
|
|
85
|
+
* @default window.location.hostname
|
|
86
|
+
*/
|
|
87
|
+
firstPartyDomain?: string;
|
|
50
88
|
}
|
|
51
89
|
|
|
52
90
|
export interface WindowApiOption {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whitesev/utils",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.4",
|
|
4
4
|
"description": "一个常用的工具库",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"scripts": {
|
|
47
47
|
"dev": "rollup --config --watch",
|
|
48
48
|
"build": "rollup --config",
|
|
49
|
-
"build:all": "rollup --config"
|
|
49
|
+
"build:all": "rollup --config",
|
|
50
|
+
"build:all-new": "rollup --config"
|
|
50
51
|
}
|
|
51
52
|
}
|
package/src/Utils.ts
CHANGED
package/src/UtilsGMCookie.ts
CHANGED
|
@@ -54,27 +54,27 @@ class UtilsGMCookie {
|
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
56
|
* 获取多组Cookie
|
|
57
|
-
* @param
|
|
57
|
+
* @param option 配置
|
|
58
58
|
* @param callback 获取操作后的回调
|
|
59
59
|
* + cookies object[]
|
|
60
60
|
* + error string|undefined
|
|
61
61
|
**/
|
|
62
62
|
list(
|
|
63
|
-
|
|
63
|
+
option: UtilsGMCookieListOptions,
|
|
64
64
|
callback?: (data: UtilsGMCookieResult[], error?: Error) => void
|
|
65
65
|
) {
|
|
66
|
-
if (
|
|
66
|
+
if (option == null) {
|
|
67
67
|
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
68
68
|
}
|
|
69
69
|
let resultData: UtilsGMCookieResult[] = [];
|
|
70
70
|
try {
|
|
71
|
-
let
|
|
71
|
+
let defaultOption: Required<UtilsGMCookieListOptions> = {
|
|
72
72
|
url: this.windowApi.window.location.href,
|
|
73
73
|
domain: this.windowApi.window.location.hostname,
|
|
74
74
|
name: "",
|
|
75
75
|
path: "/",
|
|
76
76
|
};
|
|
77
|
-
|
|
77
|
+
defaultOption = Utils.assign(defaultOption, option);
|
|
78
78
|
let cookies = this.windowApi.document.cookie.split(";");
|
|
79
79
|
cookies.forEach((item) => {
|
|
80
80
|
item = item.trim();
|
|
@@ -83,9 +83,9 @@ class UtilsGMCookie {
|
|
|
83
83
|
itemSplit.splice(0, 1);
|
|
84
84
|
let itemValue = decodeURIComponent(itemSplit.join(""));
|
|
85
85
|
let nameRegexp =
|
|
86
|
-
(
|
|
87
|
-
?
|
|
88
|
-
: new RegExp("^" +
|
|
86
|
+
(defaultOption.name as RegExp) instanceof RegExp
|
|
87
|
+
? defaultOption.name
|
|
88
|
+
: new RegExp("^" + defaultOption.name, "g");
|
|
89
89
|
if (itemName.match(nameRegexp as RegExp)) {
|
|
90
90
|
resultData.push({
|
|
91
91
|
domain: this.windowApi.window.location.hostname,
|
|
@@ -112,22 +112,20 @@ class UtilsGMCookie {
|
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
114
114
|
* 获取多组Cookie
|
|
115
|
-
* @param
|
|
115
|
+
* @param option 配置
|
|
116
116
|
**/
|
|
117
|
-
getList(
|
|
118
|
-
|
|
119
|
-
): UtilsGMCookieResult[] {
|
|
120
|
-
if (paramDetails == null) {
|
|
117
|
+
getList(option: UtilsGMCookieListOptions): UtilsGMCookieResult[] {
|
|
118
|
+
if (option == null) {
|
|
121
119
|
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
122
120
|
}
|
|
123
121
|
let resultData: UtilsGMCookieResult[] = [];
|
|
124
|
-
let
|
|
122
|
+
let defaultOption: Required<UtilsGMCookieListOptions> = {
|
|
125
123
|
url: this.windowApi.window.location.href,
|
|
126
124
|
domain: this.windowApi.window.location.hostname,
|
|
127
125
|
name: "",
|
|
128
126
|
path: "/",
|
|
129
127
|
};
|
|
130
|
-
|
|
128
|
+
defaultOption = Utils.assign(defaultOption, option);
|
|
131
129
|
let cookies = this.windowApi.document.cookie.split(";");
|
|
132
130
|
cookies.forEach((item) => {
|
|
133
131
|
item = item.trim();
|
|
@@ -136,9 +134,9 @@ class UtilsGMCookie {
|
|
|
136
134
|
itemSplit.splice(0, 1);
|
|
137
135
|
let itemValue = decodeURIComponent(itemSplit.join(""));
|
|
138
136
|
let nameRegexp =
|
|
139
|
-
(
|
|
140
|
-
?
|
|
141
|
-
: new RegExp("^" +
|
|
137
|
+
(defaultOption.name as RegExp) instanceof RegExp
|
|
138
|
+
? defaultOption.name
|
|
139
|
+
: new RegExp("^" + defaultOption.name, "g");
|
|
142
140
|
if (itemName.match(nameRegexp as RegExp)) {
|
|
143
141
|
resultData.push({
|
|
144
142
|
domain: this.windowApi.window.location.hostname,
|
|
@@ -158,15 +156,13 @@ class UtilsGMCookie {
|
|
|
158
156
|
}
|
|
159
157
|
/**
|
|
160
158
|
* 设置Cookie
|
|
161
|
-
* @param
|
|
159
|
+
* @param option 配置
|
|
162
160
|
* @param callback 设置操作后的回调(成功/失败)
|
|
163
161
|
*/
|
|
164
|
-
set(
|
|
165
|
-
|
|
166
|
-
callback = (error?: Error) => {}
|
|
167
|
-
) {
|
|
162
|
+
set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
|
|
163
|
+
let errorInfo;
|
|
168
164
|
try {
|
|
169
|
-
let
|
|
165
|
+
let defaultOption: Required<UtilsGMCookieSetOptions> = {
|
|
170
166
|
url: this.windowApi.window.location.href,
|
|
171
167
|
name: "",
|
|
172
168
|
value: "",
|
|
@@ -179,46 +175,52 @@ class UtilsGMCookie {
|
|
|
179
175
|
*/
|
|
180
176
|
expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
|
|
181
177
|
};
|
|
182
|
-
|
|
183
|
-
let life =
|
|
184
|
-
?
|
|
178
|
+
defaultOption = Utils.assign(defaultOption, option);
|
|
179
|
+
let life = defaultOption.expirationDate
|
|
180
|
+
? defaultOption.expirationDate
|
|
185
181
|
: Math.floor(Date.now()) + 60 * 60 * 24 * 30;
|
|
186
182
|
let cookieStr =
|
|
187
|
-
|
|
183
|
+
defaultOption.name +
|
|
188
184
|
"=" +
|
|
189
|
-
decodeURIComponent(
|
|
185
|
+
decodeURIComponent(defaultOption.value as string) +
|
|
190
186
|
";expires=" +
|
|
191
187
|
(new Date(life) as any).toGMTString() +
|
|
192
188
|
"; path=/";
|
|
193
189
|
this.windowApi.document.cookie = cookieStr;
|
|
194
|
-
callback();
|
|
195
190
|
} catch (error: any) {
|
|
196
|
-
|
|
191
|
+
errorInfo = error;
|
|
192
|
+
} finally {
|
|
193
|
+
if (typeof callback === "function") {
|
|
194
|
+
callback(errorInfo);
|
|
195
|
+
}
|
|
197
196
|
}
|
|
198
197
|
}
|
|
199
198
|
/**
|
|
200
199
|
* 删除Cookie
|
|
201
|
-
* @param
|
|
200
|
+
* @param option 配置
|
|
202
201
|
* @param callback 删除操作后的回调(成功/失败)
|
|
203
202
|
*/
|
|
204
203
|
delete(
|
|
205
|
-
|
|
206
|
-
callback
|
|
204
|
+
option: UtilsGMCookieDeleteOptions,
|
|
205
|
+
callback?: (error?: Error) => void
|
|
207
206
|
) {
|
|
207
|
+
let errorInfo;
|
|
208
208
|
try {
|
|
209
|
-
let
|
|
209
|
+
let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
|
|
210
210
|
url: this.windowApi.window.location.href,
|
|
211
211
|
name: "",
|
|
212
|
-
|
|
213
|
-
firstPartyDomain:
|
|
212
|
+
path: "/",
|
|
213
|
+
firstPartyDomain: this.windowApi.window.location.hostname,
|
|
214
214
|
};
|
|
215
|
-
|
|
216
|
-
let cookieStr =
|
|
217
|
-
details.name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
215
|
+
defaultOption = Utils.assign(defaultOption, option);
|
|
216
|
+
let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}; domain=${defaultOption.firstPartyDomain};`;
|
|
218
217
|
this.windowApi.document.cookie = cookieStr;
|
|
219
|
-
callback();
|
|
220
218
|
} catch (error: any) {
|
|
221
|
-
|
|
219
|
+
errorInfo = error;
|
|
220
|
+
} finally {
|
|
221
|
+
if (typeof callback === "function") {
|
|
222
|
+
callback(errorInfo);
|
|
223
|
+
}
|
|
222
224
|
}
|
|
223
225
|
}
|
|
224
226
|
}
|
|
@@ -1,52 +1,90 @@
|
|
|
1
1
|
export interface UtilsGMCookieResult {
|
|
2
2
|
/** 为 window.location.hostname */
|
|
3
3
|
domain: string;
|
|
4
|
+
/** 过期时间 */
|
|
4
5
|
expirationDate: null;
|
|
5
6
|
hostOnly: true;
|
|
6
7
|
httpOnly: false;
|
|
8
|
+
/** Cookie名 */
|
|
7
9
|
name: string;
|
|
10
|
+
/** Cookie的路径 */
|
|
8
11
|
path: "/";
|
|
12
|
+
/** Cookie是否同源策略 */
|
|
9
13
|
sameSite: "unspecified";
|
|
10
14
|
secure: true;
|
|
11
15
|
session: false;
|
|
16
|
+
/** Cookie值 */
|
|
12
17
|
value: string;
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
export interface UtilsGMCookieListOptions {
|
|
16
21
|
/** 默认为当前的url */
|
|
17
|
-
url
|
|
18
|
-
/**
|
|
19
|
-
|
|
22
|
+
url?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Cookie所在域
|
|
25
|
+
* @default window.location.hostname
|
|
26
|
+
*/
|
|
27
|
+
domain?: string;
|
|
20
28
|
/** 需要检索的Cookie的名字 */
|
|
21
29
|
name: string | RegExp;
|
|
22
|
-
/**
|
|
23
|
-
|
|
30
|
+
/**
|
|
31
|
+
* 需要检索的Cookie的路径
|
|
32
|
+
* @default "/"
|
|
33
|
+
*/
|
|
34
|
+
path?: string;
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
export interface UtilsGMCookieSetOptions {
|
|
27
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* 默认为当前的url
|
|
40
|
+
*/
|
|
28
41
|
url?: string;
|
|
29
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Cookie所在域
|
|
44
|
+
* @default window.location.hostname
|
|
45
|
+
*/
|
|
30
46
|
domain?: string;
|
|
31
47
|
/** 需要检索的Cookie的名字 */
|
|
32
|
-
name
|
|
33
|
-
/**
|
|
48
|
+
name: string;
|
|
49
|
+
/**
|
|
50
|
+
* 需要检索的Cookie的路径
|
|
51
|
+
* @default "/"
|
|
52
|
+
*/
|
|
34
53
|
path?: string;
|
|
35
|
-
/** 值 */
|
|
36
|
-
value
|
|
37
|
-
/**
|
|
54
|
+
/** Cookie值 */
|
|
55
|
+
value: string | number;
|
|
56
|
+
/**
|
|
57
|
+
* 确保Cookie只在通过安全协议(如HTTPS)的情况下传输
|
|
58
|
+
* @default true
|
|
59
|
+
*/
|
|
38
60
|
secure?: boolean;
|
|
39
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* 是否防止JavaScript代码访问Cookie
|
|
63
|
+
* @default false
|
|
64
|
+
*/
|
|
40
65
|
httpOnly?: boolean;
|
|
41
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* Cookie过期时间的时间戳,默认为30天
|
|
68
|
+
* @default Math.floor(Date.now()) + 60 * 60 * 24 * 30
|
|
69
|
+
*/
|
|
42
70
|
expirationDate?: number;
|
|
43
71
|
}
|
|
44
72
|
|
|
45
73
|
export interface UtilsGMCookieDeleteOptions {
|
|
46
|
-
/** 默认为当前的url */
|
|
47
|
-
url: string;
|
|
48
74
|
/** 需要检索的Cookie的名字 */
|
|
49
75
|
name: string;
|
|
76
|
+
/** 默认为当前的url */
|
|
77
|
+
url?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Cookie的路径
|
|
80
|
+
* @default "/"
|
|
81
|
+
*/
|
|
82
|
+
path?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Cookie所在域
|
|
85
|
+
* @default window.location.hostname
|
|
86
|
+
*/
|
|
87
|
+
firstPartyDomain?: string;
|
|
50
88
|
}
|
|
51
89
|
|
|
52
90
|
export interface WindowApiOption {
|