1 | # dts-from-thrift
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/dts-from-thrift.svg)](https://www.npmjs.com/package/dts-from-thrift)[![build](https://travis-ci.org/LPegasus/dts-from-thrift.svg?branch=master)](https://travis-ci.org/LPegasus/dts-from-thrift)[![coverage](https://img.shields.io/codecov/c/github/LPegasus/dts-from-thrift.svg?style=flat-square)](https://codecov.io/gh/LPegasus/dts-from-thrift)[![install size](https://packagephobia.now.sh/badge?p=dts-from-thrift)](https://packagephobia.now.sh/result?p=dts-from-thrift)
|
4 |
|
5 | thrift RPC 定义文件转 d.ts 工具
|
6 |
|
7 | **安装(install):** `npm install dts-from-thrift -g`
|
8 |
|
9 | **运行(exec):**
|
10 | ```sh
|
11 | # for thrift
|
12 | dts-from-thrift -p ~/git/my-thrift-repo/thrift -o ~/git/my-ts-repo/typings
|
13 | # for pb
|
14 | dts-from-protobuf -p ~/git/my-protobuf-repo/proto -o ~/git/my-ts-repo/typings
|
15 | ```
|
16 |
|
17 | # 变更历史(ChangeLog)
|
18 |
|
19 | ## 1.2.2 - 2020.11.19
|
20 |
|
21 | ### Fixed
|
22 |
|
23 | - 修复 pb message 下的 enum 没有输出在 enums.json 中的问题 ([#39](https://github.com/LPegasus/dts-from-thrift/pull/39/commits/3fc0624f32b9cc79b6e6865c5803c20e77d726f6) by [@chengcyber](https://github.com/chengcyber))
|
24 |
|
25 | - 修复 pb 变量名不能是 list 的 bug ([#37](https://github.com/LPegasus/dts-from-thrift/pull/37/commits/60c934943a9b82328d5d7939aa95856b5a6c607c) by [@nameliuqi](https://github.com/nameliuqi))
|
26 |
|
27 | ## 1.2.1 - 2020.11.18
|
28 |
|
29 | ### Fixed
|
30 |
|
31 | - 修复 thrift `--bail` 没效果的 bug
|
32 |
|
33 | ## 1.2.0 - 2020.10.09
|
34 |
|
35 | ### Changed
|
36 |
|
37 | - pb 强制使用 protobuf.js 解析
|
38 |
|
39 | ### Fixed
|
40 |
|
41 | - pb 文件中的 enum 没有在 message 中使用,也会在 enums.json 中生成,之前没有生成
|
42 |
|
43 | ## 1.1.10 - 2020.08.19
|
44 |
|
45 | ### Fixed
|
46 |
|
47 | - thrift 文件带下划线的 namespace 匹配错误问题
|
48 |
|
49 | ## 1.1.7 - 2020.08.04
|
50 |
|
51 | ### Added
|
52 |
|
53 | - pb 增加 `--bail` 参数,满足 idl 校验需求
|
54 |
|
55 | ## 1.1.6 - 2020.07.28
|
56 |
|
57 | ### Fixed
|
58 |
|
59 | - thrift 文件中单行注释包含 `*/` 字符的文件注释会在替换成多行注释时,错误地闭合多行注释。现将所有单行注释中的 `*/` 字符直接删掉
|
60 |
|
61 | - 注释错位问题 fix
|
62 |
|
63 | ## 1.1.4 - 2020.04.28
|
64 |
|
65 | ### Changed
|
66 |
|
67 | - 优化 thrift 文件生成的代码格式 ([@mehwww](https://github.com/mehwww) in [#22](https://github.com/LPegasus/dts-from-thrift/pull/22))
|
68 |
|
69 | ## 1.1.3 - 2020.03.29
|
70 |
|
71 | ### Breaking Change
|
72 |
|
73 | - protobuf 所有字段均为 optional,与 pb3 一致
|
74 |
|
75 | ## 1.1.0 - 2020.03.19
|
76 |
|
77 | ### Breaking Change
|
78 |
|
79 | - 删除 `--new` 传参,只保留 ast 解析
|
80 |
|
81 | ### Changed
|
82 |
|
83 | - 将行尾注释放到该行上面,使用 `/** */` 标注,以使得 vscode 可提示注释内容
|
84 |
|
85 | ## 1.0.3 - 2019.11.18
|
86 |
|
87 | ### Bugfix
|
88 |
|
89 | - 修复 const enum 为空的报错 [commit](https://github.com/LPegasus/dts-from-thrift/pull/19)
|
90 |
|
91 | ## 1.0.2 - 2019.10.29
|
92 |
|
93 | ### Bugfix
|
94 |
|
95 | - 老的 protobuf 转换类型逻辑受 1.0.0 功能影响,回滚老的代码逻辑
|
96 |
|
97 | ## 1.0.0 - 2019.10.20
|
98 |
|
99 | ### Added
|
100 |
|
101 | - 新增 `--i64` 参数
|
102 |
|
103 | i64 转换成 string 或 Int64 对象**(需要加 --new 参数)**
|
104 |
|
105 | ```shell
|
106 | node ./bin/dts-from-[thrift|protobuf] -p ./test/idl/ \
|
107 | -o ./test/idl/out/ \
|
108 | --new --i64 string
|
109 | ```
|
110 |
|
111 | - 新增 `--enum-json` 参数
|
112 |
|
113 | 把 thrift idl 中的 const 输出到 d.ts 和 enum.json 中,用于类型提示或者自定义 babel 插件(for byted-react-scripts)
|
114 |
|
115 | 默认输出在 enums.json 文件中
|
116 |
|
117 | > 注意:d.ts 中的 const 在编译过程中并不会被转换。效果可以运行根目录下的 run.sh 来查看。
|
118 |
|
119 | - 新增 `--map` 参数
|
120 |
|
121 | 支持 thrift idl 中的 `map<type_a, type_b>` 输出到 `Record<string, type_b>`
|
122 |
|
123 | > 注意:Record 对于 key 的支持只有 number,string,symbol,所以 `type_a` 强制为 `string`。
|
124 |
|
125 | 栗子:
|
126 |
|
127 | ```shell
|
128 | node ./bin/dts-from-[thrift|protobuf] -p ./test/idl/ \
|
129 | -o ./test/idl/out/ \
|
130 | --new --i64 string --map
|
131 | ```
|
132 |
|
133 | ## 1.0.0-rc.10 - 2019.9.8
|
134 |
|
135 | 针对 struct 中的 field 是否是 optional 进行了梳理,详见`test/thriftNew/readCode.test.ts` line649 和下表,主要是针对`/\w+Response/i`和`/\w+Request/i`做了特殊处理,以符合实际的 idl 语义
|
136 |
|
137 | | 是否可选 | requeird | 无(默认) | optional |
|
138 | | ------------------------------------ | -------- | ------------- | -------- |
|
139 | | \*response | false | false | true |
|
140 | | \*response & defualt value | False | false | true |
|
141 | | \*request | false | false\|true\* | true |
|
142 | | \*request & default value | false | true | true |
|
143 | | Use-strict | false | true | true |
|
144 | | No-use-strict | false | false | true |
|
145 | | [^request\|response] & Default value | false | true | true |
|
146 |
|
147 | "\*"表示需要标记`strict-request`开启,表示是业务定制
|
148 |
|
149 | ## 1.0.0-rc.6 - 2019.8.19
|
150 |
|
151 | ### Added
|
152 |
|
153 | - 新增 `--strict-response` 传参
|
154 |
|
155 | 指定后如果 struct 名称包含 Response(例如:`ListResponse`),那么它的字段如果有 defaultValue 词缀,则此字段不会有 optional 标识
|
156 |
|
157 | ```ts
|
158 | /*
|
159 | struct CreateResponse {
|
160 | 255: optional i32 err_no (defaultValue=0);
|
161 | }
|
162 | */
|
163 |
|
164 | interface CreateResponse {
|
165 | err_no: number; // 不指定的时候是 err_no?: number;
|
166 | }
|
167 | ```
|
168 |
|
169 | ## 1.0.0-rc.5 - 2019.8.9
|
170 |
|
171 | ### Added
|
172 |
|
173 | - dts-from-thrift 保留注释和注解
|
174 |
|
175 | ## 1.0.0-rc.3 - 2019.7.12
|
176 |
|
177 | ### Fixed
|
178 |
|
179 | - dts-from-protobuf 旧逻辑没有适配 `--i64_as_number` 参数
|
180 |
|
181 | ## 1.0.0-rc.2 - 2019.6.25
|
182 |
|
183 | ### Added
|
184 |
|
185 | - protobuf 如果没有 package,将尝试从 options 中取一个名称作为 namespace。如果没有 options,取文件路径为 namespace
|
186 | - dts-from-protobuf 新增 `--i64_as_number` 参数,将 i64 降级为 number
|
187 |
|
188 | ## 1.0.0-rc.1 - 2019.6.24
|
189 |
|
190 | ### Changed
|
191 |
|
192 | - protobuf 如果没有 package,将跳过该文件,而非中断报错
|
193 |
|
194 | ## 1.0.0-rc.0 - 2019.6.5
|
195 |
|
196 | ### Added
|
197 |
|
198 | - 新增 `--new` 参数,分别使用 [protobufjs](https://github.com/protobufjs/protobuf.js) 解析 pb、使用 [@creditkarma/thrift-parser](https://github.com/creditkarma/thrift-parser) 解析 thrift
|
199 |
|
200 | ## 0.9.0-beta.1 - 2019.1.7
|
201 |
|
202 | ### Bugfix
|
203 |
|
204 | - `thrift` `Struct` 字段如果定义了 `defaultValue`,生成的 dts 字段将带 `?`
|
205 |
|
206 | fields in `thrift Struct` with `defaultValue` will attach a `?` when in `d.ts` file
|
207 |
|
208 | ## 0.9.0-beta.0 - 2019.1.2
|
209 |
|
210 | ### Breaking Change
|
211 |
|
212 | - thrift `i64` 类型会被生成为 `Int64` 的接口类型。该接口定义在 `tsHelper.d.ts` 中
|
213 |
|
214 | ## 0.8.8 - 2018.12.28
|
215 |
|
216 | ### Bugfix
|
217 |
|
218 | - 正确解析有多个形参的 service
|
219 |
|
220 | parse service with multiple params currectly
|
221 |
|
222 | ## 0.8.6 - 2018.12.27
|
223 |
|
224 | ### Bugfix
|
225 |
|
226 | - 正确解析单行 service 声明
|
227 |
|
228 | parse single line service declaration currectly.
|
229 |
|
230 | - typeof 泛型映射修复
|
231 |
|
232 | typedef generic type fix
|
233 |
|
234 | ## 0.8 - 2018.11.26
|
235 |
|
236 | ### Added
|
237 |
|
238 | - 为 `${namespace}._Summary_` 接口增加了 `WrapperService` 推导函数。用来解决 `i64` 转 number 在 js 下需要特殊处理的问题。可以通过扩展 `Int64Type` 接口来实现自己的 `i64` 类型适配
|
239 |
|
240 | Add a type inference wrapper function to process the i64 type. Extends the `Int64Type` interface for customization.
|
241 |
|
242 | ## 0.7 - 2018.11.20
|
243 |
|
244 | ### Added
|
245 |
|
246 | - `dts-from-thrift` 支持合并 service 到一个定义文件中
|
247 |
|
248 | 使用 `--rpc-namespace=xxx` 来指定需要合并到的 namespace 中,并且可以通过 `${namespace}._Summary_` 来获得一个总的 interface 用以继承
|
249 |
|
250 | ```javascript
|
251 | > dts-from-thrift -p ./ -o ../typings --rpc-namespace=demo.rpc
|
252 | // 你输入的命令 (the Command you input)
|
253 | > RPC d.ts files is /Users/xxx/projectpath/typings/demo.rpc-rpc.d.ts
|
254 | // 生成的 rpc 定义文件 (the rpc definition file name)
|
255 | ```
|
256 |
|
257 | 使用 \_Summary* 继承 rpc 接口 (use \_Summary* to extends)
|
258 |
|
259 | ```typescript
|
260 | interface MyRPCContainer extends demo.rpc._Summary_ {}
|
261 | ```
|
262 |
|
263 | ### Bugfix
|
264 |
|
265 | - 修复 service 无形参接口的报错问题
|
266 |
|
267 | fix service interfaces which are without input params
|
268 |
|
269 | ## 0.6 - 2018.11.13
|
270 |
|
271 | ### Added
|
272 |
|
273 | - thrift 现在支持生成 `service RpcService {}` 定义
|
274 |
|
275 | (support service definition)
|
276 |
|
277 | ```typescript
|
278 | // thrift sample
|
279 | service RpcService {
|
280 | ResponseType interface1 (1: RequestType req);
|
281 | }
|
282 |
|
283 | // generate code
|
284 | export interface RpcService {
|
285 | interface1(req: RequestType): Promise<ResponseType>;
|
286 | }
|
287 | ```
|
288 |
|
289 | ## 0.5
|
290 |
|
291 | ### Added
|
292 |
|
293 | - dts-from-protobuf 支持
|
294 |
|
295 | (add dts-from-protobuf CLI, support protobuf)
|
296 |
|
297 | `dts-from-protobuf -p <protobuf_idl_dir> -o <output_dir>`
|
298 |
|
299 | ## 0.4
|
300 |
|
301 | ### Added
|
302 |
|
303 | - --use-tag CLI option,来支持下面的语法
|
304 |
|
305 | (add a new option [--use-tag] to replace original fieldname with tagname)
|
306 |
|
307 | ```csharp
|
308 | struct Foo {
|
309 | 1: list<string> comments (go.tag="json:\\"list,omitempty\\""),
|
310 | }
|
311 |
|
312 | // before 0.4.0
|
313 | interface Foo {
|
314 | comments: string[];
|
315 | }
|
316 |
|
317 | // after 0.4.0 with [--use-tag go]
|
318 | interface Foo {
|
319 | list: string[];
|
320 | }
|
321 | // detail in parseStruct.test.ts
|
322 | ```
|
323 |
|
324 | ### Changed
|
325 |
|
326 | - codestyle: 生成文件增加 `// prettier-ignore` 避免 format 导致的 git 提交
|
327 |
|
328 | (add `// prettier-ignore` at head of d.ts file to prevent useless git commit)
|
329 |
|
\ | No newline at end of file |