-
Notifications
You must be signed in to change notification settings - Fork 2
/
cookie.ts
54 lines (50 loc) · 1.35 KB
/
cookie.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Cookies, { CookieAttributes } from "js-cookie";
/**
* 读取cookie值
* @function getCookie
*
* @param {string} name cookie名称
* @returns {string | undefined} cookie值
*
* @example
* const cookie = getCookie('user');
* console.log('cookie值', cookie);
*/
export const getCookie = (name: string): string | undefined => Cookies.get(name);
/**
* 读取所有cookie
* @function getAllCookies
*
* @returns {object} cookie信息,例:{userName: 'admin'}
*
* @example
* const cookie = getAllCookies();
* console.log('所有cookie信息', cookie);
*/
export const getAllCookies = (): { [key: string]: string } => Cookies.get();
/**
* 设置cookie值
* @function setCookie
*
* @param {string} name cookie名称
* @param {string} value cookie值
* @param {CookieAttributes | undefined} options cookie属性
*
* @returns {string | undefined}
*
* @example
* setCookie('userName', 'admin');
*/
export const setCookie = (name: string, value: string, options?: CookieAttributes | undefined): string | undefined =>
Cookies.set(name, value, options);
/**
* 删除cookie
* @function removeCookie
*
* @param {string} name cookie名称
* @param {CookieAttributes} options cookie属性
*
* @example
* removeCookie('userName');
*/
export const removeCookie = (name: string, options?: CookieAttributes): void => Cookies.remove(name, options);