0.3.39 • Published 9 days ago

tmc.js v0.3.39

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

tmc.js

Event driven and chained Taobao Message Channel(TMC) for NodeJS

release advisor types requirement downloads license

设计

核心包以事件作为驱动,通过注册TOPIC回调处理函数,实现了TMC产品服务的消息消费处理能力,同时通过Proxy代理,以TOPIC作为键值,平序注册消息消费处理函数,序图如下:

SDK Sequence

使用

npm i tmc.js

import Tmc from 'tmc.js';

new Tmc('your_app_key', 'your_app_secret')
.on('taobao_trade_TradeChanged', msg => console.info(msg))
.taobao_trade_TradeClose(msg => console.info(msg))
.connect();

API

new Tmc(appKey: string, appSecret: BinaryLike, groupName?: string | ConsumerOptions, options?: ConsumerOptions)

参数类型说明
appKeystring应用ID
appSecretBinaryLike应用密钥
groupNamestring \| ConsumerOptions消息分组(可选,默认default)
optionsConsumerOptions消费端配置参数(可选)
options.pullRequestIntervalnumber定时发送pullRequest请求时间间隔(默认5000毫秒)
options.onErrorReconnectionnumber当消费端出现错误,重试连接间隔(默认15000毫秒)
options.onCloseReconnectionnumber当消费端断开连接,重试连接间隔(默认3000毫秒)
options.autoParseContentJsonboolean自动解析推送消息$.content.content字段为对象(默认true)
options.autoReplyConfirmationboolean以推送的$.content.id字段自动Confirm消息(默认true)
options.autoGroupedEmittingboolean/^(([^_]+)_[^_]+)_.+/规则切分$.content.topic主题,开关消费端多维监听功能(默认true)

tmc.on(topic: string, listener: (this: Tmc, message: Message) => void) => Tmc

注册 topic 消息通知处理函数,默认已内置 消息 说明。 自v0.3.6起,默认开启消费端多维监听功能,topic字符串也可以是BUBU_G单位,例如:

.on('taobao', console.info)
.on('alibaba_einvoice', console.info)

tmc[<topic>](fn: (this: Tmc, message: Message) => void) => Tmc

直接以 topic 为键值,注册消息通知处理函数。 自v0.3.6起,默认开启消费端多维监听功能,topic字符串也可以是BUBU_G单位,例如:

.taobao(console.info)
.alibaba_einvoice(console.info)

tmc.reconnect(ms: number) => Tmc

当消费端 onerror/onclose 事件发生时,延迟 ms 毫秒自动重新建立连接。

tmc.connect(address?: string) => Tmc

消费端发起建立连接 onopen 事件,address 默认为 ws://mc.api.taobao.com/

tmc.send(msg: Message, options?: { mask?: true, binary?: true }, cb?: (err: Error) => void) => void

v0.3.4起,当自动应答确认消息无法满足需求的时候,比如消息处理失败,需要Publisher再次重推消息,在实例初始化时置options.autoReplyConfirmation=false,则在消息处理函数内,可以通过 this.send() 函数回复确认或者失败消息。例如:

new Tmc('your_app_key', 'your_app_secret', { autoReplyConfirmation: false })
.taobao_trade_TradeDelayConfirmPay(function needDoubleRetriesThenConfirm(msg) {
  if (msg instanceof Message && msg.content?.retried === 0) {
    this.send(
      new Message(MessageType.SENDACK, MessageKind.Failed)
      .with(MessageFields.ID, msg.content?.id)
      .with(MessageFields.MSG, 'Something went wrong, please retries this ID.')
    );
  }
})
.connect();

也可以使用此方法发送To淘宝消息。在>=v0.3.4 && <v0.3.7版本期间,需要自主对$.content.content数据做JSON字符串化(特别注意: 原生JSONBigInt类型无法处理)。自v0.3.7起,可直接对$.content.content以原生object类型描述,SDK会将此对象转化成JSON字符串。例如:

