Convert thrift IDL file to .d.ts 😃
Go to file
2018-09-16 01:44:41 +03:00
bin chore(config): 增加各种配置 2017-11-09 17:59:38 +08:00
lib Merge branch 'master' of git://github.com/samueltuckey/thrift-ts into samueltuckey-master 2018-07-25 15:50:44 +08:00
src add ci icon to README 2018-07-27 01:37:17 +08:00
test add ci icon to README 2018-07-27 01:37:17 +08:00
.gitignore add ci icon to README 2018-07-27 01:37:17 +08:00
.npmignore update thrift-parser version 2018-07-25 15:29:30 +08:00
.travis.yml add travis ci support 2018-07-27 01:20:26 +08:00
package-lock.json add ci icon to README 2018-07-27 01:37:17 +08:00
package.json add build script 2018-09-16 01:44:41 +03:00
README.md add ci icon to README 2018-07-27 01:37:17 +08:00
tsconfig.json update thrift-parser version 2018-07-25 15:29:30 +08:00
tslint.json init 2017-04-18 10:12:37 +08:00

Build Status

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>;
}