beer-network 1.1.1-alpha.4 → 1.1.1-alpha.6
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/api.d.ts +12 -2
- package/api.js +53 -9
- package/elementUtils.js +1 -4
- package/package.json +1 -1
- package/session.js +1 -5
- package/utils.js +1 -4
package/api.d.ts
CHANGED
|
@@ -27,14 +27,24 @@ export default class Fetch {
|
|
|
27
27
|
* @param path 路径.
|
|
28
28
|
* @param params 参数.
|
|
29
29
|
* @param body 内容体.
|
|
30
|
+
* @param header 头部.
|
|
30
31
|
*/
|
|
31
|
-
post<T>(path: string, params?: any, body?: any): Promise<JsonResponse<T>>;
|
|
32
|
+
post<T>(path: string, params?: any, body?: any, header?: any): Promise<JsonResponse<T>>;
|
|
33
|
+
/**
|
|
34
|
+
* 发送POST/Form 请求.
|
|
35
|
+
* @param path 路径.
|
|
36
|
+
* @param params 参数.
|
|
37
|
+
* @param form Form参数.
|
|
38
|
+
* @param header 头部.
|
|
39
|
+
*/
|
|
40
|
+
postForm<T>(path: string, params?: any, form?: any, header?: any): Promise<JsonResponse<T>>;
|
|
32
41
|
/**
|
|
33
42
|
* 发送Get 请求.
|
|
34
43
|
* @param path 路径.
|
|
35
44
|
* @param params 参数.
|
|
45
|
+
* @param header 头部.
|
|
36
46
|
*/
|
|
37
|
-
get<T>(path: string, params?: any): Promise<JsonResponse<T>>;
|
|
47
|
+
get<T>(path: string, params?: any, header?: any): Promise<JsonResponse<T>>;
|
|
38
48
|
/**
|
|
39
49
|
* 缓存.
|
|
40
50
|
* @param key 缓存Key.
|
package/api.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const session_1 = require("./session");
|
|
4
|
-
class Fetch {
|
|
1
|
+
import { Session } from './session';
|
|
2
|
+
export default class Fetch {
|
|
5
3
|
constructor(pathPrefix) {
|
|
6
4
|
this.pathPrefix = pathPrefix;
|
|
7
5
|
}
|
|
@@ -10,8 +8,9 @@ class Fetch {
|
|
|
10
8
|
* @param path 路径.
|
|
11
9
|
* @param params 参数.
|
|
12
10
|
* @param body 内容体.
|
|
11
|
+
* @param header 头部.
|
|
13
12
|
*/
|
|
14
|
-
async post(path, params, body) {
|
|
13
|
+
async post(path, params, body, header) {
|
|
15
14
|
const paramQuery = new URLSearchParams();
|
|
16
15
|
Object.keys(params || {})
|
|
17
16
|
.forEach(key => {
|
|
@@ -21,18 +20,63 @@ class Fetch {
|
|
|
21
20
|
method: 'POST',
|
|
22
21
|
headers: {
|
|
23
22
|
...this.authorization(),
|
|
23
|
+
...(header || {}),
|
|
24
24
|
'Content-Type': 'application/json'
|
|
25
25
|
},
|
|
26
26
|
body: JSON.stringify(body || {})
|
|
27
27
|
});
|
|
28
28
|
return this.responseJson(response);
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* 发送POST/Form 请求.
|
|
32
|
+
* @param path 路径.
|
|
33
|
+
* @param params 参数.
|
|
34
|
+
* @param form Form参数.
|
|
35
|
+
* @param header 头部.
|
|
36
|
+
*/
|
|
37
|
+
async postForm(path, params, form, header) {
|
|
38
|
+
const paramQuery = new URLSearchParams();
|
|
39
|
+
Object.keys(params || {})
|
|
40
|
+
.forEach(key => {
|
|
41
|
+
paramQuery.append(key, params[key]);
|
|
42
|
+
});
|
|
43
|
+
const formData = new FormData();
|
|
44
|
+
const appendFormData = (data, parentKey = '') => {
|
|
45
|
+
for (const key in data) {
|
|
46
|
+
const currentKey = parentKey ? `${parentKey}[${key}]` : key;
|
|
47
|
+
if (typeof data[key] === 'object' && data[key] !== null) {
|
|
48
|
+
appendFormData(data[key], currentKey);
|
|
49
|
+
}
|
|
50
|
+
else if (Array.isArray(data[key])) {
|
|
51
|
+
data[key].forEach((item, index) => {
|
|
52
|
+
const arrayKey = `${currentKey}[${index}]`;
|
|
53
|
+
appendFormData({ [arrayKey]: item });
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
formData.append(currentKey, data[key]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
appendFormData(form);
|
|
62
|
+
const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: {
|
|
65
|
+
...this.authorization(),
|
|
66
|
+
...(header || {}),
|
|
67
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
68
|
+
},
|
|
69
|
+
body: formData
|
|
70
|
+
});
|
|
71
|
+
return this.responseJson(response);
|
|
72
|
+
}
|
|
30
73
|
/**
|
|
31
74
|
* 发送Get 请求.
|
|
32
75
|
* @param path 路径.
|
|
33
76
|
* @param params 参数.
|
|
77
|
+
* @param header 头部.
|
|
34
78
|
*/
|
|
35
|
-
async get(path, params) {
|
|
79
|
+
async get(path, params, header) {
|
|
36
80
|
const paramQuery = new URLSearchParams();
|
|
37
81
|
Object.keys(params || {})
|
|
38
82
|
.forEach(key => {
|
|
@@ -41,7 +85,8 @@ class Fetch {
|
|
|
41
85
|
const response = await fetch(this.pathPrefix + path + '?' + paramQuery.toString(), {
|
|
42
86
|
method: 'GET',
|
|
43
87
|
headers: {
|
|
44
|
-
...this.authorization()
|
|
88
|
+
...this.authorization(),
|
|
89
|
+
...(header || {})
|
|
45
90
|
}
|
|
46
91
|
});
|
|
47
92
|
return this.responseJson(response);
|
|
@@ -76,7 +121,7 @@ class Fetch {
|
|
|
76
121
|
* 默认授权方式.
|
|
77
122
|
*/
|
|
78
123
|
authorization() {
|
|
79
|
-
const bearer =
|
|
124
|
+
const bearer = Session.getBearer();
|
|
80
125
|
if (bearer === undefined || bearer === '') {
|
|
81
126
|
return {};
|
|
82
127
|
}
|
|
@@ -117,4 +162,3 @@ class Fetch {
|
|
|
117
162
|
});
|
|
118
163
|
}
|
|
119
164
|
}
|
|
120
|
-
exports.default = Fetch;
|
package/elementUtils.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
class ElementUtils {
|
|
1
|
+
export default class ElementUtils {
|
|
4
2
|
/**
|
|
5
3
|
* 选择文件本地文件.
|
|
6
4
|
*/
|
|
@@ -31,4 +29,3 @@ class ElementUtils {
|
|
|
31
29
|
document.body.removeChild(link);
|
|
32
30
|
}
|
|
33
31
|
}
|
|
34
|
-
exports.default = ElementUtils;
|
package/package.json
CHANGED
package/session.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Session = void 0;
|
|
4
|
-
class Session {
|
|
1
|
+
export class Session {
|
|
5
2
|
/**
|
|
6
3
|
* 获取令牌.
|
|
7
4
|
* <li>window.sessionKey 设置读取缓存用户信息的Key, 默认 login_user.</li>
|
|
@@ -37,4 +34,3 @@ class Session {
|
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
|
-
exports.Session = Session;
|
package/utils.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
class Utils {
|
|
1
|
+
export default class Utils {
|
|
4
2
|
/**
|
|
5
3
|
* 获取URI 地址参数.
|
|
6
4
|
* @param key 参数名称.
|
|
@@ -36,4 +34,3 @@ class Utils {
|
|
|
36
34
|
return navigator.clipboard.writeText(value);
|
|
37
35
|
}
|
|
38
36
|
}
|
|
39
|
-
exports.default = Utils;
|