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.
Files changed (53) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/Channel/Application.js +1 -1
  3. package/dist/Core/Cache/FileCache.js +46 -63
  4. package/dist/Core/Contracts/AccessTokenInterface.js +2 -15
  5. package/dist/Core/Contracts/CacheInterface.js +4 -21
  6. package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +1 -12
  7. package/dist/Core/Contracts/ServerInterface.js +1 -12
  8. package/dist/Core/Http/ServerRequest.js +34 -49
  9. package/dist/Core/HttpClient/AccessTokenAwareClient.js +6 -17
  10. package/dist/Core/HttpClient/Contracts/HttpClientInterface.js +1 -12
  11. package/dist/Core/HttpClient/HttpClient.js +74 -85
  12. package/dist/Core/HttpClient/HttpClientResponse.js +35 -50
  13. package/dist/Core/HttpClient/Mixins/HttpClientMethodsMixin.js +34 -59
  14. package/dist/Core/HttpClient/Mixins/PresetMixin.js +8 -11
  15. package/dist/Core/Message.js +20 -31
  16. package/dist/Core/Mixins/DecryptMessageMixin.js +13 -24
  17. package/dist/Core/Mixins/HandlersMixin.js +18 -31
  18. package/dist/Core/Mixins/ResponseMessageMixin.js +31 -44
  19. package/dist/Core/Support/Utils.js +8 -19
  20. package/dist/MiniApp/Application.js +1 -1
  21. package/dist/MiniApp/Utils.js +13 -24
  22. package/dist/OfficialAccount/AccessToken.js +42 -59
  23. package/dist/OfficialAccount/Application.js +1 -1
  24. package/dist/OfficialAccount/JsApiTicket.js +31 -44
  25. package/dist/OfficialAccount/Server.js +25 -40
  26. package/dist/OfficialAccount/Utils.js +6 -17
  27. package/dist/OpenPlatform/Application.js +62 -86
  28. package/dist/OpenPlatform/Authorizer/MiniApp/Utils.js +14 -25
  29. package/dist/OpenPlatform/AuthorizerAccessToken.js +6 -19
  30. package/dist/OpenPlatform/ComponentAccessToken.js +27 -42
  31. package/dist/OpenPlatform/Server.js +31 -54
  32. package/dist/OpenPlatform/VerifyTicket.js +14 -27
  33. package/dist/OpenWork/Application.js +61 -84
  34. package/dist/OpenWork/AuthorizerAccessToken.js +32 -47
  35. package/dist/OpenWork/JsApiTicket.js +64 -81
  36. package/dist/OpenWork/ProviderAccessToken.js +26 -41
  37. package/dist/OpenWork/Server.js +50 -94
  38. package/dist/OpenWork/SuiteAccessToken.js +27 -42
  39. package/dist/OpenWork/SuiteTicket.js +14 -27
  40. package/dist/Pay/Application.js +1 -2
  41. package/dist/Pay/Client.js +63 -77
  42. package/dist/Pay/LegacySignature.js +2 -3
  43. package/dist/Pay/Merchant.js +36 -52
  44. package/dist/Pay/Server.js +55 -70
  45. package/dist/Pay/Utils.js +13 -26
  46. package/dist/Pay/Validator.js +28 -39
  47. package/dist/Work/AccessToken.js +26 -41
  48. package/dist/Work/Application.js +21 -32
  49. package/dist/Work/JsApiTicket.js +60 -77
  50. package/dist/Work/Server.js +47 -86
  51. package/dist/Work/Utils.js +12 -25
  52. package/package.json +1 -1
  53. package/tsconfig.json +2 -2
