node-easywechat 3.7.0 → 3.7.2

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 (56) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +7 -3
  3. package/dist/Channel/Application.js +1 -1
  4. package/dist/Core/Cache/FileCache.js +46 -63
  5. package/dist/Core/Contracts/AccessTokenInterface.js +2 -15
  6. package/dist/Core/Contracts/CacheInterface.js +4 -21
  7. package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +1 -12
  8. package/dist/Core/Contracts/ServerInterface.js +1 -12
  9. package/dist/Core/Http/ServerRequest.js +34 -49
  10. package/dist/Core/HttpClient/AccessTokenAwareClient.js +6 -17
  11. package/dist/Core/HttpClient/Contracts/HttpClientInterface.js +1 -12
  12. package/dist/Core/HttpClient/HttpClient.js +74 -85
  13. package/dist/Core/HttpClient/HttpClientResponse.js +35 -50
  14. package/dist/Core/HttpClient/Mixins/HttpClientMethodsMixin.js +34 -59
  15. package/dist/Core/HttpClient/Mixins/PresetMixin.js +8 -11
  16. package/dist/Core/Message.js +20 -31
  17. package/dist/Core/Mixins/DecryptMessageMixin.js +13 -24
  18. package/dist/Core/Mixins/HandlersMixin.js +18 -31
  19. package/dist/Core/Mixins/ResponseMessageMixin.js +31 -44
  20. package/dist/Core/Support/Utils.js +8 -19
  21. package/dist/MiniApp/Application.js +1 -1
  22. package/dist/MiniApp/Utils.js +13 -24
  23. package/dist/OfficialAccount/AccessToken.js +42 -59
  24. package/dist/OfficialAccount/Application.js +1 -1
  25. package/dist/OfficialAccount/JsApiTicket.js +31 -44
  26. package/dist/OfficialAccount/Server.d.ts +6 -0
  27. package/dist/OfficialAccount/Server.js +39 -42
  28. package/dist/OfficialAccount/Utils.js +6 -17
  29. package/dist/OpenPlatform/Application.js +62 -86
  30. package/dist/OpenPlatform/Authorizer/MiniApp/Utils.js +14 -25
  31. package/dist/OpenPlatform/AuthorizerAccessToken.js +6 -19
  32. package/dist/OpenPlatform/ComponentAccessToken.js +27 -42
  33. package/dist/OpenPlatform/Server.d.ts +1 -1
  34. package/dist/OpenPlatform/Server.js +34 -56
  35. package/dist/OpenPlatform/VerifyTicket.js +14 -27
  36. package/dist/OpenWork/Application.js +61 -84
  37. package/dist/OpenWork/AuthorizerAccessToken.js +32 -47
  38. package/dist/OpenWork/JsApiTicket.js +64 -81
  39. package/dist/OpenWork/ProviderAccessToken.js +26 -41
  40. package/dist/OpenWork/Server.js +54 -95
  41. package/dist/OpenWork/SuiteAccessToken.js +27 -42
  42. package/dist/OpenWork/SuiteTicket.js +14 -27
  43. package/dist/Pay/Application.js +1 -2
  44. package/dist/Pay/Client.js +63 -77
  45. package/dist/Pay/LegacySignature.js +2 -3
  46. package/dist/Pay/Merchant.js +36 -52
  47. package/dist/Pay/Server.js +55 -70
  48. package/dist/Pay/Utils.js +13 -26
  49. package/dist/Pay/Validator.js +28 -39
  50. package/dist/Work/AccessToken.js +26 -41
  51. package/dist/Work/Application.js +21 -32
  52. package/dist/Work/JsApiTicket.js +60 -77
  53. package/dist/Work/Server.js +51 -87
  54. package/dist/Work/Utils.js +12 -25
  55. package/package.json +1 -1
  56. package/tsconfig.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
3
 
