node-easywechat 3.6.3 → 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 +11 -0
- package/README.md +25 -0
- package/dist/Channel/AccessToken.d.ts +8 -0
- package/dist/Channel/AccessToken.js +15 -0
- package/dist/Channel/Account.d.ts +4 -0
- package/dist/Channel/Account.js +8 -0
- package/dist/Channel/Application.d.ts +60 -0
- package/dist/Channel/Application.js +125 -0
- package/dist/Channel/Contracts/AccountInterface.d.ts +23 -0
- package/dist/Channel/Contracts/AccountInterface.js +25 -0
- package/dist/Channel/Contracts/ApplicationInterface.d.ts +62 -0
- package/dist/Channel/Contracts/ApplicationInterface.js +55 -0
- package/dist/Channel/Message.d.ts +542 -0
- package/dist/Channel/Message.js +9 -0
- package/dist/Channel/Server.d.ts +91 -0
- package/dist/Channel/Server.js +16 -0
- 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.d.ts +3 -3
- package/dist/Core/Contracts/ServerInterface.js +4 -15
- 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/{DecryptXmlMessageMixin.d.ts → DecryptMessageMixin.d.ts} +2 -2
- package/dist/Core/Mixins/DecryptMessageMixin.js +34 -0
- package/dist/Core/Mixins/HandlersMixin.d.ts +8 -8
- package/dist/Core/Mixins/HandlersMixin.js +26 -39
- package/dist/Core/Mixins/{ResponseXmlMessageMixin.d.ts → ResponseMessageMixin.d.ts} +4 -3
- package/dist/Core/Mixins/ResponseMessageMixin.js +61 -0
- 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.d.ts +24 -0
- package/dist/OfficialAccount/Server.js +25 -37
- 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/Types/global.d.ts +31 -0
- 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/dist/index.d.ts +14 -2
- package/dist/index.js +19 -1
- package/package.json +1 -1
- package/tsconfig.json +2 -2
- package/dist/Core/Mixins/DecryptXmlMessageMixin.js +0 -38
- package/dist/Core/Mixins/ResponseXmlMessageMixin.js +0 -63
|
@@ -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
|
};
|
|
@@ -35,46 +26,44 @@ class HttpClientResponse {
|
|
|
35
26
|
* @param throwError
|
|
36
27
|
* @returns
|
|
37
28
|
*/
|
|
38
|
-
parseContent() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
29
|
+
async parseContent(throwError = false) {
|
|
30
|
+
throwError = throwError ?? this.throwError;
|
|
31
|
+
let content = this.response.data;
|
|
32
|
+
if (!content) {
|
|
33
|
+
if (throwError) {
|
|
34
|
+
throw new Error('Response body is empty.');
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (typeof content === 'string') {
|
|
39
|
+
if (this.is('xml') || content.indexOf('<xml>') > -1) {
|
|
40
|
+
this.parsedContent = await (0, Utils_1.parseXml)(content);
|
|
47
41
|
}
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
this.parsedContent =
|
|
42
|
+
else if (this.is('json')) {
|
|
43
|
+
try {
|
|
44
|
+
this.parsedContent = JSON.parse(content);
|
|
51
45
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
catch (e) {
|
|
57
|
-
if (throwError) {
|
|
58
|
-
throw new Error('Fail to parse JSON content.');
|
|
59
|
-
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
if (throwError) {
|
|
48
|
+
throw new Error('Fail to parse JSON content.');
|
|
60
49
|
}
|
|
61
50
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this.parsedContent = {};
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
try {
|
|
54
|
+
this.parsedContent = (0, Utils_1.parseQueryString)(content);
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
if (throwError) {
|
|
58
|
+
throw new Error('Fail to parse QueryString content.');
|
|
71
59
|
}
|
|
60
|
+
this.parsedContent = {};
|
|
72
61
|
}
|
|
73
62
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
63
|
+
}
|
|
64
|
+
else if (!Buffer.isBuffer(content)) {
|
|
65
|
+
this.parsedContent = content;
|
|
66
|
+
}
|
|
78
67
|
}
|
|
79
68
|
withThrowError(throwError) {
|
|
80
69
|
this.throwError = throwError;
|
|
@@ -232,15 +221,11 @@ class HttpClientResponse {
|
|
|
232
221
|
getInfo(type) {
|
|
233
222
|
return this.response.config;
|
|
234
223
|
}
|
|
235
|
-
offsetExists(key) {
|
|
236
|
-
return
|
|
237
|
-
return Object.keys(yield this.toObject()).findIndex(o => o === key) > -1;
|
|
238
|
-
});
|
|
224
|
+
async offsetExists(key) {
|
|
225
|
+
return Object.keys(await this.toObject()).findIndex(o => o === key) > -1;
|
|
239
226
|
}
|
|
240
|
-
offsetGet(key) {
|
|
241
|
-
return
|
|
242
|
-
return (yield this.toObject())[key] || null;
|
|
243
|
-
});
|
|
227
|
+
async offsetGet(key) {
|
|
228
|
+
return (await this.toObject())[key] || null;
|
|
244
229
|
}
|
|
245
230
|
/**
|
|
246
231
|
* 转换为标准的 http 响应类
|
|
@@ -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
|
};
|
|
@@ -20,10 +11,8 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
20
11
|
* @param payload axios配置项
|
|
21
12
|
* @returns
|
|
22
13
|
*/
|
|
23
|
-
get(
|
|
24
|
-
return
|
|
25
|
-
return this.request('get', url, payload);
|
|
26
|
-
});
|
|
14
|
+
async get(url, payload = {}) {
|
|
15
|
+
return this.request('get', url, payload);
|
|
27
16
|
}
|
|
28
17
|
/**
|
|
29
18
|
* 发送post请求
|
|
@@ -31,10 +20,8 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
31
20
|
* @param payload axios配置项
|
|
32
21
|
* @returns
|
|
33
22
|
*/
|
|
34
|
-
post(
|
|
35
|
-
return
|
|
36
|
-
return this.request('post', url, payload);
|
|
37
|
-
});
|
|
23
|
+
async post(url, payload = {}) {
|
|
24
|
+
return this.request('post', url, payload);
|
|
38
25
|
}
|
|
39
26
|
/**
|
|
40
27
|
* 发送patch请求
|
|
@@ -42,10 +29,8 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
42
29
|
* @param payload axios配置项
|
|
43
30
|
* @returns
|
|
44
31
|
*/
|
|
45
|
-
patch(
|
|
46
|
-
return
|
|
47
|
-
return this.request('patch', url, payload);
|
|
48
|
-
});
|
|
32
|
+
async patch(url, payload = {}) {
|
|
33
|
+
return this.request('patch', url, payload);
|
|
49
34
|
}
|
|
50
35
|
/**
|
|
51
36
|
* 发送put请求
|
|
@@ -53,10 +38,8 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
53
38
|
* @param payload axios配置项
|
|
54
39
|
* @returns
|
|
55
40
|
*/
|
|
56
|
-
put(
|
|
57
|
-
return
|
|
58
|
-
return this.request('put', url, payload);
|
|
59
|
-
});
|
|
41
|
+
async put(url, payload = {}) {
|
|
42
|
+
return this.request('put', url, payload);
|
|
60
43
|
}
|
|
61
44
|
/**
|
|
62
45
|
* 发送delete请求
|
|
@@ -64,10 +47,8 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
64
47
|
* @param payload axios配置项
|
|
65
48
|
* @returns
|
|
66
49
|
*/
|
|
67
|
-
delete(
|
|
68
|
-
return
|
|
69
|
-
return this.request('delete', url, payload);
|
|
70
|
-
});
|
|
50
|
+
async delete(url, payload = {}) {
|
|
51
|
+
return this.request('delete', url, payload);
|
|
71
52
|
}
|
|
72
53
|
/**
|
|
73
54
|
* 发送post请求(JSON数据)
|
|
@@ -76,16 +57,14 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
76
57
|
* @param payload axios配置项
|
|
77
58
|
* @returns
|
|
78
59
|
*/
|
|
79
|
-
postJson(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return this.request('post', url, payload);
|
|
88
|
-
});
|
|
60
|
+
async postJson(url, data, payload = {}) {
|
|
61
|
+
if (!payload)
|
|
62
|
+
payload = {};
|
|
63
|
+
if (!payload['headers'])
|
|
64
|
+
payload['headers'] = {};
|
|
65
|
+
payload['headers']['Content-Type'] = 'application/json';
|
|
66
|
+
payload.json = merge_1.default.recursive({}, data);
|
|
67
|
+
return this.request('post', url, payload);
|
|
89
68
|
}
|
|
90
69
|
/**
|
|
91
70
|
* 发送patch请求(JSON数据)
|
|
@@ -94,16 +73,14 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
94
73
|
* @param payload axios配置项
|
|
95
74
|
* @returns
|
|
96
75
|
*/
|
|
97
|
-
patchJson(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return this.request('patch', url, payload);
|
|
106
|
-
});
|
|
76
|
+
async patchJson(url, data, payload = {}) {
|
|
77
|
+
if (!payload)
|
|
78
|
+
payload = {};
|
|
79
|
+
if (!payload['headers'])
|
|
80
|
+
payload['headers'] = {};
|
|
81
|
+
payload['headers']['Content-Type'] = 'application/json';
|
|
82
|
+
payload.json = merge_1.default.recursive({}, data);
|
|
83
|
+
return this.request('patch', url, payload);
|
|
107
84
|
}
|
|
108
85
|
/**
|
|
109
86
|
* 发送post请求(XML数据)
|
|
@@ -112,16 +89,14 @@ class HttpClientMethodsMixin extends HttpClientInterface_1.default {
|
|
|
112
89
|
* @param payload axios配置项
|
|
113
90
|
* @returns
|
|
114
91
|
*/
|
|
115
|
-
postXml(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return this.request('post', url, payload);
|
|
124
|
-
});
|
|
92
|
+
async postXml(url, data, payload = {}) {
|
|
93
|
+
if (!payload)
|
|
94
|
+
payload = {};
|
|
95
|
+
if (!payload['headers'])
|
|
96
|
+
payload['headers'] = {};
|
|
97
|
+
payload['headers']['Content-Type'] = 'text/xml';
|
|
98
|
+
payload.xml = typeof data === 'object' ? merge_1.default.recursive({}, data) : data;
|
|
99
|
+
return this.request('post', url, payload);
|
|
125
100
|
}
|
|
126
101
|
}
|
|
127
102
|
module.exports = HttpClientMethodsMixin;
|
|
@@ -64,15 +64,13 @@ class PresetMixin {
|
|
|
64
64
|
with(key, value = null) {
|
|
65
65
|
if (Array.isArray(key)) {
|
|
66
66
|
key.map((k) => {
|
|
67
|
-
|
|
68
|
-
this.with(k, (_a = this.presets[k]) !== null && _a !== void 0 ? _a : null);
|
|
67
|
+
this.with(k, this.presets[k] ?? null);
|
|
69
68
|
});
|
|
70
69
|
return this;
|
|
71
70
|
}
|
|
72
71
|
else if (typeof key === 'object') {
|
|
73
72
|
key.map((v, k) => {
|
|
74
|
-
|
|
75
|
-
this.with(k, (_a = v !== null && v !== void 0 ? v : this.presets[k]) !== null && _a !== void 0 ? _a : null);
|
|
73
|
+
this.with(k, v ?? this.presets[k] ?? null);
|
|
76
74
|
});
|
|
77
75
|
return this;
|
|
78
76
|
}
|
|
@@ -145,27 +143,26 @@ class PresetMixin {
|
|
|
145
143
|
* @returns
|
|
146
144
|
*/
|
|
147
145
|
mergeThenResetPrepends(payload, method = 'get') {
|
|
148
|
-
var _a, _b, _c, _d;
|
|
149
146
|
let field = method.toLowerCase() === 'get' ? 'params' : 'data';
|
|
150
|
-
let options =
|
|
147
|
+
let options = { ...payload };
|
|
151
148
|
if (!options.headers)
|
|
152
149
|
options.headers = {};
|
|
153
150
|
if (!options.formData)
|
|
154
151
|
options.formData = {};
|
|
155
|
-
if ((
|
|
152
|
+
if ((options.headers['Content-Type'] ?? options.headers['content-type'] ?? null) === 'application/json' || !!options.json) {
|
|
156
153
|
field = 'json';
|
|
157
154
|
}
|
|
158
|
-
if ((
|
|
155
|
+
if ((options.headers['Content-Type'] ?? options.headers['content-type'] ?? null) === 'text/xml' || !!options.xml) {
|
|
159
156
|
field = 'xml';
|
|
160
157
|
}
|
|
161
158
|
if (this.prependData && Object.keys(this.prependData).length > 0) {
|
|
162
|
-
options[field] =
|
|
159
|
+
options[field] = { ...this.prependData, ...options[field] };
|
|
163
160
|
}
|
|
164
161
|
if (this.prependHeaders && Object.keys(this.prependHeaders).length > 0) {
|
|
165
|
-
options.headers =
|
|
162
|
+
options.headers = { ...this.prependHeaders, ...options.headers };
|
|
166
163
|
}
|
|
167
164
|
if (this.prependFiles && Object.keys(this.prependFiles).length > 0) {
|
|
168
|
-
options.formData =
|
|
165
|
+
options.formData = { ...this.prependFiles, ...options.formData };
|
|
169
166
|
}
|
|
170
167
|
this.prependData = {};
|
|
171
168
|
this.prependHeaders = {};
|
package/dist/Core/Message.js
CHANGED
|
@@ -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
|
};
|
|
@@ -51,29 +42,27 @@ class Message {
|
|
|
51
42
|
* @param request
|
|
52
43
|
* @returns
|
|
53
44
|
*/
|
|
54
|
-
static createFromRequest(request) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
attributes = JSON.parse(originContent);
|
|
69
|
-
}
|
|
70
|
-
catch (e) { }
|
|
71
|
-
}
|
|
72
|
-
if (Object.keys(attributes).length === 0) {
|
|
73
|
-
throw new Error('Failed to decode request contents.');
|
|
45
|
+
static async createFromRequest(request) {
|
|
46
|
+
let originContent = '';
|
|
47
|
+
let body = request.getBody();
|
|
48
|
+
if (body) {
|
|
49
|
+
originContent = body.toString();
|
|
50
|
+
}
|
|
51
|
+
let attributes = {};
|
|
52
|
+
if ('<' === originContent.substring(0, 1)) {
|
|
53
|
+
attributes = await (0, Utils_1.parseXml)(originContent);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
// Handle JSON format.
|
|
57
|
+
try {
|
|
58
|
+
attributes = JSON.parse(originContent);
|
|
74
59
|
}
|
|
75
|
-
|
|
76
|
-
}
|
|
60
|
+
catch (e) { }
|
|
61
|
+
}
|
|
62
|
+
if (Object.keys(attributes).length === 0) {
|
|
63
|
+
throw new Error('Failed to decode request contents.');
|
|
64
|
+
}
|
|
65
|
+
return new Message(attributes, originContent);
|
|
77
66
|
}
|
|
78
67
|
/**
|
|
79
68
|
* 获取原始消息内容
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Encryptor from "../Encryptor";
|
|
2
2
|
import Message from "../Message";
|
|
3
|
-
declare class
|
|
3
|
+
declare class DecryptMessageMixin {
|
|
4
4
|
/**
|
|
5
5
|
* 解密消息
|
|
6
6
|
* @returns
|
|
@@ -8,4 +8,4 @@ declare class DecryptXmlMessageMixin {
|
|
|
8
8
|
decryptMessage(message: Message, encryptor: Encryptor, signature: string, timestamp: number, nonce: string): Promise<Message>;
|
|
9
9
|
protected validateSignature(token: string, ciphertext: string, signature: string, timestamp: number, nonce: string): void;
|
|
10
10
|
}
|
|
11
|
-
export =
|
|
11
|
+
export = DecryptMessageMixin;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const Utils_1 = require("../Support/Utils");
|
|
3
|
+
class DecryptMessageMixin {
|
|
4
|
+
/**
|
|
5
|
+
* 解密消息
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
async decryptMessage(message, encryptor, signature, timestamp, nonce) {
|
|
9
|
+
const ciphertext = message['Encrypt'];
|
|
10
|
+
this.validateSignature(encryptor.getToken(), ciphertext, signature, timestamp, nonce);
|
|
11
|
+
const plaintext = encryptor.decrypt(ciphertext, signature, nonce, timestamp);
|
|
12
|
+
let attributes;
|
|
13
|
+
if (plaintext.substring(0, 1) === '<') {
|
|
14
|
+
attributes = await (0, Utils_1.parseXml)(plaintext);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
attributes = JSON.parse(plaintext);
|
|
18
|
+
}
|
|
19
|
+
message.merge(attributes);
|
|
20
|
+
return message;
|
|
21
|
+
}
|
|
22
|
+
validateSignature(token, ciphertext, signature, timestamp, nonce) {
|
|
23
|
+
if (!signature) {
|
|
24
|
+
throw new Error('Request signature must not be empty.');
|
|
25
|
+
}
|
|
26
|
+
let params = [token, timestamp, nonce, ciphertext];
|
|
27
|
+
params.sort();
|
|
28
|
+
if (signature !== (0, Utils_1.createHash)(params.join(''), 'sha1')) {
|
|
29
|
+
throw new Error('Invalid request signature.');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
;
|
|
34
|
+
module.exports = DecryptMessageMixin;
|
|
@@ -20,33 +20,33 @@ declare class HandlersMixin {
|
|
|
20
20
|
*/
|
|
21
21
|
protected getHandlerHash(handler: ServerHandlerClosure<Message>): string;
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* 从后添加处理器
|
|
24
|
+
* @param handler
|
|
24
25
|
*/
|
|
25
26
|
with(handler: ServerHandlerClosure<Message>): this;
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
+
* 从后添加处理器
|
|
28
29
|
* @param handler
|
|
29
|
-
* @returns
|
|
30
30
|
*/
|
|
31
31
|
withHandler(handler: ServerHandlerClosure<Message>): this;
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
33
|
+
* 从前添加处理器
|
|
34
|
+
* @param handler
|
|
34
35
|
*/
|
|
35
36
|
prepend(handler: ServerHandlerClosure<Message>): this;
|
|
36
37
|
/**
|
|
37
|
-
*
|
|
38
|
+
* 从前添加处理器
|
|
38
39
|
* @param handler
|
|
39
|
-
* @returns
|
|
40
40
|
*/
|
|
41
41
|
prependHandler(handler: ServerHandlerClosure<Message>): this;
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* 删除处理器
|
|
44
|
+
* @param handler
|
|
44
45
|
*/
|
|
45
46
|
without(handler: ServerHandlerClosure<Message>): this;
|
|
46
47
|
/**
|
|
47
48
|
* 删除处理器
|
|
48
49
|
* @param handler
|
|
49
|
-
* @returns
|
|
50
50
|
*/
|
|
51
51
|
withoutHandler(handler: ServerHandlerClosure<Message>): this;
|
|
52
52
|
/**
|
|
@@ -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
|
const Utils_1 = require("../Support/Utils");
|
|
12
3
|
class HandlersMixin {
|
|
13
4
|
constructor() {
|
|
@@ -40,37 +31,38 @@ class HandlersMixin {
|
|
|
40
31
|
return (0, Utils_1.createHash)(handler.toString(), 'md5');
|
|
41
32
|
}
|
|
42
33
|
/**
|
|
43
|
-
*
|
|
34
|
+
* 从后添加处理器
|
|
35
|
+
* @param handler
|
|
44
36
|
*/
|
|
45
37
|
with(handler) {
|
|
46
38
|
return this.withHandler(handler);
|
|
47
39
|
}
|
|
48
40
|
/**
|
|
49
|
-
*
|
|
41
|
+
* 从后添加处理器
|
|
50
42
|
* @param handler
|
|
51
|
-
* @returns
|
|
52
43
|
*/
|
|
53
44
|
withHandler(handler) {
|
|
54
45
|
this.handlers.push(this.createHandlerItem(handler));
|
|
55
46
|
return this;
|
|
56
47
|
}
|
|
57
48
|
/**
|
|
58
|
-
*
|
|
49
|
+
* 从前添加处理器
|
|
50
|
+
* @param handler
|
|
59
51
|
*/
|
|
60
52
|
prepend(handler) {
|
|
61
53
|
return this.prependHandler(handler);
|
|
62
54
|
}
|
|
63
55
|
/**
|
|
64
|
-
*
|
|
56
|
+
* 从前添加处理器
|
|
65
57
|
* @param handler
|
|
66
|
-
* @returns
|
|
67
58
|
*/
|
|
68
59
|
prependHandler(handler) {
|
|
69
60
|
this.handlers.unshift(this.createHandlerItem(handler));
|
|
70
61
|
return this;
|
|
71
62
|
}
|
|
72
63
|
/**
|
|
73
|
-
*
|
|
64
|
+
* 删除处理器
|
|
65
|
+
* @param handler
|
|
74
66
|
*/
|
|
75
67
|
without(handler) {
|
|
76
68
|
return this.withoutHandler(handler);
|
|
@@ -78,7 +70,6 @@ class HandlersMixin {
|
|
|
78
70
|
/**
|
|
79
71
|
* 删除处理器
|
|
80
72
|
* @param handler
|
|
81
|
-
* @returns
|
|
82
73
|
*/
|
|
83
74
|
withoutHandler(handler) {
|
|
84
75
|
let index = this.indexOf(handler);
|
|
@@ -126,29 +117,25 @@ class HandlersMixin {
|
|
|
126
117
|
* @param payload
|
|
127
118
|
* @returns
|
|
128
119
|
*/
|
|
129
|
-
handle(result, payload) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
120
|
+
async handle(result, payload) {
|
|
121
|
+
// 默认返回值处理函数
|
|
122
|
+
let resultClosure = async (p) => { return result; };
|
|
123
|
+
if (typeof result === 'function') {
|
|
124
|
+
resultClosure = result;
|
|
125
|
+
}
|
|
126
|
+
let handlers = [...this.handlers];
|
|
127
|
+
const nextClosure = async function (p) {
|
|
128
|
+
if (handlers.length > 0) {
|
|
129
|
+
let item = handlers.shift();
|
|
130
|
+
let closureRes = await item.handler(p, nextClosure);
|
|
131
|
+
if (closureRes)
|
|
132
|
+
return closureRes;
|
|
135
133
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
let closureRes = yield item.handler(p, nextClosure);
|
|
142
|
-
if (closureRes)
|
|
143
|
-
return closureRes;
|
|
144
|
-
}
|
|
145
|
-
// 处理器无返回值则返回默认的返回值
|
|
146
|
-
return yield resultClosure(p);
|
|
147
|
-
});
|
|
148
|
-
};
|
|
149
|
-
// 开始处理
|
|
150
|
-
return yield nextClosure(payload);
|
|
151
|
-
});
|
|
134
|
+
// 处理器无返回值则返回默认的返回值
|
|
135
|
+
return await resultClosure(p);
|
|
136
|
+
};
|
|
137
|
+
// 开始处理
|
|
138
|
+
return await nextClosure(payload);
|
|
152
139
|
}
|
|
153
140
|
/**
|
|
154
141
|
* 判断处理器是否已存在
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import Encryptor from "../Encryptor";
|
|
2
2
|
import ResponseInterface from "../Http/Contracts/ResponseInterface";
|
|
3
3
|
import Message from "../Message";
|
|
4
|
-
declare class
|
|
4
|
+
declare class ResponseMessageMixin {
|
|
5
5
|
/**
|
|
6
6
|
* 转化为回复消息
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
|
-
transformToReply(response: any, message: Message, encryptor?: Encryptor): Promise<ResponseInterface>;
|
|
9
|
+
transformToReply(response: any, message: Message, encryptor?: Encryptor, isXml?: boolean): Promise<ResponseInterface>;
|
|
10
10
|
protected normalizeResponse(response: any): Promise<Record<string, any>>;
|
|
11
11
|
protected createXmlResponse(attributes: Record<string, any>, encryptor?: Encryptor): ResponseInterface;
|
|
12
|
+
protected createJsonResponse(attributes: Record<string, any>, encryptor?: Encryptor): ResponseInterface;
|
|
12
13
|
}
|
|
13
|
-
export =
|
|
14
|
+
export = ResponseMessageMixin;
|