tmc.js 0.3.2 → 0.3.3

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 CHANGED
@@ -9,6 +9,12 @@ Events driven and chained Taobao Message Channel(TMC) for NodeJS
9
9
  [![downloads](https://img.shields.io/npm/dm/tmc.js)](https://www.npmjs.com/package/tmc.js)
10
10
  [![license](https://img.shields.io/npm/l/tmc.js)](https://www.npmjs.com/package/tmc.js)
11
11
 
12
+ ## 设计
13
+
14
+ 核心包以事件作为驱动,通过注册`TOPIC`回调处理函数,实现了TMC产品服务的消息消费处理能力,同时通过`Proxy`代理,以`TOPIC`作为键值,平序注册消息消费处理函数,序图如下:
15
+
16
+ [![SDK Sequence](./.github/sdk-sequence.svg)](./.github/sdk-sequence.mmd)
17
+
12
18
  ## 使用
13
19
 
14
20
  `npm i tmc.js`
package/lib/consumer.js CHANGED
@@ -187,16 +187,12 @@ class TaoMessageConsumer extends EventEmitter {
187
187
  connect(address) {
188
188
  if (address) { this[kAddress] = address; }
189
189
 
190
- this[kWebSocket]?.close();
191
-
192
- const ws = new WebSocket(this[kAddress]);
193
- Reflect.set(this, kWebSocket, ws);
194
-
195
- ws.on('open', () => this.onopen());
196
- ws.on('ping', (...arg) => this.onping(...arg));
197
- ws.on('error', (err) => this.onerror(err));
198
- ws.on('close', (...arg) => this.onclose(...arg));
199
- ws.on('message', (...arg) => this.onmessage(...arg));
190
+ this[kWebSocket] = new WebSocket(this[kAddress])
191
+ .on('message', (data, isBinary) => this.onmessage(data, isBinary))
192
+ .on('close', (code, reason) => this.onclose(code, reason))
193
+ .on('error', (err) => this.onerror(err))
194
+ .on('ping', (data) => this.onping(data))
195
+ .on('open', () => this.onopen());
200
196
 
201
197
  return this;
202
198
  }
@@ -12,6 +12,11 @@ const SDK = 'sdk';
12
12
  const INTRANET_IP = 'intranet_ip';
13
13
  const ID = 'id';
14
14
  const TOPIC = 'topic';
15
+ const MSG = 'msg';
16
+ const DATAID = 'dataid';
17
+ const USERID = 'userid';
18
+ const OUTTIME = 'outtime';
19
+ const PUBLISHER = 'publisher';
15
20
 
16
21
  class MessageFields {
17
22
  static TYPE = TYPE;
@@ -41,6 +46,16 @@ class MessageFields {
41
46
  static ID = ID;
42
47
 
43
48
  static TOPIC = TOPIC;
49
+
50
+ static MSG = MSG;
51
+
52
+ static DATAID = DATAID;
53
+
54
+ static USERID = USERID;
55
+
56
+ static OUTTIME = OUTTIME;
57
+
58
+ static PUBLISHER = PUBLISHER;
44
59
  }
45
60
 
46
61
  module.exports = MessageFields;
@@ -59,3 +74,8 @@ module.exports.SDK = SDK;
59
74
  module.exports.INTRANET_IP = INTRANET_IP;
60
75
  module.exports.ID = ID;
61
76
  module.exports.TOPIC = TOPIC;
77
+ module.exports.MSG = MSG;
78
+ module.exports.DATAID = DATAID;
79
+ module.exports.USERID = USERID;
80
+ module.exports.OUTTIME = OUTTIME;
81
+ module.exports.PUBLISHER = PUBLISHER;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmc.js",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Events driven and chained Taobao Message Channel(TMC) for NodeJS",
5
5
  "author": "James ZHANG",
6
6
  "license": "MIT",
package/types/index.d.ts CHANGED
@@ -784,6 +784,10 @@ declare interface TaoTopicsDescriptor {
784
784
  tmall_auto_TradeModify(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTradeModify) => void): TaoMessageConsumer;
785
785
  /** {@link IncomingMessage.TmallAutoTwoWheelsReceiptCreate 天猫汽车 > 天猫二轮车服务工单创建开放} */
786
786
  tmall_auto_TwoWheelsReceiptCreate(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTwoWheelsReceiptCreate) => void): TaoMessageConsumer;
787
+ /** {@link IncomingMessage.TmallCarContractSign 天猫汽车 > 合同签署消息} */
788
+ tmall_car_ContractSign(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarContractSign) => void): TaoMessageConsumer;
789
+ /** {@link IncomingMessage.TmallCarFinanceMsg 天猫汽车 > 汽车金融消息} */
790
+ tmall_car_FinanceMsg(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarFinanceMsg) => void): TaoMessageConsumer;
787
791
  /** {@link IncomingMessage.TmallChannelApplyOrderChange 渠道中心API > 申请单变更消息} */
788
792
  tmall_channel_ApplyOrderChange(fn: (this: TaoMessageConsumer, message: IncomingMessage.TmallChannelApplyOrderChange) => void): TaoMessageConsumer;
789
793
  /** {@link IncomingMessage.TmallChannelDeliverOrderChange 渠道中心API > 发货单消息变更} */
@@ -1609,6 +1613,10 @@ declare interface TaoEventsListener {
1609
1613
  on(topic: 'tmall_auto_TradeModify', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTradeModify) => void): TaoMessageConsumer;
1610
1614
  /** {@link IncomingMessage.TmallAutoTwoWheelsReceiptCreate 天猫汽车 > 天猫二轮车服务工单创建开放} */
1611
1615
  on(topic: 'tmall_auto_TwoWheelsReceiptCreate', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallAutoTwoWheelsReceiptCreate) => void): TaoMessageConsumer;
1616
+ /** {@link IncomingMessage.TmallCarContractSign 天猫汽车 > 合同签署消息} */
1617
+ on(topic: 'tmall_car_ContractSign', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarContractSign) => void): TaoMessageConsumer;
1618
+ /** {@link IncomingMessage.TmallCarFinanceMsg 天猫汽车 > 汽车金融消息} */
1619
+ on(topic: 'tmall_car_FinanceMsg', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallCarFinanceMsg) => void): TaoMessageConsumer;
1612
1620
  /** {@link IncomingMessage.TmallChannelApplyOrderChange 渠道中心API > 申请单变更消息} */
