1 Star 6 Fork 0

TheNorthMemory / tmc.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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)

参数 类型 说明
appKey string 应用ID
appSecret BinaryLike 应用密钥
groupName string | ConsumerOptions 消息分组(可选,默认default)
options ConsumerOptions 消费端配置参数(可选)
options.pullRequestInterval number 定时发送pullRequest请求时间间隔(默认5000毫秒)
options.onErrorReconnection number 当消费端出现错误,重试连接间隔(默认15000毫秒)
options.onCloseReconnection number 当消费端断开连接,重试连接间隔(默认3000毫秒)
options.autoParseContentJson boolean 自动解析推送消息$.content.content字段为对象(默认true)
options.autoReplyConfirmation boolean 以推送的$.content.id字段自动Confirm消息(默认true)
options.autoGroupedEmitting boolean /^(([^_]+)_[^_]+)_.+/规则切分$.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"}')
)
可选设置的 NODE_DEBUG=< label > 环境变量
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

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

链接

变更历史

It used the github release page for the Changelog here.

License

MIT

Copyright (c) 2022-2024 James ZHANG(TheNorthMemory) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Event driven and chained Taobao Message Channel(TMC) for NodeJS 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
NodeJS
1
https://gitee.com/TheNorthMemory/tmc.js.git
git@gitee.com:TheNorthMemory/tmc.js.git
TheNorthMemory
tmc.js
tmc.js
main

搜索帮助