mirror of
https://github.com/valitydev/thrift-ts.git
synced 2024-11-06 00:35:23 +00:00
Convert thrift IDL file to .d.ts 😃
.github/workflows | ||
bin | ||
src | ||
test | ||
.gitignore | ||
.npmignore | ||
.prettierignore | ||
.travis.yml | ||
package-lock.json | ||
package.json | ||
prettier.config.js | ||
README.md | ||
tsconfig.json | ||
tslint.json |
thrift-ts
thrift-ts
is a typescript compiler that compile *.thrift files to *.d.ts files.
It should works with thrift --gen js:node
commands.
Installation
$ npm install -g thrift-ts
How to use
CLI
// just compile one file
thrift-ts Model.thrift
// compile all IDL files in the folder and output in other folder
thrift-ts ./src -o ./dist
// learn more
thrift-ts -h
Node
import thriftTs from 'thriftTs';
import fs = require('fs');
const filename = './Model.thrift';
const files = thriftTs({
filename,
content: fs.readFileSync(filename),
})
console.log(files);
// [{ filename: 'Model_types.d.ts', content: '...'}]
Example
thrift file:
namespace java com.my.test
struct Result {
1: i32 id;
2: string name;
}
enum Status {
Success = 1;
Error = 2;
}
struct Response {
1:required Status status;
2:optional list<Result> result;
}
struct Request {
1: required string query;
2: optional number page;
}
service MyTestService {
Response search(1:Request request);
}
.d.ts file:
type Callback<T, E> = (err: E, resp: T) => void;
interface Int64 {
constructor(o?: number | string): this;
toString(): string;
toJson(): string;
}
export enum Status {
Success = 1,
Error = 2,
}
export class Result {
id: number;
name: string;
constructor(arg?: {
id: number;
name: string;
})
}
export class Response {
status: Status;
result?: Result[];
constructor(arg?: {
status: Status;
result?: Result[];
})
}
export class Request {
query: string;
page?: number;
constructor(arg?: {
query: string;
page?: number;
})
}
export class Client {
search(request: Request, callback: Callback<Response, Error>): void;
search(request: Request): Promise<Response>;
}