@@ -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,22 +14,20 @@ class Utils {
23
14
  * @param code
24
15
  * @returns
25
16
  */
26
- codeToSession(code) {
27
- return __awaiter(this, void 0, void 0, function* () {
28
- let client = this.app.getHttpClient();
29
- let response = yield (yield client.request('GET', '/sns/jscode2session', {
30
- params: {
31
- appid: this.app.getAccount().getAppId(),
32
- secret: this.app.getAccount().getSecret(),
33
- js_code: code,
34
- grant_type: 'authorization_code',
35
- }
36
- })).toObject();
37
- if (!response['openid']) {
38
- throw new Error(`code2Session error: ${JSON.stringify(response)}`);
17
+ async codeToSession(code) {
18
+ let client = this.app.getHttpClient();
19
+ let response = await (await client.request('GET', '/sns/jscode2session', {
20
+ params: {
21
+ appid: this.app.getAccount().getAppId(),
22
+ secret: this.app.getAccount().getSecret(),
23
+ js_code: code,
24
+ grant_type: 'authorization_code',
39
25
  }
40
- return response;
41
- });
26
+ })).toObject();
27
+ if (!response['openid']) {
28
+ throw new Error(`code2Session error: ${JSON.stringify(response)}`);
29
+ }
30
+ return response;
42
31
  }
43
32
  /**
44
33
  * 数据解密
@@ -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
  };
@@ -49,24 +40,20 @@ class AccessToken {
49
40
  this.key = key;
50
41
  return this;
51
42
  }
52
- getToken() {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- let token = '';
55
- if (this.cache) {
56
- token = yield this.cache.get(this.getKey());
57
- }
58
- if (!!token && typeof token === 'string') {
59
- return token;
60
- }
61
- return this.refresh();
62
- });
43
+ async getToken() {
44
+ let token = '';
45
+ if (this.cache) {
46
+ token = await this.cache.get(this.getKey());
47
+ }
48
+ if (!!token && typeof token === 'string') {
49
+ return token;
50
+ }
51
+ return this.refresh();
63
52
  }
64
- toQuery() {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- return {
67
- access_token: yield this.getToken(),
68
- };
69
- });
53
+ async toQuery() {
54
+ return {
55
+ access_token: await this.getToken(),
56
+ };
70
57
  }
71
58
  refresh() {
72
59
  return this.stable ? this.getStableAccessToken() : this.getAccessToken();
@@ -75,45 +62,41 @@ class AccessToken {
75
62
  * 获取稳定版接口调用凭据
76
63
  * @param forceRefresh 是否强制刷新,默认:false
77
64
  */
78
- getStableAccessToken() {
79
- return __awaiter(this, arguments, void 0, function* (forceRefresh = false) {
80
- let response = (yield this.httpClient.request('post', 'cgi-bin/stable_token', {
81
- json: {
82
- grant_type: 'client_credential',
83
- appid: this.appId,
84
- secret: this.secret,
85
- force_refresh: forceRefresh,
86
- }
87
- })).toObject();
88
- if (!response['access_token']) {
89
- throw new Error('Failed to get stable access_token: ' + JSON.stringify(response));
90
- }
91
- if (this.cache) {
92
- yield this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
65
+ async getStableAccessToken(forceRefresh = false) {
66
+ let response = (await this.httpClient.request('post', 'cgi-bin/stable_token', {
67
+ json: {
68
+ grant_type: 'client_credential',
69
+ appid: this.appId,
70
+ secret: this.secret,
71
+ force_refresh: forceRefresh,
93
72
  }
94
- return response['access_token'];
95
- });
73
+ })).toObject();
74
+ if (!response['access_token']) {
75
+ throw new Error('Failed to get stable access_token: ' + JSON.stringify(response));
76
+ }
77
+ if (this.cache) {
78
+ await this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
79
+ }
80
+ return response['access_token'];
96
81
  }
97
82
  /**
98
83
  * 获取接口调用凭据
99
84
  */
100
- getAccessToken() {
101
- return __awaiter(this, void 0, void 0, function* () {
102
- let response = (yield this.httpClient.request('get', 'cgi-bin/token', {
103
- params: {
104
- grant_type: 'client_credential',
105
- appid: this.appId,
106
- secret: this.secret,
107
- }
108
- })).toObject();
109
- if (!response['access_token']) {
110
- throw new Error('Failed to get access_token: ' + JSON.stringify(response));
111
- }
112
- if (this.cache) {
113
- yield this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
85
+ async getAccessToken() {
86
+ let response = (await this.httpClient.request('get', 'cgi-bin/token', {
87
+ params: {
88
+ grant_type: 'client_credential',
89
+ appid: this.appId,
90
+ secret: this.secret,
114
91
  }
115
- return response['access_token'];
116
- });
92
+ })).toObject();
93
+ if (!response['access_token']) {
94
+ throw new Error('Failed to get access_token: ' + JSON.stringify(response));
95
+ }
96
+ if (this.cache) {
97
+ await this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
98
+ }
99
+ return response['access_token'];
117
100
  }
118
101
  }
119
102
  module.exports = AccessToken;
@@ -156,7 +156,7 @@ class Application {
156
156
  return this.utils;
157
157
  }
158
158
  createClient() {
159
- return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => { var _a; return (_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0; }, this.getConfig().get('http.throw', true)))
159
+ return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => response.toObject()['errcode'] ?? 0, this.getConfig().get('http.throw', true)))
160
160
  .setPresets(this.getConfig().all());
161
161
  }
162
162
  /**
@@ -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
  };
@@ -28,29 +19,27 @@ class JsApiTicket extends AccessToken_1.default {
28
19
  * 获取签名凭证jsapi_ticket
29
20
  * @returns
30
21
  */
31
- getTicket() {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- let key = this.getKey();
34
- let ticket = '';
35
- if (this.cache) {
36
- ticket = yield this.cache.get(key);
37
- }
38
- if (!!ticket && typeof ticket === 'string') {
39
- return ticket;
40
- }
41
- let response = (yield this.httpClient.request('get', '/cgi-bin/ticket/getticket', {
42
- params: {
43
- type: 'jsapi',
44
- }
45
- })).toObject();
46
- if (!response['ticket']) {
47
- throw new Error('Failed to get jssdk_ticket: ' + JSON.stringify(response));
48
- }
49
- if (this.cache) {
50
- yield this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
22
+ async getTicket() {
23
+ let key = this.getKey();
24
+ let ticket = '';
25
+ if (this.cache) {
26
+ ticket = await this.cache.get(key);
27
+ }
28
+ if (!!ticket && typeof ticket === 'string') {
29
+ return ticket;
30
+ }
31
+ let response = (await this.httpClient.request('get', '/cgi-bin/ticket/getticket', {
32
+ params: {
33
+ type: 'jsapi',
51
34
  }
52
- return response['ticket'];
53
- });
35
+ })).toObject();
36
+ if (!response['ticket']) {
37
+ throw new Error('Failed to get jssdk_ticket: ' + JSON.stringify(response));
38
+ }
39
+ if (this.cache) {
40
+ await this.cache.set(key, response['ticket'], parseInt(response['expires_in']));
41
+ }
42
+ return response['ticket'];
54
43
  }
55
44
  /**
56
45
  * 获取签名配置
@@ -59,19 +48,17 @@ class JsApiTicket extends AccessToken_1.default {
59
48
  * @param timestamp 时间长,默认:当前时间
60
49
  * @returns
61
50
  */
62
- configSignature(url_1) {
63
- return __awaiter(this, arguments, void 0, function* (url, nonce = null, timestamp = null) {
64
- nonce = nonce || (0, Utils_1.randomString)(10);
65
- timestamp = timestamp || (0, Utils_1.getTimestamp)();
66
- let ticket = yield this.getTicket();
67
- return {
68
- url,
69
- nonceStr: nonce,
70
- timestamp,
71
- appId: this.appId,
72
- signature: this.getTicketSignature(ticket, nonce, timestamp, url),
73
- };
74
- });
51
+ async configSignature(url, nonce = null, timestamp = null) {
52
+ nonce = nonce || (0, Utils_1.randomString)(10);
53
+ timestamp = timestamp || (0, Utils_1.getTimestamp)();
54
+ let ticket = await this.getTicket();
55
+ return {
56
+ url,
57
+ nonceStr: nonce,
58
+ timestamp,
59
+ appId: this.appId,
60
+ signature: this.getTicketSignature(ticket, nonce, timestamp, url),
61
+ };
75
62
  }
76
63
  getTicketSignature(ticket, nonce, timestamp, url) {
77
64
  return (0, Utils_1.createHash)(`jsapi_ticket=${ticket}&noncestr=${nonce}&timestamp=${timestamp}&url=${url}`, 'sha1');
@@ -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
  };
@@ -24,26 +15,24 @@ class Server extends ServerInterface_1.default {
24
15
  * 服务端消息处理
25
16
  * @returns
26
17
  */
27
- serve() {
28
- return __awaiter(this, void 0, void 0, function* () {
29
- let echostr = this.request.getQueryParams()['echostr'] || '';
30
- if (!!echostr) {
31
- return new Response_1.default(200, { 'Content-Type': 'text/html' }, echostr);
32
- }
33
- let message = yield this.getRequestMessage(this.request);
34
- let query = this.request.getQueryParams();
35
- if (this.encryptor && query['msg_signature']) {
36
- this.prepend(this.decryptRequestMessage(query));
37
- }
38
- let response = yield this.handle(new Response_1.default(200, {}, 'success'), message);
39
- if (!(response instanceof Response_1.default)) {
40
- const contentType = this.request.getHeader('content-type');
41
- const contentBody = this.request.getBody().toString();
42
- const isXml = (contentType && contentType.indexOf('xml') > -1) || contentBody.substring(0, 1) === '<';
43
- response = yield this.transformToReply(response, message, this.encryptor, isXml);
44
- }
45
- return response;
46
- });
18
+ async serve() {
19
+ let echostr = this.request.getQueryParams()['echostr'] || '';
20
+ if (!!echostr) {
21
+ return new Response_1.default(200, { 'Content-Type': 'text/html' }, echostr);
22
+ }
23
+ let message = await this.getRequestMessage(this.request);
24
+ let query = this.request.getQueryParams();
25
+ if (this.encryptor && query['msg_signature']) {
26
+ this.prepend(this.decryptRequestMessage(query));
27
+ }
28
+ let response = await this.handle(new Response_1.default(200, {}, 'success'), message);
29
+ if (!(response instanceof Response_1.default)) {
30
+ const contentType = this.request.getHeader('content-type');
31
+ const contentBody = this.request.getBody().toString();
32
+ const isXml = (contentType && contentType.indexOf('xml') > -1) || contentBody.substring(0, 1) === '<';
33
+ response = await this.transformToReply(response, message, this.encryptor, isXml);
34
+ }
35
+ return response;
47
36
  }
48
37
  /**
49
38
  * 添加普通消息处理器
@@ -52,10 +41,8 @@ class Server extends ServerInterface_1.default {
52
41
  * @returns
53
42
  */
54
43
  addMessageListener(type, handler) {
55
- return this.withHandler(function (message, next) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- return message.MsgType === type ? handler(message, next) : next(message);
58
- });
44
+ return this.withHandler(async function (message, next) {
45
+ return message.MsgType === type ? handler(message, next) : next(message);
59
46
  });
60
47
  }
61
48
  /**
@@ -65,10 +52,8 @@ class Server extends ServerInterface_1.default {
65
52
  * @returns
66
53
  */
67
54
  addEventListener(event, handler) {
68
- return this.withHandler(function (message, next) {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- return message.Event === event ? handler(message, next) : next(message);
71
- });
55
+ return this.withHandler(async function (message, next) {
56
+ return message.Event === event ? handler(message, next) : next(message);
72
57
  });
73
58
  }
74
59
  /**
@@ -80,12 +65,12 @@ class Server extends ServerInterface_1.default {
80
65
  return Message_1.default.createFromRequest(request || this.request);
81
66
  }
82
67
  decryptRequestMessage(query) {
83
- return (message, next) => __awaiter(this, void 0, void 0, function* () {
68
+ return async (message, next) => {
84
69
  if (!this.encryptor)
85
70
  return null;
86
- yield this.decryptMessage(message, this.encryptor, query['msg_signature'] || '', query['timestamp'] || '', query['nonce'] || '');
71
+ await this.decryptMessage(message, this.encryptor, query['msg_signature'] || '', query['timestamp'] || '', query['nonce'] || '');
87
72
  return next(message);
88
- });
73
+ };
89
74
  }
90
75
  }
91
76
  ;
@@ -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
  };
@@ -24,14 +15,12 @@ class Utils {
24
15
  * @param debug 是否开启调试模式,默认:false
25
16
  * @returns
26
17
  */
27
- buildJsSdkConfig(url_1) {
28
- return __awaiter(this, arguments, void 0, function* (url, jsApiList = [], openTagList = [], debug = false) {
29
- return (0, merge_1.default)({
30
- jsApiList,
31
- openTagList,
32
- debug,
33
- }, yield this.app.getTicket().configSignature(url));
34
- });
18
+ async buildJsSdkConfig(url, jsApiList = [], openTagList = [], debug = false) {
19
+ return (0, merge_1.default)({
20
+ jsApiList,
21
+ openTagList,
22
+ debug,
23
+ }, await this.app.getTicket().configSignature(url));
35
24
  }
36
25
  }
37
26
  ;
@@ -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
  };
@@ -145,19 +136,17 @@ class Application {
145
136
  * @param authorizationCode
146
137
  * @returns
147
138
  */
148
- getAuthorization(authorizationCode) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- let response = (yield this.getClient().request('post', 'cgi-bin/component/api_query_auth', {
151
- json: {
152
- component_appid: this.getAccount().getAppId(),
153
- authorization_code: authorizationCode,
154
- }
155
- })).toObject();
156
- if (!response['authorization_info']) {
157
- throw new Error('Failed to get authorization_info: ' + JSON.stringify(response));
139
+ async getAuthorization(authorizationCode) {
140
+ let response = (await this.getClient().request('post', 'cgi-bin/component/api_query_auth', {
141
+ json: {
142
+ component_appid: this.getAccount().getAppId(),
143
+ authorization_code: authorizationCode,
158
144
  }
159
- return new Authorization_1.default(response);
160
- });
145
+ })).toObject();
146
+ if (!response['authorization_info']) {
147
+ throw new Error('Failed to get authorization_info: ' + JSON.stringify(response));
148
+ }
149
+ return new Authorization_1.default(response);
161
150
  }
162
151
  /**
163
152
  * 获取/刷新接口调用令牌
@@ -166,38 +155,34 @@ class Application {
166
155
  * @param authorizerRefreshToken
167
156
  * @returns
168
157
  */
169
- refreshAuthorizerToken(authorizerAppId, authorizerRefreshToken) {
170
- return __awaiter(this, void 0, void 0, function* () {
171
- let response = (yield this.getClient().request('post', 'cgi-bin/component/api_authorizer_token', {
172
- json: {
173
- component_appid: this.getAccount().getAppId(),
174
- authorizer_appid: authorizerAppId,
175
- authorizer_refresh_token: authorizerRefreshToken,
176
- }
177
- })).toObject();
178
- if (!response['authorizer_access_token']) {
179
- throw new Error('Failed to get authorizer_access_token: ' + JSON.stringify(response));
158
+ async refreshAuthorizerToken(authorizerAppId, authorizerRefreshToken) {
159
+ let response = (await this.getClient().request('post', 'cgi-bin/component/api_authorizer_token', {
160
+ json: {
161
+ component_appid: this.getAccount().getAppId(),
162
+ authorizer_appid: authorizerAppId,
163
+ authorizer_refresh_token: authorizerRefreshToken,
180
164
  }
181
- return response;
182
- });
165
+ })).toObject();
166
+ if (!response['authorizer_access_token']) {
167
+ throw new Error('Failed to get authorizer_access_token: ' + JSON.stringify(response));
168
+ }
169
+ return response;
183
170
  }
184
171
  /**
185
172
  * 获取预授权码
186
173
  * @see https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html
187
174
  * @returns
188
175
  */
189
- createPreAuthorizationCode() {
190
- return __awaiter(this, void 0, void 0, function* () {
191
- let response = (yield this.getClient().request('post', 'cgi-bin/component/api_create_preauthcode', {
192
- json: {
193
- component_appid: this.getAccount().getAppId(),
194
- }
195
- })).toObject();
196
- if (!response['pre_auth_code']) {
197
- throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
176
+ async createPreAuthorizationCode() {
177
+ let response = (await this.getClient().request('post', 'cgi-bin/component/api_create_preauthcode', {
178
+ json: {
179
+ component_appid: this.getAccount().getAppId(),
198
180
  }
199
- return response;
200
- });
181
+ })).toObject();
182
+ if (!response['pre_auth_code']) {
183
+ throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
184
+ }
185
+ return response;
201
186
  }
202
187
  /**
203
188
  * 生成授权页地址
@@ -205,24 +190,22 @@ class Application {
205
190
  * @param optional 预授权码,不传
206
191
  * @returns
207
192
  */
208
- createPreAuthorizationUrl(callbackUrl, optional) {
209
- return __awaiter(this, void 0, void 0, function* () {
210
- if (typeof optional === 'string') {
211
- optional = {
212
- pre_auth_code: optional,
213
- };
214
- }
215
- else if (!optional || !optional['pre_auth_code']) {
216
- optional = {
217
- pre_auth_code: (yield this.createPreAuthorizationCode()).pre_auth_code,
218
- };
219
- }
220
- let queries = (0, merge_1.default)({
221
- component_appid: this.getAccount().getAppId(),
222
- redirect_uri: callbackUrl,
223
- }, optional);
224
- return `https://mp.weixin.qq.com/cgi-bin/componentloginpage?${(0, Utils_1.buildQueryString)(queries)}`;
225
- });
193
+ async createPreAuthorizationUrl(callbackUrl, optional) {
194
+ if (typeof optional === 'string') {
195
+ optional = {
196
+ pre_auth_code: optional,
197
+ };
198
+ }
199
+ else if (!optional || !optional['pre_auth_code']) {
200
+ optional = {
201
+ pre_auth_code: (await this.createPreAuthorizationCode()).pre_auth_code,
202
+ };
203
+ }
204
+ let queries = (0, merge_1.default)({
205
+ component_appid: this.getAccount().getAppId(),
206
+ redirect_uri: callbackUrl,
207
+ }, optional);
208
+ return `https://mp.weixin.qq.com/cgi-bin/componentloginpage?${(0, Utils_1.buildQueryString)(queries)}`;
226
209
  }
227
210
  getOAuth() {
228
211
  let oauthFactory = ((app) => {
@@ -245,10 +228,8 @@ class Application {
245
228
  * @param config
246
229
  * @returns
247
230
  */
248
- getOfficialAccountWithRefreshToken(appId_1, refreshToken_1) {
249
- return __awaiter(this, arguments, void 0, function* (appId, refreshToken, config = {}) {
250
- return this.getOfficialAccountWithAccessToken(appId, yield this.getAuthorizerAccessToken(appId, refreshToken), config);
251
- });
231
+ async getOfficialAccountWithRefreshToken(appId, refreshToken, config = {}) {
232
+ return this.getOfficialAccountWithAccessToken(appId, await this.getAuthorizerAccessToken(appId, refreshToken), config);
252
233
  }
253
234
  /**
254
235
  * 根据访问令牌获取公众号实例
@@ -313,10 +294,8 @@ class Application {
313
294
  * @param config
314
295
  * @returns
315
296
  */
316
- getMiniAppWithRefreshToken(appId_1, refreshToken_1) {
317
- return __awaiter(this, arguments, void 0, function* (appId, refreshToken, config = {}) {
318
- return this.getMiniAppWithAccessToken(appId, yield this.getAuthorizerAccessToken(appId, refreshToken), config);
319
- });
297
+ async getMiniAppWithRefreshToken(appId, refreshToken, config = {}) {
298
+ return this.getMiniAppWithAccessToken(appId, await this.getAuthorizerAccessToken(appId, refreshToken), config);
320
299
  }
321
300
  /**
322
301
  * 根据访问令牌获取小程序实例
@@ -361,23 +340,20 @@ class Application {
361
340
  * @param refreshToken
362
341
  * @returns
363
342
  */
364
- getAuthorizerAccessToken(appId, refreshToken) {
365
- return __awaiter(this, void 0, void 0, function* () {
366
- var _a;
367
- let md5RefreshToken = (0, Utils_1.createHash)(refreshToken, 'md5');
368
- let cacheKey = `open-platform.authorizer_access_token.${appId}.${md5RefreshToken}`;
369
- let cache = this.getCache();
370
- let authorizerAccessToken = yield cache.get(cacheKey);
371
- if (!authorizerAccessToken) {
372
- let response = yield this.refreshAuthorizerToken(appId, refreshToken);
373
- authorizerAccessToken = response['authorizer_access_token'];
374
- yield cache.set(cacheKey, authorizerAccessToken, ((_a = parseInt(response['expires_in'])) !== null && _a !== void 0 ? _a : 7200) - 500);
375
- }
376
- return authorizerAccessToken;
377
- });
343
+ async getAuthorizerAccessToken(appId, refreshToken) {
344
+ let md5RefreshToken = (0, Utils_1.createHash)(refreshToken, 'md5');
345
+ let cacheKey = `open-platform.authorizer_access_token.${appId}.${md5RefreshToken}`;
346
+ let cache = this.getCache();
347
+ let authorizerAccessToken = await cache.get(cacheKey);
348
+ if (!authorizerAccessToken) {
349
+ let response = await this.refreshAuthorizerToken(appId, refreshToken);
350
+ authorizerAccessToken = response['authorizer_access_token'];
351
+ await cache.set(cacheKey, authorizerAccessToken, (parseInt(response['expires_in']) ?? 7200) - 500);
352
+ }
353
+ return authorizerAccessToken;
378
354
  }
379
355
  createClient() {
380
- return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => { var _a; return (_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0; }, this.getConfig().get('http.throw', true)))
356
+ return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => response.toObject()['errcode'] ?? 0, this.getConfig().get('http.throw', true)))
381
357
  .setPresets(this.getConfig().all());
382
358
  }
383
359
  /**