tmc.js 0.3.5 → 0.3.7
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/README.md +37 -1
- package/lib/consumer.js +20 -6
- package/package.json +1 -1
- package/types/index.d.ts +591 -1
- package/types/message.in.d.ts +1370 -0
package/README.md
CHANGED
|
@@ -43,14 +43,27 @@ new Tmc('your_app_key', 'your_app_secret')
|
|
|
43
43
|
| options.onCloseReconnection | `number` | 当消费端断开连接,重试连接间隔(默认`3000`毫秒) |
|
|
44
44
|
| options.autoParseContentJson | `boolean` | 自动解析推送消息`$.content.content`字段为对象(默认`true`) |
|
|
45
45
|
| options.autoReplyConfirmation | `boolean` | 以推送的`$.content.id`字段自动`Confirm`消息(默认`true`) |
|
|
46
|
+
| options.autoGroupedEmitting | `boolean` | 以`/^(([^_]+)_[^_]+)_.+/`规则切分`$.content.topic`主题,开关消费端多维监听功能(默认`true`) |
|
|
46
47
|
|
|
47
48
|
**`tmc.on(topic: string, listener: (this: Tmc, message: Message) => void) => Tmc`**
|
|
48
49
|
|
|
49
50
|
注册 `topic` 消息通知处理函数,默认已内置 [消息](./types/message.in.d.ts) 说明。
|
|
51
|
+
自`v0.3.6`起,默认开启消费端多维监听功能,`topic`字符串也可以是`BU`及`BU_G`单位,例如:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
.on('taobao', console.info)
|
|
55
|
+
.on('alibaba_einvoice', console.info)
|
|
56
|
+
```
|
|
50
57
|
|
|
51
58
|
**`tmc[<topic>](fn: (this: Tmc, message: Message) => void) => Tmc`**
|
|
52
59
|
|
|
53
60
|
直接以 `topic` 为键值,注册消息通知处理函数。
|
|
61
|
+
自`v0.3.6`起,默认开启消费端多维监听功能,`topic`字符串也可以是`BU`及`BU_G`单位,例如:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
.taobao(console.info)
|
|
65
|
+
.alibaba_einvoice(console.info)
|
|
66
|
+
```
|
|
54
67
|
|
|
55
68
|
**`tmc.reconnect(ms: number) => Tmc`**
|
|
56
69
|
|
|
@@ -62,7 +75,7 @@ new Tmc('your_app_key', 'your_app_secret')
|
|
|
62
75
|
|
|
63
76
|
**`tmc.send(msg: Message, options?: { mask?: true, binary?: true }, cb?: (err: Error) => void) => void`**
|
|
64
77
|
|
|
65
|
-
|
|
78
|
+
自`v0.3.4`起,当自动应答确认消息无法满足需求的时候,比如消息处理失败,需要`Publisher`再次重推消息,在实例初始化时置`options.autoReplyConfirmation=false`,则在消息处理函数内,可以通过 `this.send()` 函数回复`确认`或者`失败`消息。例如:
|
|
66
79
|
|
|
67
80
|
```js
|
|
68
81
|
new Tmc('your_app_key', 'your_app_secret', { autoReplyConfirmation: false })
|
|
@@ -78,6 +91,29 @@ new Tmc('your_app_key', 'your_app_secret', { autoReplyConfirmation: false })
|
|
|
78
91
|
.connect();
|
|
79
92
|
```
|
|
80
93
|
|
|
94
|
+
也可以使用此方法发送`To淘宝`消息,`>=v0.3.4 && <v0.3.7`版本,需要自主对`$.content.content`数据做`JSON`字符串化(**特别注意:**原生`JSON`对`BigInt`类型无法处理)。自`v0.3.7`起,可直接对`$.content.content`以原生数据类型描述。例如:
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
/** @see https://open.taobao.com/tmc.htm?docId=732&docType=9 */
|
|
98
|
+
.send(
|
|
99
|
+
new Mesage(MessageType.SEND, MessageKind.Data)
|
|
100
|
+
.with(MessageFields.TOPIC, 'taobao_fuwu_ElectronicInvoice')
|
|
101
|
+
.with(MessageFields.CONTENT, {
|
|
102
|
+
id: 12345678901234567n, // 支持 BigInt 类型
|
|
103
|
+
tid: 12345678901234567n,
|
|
104
|
+
oid: 12345678901234567n,
|
|
105
|
+
invoice_file: 12345678901234567n,
|
|
106
|
+
e_invoice_no: '12342243435466',
|
|
107
|
+
invoice_time: '2015-04-10 10:33:49', // 时区 +08:00
|
|
108
|
+
invoice_no: '123456',
|
|
109
|
+
invoice_code: '123456',
|
|
110
|
+
amount: '100.00',
|
|
111
|
+
})
|
|
112
|
+
// >=v0.3.4 && <v0.3.7 写法
|
|
113
|
+
// .with(MessageFields.CONTENT, '{"id":12345678901234567,"tid":12345678901234567,"amount":"100.00"}')
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
81
117
|
<details><summary>可选设置的 NODE_DEBUG=< label > 环境变量</summary>
|
|
82
118
|
|
|
83
119
|
| label | 说明 |
|
package/lib/consumer.js
CHANGED
|
@@ -46,6 +46,8 @@ class TaoMessageConsumer extends EventEmitter {
|
|
|
46
46
|
onCloseReconnection: 3e3,
|
|
47
47
|
autoParseContentJson: true,
|
|
48
48
|
autoReplyConfirmation: true,
|
|
49
|
+
/** @since v0.3.6 */
|
|
50
|
+
autoGroupedEmitting: true,
|
|
49
51
|
};
|
|
50
52
|
|
|
51
53
|
[kWebSocket];
|
|
@@ -97,10 +99,16 @@ class TaoMessageConsumer extends EventEmitter {
|
|
|
97
99
|
* @param {object|Function} [options] - The options
|
|
98
100
|
* @param {Function} [cb] - The Callback
|
|
99
101
|
* @returns {void}
|
|
100
|
-
* @since v0.3.4
|
|
102
|
+
* @since v0.3.4 Exposed for `send(new Message(SENDACK, Failed))` scene
|
|
103
|
+
* @since v0.3.7 The `data.content.content` can be a plain `object` now
|
|
101
104
|
*/
|
|
102
105
|
send(data, options = { mask: true, binary: true }, cb = undefined) {
|
|
103
|
-
if (data instanceof Message) {
|
|
106
|
+
if (data instanceof Message) {
|
|
107
|
+
if (typeof data[CONTENT][CONTENT] === 'object' && data[CONTENT][CONTENT] !== null) {
|
|
108
|
+
Reflect.set(data[CONTENT], CONTENT, stringify(data[CONTENT][CONTENT]));
|
|
109
|
+
}
|
|
110
|
+
this[kWebSocket]?.send(data.with(TOKEN, this[kToken]).buffer, options, cb);
|
|
111
|
+
}
|
|
104
112
|
}
|
|
105
113
|
|
|
106
114
|
onopen() {
|
|
@@ -129,7 +137,7 @@ class TaoMessageConsumer extends EventEmitter {
|
|
|
129
137
|
}
|
|
130
138
|
|
|
131
139
|
onping(data) {
|
|
132
|
-
logger.onping('The channel is onping with data [%s].',
|
|
140
|
+
logger.onping('The channel is onping with data [%s].', data);
|
|
133
141
|
this[kWebSocket]?.pong(data, true);
|
|
134
142
|
}
|
|
135
143
|
|
|
@@ -138,8 +146,8 @@ class TaoMessageConsumer extends EventEmitter {
|
|
|
138
146
|
this.reconnect(this[kOptions].onErrorReconnection);
|
|
139
147
|
}
|
|
140
148
|
|
|
141
|
-
onclose(
|
|
142
|
-
logger.onclose('The channel is onclose(%s), let us reconnect.',
|
|
149
|
+
onclose(code, reason) {
|
|
150
|
+
logger.onclose('The channel is onclose(%d: %s), let us reconnect.', code, reason);
|
|
143
151
|
this.reconnect(this[kOptions].onCloseReconnection);
|
|
144
152
|
}
|
|
145
153
|
|
|
@@ -148,7 +156,13 @@ class TaoMessageConsumer extends EventEmitter {
|
|
|
148
156
|
}
|
|
149
157
|
|
|
150
158
|
[`process${SEND}`](msg) {
|
|
151
|
-
if (msg && msg[CONTENT] && msg[CONTENT][TOPIC]) {
|
|
159
|
+
if (msg && msg[CONTENT] && msg[CONTENT][TOPIC]) {
|
|
160
|
+
[msg[CONTENT][TOPIC]].concat(
|
|
161
|
+
this[kOptions].autoGroupedEmitting
|
|
162
|
+
? msg[CONTENT][TOPIC].split(/^(([^_]+)_[^_]+)_.+/).slice(1, -1)
|
|
163
|
+
: [],
|
|
164
|
+
).forEach((t) => this.emit(t, msg));
|
|
165
|
+
}
|
|
152
166
|
|
|
153
167
|
if (this[kOptions].autoReplyConfirmation && msg && msg[CONTENT] && msg[CONTENT][ID]) {
|
|
154
168
|
const reply = new Message(SEND, Confirm).with(TOKEN, this[kToken]).with(ID, msg[CONTENT][ID]);
|