add ci icon to README

This commit is contained in:
Eric Time 2018-07-27 01:37:17 +08:00
parent e4dd40a107
commit eebee1d4fa
10 changed files with 156 additions and 550 deletions

5
.gitignore vendored
View File

@ -67,12 +67,13 @@ Session.vim
*~
dist
!test/mock/dist
spm_modules
.vscode
src/**/*.js
src/**/*.d.ts
test/**/*.js
test/**/*.d.ts
lib/**/*.d.ts
!test/mock/**/*.d.ts
test/**/*.d.ts
!test/mock/dist/*.d.ts
**/*.js.map

View File

@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/windwhinny/thrift-ts.svg?branch=master)](https://travis-ci.org/windwhinny/thrift-ts)
# thrift-ts
`thrift-ts` is a typescript compiler that compile *.thrift files to *.d.ts files.

190
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "thrift-ts",
"version": "1.1.3",
"version": "1.1.5",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -31,9 +31,9 @@
"integrity": "sha512-wc+VveszMLyMWFvXLkloixT4n0harUIVZjnpzztaZ0nKLuul7Z32iMt2fUFGAaZ4y1XWjFRMtCI5ewvyh4aIeg==",
"dev": true,
"requires": {
"@types/events": "1.2.0",
"@types/minimatch": "3.0.3",
"@types/node": "7.0.13"
"@types/events": "*",
"@types/minimatch": "*",
"@types/node": "*"
}
},
"@types/minimatch": {
@ -81,7 +81,7 @@
"resolved": "http://r.npm.sankuai.com/brace-expansion/download/brace-expansion-1.1.7.tgz",
"integrity": "sha1-Pv/DxQ4ABTH7cg6v+A8K6O8jz1k=",
"requires": {
"balanced-match": "0.4.2",
"balanced-match": "^0.4.1",
"concat-map": "0.0.1"
}
},
@ -107,9 +107,9 @@
"integrity": "sha1-TQJjewZ/6Vi9v906QOxW/vc3Mkc=",
"dev": true,
"requires": {
"assertion-error": "1.0.2",
"deep-eql": "0.1.3",
"type-detect": "1.0.0"
"assertion-error": "^1.0.1",
"deep-eql": "^0.1.3",
"type-detect": "^1.0.0"
}
},
"cliui": {
@ -117,9 +117,9 @@
"resolved": "http://r.npm.sankuai.com/cliui/download/cliui-3.2.0.tgz",
"integrity": "sha1-EgYBU3qRbSmUD5NNo7SNWFo5IT0=",
"requires": {
"string-width": "1.0.2",
"strip-ansi": "3.0.1",
"wrap-ansi": "2.1.0"
"string-width": "^1.0.1",
"strip-ansi": "^3.0.1",
"wrap-ansi": "^2.0.0"
}
},
"code-point-at": {
@ -133,7 +133,7 @@
"integrity": "sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=",
"dev": true,
"requires": {
"graceful-readlink": "1.0.1"
"graceful-readlink": ">= 1.0.0"
}
},
"concat-map": {
@ -183,7 +183,7 @@
"resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.1.tgz",
"integrity": "sha1-+FWobOYa3E6GIcPNoh56dhLDqNw=",
"requires": {
"is-arrayish": "0.2.1"
"is-arrayish": "^0.2.1"
}
},
"escape-string-regexp": {
@ -197,8 +197,8 @@
"resolved": "http://r.npm.sankuai.com/find-up/download/find-up-1.1.2.tgz",
"integrity": "sha1-ay6YIrGizgpgq2TWEOzK1TyyTQ8=",
"requires": {
"path-exists": "2.1.0",
"pinkie-promise": "2.0.1"
"path-exists": "^2.0.0",
"pinkie-promise": "^2.0.0"
}
},
"fs.realpath": {
@ -216,12 +216,12 @@
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.1.tgz",
"integrity": "sha1-gFIR3wT6rxxjo2ADBs31reULLsg=",
"requires": {
"fs.realpath": "1.0.0",
"inflight": "1.0.6",
"inherits": "2.0.3",
"minimatch": "3.0.3",
"once": "1.4.0",
"path-is-absolute": "1.0.1"
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
"inherits": "2",
"minimatch": "^3.0.2",
"once": "^1.3.0",
"path-is-absolute": "^1.0.0"
}
},
"graceful-fs": {
@ -263,8 +263,8 @@
"resolved": "http://r.npm.sankuai.com/inflight/download/inflight-1.0.6.tgz",
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
"requires": {
"once": "1.4.0",
"wrappy": "1.0.2"
"once": "^1.3.0",
"wrappy": "1"
}
},
"inherits": {
@ -287,7 +287,7 @@
"resolved": "http://r.npm.sankuai.com/is-builtin-module/download/is-builtin-module-1.0.0.tgz",
"integrity": "sha1-VAVy0096wxGfj3bDDLwbHgN6/74=",
"requires": {
"builtin-modules": "1.1.1"
"builtin-modules": "^1.0.0"
}
},
"is-fullwidth-code-point": {
@ -295,7 +295,7 @@
"resolved": "http://r.npm.sankuai.com/is-fullwidth-code-point/download/is-fullwidth-code-point-1.0.0.tgz",
"integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=",
"requires": {
"number-is-nan": "1.0.1"
"number-is-nan": "^1.0.0"
}
},
"is-utf8": {
@ -326,7 +326,7 @@
"resolved": "http://r.npm.sankuai.com/lcid/download/lcid-1.0.0.tgz",
"integrity": "sha1-MIrMr6C8SDo4Z7S28rlQYlHRuDU=",
"requires": {
"invert-kv": "1.0.0"
"invert-kv": "^1.0.0"
}
},
"load-json-file": {
@ -334,11 +334,11 @@
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz",
"integrity": "sha1-lWkFcI1YtLq0wiYbBPWfMcmTdMA=",
"requires": {
"graceful-fs": "4.1.11",
"parse-json": "2.2.0",
"pify": "2.3.0",
"pinkie-promise": "2.0.1",
"strip-bom": "2.0.0"
"graceful-fs": "^4.1.2",
"parse-json": "^2.2.0",
"pify": "^2.0.0",
"pinkie-promise": "^2.0.0",
"strip-bom": "^2.0.0"
}
},
"lodash._baseassign": {
@ -347,8 +347,8 @@
"integrity": "sha1-jDigmVAPIVrQnlnxci/QxSv+Ck4=",
"dev": true,
"requires": {
"lodash._basecopy": "3.0.1",
"lodash.keys": "3.1.2"
"lodash._basecopy": "^3.0.0",
"lodash.keys": "^3.0.0"
}
},
"lodash._basecopy": {
@ -381,9 +381,9 @@
"integrity": "sha1-1/KEnw29p+BGgruM1yqwIkYd6+c=",
"dev": true,
"requires": {
"lodash._baseassign": "3.2.0",
"lodash._basecreate": "3.0.3",
"lodash._isiterateecall": "3.0.9"
"lodash._baseassign": "^3.0.0",
"lodash._basecreate": "^3.0.0",
"lodash._isiterateecall": "^3.0.0"
}
},
"lodash.get": {
@ -410,9 +410,9 @@
"integrity": "sha1-TbwEcrFWvlCgsoaFXRvQsMZWCYo=",
"dev": true,
"requires": {
"lodash._getnative": "3.9.1",
"lodash.isarguments": "3.1.0",
"lodash.isarray": "3.0.4"
"lodash._getnative": "^3.0.0",
"lodash.isarguments": "^3.0.0",
"lodash.isarray": "^3.0.0"
}
},
"lolex": {
@ -426,7 +426,7 @@
"resolved": "http://r.npm.sankuai.com/minimatch/download/minimatch-3.0.3.tgz",
"integrity": "sha1-Kk5AkLlrLbBqnX3wEFWmKnfJt3Q=",
"requires": {
"brace-expansion": "1.1.7"
"brace-expansion": "^1.0.0"
}
},
"minimist": {
@ -484,11 +484,11 @@
"integrity": "sha1-qaOADjmUmUr55FIzPVSdYPcrjow=",
"dev": true,
"requires": {
"@sinonjs/formatio": "2.0.0",
"just-extend": "1.1.27",
"lolex": "2.7.1",
"path-to-regexp": "1.7.0",
"text-encoding": "0.6.4"
"@sinonjs/formatio": "^2.0.0",
"just-extend": "^1.1.27",
"lolex": "^2.3.2",
"path-to-regexp": "^1.7.0",
"text-encoding": "^0.6.4"
}
},
"normalize-package-data": {
@ -496,10 +496,10 @@
"resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.3.8.tgz",
"integrity": "sha1-2Bntoqne29H/pWPqQHHZNngilbs=",
"requires": {
"hosted-git-info": "2.4.2",
"is-builtin-module": "1.0.0",
"semver": "5.3.0",
"validate-npm-package-license": "3.0.1"
"hosted-git-info": "^2.1.4",
"is-builtin-module": "^1.0.0",
"semver": "2 || 3 || 4 || 5",
"validate-npm-package-license": "^3.0.1"
}
},
"number-is-nan": {
@ -512,7 +512,7 @@
"resolved": "http://r.npm.sankuai.com/once/download/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"requires": {
"wrappy": "1.0.2"
"wrappy": "1"
}
},
"os-locale": {
@ -520,7 +520,7 @@
"resolved": "http://r.npm.sankuai.com/os-locale/download/os-locale-1.4.0.tgz",
"integrity": "sha1-IPnxeuKe00XoveWDsT0gCYA8FNk=",
"requires": {
"lcid": "1.0.0"
"lcid": "^1.0.0"
}
},
"parse-json": {
@ -528,7 +528,7 @@
"resolved": "http://r.npm.sankuai.com/parse-json/download/parse-json-2.2.0.tgz",
"integrity": "sha1-9ID0BDTvgHQfhGkJn43qGPVaTck=",
"requires": {
"error-ex": "1.3.1"
"error-ex": "^1.2.0"
}
},
"path-exists": {
@ -536,7 +536,7 @@
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz",
"integrity": "sha1-D+tsZPD8UY2adU3V77YscCJ2H0s=",
"requires": {
"pinkie-promise": "2.0.1"
"pinkie-promise": "^2.0.0"
}
},
"path-is-absolute": {
@ -558,9 +558,9 @@
"resolved": "https://registry.npmjs.org/path-type/-/path-type-1.1.0.tgz",
"integrity": "sha1-WcRPfuSR2nBNpBXaWkBwuk+P5EE=",
"requires": {
"graceful-fs": "4.1.11",
"pify": "2.3.0",
"pinkie-promise": "2.0.1"
"graceful-fs": "^4.1.2",
"pify": "^2.0.0",
"pinkie-promise": "^2.0.0"
}
},
"pify": {
@ -578,7 +578,7 @@
"resolved": "http://r.npm.sankuai.com/pinkie-promise/download/pinkie-promise-2.0.1.tgz",
"integrity": "sha1-ITXW36ejWMBprJsXh3YogihFD/o=",
"requires": {
"pinkie": "2.0.4"
"pinkie": "^2.0.0"
}
},
"read-pkg": {
@ -586,9 +586,9 @@
"resolved": "http://r.npm.sankuai.com/read-pkg/download/read-pkg-1.1.0.tgz",
"integrity": "sha1-9f+qXs0pyzHAR0vKfXVra7KePyg=",
"requires": {
"load-json-file": "1.1.0",
"normalize-package-data": "2.3.8",
"path-type": "1.1.0"
"load-json-file": "^1.0.0",
"normalize-package-data": "^2.3.2",
"path-type": "^1.0.0"
}
},
"read-pkg-up": {
@ -596,8 +596,8 @@
"resolved": "http://r.npm.sankuai.com/read-pkg-up/download/read-pkg-up-1.0.1.tgz",
"integrity": "sha1-nWPBMnbAZZGNV/ACpX9AobZD+wI=",
"requires": {
"find-up": "1.1.2",
"read-pkg": "1.1.0"
"find-up": "^1.0.0",
"read-pkg": "^1.0.0"
}
},
"require-directory": {
@ -632,13 +632,13 @@
"integrity": "sha1-QnrjEqM308UWgEzidU6MDVAoywQ=",
"dev": true,
"requires": {
"@sinonjs/formatio": "2.0.0",
"diff": "3.5.0",
"lodash.get": "4.4.2",
"lolex": "2.7.1",
"nise": "1.4.2",
"supports-color": "5.4.0",
"type-detect": "4.0.8"
"@sinonjs/formatio": "^2.0.0",
"diff": "^3.1.0",
"lodash.get": "^4.4.2",
"lolex": "^2.2.0",
"nise": "^1.2.0",
"supports-color": "^5.1.0",
"type-detect": "^4.0.5"
},
"dependencies": {
"diff": {
@ -659,7 +659,7 @@
"integrity": "sha1-HGszdALCE3YF7+GfEP7DkPb6q1Q=",
"dev": true,
"requires": {
"has-flag": "3.0.0"
"has-flag": "^3.0.0"
}
},
"type-detect": {
@ -675,7 +675,7 @@
"resolved": "http://r.npm.sankuai.com/spdx-correct/download/spdx-correct-1.0.2.tgz",
"integrity": "sha1-SzBz2TP/UfORLwOsVRlJikFQ20A=",
"requires": {
"spdx-license-ids": "1.2.2"
"spdx-license-ids": "^1.0.2"
}
},
"spdx-expression-parse": {
@ -693,9 +693,9 @@
"resolved": "http://r.npm.sankuai.com/string-width/download/string-width-1.0.2.tgz",
"integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=",
"requires": {
"code-point-at": "1.1.0",
"is-fullwidth-code-point": "1.0.0",
"strip-ansi": "3.0.1"
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
"strip-ansi": "^3.0.0"
}
},
"strip-ansi": {
@ -703,7 +703,7 @@
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
"integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",
"requires": {
"ansi-regex": "2.1.1"
"ansi-regex": "^2.0.0"
}
},
"strip-bom": {
@ -711,7 +711,7 @@
"resolved": "http://r.npm.sankuai.com/strip-bom/download/strip-bom-2.0.0.tgz",
"integrity": "sha1-YhmoVhZSBJHzV4i9vxRHqZx+aw4=",
"requires": {
"is-utf8": "0.2.1"
"is-utf8": "^0.2.0"
}
},
"supports-color": {
@ -720,7 +720,7 @@
"integrity": "sha1-cqJiiU2dQIuVbKBf83su2KbiotU=",
"dev": true,
"requires": {
"has-flag": "1.0.0"
"has-flag": "^1.0.0"
}
},
"text-encoding": {
@ -751,8 +751,8 @@
"resolved": "http://r.npm.sankuai.com/validate-npm-package-license/download/validate-npm-package-license-3.0.1.tgz",
"integrity": "sha1-KAS6vnEq0zeUWaz74kdGqywwP7w=",
"requires": {
"spdx-correct": "1.0.2",
"spdx-expression-parse": "1.0.4"
"spdx-correct": "~1.0.0",
"spdx-expression-parse": "~1.0.0"
}
},
"which-module": {
@ -765,8 +765,8 @@
"resolved": "http://r.npm.sankuai.com/wrap-ansi/download/wrap-ansi-2.1.0.tgz",
"integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=",
"requires": {
"string-width": "1.0.2",
"strip-ansi": "3.0.1"
"string-width": "^1.0.1",
"strip-ansi": "^3.0.1"
}
},
"wrappy": {
@ -784,19 +784,19 @@
"resolved": "https://registry.npmjs.org/yargs/-/yargs-7.1.0.tgz",
"integrity": "sha1-a6MY6xaWFyf10oT46gA+jWFU0Mg=",
"requires": {
"camelcase": "3.0.0",
"cliui": "3.2.0",
"decamelize": "1.2.0",
"get-caller-file": "1.0.2",
"os-locale": "1.4.0",
"read-pkg-up": "1.0.1",
"require-directory": "2.1.1",
"require-main-filename": "1.0.1",
"set-blocking": "2.0.0",
"string-width": "1.0.2",
"which-module": "1.0.0",
"y18n": "3.2.1",
"yargs-parser": "5.0.0"
"camelcase": "^3.0.0",
"cliui": "^3.2.0",
"decamelize": "^1.1.1",
"get-caller-file": "^1.0.1",
"os-locale": "^1.4.0",
"read-pkg-up": "^1.0.1",
"require-directory": "^2.1.1",
"require-main-filename": "^1.0.1",
"set-blocking": "^2.0.0",
"string-width": "^1.0.2",
"which-module": "^1.0.0",
"y18n": "^3.2.1",
"yargs-parser": "^5.0.0"
}
},
"yargs-parser": {
@ -804,7 +804,7 @@
"resolved": "http://r.npm.sankuai.com/yargs-parser/download/yargs-parser-5.0.0.tgz",
"integrity": "sha1-J17PDX/+Bcd+ZOfIbkzZS/DhIoo=",
"requires": {
"camelcase": "3.0.0"
"camelcase": "^3.0.0"
}
}
}

View File

@ -1,7 +1,7 @@
{
"name": "thrift-ts",
"version": "1.1.5",
"description": "",
"description": "parse .thrift file to .d.ts",
"main": "src/index.js",
"scripts": {
"test": "tsc && mocha"

View File

@ -1,388 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
class BaseCompiler {
constructor(options) {
this.intends = 0;
this.intendMode = types_1.INTEND_MODE.TAB;
this.tabSize = 2;
this.buffer = [];
this.int64AsString = false;
if (options) {
if (typeof options.tabSize !== 'undefined') {
this.tabSize = options.tabSize;
}
if (typeof options.spaceAsTab !== 'undefined') {
this.intendMode = options.spaceAsTab ? types_1.INTEND_MODE.SPACE : types_1.INTEND_MODE.TAB;
}
if (typeof options.int64AsString !== 'undefined') {
this.int64AsString = options.int64AsString;
}
}
}
write(...chunks) {
chunks.forEach(chunk => {
this.buffer.push(chunk.toString());
});
}
wExport(next) {
this.wIntend();
this.write('export', types_1.SPACE);
return next();
}
wBlock(inline = false, next) {
if (!inline)
this.wIntend();
this.write('{');
next();
this.wIntend();
this.write('}');
if (!inline)
this.write('\n');
}
;
getThriftTypeName(t) {
switch (t) {
case 'int': return 'number';
case 'bool': return 'boolean';
case 'double':
case 'i32':
case 'i16':
return 'number';
case 'i64':
return 'Int64';
case 'string':
return 'string';
default:
return null;
}
}
getTypeName(str) {
const t = this.getThriftTypeName(str);
if (t === null)
return str;
return t;
}
wField(f) {
this.wIntend();
this.write(f.name);
if (f.option === 'optional')
this.write('?');
this.write(':', types_1.SPACE);
this.wValueType(f.type);
this.write(';', '\n');
}
wNewline() {
return () => {
this.write('\n');
this.write('\n');
};
}
wIntend() {
for (let j = 0; j < this.intends; j++) {
if (this.intendMode === types_1.INTEND_MODE.SPACE) {
for (let i = 0; i < this.tabSize; i++) {
this.write(types_1.SPACE);
}
}
else {
this.write(types_1.TAB);
}
}
}
wReturn() {
this.write('\n');
this.wIntend();
}
increaseIntend() {
this.write('\n');
this.intends += this.tabSize;
}
decreaseIntend(newline = true) {
if (newline) {
this.write('\n');
}
this.intends -= this.tabSize;
}
wBrackets(next) {
this.write('(');
next();
this.write(')');
}
wValueType(vt) {
if (Array.isArray(vt)) {
vt.forEach((v, i) => {
this.wValueType(v);
if (i !== vt.length - 1) {
this.write(types_1.SPACE, '|', types_1.SPACE);
}
});
return;
}
if (typeof vt === 'string') {
return this.write(this.getTypeName(vt));
}
if (vt.name === 'list') {
this.wValueType(vt.valueType);
this.write('[]');
}
if (vt.name === 'map') {
this.write('Map<');
this.wValueType(vt.keyType);
this.write(',');
this.wValueType(vt.valueType);
this.write('>');
}
if (vt.name === 'set') {
this.write('Set<');
this.wValueType(vt.valueType);
this.write('>');
}
}
wStructBody(ast, next) {
this.wBlock(false, () => {
this.increaseIntend();
ast.forEach(this.wField.bind(this));
next && next();
this.decreaseIntend(false);
});
}
wClass(name, ast) {
this.wIntend();
this.write('class', types_1.SPACE, name, types_1.SPACE);
this.wStructBody(ast, () => {
this.write('\n');
this.wIntend();
this.write('constructor');
this.wBrackets(() => {
this.write('arg?', ':', types_1.SPACE);
this.wBlock(true, () => {
this.increaseIntend();
ast.forEach(this.wField.bind(this));
this.decreaseIntend(false);
});
});
this.write('\n');
});
this.write('\n');
}
wException(name, ast) {
this.wIntend();
this.write('type', types_1.SPACE, name, types_1.SPACE, '=', types_1.SPACE);
this.wStructBody(ast);
this.write('\n');
}
writeStructs(structs) {
Object.keys(structs).forEach((k) => {
const s = structs[k];
this.wExport(() => this.wClass(String(k), s));
});
}
writeExceptions(exceptions) {
Object.keys(exceptions).forEach((k) => {
const e = exceptions[k];
this.wExport(() => this.wException(String(k), e));
});
}
writeInclude(includes) {
const getIncludePath = (path) => {
return './' + path.replace(/.*\//, '').replace(/.thrift$/, '') + '_types';
};
Object.keys(includes).forEach((k) => {
const include = includes[k];
this.write('import', types_1.SPACE, '*', types_1.SPACE, 'as', types_1.SPACE, String(k), types_1.SPACE, 'from', types_1.SPACE, `'`, getIncludePath(include.path), `';\n`);
});
}
wEnum(name, e) {
this.wIntend();
this.wExport(() => {
this.write('enum', types_1.SPACE, name, types_1.SPACE);
this.wBlock(false, () => {
this.increaseIntend();
e.items.forEach(item => {
this.wIntend();
this.write(item.name);
if (typeof item.value === 'string') {
this.write(types_1.SPACE, '=', types_1.SPACE, `'`, item.value, `'`);
}
else if (typeof item.value === 'number') {
this.write(types_1.SPACE, '=', types_1.SPACE, String(item.value));
}
else if (typeof item.value === 'boolean') {
this.write(types_1.SPACE, '=', types_1.SPACE, item.value ? 'true' : 'false');
}
this.write(',\n');
});
this.decreaseIntend(false);
});
});
this.write('\n');
}
writeEnum(enums) {
Object.keys(enums).forEach((k) => {
const e = enums[k];
this.wEnum(String(k), e);
});
}
wString(s) {
this.write(`'`, s, `'`);
}
wNumber(n) {
this.write(String(n));
}
wValue(v) {
if (typeof v === 'string') {
this.wString(v);
}
else if (typeof v === 'number') {
this.wNumber(v);
}
else if (Array.isArray(v)) {
this.write('[');
v.forEach((_v, i) => {
this.wValue(_v);
if (i !== v.length - 1) {
this.write(',', types_1.SPACE);
}
});
this.write(']');
}
else if (typeof v === 'object') {
this.wBlock(true, () => {
this.increaseIntend();
Object.keys(v).forEach((k) => {
this.wIntend();
this.write(String(k), ':', types_1.SPACE);
this.wValue(v[k]);
this.write(',\n');
});
this.decreaseIntend();
});
}
}
wConst(name, t) {
this.wIntend();
this.write('export', types_1.SPACE, 'const', types_1.SPACE, name);
if (typeof t.type === 'string') {
this.write(types_1.SPACE, '=', types_1.SPACE);
this.wValue(t.value);
}
else {
this.write(':', types_1.SPACE);
this.wValueType(t.type);
}
this.write(';\n');
}
;
writeConst(consts) {
Object.keys(consts).forEach((k) => {
const c = consts[k];
this.wConst(String(k), c);
});
}
writeTypeof(typedefs) {
Object.keys(typedefs).forEach((k) => {
const typedef = typedefs[k];
this.wIntend();
this.write('type', types_1.SPACE, String(k), types_1.SPACE, '=', types_1.SPACE);
this.wValueType(typedef.type);
this.write(';\n');
});
}
writeCallbackTypeDeclare() {
this.wIntend();
this.write('type Callback<T, E> = (err: E, resp: T) => void;\n\n');
}
writeCommonType() {
if (this.int64AsString) {
this.wIntend();
this.write('type', types_1.SPACE, 'Int64', types_1.SPACE, '=', types_1.SPACE, 'string;', '\n');
}
else {
this.wIntend();
this.write('interface Int64 {');
this.increaseIntend();
this.wIntend();
this.write('constructor(o?: number | string): this;', '\n');
this.wIntend();
this.write('toString(): string;', '\n');
this.wIntend();
this.write('toJson(): string;');
this.decreaseIntend();
this.wIntend();
this.write('}\n\n');
}
}
wPromise(type, err) {
this.write('Promise', '<');
this.wValueType(type);
if (err.length) {
this.write(',', types_1.SPACE);
this.write(err.map(e => e.type).join(' | '));
}
this.write('>');
}
wMethodArgs(args, callback) {
let left = callback ? args.length + 1 : args.length;
args.forEach((arg) => {
this.write(arg.name);
this.write(':', types_1.SPACE);
this.wValueType(arg.type);
if (--left !== 0) {
this.write(',', types_1.SPACE);
}
});
if (callback) {
this.write('callback', ':', types_1.SPACE, 'Callback', '<');
this.wValueType(callback.returnType);
this.write(',', types_1.SPACE);
if (callback.expections.length) {
let type = callback.expections.map(e => e.type);
type = type.concat('Error');
this.wValueType(type);
this.write('>');
}
else {
this.write('Error', '>');
}
}
}
wMethod(method) {
this.wIntend();
this.write(method.name);
this.wBrackets(() => {
const args = method.args;
this.wMethodArgs(args, {
returnType: method.type,
expections: method.throws,
});
});
this.write(':', types_1.SPACE, 'void');
this.write(';');
this.write('\n');
this.wIntend();
this.write(method.name);
this.wBrackets(() => {
this.wMethodArgs(method.args);
});
this.write(':', types_1.SPACE);
this.wPromise(method.type, method.throws);
this.write(';');
this.write('\n');
}
wService(service) {
this.wIntend();
this.write('class', types_1.SPACE, 'Client', types_1.SPACE);
this.wBlock(false, () => {
this.increaseIntend();
Object.values(service.functions).forEach((method, index, array) => {
this.wMethod(method);
if (index !== array.length - 1) {
this.write('\n');
}
});
this.decreaseIntend(false);
});
}
}
exports.default = BaseCompiler;

View File

@ -222,7 +222,7 @@ export default class BaseCompiler {
writeUnions(unions: Unions) {
Object.keys(unions).forEach((k: keyof typeof unions) => {
const s = unions[k];
this.wExport(() => this.wClass(k, s));
this.wExport(() => this.wClass(String(k), s));
});
}

View File

@ -1,62 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const thriftPraser = require("../lib/thriftParser");
const BaseCompiler_1 = require("./BaseCompiler");
const ServiceCompiler_1 = require("./ServiceCompiler");
class Compiler extends BaseCompiler_1.default {
constructor(file, options) {
super(options);
this.serviceCompilers = [];
this.filename = file.filename;
this.ast = thriftPraser(file.content);
if (this.ast.service) {
const services = this.ast.service;
const basename = path.basename(this.filename, '.thrift');
const include = Object.assign({}, this.ast.include, {
[basename]: {
path: basename,
},
});
this.serviceCompilers = Object.keys(services).map((k) => {
return new ServiceCompiler_1.default(String(k), services[k], include, options);
});
}
}
flush() {
this.writeCommonType();
if (this.ast.include) {
this.writeInclude(this.ast.include);
}
if (this.ast.const) {
this.writeConst(this.ast.const);
}
if (this.ast.typedef) {
this.writeTypeof(this.ast.typedef);
}
if (this.ast.enum) {
this.writeEnum(this.ast.enum);
}
if (this.ast.struct) {
this.writeStructs(this.ast.struct);
}
if (this.ast.exception) {
this.writeExceptions(this.ast.exception);
}
let files = [];
if (this.serviceCompilers.length) {
this.serviceCompilers.forEach(s => {
files.push(s.flush());
});
}
files.push({
filename: `${path.basename(this.filename, '.thrift')}_types.d.ts`,
content: this.buffer.join(''),
});
return files;
}
}
exports.default = (sourceFile, options) => {
const compiler = new Compiler(sourceFile, options);
return compiler.flush();
};

13
test/mock/dist/MyTestService.d.ts vendored Normal file
View File

@ -0,0 +1,13 @@
import * as test from './test_types';
type Callback<T, E> = (err: E, resp: T) => void;
interface Int64 {
constructor(o?: number | string): this;
toString(): string;
toJson(): string;
}
export class Client {
search(request: Request, callback: Callback<Response, Error>): void;
search(request: Request): Promise<Response>;
}

41
test/mock/dist/test_types.d.ts vendored Normal file
View File

@ -0,0 +1,41 @@
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;
})
}

View File

@ -10,7 +10,6 @@ describe('thriftTs', () => {
filename,
content,
}, {
tabSize: 2,
spaceAsTab: true,
}).forEach(newFile => {
const newFilename = Path.join(__dirname, './mock/dist', newFile.filename);