@hzab/data-model 0.0.3 → 0.1.0

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.
Files changed (2) hide show
  1. package/package.json +12 -3
  2. package/src/axios.js +40 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/data-model",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
4
4
  "description": "data model",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -25,7 +25,6 @@
25
25
  "@types/react": "^17.0.62",
26
26
  "@types/react-dom": "^17.0.20",
27
27
  "antd": "^4.14.0",
28
- "axios": "^1.4.0",
29
28
  "eslint": "^8.30.0",
30
29
  "less": "^4.1.3",
31
30
  "mobx": "^6.7.0",
@@ -34,7 +33,17 @@
34
33
  "react-dom": "^17.0.2",
35
34
  "react-router-dom": "^6.14.1",
36
35
  "typedoc": "^0.24.8",
37
- "typescript": "^4.9.4"
36
+ "typescript": "^4.9.4",
37
+ "axios": "^1.6.2",
38
+ "js-cookie": "^3.0.5"
39
+ },
40
+ "peerDependencies": {
41
+ "axios": ">=1.6.2",
42
+ "js-cookie": ">=3.0.5"
43
+ },
44
+ "dependencies": {
45
+ "axios": "^1.6.2",
46
+ "js-cookie": "^3.0.5"
38
47
  },
39
48
  "directories": {
40
49
  "lib": "lib"
package/src/axios.js CHANGED
@@ -1,4 +1,43 @@
1
1
  import axios from "axios";
2
+ import Cookies from "js-cookie";
3
+ import _ from "lodash";
4
+
5
+ export const TOKEN = "TOKEN";
6
+
7
+ /**
8
+ * 设置 token
9
+ * @param {string} token
10
+ * @param {Object} opt
11
+ */
12
+ export function setToken(token = "", opt) {
13
+ const { hasCookie } = opt || {};
14
+ const _t = token || "";
15
+ if (hasCookie !== false) {
16
+ const { cookieAttrs } = opt || {};
17
+ const cookieOpt = _.merge(
18
+ {
19
+ expires: 365,
20
+ },
21
+ cookieAttrs,
22
+ );
23
+ Cookies.set(TOKEN, _t, cookieOpt);
24
+ }
25
+ localStorage.setItem(TOKEN, _t);
26
+ setAxAuthorization(_t);
27
+ }
28
+
29
+ /**
30
+ * 获取 token
31
+ * @returns
32
+ */
33
+ export function getToken() {
34
+ let token = Cookies.get(TOKEN) || "";
35
+ if (token) {
36
+ return token;
37
+ }
38
+ token = localStorage.getItem(TOKEN);
39
+ return token;
40
+ }
2
41
 
3
42
  export const temp = { axiosList: [axios] };
4
43
 
@@ -67,7 +106,7 @@ export function setAxTimeout(timeout = 5000) {
67
106
  }
68
107
 
69
108
  /**
70
- * 设置 token
109
+ * 设置 Authorization token
71
110
  * @param {string} token
72
111
  */
73
112
  export function setAxAuthorization(token) {