@whitesev/utils 1.1.3 → 1.1.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 +84 -6
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +84 -6
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +84 -6
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +84 -6
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +84 -6
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +84 -6
- package/dist/index.umd.js.map +1 -1
- package/dist/src/UtilsGMCookie.d.ts +33 -2
- package/package.json +1 -1
- package/src/UtilsGMCookie.ts +93 -11
|
@@ -11,15 +11,46 @@ declare interface UtilsGMCookieListResult {
|
|
|
11
11
|
session: false;
|
|
12
12
|
value: string;
|
|
13
13
|
}
|
|
14
|
+
declare interface UtilsGMCookieListOptions {
|
|
15
|
+
/** 默认为当前的url */
|
|
16
|
+
url: string;
|
|
17
|
+
/** 默认为当前的域名(window.location.hostname) */
|
|
18
|
+
domain: string;
|
|
19
|
+
/** 需要检索的Cookie的名字 */
|
|
20
|
+
name: string | RegExp;
|
|
21
|
+
/** 需要检索的Cookie的路径,默认为"/" */
|
|
22
|
+
path: string;
|
|
23
|
+
}
|
|
14
24
|
declare class UtilsGMCookie {
|
|
15
25
|
/**
|
|
16
|
-
*
|
|
26
|
+
* 获取单个cookie
|
|
27
|
+
* @param cookieName cookie名
|
|
28
|
+
*/
|
|
29
|
+
get(cookieName: string): {
|
|
30
|
+
domain: string;
|
|
31
|
+
expirationDate: null;
|
|
32
|
+
hostOnly: boolean;
|
|
33
|
+
httpOnly: boolean;
|
|
34
|
+
name: string;
|
|
35
|
+
path: string;
|
|
36
|
+
sameSite: string;
|
|
37
|
+
secure: boolean;
|
|
38
|
+
session: boolean;
|
|
39
|
+
value: string;
|
|
40
|
+
} | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* 获取多组Cookie
|
|
17
43
|
* @param paramDetails
|
|
18
44
|
* @param callback
|
|
19
45
|
* + cookies object[]
|
|
20
46
|
* + error string|undefined
|
|
21
47
|
**/
|
|
22
|
-
list(paramDetails
|
|
48
|
+
list(paramDetails: Partial<UtilsGMCookieListOptions>, callback?: (data: UtilsGMCookieListResult[], error?: Error) => void): void;
|
|
49
|
+
/**
|
|
50
|
+
* 获取多组Cookie
|
|
51
|
+
* @param paramDetails
|
|
52
|
+
**/
|
|
53
|
+
getList(paramDetails: Partial<UtilsGMCookieListOptions>): UtilsGMCookieListResult[];
|
|
23
54
|
/**
|
|
24
55
|
* 设置Cookie
|
|
25
56
|
* @param paramDetails
|
package/package.json
CHANGED
package/src/UtilsGMCookie.ts
CHANGED
|
@@ -20,7 +20,7 @@ declare interface UtilsGMCookieListOptions {
|
|
|
20
20
|
/** 默认为当前的域名(window.location.hostname) */
|
|
21
21
|
domain: string;
|
|
22
22
|
/** 需要检索的Cookie的名字 */
|
|
23
|
-
name: string;
|
|
23
|
+
name: string | RegExp;
|
|
24
24
|
/** 需要检索的Cookie的路径,默认为"/" */
|
|
25
25
|
path: string;
|
|
26
26
|
}
|
|
@@ -53,20 +53,55 @@ declare interface UtilsGMCookieDeleteOptions {
|
|
|
53
53
|
|
|
54
54
|
class UtilsGMCookie {
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
56
|
+
* 获取单个cookie
|
|
57
|
+
* @param cookieName cookie名
|
|
58
|
+
*/
|
|
59
|
+
get(cookieName: string) {
|
|
60
|
+
if (typeof cookieName !== "string") {
|
|
61
|
+
throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let cookies = document.cookie.split(";");
|
|
65
|
+
let findCookie = cookies.find((item) => {
|
|
66
|
+
item = item.trimStart();
|
|
67
|
+
let itemName = item.split("=")[0];
|
|
68
|
+
let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
|
|
69
|
+
if (itemName === cookieName) {
|
|
70
|
+
return itemValue;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
if (findCookie) {
|
|
74
|
+
return {
|
|
75
|
+
domain: globalThis.location.hostname,
|
|
76
|
+
expirationDate: null,
|
|
77
|
+
hostOnly: true,
|
|
78
|
+
httpOnly: false,
|
|
79
|
+
name: cookieName,
|
|
80
|
+
path: "/",
|
|
81
|
+
sameSite: "unspecified",
|
|
82
|
+
secure: true,
|
|
83
|
+
session: false,
|
|
84
|
+
value: findCookie,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 获取多组Cookie
|
|
57
90
|
* @param paramDetails
|
|
58
91
|
* @param callback
|
|
59
92
|
* + cookies object[]
|
|
60
93
|
* + error string|undefined
|
|
61
94
|
**/
|
|
62
95
|
list(
|
|
63
|
-
paramDetails: Partial<
|
|
64
|
-
callback
|
|
96
|
+
paramDetails: Partial<UtilsGMCookieListOptions>,
|
|
97
|
+
callback?: (data: UtilsGMCookieListResult[], error?: Error) => void
|
|
65
98
|
) {
|
|
99
|
+
if (paramDetails == null) {
|
|
100
|
+
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
101
|
+
}
|
|
66
102
|
let resultData: UtilsGMCookieListResult[] = [];
|
|
67
103
|
try {
|
|
68
|
-
let details: Partial<
|
|
69
|
-
// @ts-ignore
|
|
104
|
+
let details: Partial<UtilsGMCookieListOptions> = {
|
|
70
105
|
url: globalThis.location.href,
|
|
71
106
|
domain: globalThis.location.hostname,
|
|
72
107
|
name: "",
|
|
@@ -79,7 +114,7 @@ class UtilsGMCookie {
|
|
|
79
114
|
let itemName = item.split("=")[0];
|
|
80
115
|
let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
|
|
81
116
|
let nameRegexp =
|
|
82
|
-
(details.name as
|
|
117
|
+
(details.name as RegExp) instanceof RegExp
|
|
83
118
|
? details.name
|
|
84
119
|
: new RegExp("^" + details.name, "g");
|
|
85
120
|
if (itemName.match(nameRegexp as RegExp)) {
|
|
@@ -95,13 +130,60 @@ class UtilsGMCookie {
|
|
|
95
130
|
session: false,
|
|
96
131
|
value: itemValue,
|
|
97
132
|
});
|
|
98
|
-
return;
|
|
99
133
|
}
|
|
100
134
|
});
|
|
101
|
-
callback
|
|
135
|
+
if (typeof callback === "function") {
|
|
136
|
+
callback(resultData);
|
|
137
|
+
}
|
|
102
138
|
} catch (error) {
|
|
103
|
-
|
|
139
|
+
if (typeof callback === "function") {
|
|
140
|
+
callback(resultData, error as Error);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* 获取多组Cookie
|
|
146
|
+
* @param paramDetails
|
|
147
|
+
**/
|
|
148
|
+
getList(
|
|
149
|
+
paramDetails: Partial<UtilsGMCookieListOptions>
|
|
150
|
+
): UtilsGMCookieListResult[] {
|
|
151
|
+
if (paramDetails == null) {
|
|
152
|
+
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
104
153
|
}
|
|
154
|
+
let resultData: UtilsGMCookieListResult[] = [];
|
|
155
|
+
let details: Partial<UtilsGMCookieListOptions> = {
|
|
156
|
+
url: globalThis.location.href,
|
|
157
|
+
domain: globalThis.location.hostname,
|
|
158
|
+
name: "",
|
|
159
|
+
path: "/",
|
|
160
|
+
};
|
|
161
|
+
details = Utils.assign(details, paramDetails);
|
|
162
|
+
let cookies = document.cookie.split(";");
|
|
163
|
+
cookies.forEach((item) => {
|
|
164
|
+
item = item.trimStart();
|
|
165
|
+
let itemName = item.split("=")[0];
|
|
166
|
+
let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
|
|
167
|
+
let nameRegexp =
|
|
168
|
+
(details.name as RegExp) instanceof RegExp
|
|
169
|
+
? details.name
|
|
170
|
+
: new RegExp("^" + details.name, "g");
|
|
171
|
+
if (itemName.match(nameRegexp as RegExp)) {
|
|
172
|
+
resultData.push({
|
|
173
|
+
domain: globalThis.location.hostname,
|
|
174
|
+
expirationDate: null,
|
|
175
|
+
hostOnly: true,
|
|
176
|
+
httpOnly: false,
|
|
177
|
+
name: itemName,
|
|
178
|
+
path: "/",
|
|
179
|
+
sameSite: "unspecified",
|
|
180
|
+
secure: true,
|
|
181
|
+
session: false,
|
|
182
|
+
value: itemValue,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
return resultData;
|
|
105
187
|
}
|
|
106
188
|
/**
|
|
107
189
|
* 设置Cookie
|
|
@@ -150,7 +232,7 @@ class UtilsGMCookie {
|
|
|
150
232
|
let details: Partial<UtilsGMCookieDeleteOptions> = {
|
|
151
233
|
url: window.location.href,
|
|
152
234
|
name: "",
|
|
153
|
-
|
|
235
|
+
// @ts-ignore
|
|
154
236
|
firstPartyDomain: "",
|
|
155
237
|
};
|
|
156
238
|
details = Utils.assign(details, paramDetails);
|