N-Thrift
English document is being prepared
简介
n-thrift
是 Apache Thrift 的一个 nodejs 的实现,包含了 thrift compiler 和运行类库,相较于官方维护的 thrift
, n-thrift
具有以下特点:
- 支持出参和入参自动实例化
- 支持es2016+,支持map、set等数据结构,支持服务端客户端都使用Promise
- 支持namespace,生成文件添加index.js文件,可以极为方便的发布到私有库
- 灵活配置,支持i64默认转换为string
- 支持esm,支持生成.mjs文件
- 支持struct field编码风格,如thrift定义风格为snakeCase,可以选择编译为camelCase,node端返回的都是camelCase的数据
安装
npm i n-thrift -S
全局安装
npm i n-thrift -g
使用
定义thrift文件 tutorial.thrift
namespace js test struct Result { 1:string name;} struct Request { 1:string some;} service MyService { Result hello(1:Request request)}
编译
n-thrift <thriftFileDir> [options]//项目内安装用npxnpx n-thrift <thriftFileDir> [options]
如
node-thrift ./thrift -o lib -e mjs -m esm
options
配置项 | 简写 | 默认 | 描述 |
---|---|---|---|
--out | -o | '.' | 输出文件路径 |
--module | -m | 'cjs' | js模块方式,支持cjs 和esm |
--extname | -e | 'js' | 生成文件后缀 |
--fieldcase | -fc | 'default' | field代码风格,支持camelCase 和snakeCase |
服务端
const nThrift = ;const thriftType = ; const MyServiceServer = thriftTypetesttutorial async { //do something return name: requestsome ; } const server = nThrift;server;
客户端
const nThrift = ;const thriftType = ; const MyServiceClient = thriftTypetesttutorialconst connection = nThriftconst myService = nThrift; myService //可以在异步方法中使用{ const result = await myService; console // 'world'}