4
+ ## v3.7.2 (2024-07-22)
5
+
6
+ - Fix: 补充公众号和小程序服务端缺失的获取解密消息的方法,并优化其它模块该方法的逻辑;(#86)
7
+
8
+ ## v3.7.1 (2024-06-07)
9
+
10
+ - Fix: 调整ts编译目标版本为ES2020 (#79)
11
+
4
12
  ## v3.7.0 (2024-05-16)
5
13
 
6
14
  - Feat: 增加视频号模块
package/README.md CHANGED
@@ -177,13 +177,17 @@ let data = response.toObject();
177
177
  // 商户号
178
178
  mch_id: '',
179
179
  // 商户证书路径
180
- certificate: '',
180
+ certificate: '/path/to/apiclient_cert.pem',
181
181
  // 商户证书私钥路径
182
- private_key: '',
182
+ private_key: '/path/to/apiclient_key.pem',
183
183
  // 平台证书(v3接口需要)
184
184
  // 持路径列表或者PublicKey对象列表或者,以serial_no为key,证书内容或PublicKey对象为value的对象
185
185
  // 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader
186
- platform_certs: '',
186
+ // 从 3.5.15 版本开始,node-easywechat 会自动下载并缓存平台证书,开发者可以不再配置该参数,当然配置也还是可以的。
187
+ platform_certs: [
188
+ '/path/to/platform_cert_1.pem',
189
+ '/path/to/platform_cert_2.pem',
190
+ ],
187
191
  // v3 API密钥
188
192
  secret_key: '',
189
193
  // v2 API密钥
@@ -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) => { var _a; return ((_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0) || (response.toObject()['error'] !== null && response.toObject()['error'] !== undefined); }, this.getConfig().get('http.throw', true)))
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
- return __awaiter(this, void 0, void 0, function* () {
62
- let content = null;
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
- try {
90
- let dataItem = {
91
- data,
92
- lifeTime: lifeTime > 0 ? lifeTime + (0, Utils_1.getTimestamp)() : 0
93
- };
94
- fs_1.default.writeFileSync(file, JSON.stringify(dataItem), {
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
- delete(id) {
107
- return __awaiter(this, void 0, void 0, function* () {
62
+ async has(id) {
63
+ try {
108
64
  let file = this.getCacheFile(id);
109
- try {
110
- fs_1.default.unlinkSync(file);
111
- }
112
- catch (e) {
113
- return false;
114
- }
115
- return true;
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(key_1) {
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
- return __awaiter(this, void 0, void 0, function* () {
47
- let body = this.content.toString();
48
- if (body.startsWith('<xml')) {
49
- let res = yield (0, Utils_1.parseXml)(body);
50
- this.parsedBody = res;
51
- this.headers['content-type'] = 'text/xml';
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
- else if (body.startsWith('{') || body.startsWith('[')) {
54
- try {
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
- else {
63
- try {
64
- this.parsedBody = (0, Utils_1.parseQueryString)(body);
65
- this.headers['content-type'] = 'application/x-www-form-urlencoded';
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
- return __awaiter(this, void 0, void 0, function* () {
99
- if (!this.parsedBody || Object.keys(this.parsedBody).length === 0) {
100
- yield this.parseBody();
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(req_1) {
139
- return __awaiter(this, arguments, void 0, function* (req, body = null) {
140
- let request = new ServerRequest(req.method, req.url, req.headers || {}, body, req.httpVersion);
141
- if (req.method.toUpperCase() === 'POST' && !body) {
142
- let res = yield (0, raw_body_1.default)(req);
143
- request.withBody(res);
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(method_1, url_1) {
42
- return __awaiter(this, arguments, void 0, function* (method, url, payload = {}) {
43
- if (this.accessToken) {
44
- payload.params = (0, merge_1.default)(true, payload.params || {}, yield this.accessToken.toQuery());
45
- }
46
- let options = this.mergeThenResetPrepends(payload, method);
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(method_1, url_1) {
38
- return __awaiter(this, arguments, void 0, function* (method, url, payload = {}) {
39
- let options = Object.assign({}, payload);
40
- if (!options.headers)
41
- options.headers = {};
42
- options.method = method;
43
- options.url = url;
44
- if (options['xml'] !== undefined) {
45
- let xml = '';
46
- if (typeof options['xml'] === 'object') {
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['json'] !== undefined) {
63
- let json = '';
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
- if (options['formData'] && Object.keys(options['formData']).length > 0) {
81
- let formData = new form_data_1.default();
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
- // 如果 data FormData 对象,则从中提取 headers
99
- if (options.data && options.data instanceof form_data_1.default) {
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.validateStatus = (status) => {
104
- return this.throwError ? status >= 200 && status < 300 : true;
105
- };
106
- let starttime = Date.now();
107
- if (typeof this.logger === 'function') {
108
- yield this.logger('before', options);
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
- let response = yield this.axios.request(options);
111
- if (typeof this.logger === 'function') {
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
- let resp = new HttpClientResponse_1.default(response, this.failureJudge, this.throwError);
116
- yield resp.parseContent(this.throwError);
117
- return resp;
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;