/** @see https://open.taobao.com/tmc.htm?docId=732&docType=9 */
.send(
  new Message(MessageType.SEND, MessageKind.Data)
  .with(MessageFields.TOPIC, 'taobao_fuwu_ElectronicInvoice')
  .with(MessageFields.CONTENT, {
    id: 12345678901234567n, // 支持 BigInt 类型
    tid: 12345678901234567n,
    oid: 12345678901234567n,
    invoice_file: 12345678901234567n,
    e_invoice_no: '12342243435466',
    invoice_time: '2015-04-10 10:33:49', // 时区 +08:00
    invoice_no: '123456',
    invoice_code: '123456',
    amount: '100.00',
  })
  // >=v0.3.4 && <v0.3.7 写法
  // .with(MessageFields.CONTENT, '{"tid":12345678901234567,"amount":"100.00"}')
)
label说明
tmc:onping开启 onping 时的日志
tmc:onopen开启 onopen 时的日志
tmc:onpull开启 onpull 时的日志
tmc:onerror开启 onerror 时的日志
tmc:onclose开启 onclose 时的日志
tmc:onmessage*开启全部 onmessage 时的日志(即From淘宝消息)
tmc:onmessage:connect开启消费端发起连接 connect 时的日志
tmc:onmessage:connectack开启消费端回复连接 connectack 时的日志
tmc:onmessage:send开启消费端接收到(即From淘宝) send 的消息时的日志
tmc:onmessage:sendack当消费端收到(From淘宝)消息,消费端消息处理失败,需要服务端重发,须回复SENDACK(3)FLAG字典值时的日志
tmc:onmessage:send:confirm开启消费端回复接收到的(即From淘宝消息),发送自动确认 send:confirm 时的日志

支持的TOPICS

类别消息数
淘宝交易25
淘宝退款13
淘宝商品13
淘宝分销27
淘宝点点12
淘宝火车票6
平台消息9
交易全链路3
淘宝机票15
导购平台21
淘宝汽车票4
服务市场9
天猫服务27
天猫美妆2
聚石塔9
淘宝物流1
阿里通信19
天猫魔盒2
营销平台1
OpenIM消息1
网上法庭8
电子发票21
航旅度假交易8
YunOS YoC2
淘宝直播API3
阿里物联2
全球购跨境物流1
零售plus8
客户运营平台API19
AE-交易10
五道口配送5
百川2
闲鱼21
闲鱼回收商消息6
零售通POS开放平台消息4
DPAAS6
AliGenius1
智慧门店下行消息2
渠道中心API4
五道口订单22
信息平台-采购3
1688服务市场1
酒店商品消息api9
新零售终端下行消息1
新零售终端上行消息4
欢行开放平台1
阿里发票5
大麦票单状态1
五道口营销4
酒店签约中心消息3
蜂鸟物流6
商旅API3
阿里健康-O2O中台2
业务平台新零售-消息上行2
大麦第三方票务供应商接入9
TVOS应用审核平台1
Gifting送礼1
五道口商品2
HOMEAI2
HOMEAI消息对接5
零售通_公共8
酒店标准库基础信息变更消息2
菜鸟发货工作台1
IOT-智能制造2
智能制造API1
IoT售后解决方案1
OpenMall-API5
闲鱼已验货6
阿里健康三方机构2
聚石塔监控告警2
大资产拍卖Top端拍品消息2
AE-任务平台消息1
天猫汽车5
阿信消息通知前台类目4
阿里健康追溯码1
自动驾驶API3
MMC五盘货项目5
银泰开放平台消息2
阿里智付1
代发管理2
蚂蚁采购1
阿里健康&一树-电商中台对接2
阿里健康-疫苗2
智能应用1

链接

变更历史

It used the github release page for the Changelog here.

License

MIT

0.3.39

9 days ago

0.3.38

1 month ago

0.3.37

2 months ago

0.3.36

3 months ago

0.3.35

4 months ago

0.3.34

4 months ago

0.3.33

5 months ago

0.3.32

6 months ago

0.3.31

6 months ago

0.3.30

7 months ago

0.3.29

8 months ago

0.3.28

9 months ago

0.3.27

10 months ago

0.3.26

10 months ago

0.3.25

11 months ago

0.3.17

1 year ago

0.3.16

1 year ago

0.3.20

1 year ago

0.3.24

12 months ago

0.3.23

1 year ago

0.3.22

1 year ago

0.3.21

1 year ago

0.3.19

1 year ago

0.3.18

1 year ago

0.3.15

1 year ago

0.3.14

1 year ago

0.3.13

1 year ago

0.3.12

1 year ago

0.3.11

1 year ago

0.3.10

1 year ago

0.3.9

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago