diff --git a/lib/clients/authenticated.js b/lib/clients/authenticated.js index a54c2f56..7de58847 100644 --- a/lib/clients/authenticated.js +++ b/lib/clients/authenticated.js @@ -3,8 +3,8 @@ const { signRequest } = require('../../lib/request_signer'); const PublicClient = require('./public.js'); class AuthenticatedClient extends PublicClient { - constructor(key, secret, passphrase, apiURI) { - super(apiURI); + constructor(key, secret, passphrase, apiURI, options = {}) { + super(apiURI, options); this.key = key; this.secret = secret; this.passphrase = passphrase; diff --git a/lib/clients/public.js b/lib/clients/public.js index 1a0ed448..cb75dadd 100644 --- a/lib/clients/public.js +++ b/lib/clients/public.js @@ -1,8 +1,9 @@ const request = require('request'); const { Readable } = require('stream'); +const DEFAULT_TIMEOUT = 10 * 1000; // 10 sec class PublicClient { - constructor(apiURI = 'https://api.gdax.com') { + constructor(apiURI = 'https://api.gdax.com', options = {}) { this.productID = 'BTC-USD'; if (apiURI && !apiURI.startsWith('http')) { process.emitWarning( @@ -15,6 +16,7 @@ class PublicClient { this.apiURI = apiURI; this.API_LIMIT = 100; + this.timeout = +options.timeout > 0 ? options.timeout : DEFAULT_TIMEOUT; } get(...args) { @@ -86,6 +88,7 @@ class PublicClient { method: method.toUpperCase(), uri: this.makeAbsoluteURI(this.makeRelativeURI(uriParts)), qsStringifyOptions: { arrayFormat: 'repeat' }, + timeout: this.timeout, }); this.addHeaders(opts); const p = new Promise((resolve, reject) => { diff --git a/package.json b/package.json index e0a1b426..d90aa5f0 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "lint-staged": "^3.6.1", "mocha": "3.4.2", "nock": "9.0.13", - "prettier": "^1.4.4" + "prettier": "^1.9.2" }, "directories": { "lib": "./lib" diff --git a/tests/public_client.spec.js b/tests/public_client.spec.js index fe390e01..beb60c73 100644 --- a/tests/public_client.spec.js +++ b/tests/public_client.spec.js @@ -14,6 +14,7 @@ suite('PublicClient', () => { assert.equal(client.apiURI, EXCHANGE_API_URL); assert.equal(client.API_LIMIT, 100); assert.equal(client.productID, 'BTC-USD'); // deprecated + assert.equal(client.timeout, 10000); client = new Gdax.PublicClient('https://api-public.sandbox.gdax.com'); assert.equal(client.apiURI, 'https://api-public.sandbox.gdax.com');