1613
1621
  on(topic: 'tmall_channel_ApplyOrderChange', listener: (this: TaoMessageConsumer, message: IncomingMessage.TmallChannelApplyOrderChange) => void): TaoMessageConsumer;
1614
1622
  /** {@link IncomingMessage.TmallChannelDeliverOrderChange 渠道中心API > 发货单消息变更} */
@@ -13,6 +13,11 @@ declare namespace MessageFields {
13
13
  type INTRANET_IP = 'intranet_ip';
14
14
  type ID = 'id';
15
15
  type TOPIC = 'topic';
16
+ type MSG = 'msg';
17
+ type DATAID = 'dataid';
18
+ type USERID = 'userid';
19
+ type OUTTIME = 'outtime';
20
+ type PUBLISHER = 'publisher';
16
21
  }
17
22
 
18
23
  declare class MessageFields {
@@ -30,4 +35,9 @@ declare class MessageFields {
30
35
  static INTRANET_IP: MessageFields.INTRANET_IP;
31
36
  static ID: MessageFields.ID;
32
37
  static TOPIC: MessageFields.TOPIC;
38
+ static MSG: MessageFields.MSG;
39
+ static DATAID: MessageFields.DATAID;
40
+ static USERID: MessageFields.USERID;
41
+ static OUTTIME: MessageFields.OUTTIME;
42
+ static PUBLISHER: MessageFields.PUBLISHER;
33
43
  }
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- /// <reference path="header-type.d.ts" />
2
+ /// <reference path="message-type.d.ts" />
3
3
  /// <reference path="message-kind.d.ts" />
4
4
 
5
5
  declare class Message {
@@ -7,14 +7,14 @@ declare class Message {
7
7
  type?: MessageType.CONNECT | MessageType.CONNECTACK | MessageType.SEND | MessageType.SENDACK,
8
8
  kind?: MessageKind.None | MessageKind.PullRequest | MessageKind.Confirm | MessageKind.Data | MessageKind.Failed
9
9
  );
10
- protocolVersion: number;
11
- messageType: number;
10
+ protocolVersion: 2;
11
+ messageType: MessageType.CONNECT | MessageType.CONNECTACK | MessageType.SEND | MessageType.SENDACK;
12
12
  statusCode?: number;
13
13
  statusPhrase?: string;
14
14
  flag?: number;
15
15
  token?: string;
16
16
  content?: MessageContent;
17
- with(key: string, value?: number | bigint | string | Buffer | Date): this;
17
+ with(key: string | MessageKind, value?: number | bigint | string | Buffer | Date): this;
18
18
  get buffer(): Buffer;
19
19
  static from(buf: Buffer): Message;
20
20
  }
@@ -751,6 +751,10 @@ declare namespace IncomingMessage {
751
751
  type TmallAutoTradeModify = Message & { content?: MessageContent & { content?: string | Tmall.Auto.TradeModify } };
752
752
  /** {@link Tmall.Auto.TwoWheelsReceiptCreate 天猫汽车 > 天猫二轮车服务工单创建开放} */
753
753
  type TmallAutoTwoWheelsReceiptCreate = Message & { content?: MessageContent & { content?: string | Tmall.Auto.TwoWheelsReceiptCreate } };
754
+ /** {@link Tmall.Car.ContractSign 天猫汽车 > 合同签署消息} */
755
+ type TmallCarContractSign = Message & { content?: MessageContent & { content?: string | Tmall.Car.ContractSign } };
756
+ /** {@link Tmall.Car.FinanceMsg 天猫汽车 > 汽车金融消息} */
757
+ type TmallCarFinanceMsg = Message & { content?: MessageContent & { content?: string | Tmall.Car.FinanceMsg } };
754
758
  /** {@link Tmall.Channel.ApplyOrderChange 渠道中心API > 申请单变更消息} */
755
759
  type TmallChannelApplyOrderChange = Message & { content?: MessageContent & { content?: string | Tmall.Channel.ApplyOrderChange } };
756
760
  /** {@link Tmall.Channel.DeliverOrderChange 渠道中心API > 发货单消息变更} */
package/types/tmall.d.ts CHANGED
@@ -28,6 +28,25 @@ declare namespace Tmall.Auto {
28
28
  }
29
29
  }
30
30
 
31
+ /** 天猫汽车 */
32
+ declare namespace Tmall.Car {
33
+ /** 合同签署消息 */
34
+ interface ContractSign {
35
+ /** 合同下载地址,有效期10分钟 */
36
+ contract_url: string;
37
+ /** 订单id */
38
+ order_id: number;
39
+ }
40
+
41
+ /** 汽车金融消息 */
42
+ interface FinanceMsg {
43
+ /** 订单id */
44
+ order_id: number;
45
+ /** 消息类型: FULL_PURCHASE_FINANCE_CANCEL_MSG_TYPE?,因全款购车导致金融取消 REFUND_FINANCE_CANCEL,因退款导致的金融单关闭 */
46
+ type: string;
47
+ }
48
+ }
49
+
31
50
  /** 渠道中心API */
32
51
  declare namespace Tmall.Channel {
33
52
  /** 申请单变更消息 */