From e4fe62b62e5343e3979380ffe05583a356d65441 Mon Sep 17 00:00:00 2001 From: Kevin Greene <30637378+kevin-greene-ck@users.noreply.github.com> Date: Tue, 6 Feb 2018 10:23:43 -0800 Subject: [PATCH] fix(dynamic-config): use typescript compiler to load typescript configs (#48) * fix(dynamic-config): use typescript compiler to load typescript configs * Add more forgiving error handling for failed configs * cleanup * Handle requires in typescript configs * fix resolution of globals in typescript configs --- packages/consul-client/tslint.json | 28 ++-- packages/dynamic-config/package-lock.json | 89 ------------- packages/dynamic-config/package.json | 3 +- .../dynamic-config/src/main/ConfigLoader.ts | 2 + .../src/main/loaders/typescript.ts | 69 ++++++++-- .../src/tests/integration/config/foo.ts | 5 + .../tests/integration/config/production.ts | 16 ++- packages/dynamic-config/tslint.json | 40 +++--- packages/thrift-client/example/client.ts | 101 +++++++------- .../thrift-client/example/config/default.ts | 12 +- packages/thrift-client/example/index.ts | 24 ++-- packages/thrift-client/example/server.ts | 126 +++++++++--------- .../example/thrift/calculator.thrift | 6 +- packages/thrift-client/package.json | 4 +- .../src/main/connections/RequestConnection.ts | 2 + .../src/main/connections/index.ts | 6 +- packages/thrift-client/tslint.json | 20 +-- packages/thrift-server-core/tslint.json | 20 +-- packages/thrift-server-express/tslint.json | 30 ++--- packages/thrift-server-hapi/tslint.json | 20 +-- packages/vault-client/tslint.json | 40 +++--- 21 files changed, 317 insertions(+), 346 deletions(-) create mode 100644 packages/dynamic-config/src/tests/integration/config/foo.ts diff --git a/packages/consul-client/tslint.json b/packages/consul-client/tslint.json index 3a199cb3..8f434aa3 100644 --- a/packages/consul-client/tslint.json +++ b/packages/consul-client/tslint.json @@ -4,19 +4,19 @@ "max-classes-per-file": false, "object-literal-sort-keys": false, "no-console": false, - "no-var-requires": false, - "array-type": [ - true, - "generic" - ], - "semicolon": [ - true, - "never" - ], - "quotemark": [ - true, - "single", - "avoid-escape" - ] + "no-var-requires": false, + "array-type": [ + true, + "generic" + ], + "semicolon": [ + true, + "never" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ] } } diff --git a/packages/dynamic-config/package-lock.json b/packages/dynamic-config/package-lock.json index 51163bf5..dc701156 100644 --- a/packages/dynamic-config/package-lock.json +++ b/packages/dynamic-config/package-lock.json @@ -7,16 +7,6 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-8.0.51.tgz", "integrity": "sha512-El3+WJk2D/ppWNd2X05aiP5l2k4EwF7KwheknQZls+I26eSICoWRhRIJ56jGgw2dqNGQ5LtNajmBU2ajS28EvQ==" }, - "@types/strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@types/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-FKjsOVbC6B7bdSB5CuzyHCkK69I=" - }, - "@types/strip-json-comments": { - "version": "0.0.30", - "resolved": "https://registry.npmjs.org/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz", - "integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==" - }, "acorn": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.2.1.tgz", @@ -669,14 +659,6 @@ "resolved": "https://registry.npmjs.org/hoek/-/hoek-4.2.0.tgz", "integrity": "sha512-v0XCLxICi9nPfYrS9RL8HbYnXi9obYAeLbSP00BmnZwCK9+Ih9WOjoZ8YoHCoav2csqn4FOz4Orldsy2dmDwmQ==" }, - "homedir-polyfill": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/homedir-polyfill/-/homedir-polyfill-1.0.1.tgz", - "integrity": "sha1-TCu8inWJmP7r9e1oWA921GdotLw=", - "requires": { - "parse-passwd": "1.0.0" - } - }, "iconv-lite": { "version": "0.4.19", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz", @@ -902,11 +884,6 @@ "yallist": "2.1.2" } }, - "make-error": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.2.tgz", - "integrity": "sha512-l9ra35l5VWLF24y75Tg8XgfGLX0ueRhph118WKM6H5denx4bB5QF59+4UAm9oJ2qsPQZas/CQUDdtDdfvYHBdQ==" - }, "mimic-fn": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-1.1.0.tgz", @@ -1008,11 +985,6 @@ "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" }, - "parse-passwd": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/parse-passwd/-/parse-passwd-1.0.0.tgz", - "integrity": "sha1-bVuTSkVpk7I9N/QKOC1vFmao5cY=" - }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -1257,11 +1229,6 @@ } } }, - "strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=" - }, "strip-json-comments": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", @@ -1319,49 +1286,6 @@ "resolved": "https://registry.npmjs.org/tryit/-/tryit-1.0.3.tgz", "integrity": "sha1-OTvnMKlEb9Hq1tpZoBQwjzbCics=" }, - "ts-node": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-4.1.0.tgz", - "integrity": "sha512-xcZH12oVg9PShKhy3UHyDmuDLV3y7iKwX25aMVPt1SIXSuAfWkFiGPEkg+th8R4YKW/QCxDoW7lJdb15lx6QWg==", - "requires": { - "arrify": "1.0.1", - "chalk": "2.3.0", - "diff": "3.4.0", - "make-error": "1.3.2", - "minimist": "1.2.0", - "mkdirp": "0.5.1", - "source-map-support": "0.5.0", - "tsconfig": "7.0.0", - "v8flags": "3.0.1", - "yn": "2.0.0" - }, - "dependencies": { - "minimist": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", - "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" - }, - "source-map-support": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.0.tgz", - "integrity": "sha512-vUoN3I7fHQe0R/SJLKRdKYuEdRGogsviXFkHHo17AWaTGv17VLnxw+CFXvqy+y4ORZ3doWLQcxRYfwKrsd/H7Q==", - "requires": { - "source-map": "0.6.1" - } - } - } - }, - "tsconfig": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/tsconfig/-/tsconfig-7.0.0.tgz", - "integrity": "sha512-vZXmzPrL+EmC4T/4rVlT2jNVMWCi/O4DIiSj3UHg1OE5kCKbk4mfrXc6dZksLgRM/TZlKnousKH9bbTazUWRRw==", - "requires": { - "@types/strip-bom": "3.0.0", - "@types/strip-json-comments": "0.0.30", - "strip-bom": "3.0.0", - "strip-json-comments": "2.0.1" - } - }, "tslib": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.8.0.tgz", @@ -1436,14 +1360,6 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, - "v8flags": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/v8flags/-/v8flags-3.0.1.tgz", - "integrity": "sha1-3Oj8N5wX2fLJ6e142JzgAFKxt2s=", - "requires": { - "homedir-polyfill": "1.0.1" - } - }, "which": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz", @@ -1492,11 +1408,6 @@ "decamelize": "1.2.0", "window-size": "0.1.0" } - }, - "yn": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/yn/-/yn-2.0.0.tgz", - "integrity": "sha1-5a2ryKz0CPY4X8dklWhMiOavaJo=" } } } diff --git a/packages/dynamic-config/package.json b/packages/dynamic-config/package.json index 8c213125..e825ce5a 100644 --- a/packages/dynamic-config/package.json +++ b/packages/dynamic-config/package.json @@ -40,7 +40,6 @@ "lab": "^14.3.1", "rimraf": "^2.6.2", "tslint": "^5.8.0", - "typescript": "2.7.x", "wait-on": "^2.0.2" }, "dependencies": { @@ -48,6 +47,6 @@ "@creditkarma/vault-client": "^0.4.3", "@types/debug": "0.0.30", "debug": "^3.1.0", - "ts-node": "^4.1.0" + "typescript": "2.7.x" } } diff --git a/packages/dynamic-config/src/main/ConfigLoader.ts b/packages/dynamic-config/src/main/ConfigLoader.ts index 70b2c3a2..3d207130 100644 --- a/packages/dynamic-config/src/main/ConfigLoader.ts +++ b/packages/dynamic-config/src/main/ConfigLoader.ts @@ -50,8 +50,10 @@ function getConfigPath(sourceDir: string): string { async function loadFileWithName(loaders: Array, configPath: string, name: string): Promise { const configs: Array = await PromiseUtils.valuesForPromises(loaders.map((loader: IFileLoader) => { const filePath: string = path.resolve(configPath, `${name}.${loader.type}`) + return fileExists(filePath).then(() => { return loader.load(filePath) + }).catch((err: any) => { return {} }) diff --git a/packages/dynamic-config/src/main/loaders/typescript.ts b/packages/dynamic-config/src/main/loaders/typescript.ts index 4264489b..05809800 100644 --- a/packages/dynamic-config/src/main/loaders/typescript.ts +++ b/packages/dynamic-config/src/main/loaders/typescript.ts @@ -1,24 +1,65 @@ +import * as fs from 'fs' +import * as path from 'path' +import * as ts from 'typescript' +import * as vm from 'vm' +import * as logger from '../logger' import { IFileLoader } from '../types' -export const tsLoader: IFileLoader = { - type: 'ts', - async load(filePath: string): Promise { - require('ts-node').register({ - lazy: true, - cache: false, - typeCheck: true, - compilerOptions: { - allowJs: true, - rootDir: '.', +function locateFile(basePath: string, searchPath: string): string { + const resolvedPath: string = path.resolve(path.dirname(basePath), searchPath) + if (fs.existsSync(`${resolvedPath}.ts`)) { + return `${resolvedPath}.ts` + } else { + return `${resolvedPath}.js` + } +} + +function loadTypeScript(filePath: string): any { + try { + const contents: Buffer = fs.readFileSync(filePath) + const source: string = contents.toString() + const result: ts.TranspileOutput = ts.transpileModule(source, {}) + const sandbox = Object.assign({}, global, { + exports: {}, + require(pathToRequire: string) { + if (pathToRequire.startsWith('.') || pathToRequire.startsWith('/')) { + const resolvedFile: string = locateFile(filePath, pathToRequire) + + // If the file to include ends in `ts` use our custom machinery to load file + if (path.extname(resolvedFile) === '.ts') { + return loadTypeScript(resolvedFile) + + // Else use the default node system, resolving to absolute path to account for our + // shenanigans + } else { + return require(resolvedFile) + } + } else { + return require(pathToRequire) + } }, }) - const configObj = require(filePath) + vm.createContext(sandbox) - if (typeof configObj.default === 'object') { - return configObj.default + vm.runInContext(result.outputText, sandbox , { + displayErrors: true, + }) + + if ((sandbox.exports as any).default) { + return (sandbox.exports as any).default } else { - return configObj + return sandbox.exports } + } catch (err) { + logger.error(`Error parsing typescript config[${filePath}]: `, err) + return {} + } +} + +export const tsLoader: IFileLoader = { + type: 'ts', + async load(filePath: string): Promise { + return Promise.resolve(loadTypeScript(filePath)) }, } diff --git a/packages/dynamic-config/src/tests/integration/config/foo.ts b/packages/dynamic-config/src/tests/integration/config/foo.ts new file mode 100644 index 00000000..50dbdbd9 --- /dev/null +++ b/packages/dynamic-config/src/tests/integration/config/foo.ts @@ -0,0 +1,5 @@ +export const project = { + health: { + control: '/typescript', + }, + } diff --git a/packages/dynamic-config/src/tests/integration/config/production.ts b/packages/dynamic-config/src/tests/integration/config/production.ts index 68f0211b..a539f2d0 100644 --- a/packages/dynamic-config/src/tests/integration/config/production.ts +++ b/packages/dynamic-config/src/tests/integration/config/production.ts @@ -1,9 +1,17 @@ -export const project = { - health: { - control: '/typescript', - }, +import { IProjectConfig } from './default' +import { project } from './foo' + +const x: IProjectConfig = { + health: { + control: '', + response: '', + }, } +console.log('x: ', x) + +export { project } + export const database = { username: { _source: 'env', diff --git a/packages/dynamic-config/tslint.json b/packages/dynamic-config/tslint.json index ce855d7d..ea308830 100644 --- a/packages/dynamic-config/tslint.json +++ b/packages/dynamic-config/tslint.json @@ -5,24 +5,26 @@ "max-classes-per-file": false, "object-literal-sort-keys": false, "no-console": false, - "no-var-requires": false, - "variable-name": [true, - "check-format", - "allow-leading-underscore" - ], - "array-type": [ - true, - "generic" - ], - "semicolon": [ - true, - "never" - ], - "quotemark": [ - true, - "single", - "avoid-escape" - ], - "object-literal-key-quotes": false + "no-var-requires": false, + "only-arrow-functions": false, + "variable-name": [ + true, + "check-format", + "allow-leading-underscore" + ], + "array-type": [ + true, + "generic" + ], + "semicolon": [ + true, + "never" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ], + "object-literal-key-quotes": false } } diff --git a/packages/thrift-client/example/client.ts b/packages/thrift-client/example/client.ts index 9b968e5d..b411c08a 100644 --- a/packages/thrift-client/example/client.ts +++ b/packages/thrift-client/example/client.ts @@ -1,7 +1,7 @@ import { config } from '@creditkarma/dynamic-config' import { - createClient, + createClient, } from '../src/main/' import * as path from 'path' @@ -9,66 +9,67 @@ import { CoreOptions } from 'request' import * as express from 'express' import { - Work, - Operation, - Calculator, + Work, + Operation, + Calculator, } from './generated/calculator/calculator' (async function() { - const SERVER_CONFIG = await config().get('server') - const CLIENT_CONFIG = await config().get('client') + const SERVER_CONFIG = await config().get('server') + const CLIENT_CONFIG = await config().get('client') - // Get express instance - const app = express() + // Get express instance + const app = express() - // Create thrift client - const thriftClient: Calculator.Client = createClient(Calculator.Client, { - hostName: SERVER_CONFIG.host, - port: SERVER_CONFIG.port - }) + // Create thrift client + const thriftClient: Calculator.Client = createClient(Calculator.Client, { + hostName: SERVER_CONFIG.host, + port: SERVER_CONFIG.port + }) - function symbolToOperation(sym: string): Operation { - switch (sym) { - case 'add': - return Operation.ADD - case 'subtract': - return Operation.SUBTRACT - case 'multiply': - return Operation.MULTIPLY - case 'divide': - return Operation.DIVIDE - default: - throw new Error(`Unrecognized operation: ${sym}`) + function symbolToOperation(sym: string): Operation { + switch (sym) { + case 'add': + return Operation.ADD + case 'subtract': + return Operation.SUBTRACT + case 'multiply': + return Operation.MULTIPLY + case 'divide': + return Operation.DIVIDE + default: + throw new Error(`Unrecognized operation: ${sym}`) + } } - } - - app.get('/', (req: express.Request, res: express.Response): void => { - res.sendFile(path.join(__dirname, './index.html')) - }) - app.get('/ping', (req: express.Request, res: express.Response): void => { - thriftClient.ping().then(() => { - res.send('success') - }, (err: any) => { - console.log('err: ', err) - res.status(500).send(err) + app.get('/', (req: express.Request, res: express.Response): void => { + res.sendFile(path.join(__dirname, './index.html')) }) - }) - app.get('/calculate', (req: express.Request, res: express.Response): void => { - const work: Work = new Work({ - num1: req.query.left, - num2: req.query.right, - op: symbolToOperation(req.query.op) + app.get('/ping', (req: express.Request, res: express.Response): void => { + thriftClient.ping().then(() => { + res.send('success') + }, (err: any) => { + console.log('err: ', err) + res.status(500).send(err) + }) }) - thriftClient.calculate(1, work).then((val: number) => { - res.send(`result: ${val}`) - }, (err: any) => { - res.status(500).send(err) + + app.get('/calculate', (req: express.Request, res: express.Response): void => { + const work: Work = new Work({ + num1: req.query.left, + num2: req.query.right, + op: symbolToOperation(req.query.op) + }) + + thriftClient.calculate(1, work).then((val: number) => { + res.send(`result: ${val}`) + }, (err: any) => { + res.status(500).send(err) + }) }) - }) - app.listen(CLIENT_CONFIG.port, () => { - console.log(`Web server listening at http://${CLIENT_CONFIG.host}:${CLIENT_CONFIG.port}`) - }) + app.listen(CLIENT_CONFIG.port, () => { + console.log(`Web server listening at http://${CLIENT_CONFIG.host}:${CLIENT_CONFIG.port}`) + }) }()) diff --git a/packages/thrift-client/example/config/default.ts b/packages/thrift-client/example/config/default.ts index c3c67d18..337b6dc8 100644 --- a/packages/thrift-client/example/config/default.ts +++ b/packages/thrift-client/example/config/default.ts @@ -1,14 +1,14 @@ export interface IConnectionConfig { - host: string - port: number + host: string + port: number } export const server: IConnectionConfig = { - host: 'localhost', - port: 8045 + host: 'localhost', + port: 8045 } export const client: IConnectionConfig = { - host: 'localhost', - port: 8080 + host: 'localhost', + port: 8080 } diff --git a/packages/thrift-client/example/index.ts b/packages/thrift-client/example/index.ts index 4fb1e2d0..3e4c1057 100644 --- a/packages/thrift-client/example/index.ts +++ b/packages/thrift-client/example/index.ts @@ -4,25 +4,25 @@ import { generate } from '@creditkarma/thrift-typescript' process.chdir(__dirname) generate({ - rootDir: '.', - outDir: 'generated', - sourceDir: 'thrift', - target: 'thrift-server', - files: [ - './calculator.thrift' - ] + rootDir: '.', + outDir: 'generated', + sourceDir: 'thrift', + target: 'thrift-server', + files: [ + './calculator.thrift' + ] }) const clientProc = fork('./client.ts') const serverProc = fork('./server.ts') function exit(code: number) { - clientProc.kill() - serverProc.kill() - process.exitCode = code + clientProc.kill() + serverProc.kill() + process.exitCode = code } process.on('SIGINT', () => { - console.log('Caught interrupt signal') - exit(0); + console.log('Caught interrupt signal') + exit(0) }) diff --git a/packages/thrift-client/example/server.ts b/packages/thrift-client/example/server.ts index 6a286ffd..49e256b7 100644 --- a/packages/thrift-client/example/server.ts +++ b/packages/thrift-client/example/server.ts @@ -6,75 +6,75 @@ import { Operation, Calculator, Work } from './generated/calculator/calculator' import { SharedStruct, SharedUnion } from './generated/shared/shared' async function createServer(): Promise { - const SERVER_CONFIG = await config().get('server') - const server: Hapi.Server = new Hapi.Server(); - server.connection(SERVER_CONFIG); + const SERVER_CONFIG = await config().get('server') + const server: Hapi.Server = new Hapi.Server(); + server.connection(SERVER_CONFIG); - server.register(createPlugin(), (err: any) => { - if (err) { - throw err; - } - }); - - const impl = new Calculator.Processor({ - ping(): void {}, - add(a: number, b: number): number { - return a + b; - }, - calculate(logId: number, work: Work): number { - switch (work.op) { - case Operation.ADD: - return work.num1 + work.num2; - case Operation.SUBTRACT: - return work.num1 - work.num2; - case Operation.DIVIDE: - return work.num1 / work.num2; - case Operation.MULTIPLY: - return work.num1 * work.num2; - } - }, - zip(): void {}, - getStruct(): SharedStruct { - return new SharedStruct({ - key: 0, - value: 'test', - }) - }, - getUnion(index: number): SharedUnion { - if (index === 1) { - return SharedUnion.fromOption1('foo') - } else { - return SharedUnion.fromOption2('bar') + server.register(createPlugin(), (err: any) => { + if (err) { + throw err; } - }, - }) + }); + + const impl = new Calculator.Processor({ + ping(): void {}, + add(a: number, b: number): number { + return a + b; + }, + calculate(logId: number, work: Work): number { + switch (work.op) { + case Operation.ADD: + return work.num1 + work.num2; + case Operation.SUBTRACT: + return work.num1 - work.num2; + case Operation.DIVIDE: + return work.num1 / work.num2; + case Operation.MULTIPLY: + return work.num1 * work.num2; + } + }, + zip(): void {}, + getStruct(): SharedStruct { + return new SharedStruct({ + key: 0, + value: 'test', + }) + }, + getUnion(index: number): SharedUnion { + if (index === 1) { + return SharedUnion.fromOption1('foo') + } else { + return SharedUnion.fromOption2('bar') + } + }, + }) - server.route({ - method: 'POST', - path: '/', - handler: { - thrift: { - service: impl, - }, - }, - config: { - payload: { - parse: false, - }, - }, - }) + server.route({ + method: 'POST', + path: '/', + handler: { + thrift: { + service: impl, + }, + }, + config: { + payload: { + parse: false, + }, + }, + }) - return server + return server } createServer().then((server: Hapi.Server) => { - server.start((err: any) => { - if (err) { - throw err - } + server.start((err: any) => { + if (err) { + throw err + } - if (server.info !== null) { - console.log(`Thrift server running at: ${server.info.uri}`) - } - }) + if (server.info !== null) { + console.log(`Thrift server running at: ${server.info.uri}`) + } + }) }) diff --git a/packages/thrift-client/example/thrift/calculator.thrift b/packages/thrift-client/example/thrift/calculator.thrift index df25e6da..a23f6571 100644 --- a/packages/thrift-client/example/thrift/calculator.thrift +++ b/packages/thrift-client/example/thrift/calculator.thrift @@ -105,9 +105,9 @@ enum Operation { * manual management in some languages. */ struct Work { - 1: i32 num1 = 0, - 2: i32 num2, - 3: Operation op, + 1: required i32 num1 = 0, + 2: required i32 num2, + 3: required Operation op, 4: optional string comment, } diff --git a/packages/thrift-client/package.json b/packages/thrift-client/package.json index ff0541c4..d07203f3 100644 --- a/packages/thrift-client/package.json +++ b/packages/thrift-client/package.json @@ -37,6 +37,7 @@ "url": "https://github.com/creditkarma/thrift-server/tree/master/packages/thrift-client" }, "peerDependencies": { + "@creditkarma/dynamic-config": "^0.4.3", "@creditkarma/thrift-server-core": "^0.4.3", "@types/request": "^2.47.0", "request": "^2.83.0" @@ -59,7 +60,6 @@ "lab": "^14.3.1", "request": "^2.83.0", "ts-node": "^3.3.0", - "tslint": "^5.7.0", - "typescript": "2.7.x" + "tslint": "^5.7.0" } } diff --git a/packages/thrift-client/src/main/connections/RequestConnection.ts b/packages/thrift-client/src/main/connections/RequestConnection.ts index fc3fc127..7a914340 100644 --- a/packages/thrift-client/src/main/connections/RequestConnection.ts +++ b/packages/thrift-client/src/main/connections/RequestConnection.ts @@ -53,8 +53,10 @@ export class RequestConnection extends HttpConnection { this.request.post(requestOptions, (err: any, response: RequestResponse, body: Buffer) => { if (err !== null) { reject(err) + } else if (response.statusCode && (response.statusCode < 200 || response.statusCode > 299)) { reject(response) + } else { resolve(body) } diff --git a/packages/thrift-client/src/main/connections/index.ts b/packages/thrift-client/src/main/connections/index.ts index eff34e96..952973b4 100644 --- a/packages/thrift-client/src/main/connections/index.ts +++ b/packages/thrift-client/src/main/connections/index.ts @@ -1,6 +1,3 @@ -export * from './HttpConnection' -export * from './RequestConnection' - import * as request from 'request' import { @@ -13,6 +10,9 @@ import { ICreateClientOptions, } from '../types' +export * from './HttpConnection' +export * from './RequestConnection' + export function createClient( ServiceClient: IClientConstructor, options: ICreateClientOptions, diff --git a/packages/thrift-client/tslint.json b/packages/thrift-client/tslint.json index 6a152d09..38e7c8b4 100644 --- a/packages/thrift-client/tslint.json +++ b/packages/thrift-client/tslint.json @@ -3,15 +3,15 @@ "rules": { "object-literal-sort-keys": false, "no-console": false, - "no-var-requires": false, - "semicolon": [ - true, - "never" - ], - "quotemark": [ - true, - "single", - "avoid-escape" - ] + "no-var-requires": false, + "semicolon": [ + true, + "never" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ] } } diff --git a/packages/thrift-server-core/tslint.json b/packages/thrift-server-core/tslint.json index 4a82db22..5358515c 100644 --- a/packages/thrift-server-core/tslint.json +++ b/packages/thrift-server-core/tslint.json @@ -5,15 +5,15 @@ "no-bitwise": false, "object-literal-sort-keys": false, "no-console": false, - "no-var-requires": false, - "semicolon": [ - true, - "never" - ], - "quotemark": [ - true, - "single", - "avoid-escape" - ] + "no-var-requires": false, + "semicolon": [ + true, + "never" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ] } } diff --git a/packages/thrift-server-express/tslint.json b/packages/thrift-server-express/tslint.json index 6a152d09..87994b24 100644 --- a/packages/thrift-server-express/tslint.json +++ b/packages/thrift-server-express/tslint.json @@ -1,17 +1,17 @@ { - "extends": "tslint:recommended", - "rules": { - "object-literal-sort-keys": false, - "no-console": false, - "no-var-requires": false, - "semicolon": [ - true, - "never" - ], - "quotemark": [ - true, - "single", - "avoid-escape" - ] - } + "extends": "tslint:recommended", + "rules": { + "object-literal-sort-keys": false, + "no-console": false, + "no-var-requires": false, + "semicolon": [ + true, + "never" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ] + } } diff --git a/packages/thrift-server-hapi/tslint.json b/packages/thrift-server-hapi/tslint.json index 6a152d09..38e7c8b4 100644 --- a/packages/thrift-server-hapi/tslint.json +++ b/packages/thrift-server-hapi/tslint.json @@ -3,15 +3,15 @@ "rules": { "object-literal-sort-keys": false, "no-console": false, - "no-var-requires": false, - "semicolon": [ - true, - "never" - ], - "quotemark": [ - true, - "single", - "avoid-escape" - ] + "no-var-requires": false, + "semicolon": [ + true, + "never" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ] } } diff --git a/packages/vault-client/tslint.json b/packages/vault-client/tslint.json index 3a199cb3..3dafdbde 100644 --- a/packages/vault-client/tslint.json +++ b/packages/vault-client/tslint.json @@ -1,22 +1,22 @@ { - "extends": "tslint:recommended", - "rules": { - "max-classes-per-file": false, - "object-literal-sort-keys": false, - "no-console": false, - "no-var-requires": false, - "array-type": [ - true, - "generic" - ], - "semicolon": [ - true, - "never" - ], - "quotemark": [ - true, - "single", - "avoid-escape" - ] - } + "extends": "tslint:recommended", + "rules": { + "max-classes-per-file": false, + "object-literal-sort-keys": false, + "no-console": false, + "no-var-requires": false, + "array-type": [ + true, + "generic" + ], + "semicolon": [ + true, + "never" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ] + } }