beer-network 1.1.1-alpha.5 → 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 +50 -3
- package/package.json +1 -1
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
|
@@ -8,8 +8,9 @@ export default class Fetch {
|
|
|
8
8
|
* @param path 路径.
|
|
9
9
|
* @param params 参数.
|
|
10
10
|
* @param body 内容体.
|
|
11
|
+
* @param header 头部.
|
|
11
12
|
*/
|
|
12
|
-
async post(path, params, body) {
|
|
13
|
+
async post(path, params, body, header) {
|
|
13
14
|
const paramQuery = new URLSearchParams();
|
|
14
15
|
Object.keys(params || {})
|
|
15
16
|
.forEach(key => {
|
|
@@ -19,18 +20,63 @@ export default class Fetch {
|
|
|
19
20
|
method: 'POST',
|
|
20
21
|
headers: {
|
|
21
22
|
...this.authorization(),
|
|
23
|
+
...(header || {}),
|
|
22
24
|
'Content-Type': 'application/json'
|
|
23
25
|
},
|
|
24
26
|
body: JSON.stringify(body || {})
|
|
25
27
|
});
|
|
26
28
|
return this.responseJson(response);
|
|
27
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
|
+
}
|
|
28
73
|
/**
|
|
29
74
|
* 发送Get 请求.
|
|
30
75
|
* @param path 路径.
|
|
31
76
|
* @param params 参数.
|
|
77
|
+
* @param header 头部.
|
|
32
78
|
*/
|
|
33
|
-
async get(path, params) {
|
|
79
|
+
async get(path, params, header) {
|
|
34
80
|
const paramQuery = new URLSearchParams();
|
|
35
81
|
Object.keys(params || {})
|
|
36
82
|
.forEach(key => {
|
|
@@ -39,7 +85,8 @@ export default class Fetch {
|
|
|
39
85
|
const response = await fetch(this.pathPrefix + path + '?' + paramQuery.toString(), {
|
|
40
86
|
method: 'GET',
|
|
41
87
|
headers: {
|
|
42
|
-
...this.authorization()
|
|
88
|
+
...this.authorization(),
|
|
89
|
+
...(header || {})
|
|
43
90
|
}
|
|
44
91
|
});
|
|
45
92
|
return this.responseJson(response);
|