node-easywechat 3.7.0 → 3.7.1
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/CHANGELOG.md +4 -0
- package/dist/Channel/Application.js +1 -1
- package/dist/Core/Cache/FileCache.js +46 -63
- package/dist/Core/Contracts/AccessTokenInterface.js +2 -15
- package/dist/Core/Contracts/CacheInterface.js +4 -21
- package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +1 -12
- package/dist/Core/Contracts/ServerInterface.js +1 -12
- package/dist/Core/Http/ServerRequest.js +34 -49
- package/dist/Core/HttpClient/AccessTokenAwareClient.js +6 -17
- package/dist/Core/HttpClient/Contracts/HttpClientInterface.js +1 -12
- package/dist/Core/HttpClient/HttpClient.js +74 -85
- package/dist/Core/HttpClient/HttpClientResponse.js +35 -50
- package/dist/Core/HttpClient/Mixins/HttpClientMethodsMixin.js +34 -59
- package/dist/Core/HttpClient/Mixins/PresetMixin.js +8 -11
- package/dist/Core/Message.js +20 -31
- package/dist/Core/Mixins/DecryptMessageMixin.js +13 -24
- package/dist/Core/Mixins/HandlersMixin.js +18 -31
- package/dist/Core/Mixins/ResponseMessageMixin.js +31 -44
- package/dist/Core/Support/Utils.js +8 -19
- package/dist/MiniApp/Application.js +1 -1
- package/dist/MiniApp/Utils.js +13 -24
- package/dist/OfficialAccount/AccessToken.js +42 -59
- package/dist/OfficialAccount/Application.js +1 -1
- package/dist/OfficialAccount/JsApiTicket.js +31 -44
- package/dist/OfficialAccount/Server.js +25 -40
- package/dist/OfficialAccount/Utils.js +6 -17
- package/dist/OpenPlatform/Application.js +62 -86
- package/dist/OpenPlatform/Authorizer/MiniApp/Utils.js +14 -25
- package/dist/OpenPlatform/AuthorizerAccessToken.js +6 -19
- package/dist/OpenPlatform/ComponentAccessToken.js +27 -42
- package/dist/OpenPlatform/Server.js +31 -54
- package/dist/OpenPlatform/VerifyTicket.js +14 -27
- package/dist/OpenWork/Application.js +61 -84
- package/dist/OpenWork/AuthorizerAccessToken.js +32 -47
- package/dist/OpenWork/JsApiTicket.js +64 -81
- package/dist/OpenWork/ProviderAccessToken.js +26 -41
- package/dist/OpenWork/Server.js +50 -94
- package/dist/OpenWork/SuiteAccessToken.js +27 -42
- package/dist/OpenWork/SuiteTicket.js +14 -27
- package/dist/Pay/Application.js +1 -2
- package/dist/Pay/Client.js +63 -77
- package/dist/Pay/LegacySignature.js +2 -3
- package/dist/Pay/Merchant.js +36 -52
- package/dist/Pay/Server.js +55 -70
- package/dist/Pay/Utils.js +13 -26
- package/dist/Pay/Validator.js +28 -39
- package/dist/Work/AccessToken.js +26 -41
- package/dist/Work/Application.js +21 -32
- package/dist/Work/JsApiTicket.js +60 -77
- package/dist/Work/Server.js +47 -86
- package/dist/Work/Utils.js +12 -25
- package/package.json +1 -1
- package/tsconfig.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -106,7 +106,7 @@ class Application {
|
|
|
106
106
|
return this;
|
|
107
107
|
}
|
|
108
108
|
createClient() {
|
|
109
|
-
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) =>
|
|
109
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => (response.toObject()['errcode'] ?? 0) || (response.toObject()['error'] !== null && response.toObject()['error'] !== undefined), this.getConfig().get('http.throw', true)))
|
|
110
110
|
.setPresets(this.getConfig().all());
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -57,63 +48,55 @@ class FileCache {
|
|
|
57
48
|
}
|
|
58
49
|
return dataItem.data;
|
|
59
50
|
}
|
|
60
|
-
get(id) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
let file = this.getCacheFile(id);
|
|
65
|
-
content = this.getCacheContent(file);
|
|
66
|
-
}
|
|
67
|
-
catch (e) {
|
|
68
|
-
content = null;
|
|
69
|
-
}
|
|
70
|
-
return content;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
has(id) {
|
|
74
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
-
try {
|
|
76
|
-
let file = this.getCacheFile(id);
|
|
77
|
-
fs_1.default.accessSync(file, fs_1.default.constants.R_OK & fs_1.default.constants.W_OK);
|
|
78
|
-
let content = this.getCacheContent(file);
|
|
79
|
-
}
|
|
80
|
-
catch (e) {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
return true;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
set(id_1) {
|
|
87
|
-
return __awaiter(this, arguments, void 0, function* (id, data = null, lifeTime = 0) {
|
|
51
|
+
async get(id) {
|
|
52
|
+
let content = null;
|
|
53
|
+
try {
|
|
88
54
|
let file = this.getCacheFile(id);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
mode: this.options.fileMode,
|
|
96
|
-
encoding: 'utf-8',
|
|
97
|
-
flag: 'w'
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
catch (e) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
return true;
|
|
104
|
-
});
|
|
55
|
+
content = this.getCacheContent(file);
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
content = null;
|
|
59
|
+
}
|
|
60
|
+
return content;
|
|
105
61
|
}
|
|
106
|
-
|
|
107
|
-
|
|
62
|
+
async has(id) {
|
|
63
|
+
try {
|
|
108
64
|
let file = this.getCacheFile(id);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
65
|
+
fs_1.default.accessSync(file, fs_1.default.constants.R_OK & fs_1.default.constants.W_OK);
|
|
66
|
+
let content = this.getCacheContent(file);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
async set(id, data = null, lifeTime = 0) {
|
|
74
|
+
let file = this.getCacheFile(id);
|
|
75
|
+
try {
|
|
76
|
+
let dataItem = {
|
|
77
|
+
data,
|
|
78
|
+
lifeTime: lifeTime > 0 ? lifeTime + (0, Utils_1.getTimestamp)() : 0
|
|
79
|
+
};
|
|
80
|
+
fs_1.default.writeFileSync(file, JSON.stringify(dataItem), {
|
|
81
|
+
mode: this.options.fileMode,
|
|
82
|
+
encoding: 'utf-8',
|
|
83
|
+
flag: 'w'
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
async delete(id) {
|
|
92
|
+
let file = this.getCacheFile(id);
|
|
93
|
+
try {
|
|
94
|
+
fs_1.default.unlinkSync(file);
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
117
100
|
}
|
|
118
101
|
}
|
|
119
102
|
;
|
|
@@ -1,28 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
class AccessTokenInterface {
|
|
12
3
|
/**
|
|
13
4
|
* 获取token
|
|
14
5
|
* @returns
|
|
15
6
|
*/
|
|
16
|
-
getToken() {
|
|
17
|
-
return __awaiter(this, void 0, void 0, function* () { return null; });
|
|
18
|
-
}
|
|
7
|
+
async getToken() { return null; }
|
|
19
8
|
/**
|
|
20
9
|
* 转成url参数
|
|
21
10
|
* @returns
|
|
22
11
|
*/
|
|
23
|
-
toQuery() {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () { return null; });
|
|
25
|
-
}
|
|
12
|
+
async toQuery() { return null; }
|
|
26
13
|
}
|
|
27
14
|
;
|
|
28
15
|
module.exports = AccessTokenInterface;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
/**
|
|
12
3
|
* 缓存接口
|
|
13
4
|
*/
|
|
@@ -17,17 +8,13 @@ class CacheInterface {
|
|
|
17
8
|
* @param key 缓存名
|
|
18
9
|
* @returns
|
|
19
10
|
*/
|
|
20
|
-
get(key) {
|
|
21
|
-
return __awaiter(this, void 0, void 0, function* () { return null; });
|
|
22
|
-
}
|
|
11
|
+
async get(key) { return null; }
|
|
23
12
|
/**
|
|
24
13
|
* 设置缓存
|
|
25
14
|
* @param key 缓存名
|
|
26
15
|
* @returns
|
|
27
16
|
*/
|
|
28
|
-
has(key) {
|
|
29
|
-
return __awaiter(this, void 0, void 0, function* () { return false; });
|
|
30
|
-
}
|
|
17
|
+
async has(key) { return false; }
|
|
31
18
|
/**
|
|
32
19
|
* 设置缓存
|
|
33
20
|
* @param key 缓存名
|
|
@@ -35,17 +22,13 @@ class CacheInterface {
|
|
|
35
22
|
* @param lifetime 过期时间,单位:秒
|
|
36
23
|
* @returns
|
|
37
24
|
*/
|
|
38
|
-
set(
|
|
39
|
-
return __awaiter(this, arguments, void 0, function* (key, data = null, lifetime = 0) { return false; });
|
|
40
|
-
}
|
|
25
|
+
async set(key, data = null, lifetime = 0) { return false; }
|
|
41
26
|
/**
|
|
42
27
|
* 删除缓存
|
|
43
28
|
* @param key 缓存名
|
|
44
29
|
* @returns
|
|
45
30
|
*/
|
|
46
|
-
delete(key) {
|
|
47
|
-
return __awaiter(this, void 0, void 0, function* () { return false; });
|
|
48
|
-
}
|
|
31
|
+
async delete(key) { return false; }
|
|
49
32
|
}
|
|
50
33
|
;
|
|
51
34
|
module.exports = CacheInterface;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -17,9 +8,7 @@ class RefreshableAccessTokenInterface extends AccessTokenInterface_1.default {
|
|
|
17
8
|
* 刷新token
|
|
18
9
|
* @returns
|
|
19
10
|
*/
|
|
20
|
-
refresh() {
|
|
21
|
-
return __awaiter(this, void 0, void 0, function* () { return null; });
|
|
22
|
-
}
|
|
11
|
+
async refresh() { return null; }
|
|
23
12
|
}
|
|
24
13
|
;
|
|
25
14
|
module.exports = RefreshableAccessTokenInterface;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -23,9 +14,7 @@ class ServerInterface {
|
|
|
23
14
|
/**
|
|
24
15
|
* 处理消息
|
|
25
16
|
*/
|
|
26
|
-
serve() {
|
|
27
|
-
return __awaiter(this, void 0, void 0, function* () { return null; });
|
|
28
|
-
}
|
|
17
|
+
async serve() { return null; }
|
|
29
18
|
}
|
|
30
19
|
;
|
|
31
20
|
;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -42,33 +33,31 @@ class ServerRequest {
|
|
|
42
33
|
* 解析 body 内容
|
|
43
34
|
* 支持 JSON字符串、XML字符串、QueryString等格式
|
|
44
35
|
*/
|
|
45
|
-
parseBody() {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
36
|
+
async parseBody() {
|
|
37
|
+
let body = this.content.toString();
|
|
38
|
+
if (body.startsWith('<xml')) {
|
|
39
|
+
let res = await (0, Utils_1.parseXml)(body);
|
|
40
|
+
this.parsedBody = res;
|
|
41
|
+
this.headers['content-type'] = 'text/xml';
|
|
42
|
+
}
|
|
43
|
+
else if (body.startsWith('{') || body.startsWith('[')) {
|
|
44
|
+
try {
|
|
45
|
+
this.parsedBody = JSON.parse(body);
|
|
46
|
+
this.headers['content-type'] = 'application/json';
|
|
52
47
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this.parsedBody = JSON.parse(body);
|
|
56
|
-
this.headers['content-type'] = 'application/json';
|
|
57
|
-
}
|
|
58
|
-
catch (e) {
|
|
59
|
-
this.parsedBody = {};
|
|
60
|
-
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
this.parsedBody = {};
|
|
61
50
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
catch (e) {
|
|
68
|
-
this.parsedBody = {};
|
|
69
|
-
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
try {
|
|
54
|
+
this.parsedBody = (0, Utils_1.parseQueryString)(body);
|
|
55
|
+
this.headers['content-type'] = 'application/x-www-form-urlencoded';
|
|
70
56
|
}
|
|
71
|
-
|
|
57
|
+
catch (e) {
|
|
58
|
+
this.parsedBody = {};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
72
61
|
}
|
|
73
62
|
getServerParams() {
|
|
74
63
|
return this.serverParams;
|
|
@@ -94,13 +83,11 @@ class ServerRequest {
|
|
|
94
83
|
this.uploadedFiles = merge_1.default.recursive(true, files);
|
|
95
84
|
return this;
|
|
96
85
|
}
|
|
97
|
-
getParsedBody() {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return this.parsedBody;
|
|
103
|
-
});
|
|
86
|
+
async getParsedBody() {
|
|
87
|
+
if (!this.parsedBody || Object.keys(this.parsedBody).length === 0) {
|
|
88
|
+
await this.parseBody();
|
|
89
|
+
}
|
|
90
|
+
return this.parsedBody;
|
|
104
91
|
}
|
|
105
92
|
withParsedBody(data) {
|
|
106
93
|
this.parsedBody = merge_1.default.recursive(true, data);
|
|
@@ -135,15 +122,13 @@ class ServerRequest {
|
|
|
135
122
|
* @param body 支持 Buffer、object对象、JSON字符串、XML字符串、QueryString格式的 body 内容字符串
|
|
136
123
|
* @returns
|
|
137
124
|
*/
|
|
138
|
-
static createFromIncomingMessage(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return request;
|
|
146
|
-
});
|
|
125
|
+
static async createFromIncomingMessage(req, body = null) {
|
|
126
|
+
let request = new ServerRequest(req.method, req.url, req.headers || {}, body, req.httpVersion);
|
|
127
|
+
if (req.method.toUpperCase() === 'POST' && !body) {
|
|
128
|
+
let res = await (0, raw_body_1.default)(req);
|
|
129
|
+
request.withBody(res);
|
|
130
|
+
}
|
|
131
|
+
return request;
|
|
147
132
|
}
|
|
148
133
|
}
|
|
149
134
|
;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -38,14 +29,12 @@ class AccessTokenAwareClient {
|
|
|
38
29
|
this.client.setLogger(logger);
|
|
39
30
|
return this;
|
|
40
31
|
}
|
|
41
|
-
request(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return this.client.request(method, (0, Utils_1.ltrim)(url, '\\/+'), options);
|
|
48
|
-
});
|
|
32
|
+
async request(method, url, payload = {}) {
|
|
33
|
+
if (this.accessToken) {
|
|
34
|
+
payload.params = (0, merge_1.default)(true, payload.params || {}, await this.accessToken.toQuery());
|
|
35
|
+
}
|
|
36
|
+
let options = this.mergeThenResetPrepends(payload, method);
|
|
37
|
+
return this.client.request(method, (0, Utils_1.ltrim)(url, '\\/+'), options);
|
|
49
38
|
}
|
|
50
39
|
}
|
|
51
40
|
;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
class HttpClientInterface {
|
|
12
3
|
/**
|
|
13
4
|
* 获取axios实例
|
|
@@ -31,9 +22,7 @@ class HttpClientInterface {
|
|
|
31
22
|
/**
|
|
32
23
|
* 发起http请求
|
|
33
24
|
*/
|
|
34
|
-
request(method, url, payload) {
|
|
35
|
-
return __awaiter(this, void 0, void 0, function* () { return null; });
|
|
36
|
-
}
|
|
25
|
+
async request(method, url, payload) { return null; }
|
|
37
26
|
}
|
|
38
27
|
;
|
|
39
28
|
module.exports = HttpClientInterface;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -34,88 +25,86 @@ class HttpClient {
|
|
|
34
25
|
this.failureJudge = closure;
|
|
35
26
|
return this;
|
|
36
27
|
}
|
|
37
|
-
request(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
xml = (0, Utils_1.buildXml)(options['xml']);
|
|
48
|
-
}
|
|
49
|
-
else if (typeof options['xml'] === 'string') {
|
|
50
|
-
xml = options['xml'];
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
throw new Error('The type of `xml` must be string or object.');
|
|
54
|
-
}
|
|
55
|
-
if (!options.headers['Content-Type'] && !options.headers['content-type']) {
|
|
56
|
-
options.headers['content-type'] = 'text/xml';
|
|
57
|
-
}
|
|
58
|
-
options.data = xml;
|
|
59
|
-
options['xml'] = undefined;
|
|
60
|
-
delete options['xml'];
|
|
28
|
+
async request(method, url, payload = {}) {
|
|
29
|
+
let options = { ...payload };
|
|
30
|
+
if (!options.headers)
|
|
31
|
+
options.headers = {};
|
|
32
|
+
options.method = method;
|
|
33
|
+
options.url = url;
|
|
34
|
+
if (options['xml'] !== undefined) {
|
|
35
|
+
let xml = '';
|
|
36
|
+
if (typeof options['xml'] === 'object') {
|
|
37
|
+
xml = (0, Utils_1.buildXml)(options['xml']);
|
|
61
38
|
}
|
|
62
|
-
if (options['
|
|
63
|
-
|
|
64
|
-
if (typeof options['json'] === 'object') {
|
|
65
|
-
json = JSON.stringify(options['json']);
|
|
66
|
-
}
|
|
67
|
-
else if (typeof options['json'] === 'string') {
|
|
68
|
-
json = options['json'];
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
throw new Error('The type of `json` must be string or object.');
|
|
72
|
-
}
|
|
73
|
-
if (!options.headers['Content-Type'] && !options.headers['content-type']) {
|
|
74
|
-
options.headers['content-type'] = 'application/json';
|
|
75
|
-
}
|
|
76
|
-
options.data = json;
|
|
77
|
-
options['json'] = undefined;
|
|
78
|
-
delete options['json'];
|
|
39
|
+
else if (typeof options['xml'] === 'string') {
|
|
40
|
+
xml = options['xml'];
|
|
79
41
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (options['formData'] instanceof form_data_1.default) {
|
|
83
|
-
formData = options['formData'];
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
for (let key in options['formData']) {
|
|
87
|
-
formData.append(key, options['formData'][key]);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (options.data)
|
|
91
|
-
for (let key in options.data) {
|
|
92
|
-
formData.append(key, options.data[key]);
|
|
93
|
-
}
|
|
94
|
-
options.data = formData;
|
|
95
|
-
options['formData'] = undefined;
|
|
96
|
-
delete options['formData'];
|
|
42
|
+
else {
|
|
43
|
+
throw new Error('The type of `xml` must be string or object.');
|
|
97
44
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
options.headers = Object.assign(Object.assign({}, (yield this.getFormDataHeaders(options.data))), options.headers);
|
|
45
|
+
if (!options.headers['Content-Type'] && !options.headers['content-type']) {
|
|
46
|
+
options.headers['content-type'] = 'text/xml';
|
|
101
47
|
}
|
|
102
|
-
|
|
103
|
-
options
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
48
|
+
options.data = xml;
|
|
49
|
+
options['xml'] = undefined;
|
|
50
|
+
delete options['xml'];
|
|
51
|
+
}
|
|
52
|
+
if (options['json'] !== undefined) {
|
|
53
|
+
let json = '';
|
|
54
|
+
if (typeof options['json'] === 'object') {
|
|
55
|
+
json = JSON.stringify(options['json']);
|
|
109
56
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
let usedTime = Date.now() - starttime;
|
|
113
|
-
yield this.logger('after', options, usedTime, response);
|
|
57
|
+
else if (typeof options['json'] === 'string') {
|
|
58
|
+
json = options['json'];
|
|
114
59
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
60
|
+
else {
|
|
61
|
+
throw new Error('The type of `json` must be string or object.');
|
|
62
|
+
}
|
|
63
|
+
if (!options.headers['Content-Type'] && !options.headers['content-type']) {
|
|
64
|
+
options.headers['content-type'] = 'application/json';
|
|
65
|
+
}
|
|
66
|
+
options.data = json;
|
|
67
|
+
options['json'] = undefined;
|
|
68
|
+
delete options['json'];
|
|
69
|
+
}
|
|
70
|
+
if (options['formData'] && Object.keys(options['formData']).length > 0) {
|
|
71
|
+
let formData = new form_data_1.default();
|
|
72
|
+
if (options['formData'] instanceof form_data_1.default) {
|
|
73
|
+
formData = options['formData'];
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
for (let key in options['formData']) {
|
|
77
|
+
formData.append(key, options['formData'][key]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (options.data)
|
|
81
|
+
for (let key in options.data) {
|
|
82
|
+
formData.append(key, options.data[key]);
|
|
83
|
+
}
|
|
84
|
+
options.data = formData;
|
|
85
|
+
options['formData'] = undefined;
|
|
86
|
+
delete options['formData'];
|
|
87
|
+
}
|
|
88
|
+
// 如果 data 是 FormData 对象,则从中提取 headers
|
|
89
|
+
if (options.data && options.data instanceof form_data_1.default) {
|
|
90
|
+
options.headers = { ...(await this.getFormDataHeaders(options.data)), ...options.headers };
|
|
91
|
+
}
|
|
92
|
+
// 是否抛出异常
|
|
93
|
+
options.validateStatus = (status) => {
|
|
94
|
+
return this.throwError ? status >= 200 && status < 300 : true;
|
|
95
|
+
};
|
|
96
|
+
let starttime = Date.now();
|
|
97
|
+
if (typeof this.logger === 'function') {
|
|
98
|
+
await this.logger('before', options);
|
|
99
|
+
}
|
|
100
|
+
let response = await this.axios.request(options);
|
|
101
|
+
if (typeof this.logger === 'function') {
|
|
102
|
+
let usedTime = Date.now() - starttime;
|
|
103
|
+
await this.logger('after', options, usedTime, response);
|
|
104
|
+
}
|
|
105
|
+
let resp = new HttpClientResponse_1.default(response, this.failureJudge, this.throwError);
|
|
106
|
+
await resp.parseContent(this.throwError);
|
|
107
|
+
return resp;
|
|
119
108
|
}
|
|
120
109
|
getInstance() {
|
|
121
110
|
return this.axios;
|