layer-api
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -35,2 +35,10 @@ 'use strict'; | ||
if (config.agent) { | ||
if (config.agent instanceof https.Agent) this.agent = config.agent; | ||
else throw new Error(utils.i18n.layerapi.agent); | ||
} | ||
else if (config.agentOptions) { | ||
this.agent = new https.Agent(config.agentOptions); | ||
} | ||
// expose request methods | ||
@@ -61,3 +69,3 @@ API.methods.forEach(function(method) { | ||
var req = https.request({ | ||
var options = { | ||
host: API.host, | ||
@@ -68,4 +76,8 @@ port: API.port, | ||
headers: headers | ||
}); | ||
} | ||
if (this.agent) options.agent = this.agent; | ||
var req = https.request(options); | ||
req.setTimeout(this.timeout, timeout.bind(this, req, callback)); | ||
@@ -76,6 +88,10 @@ req.on('error', error.bind(this, req, callback)); | ||
req.on('socket', function(socket) { | ||
socket.on(('secureConnect'), function() { | ||
var write = function() { | ||
if (params.body) req.write(JSON.stringify(params.body)); | ||
req.end(); | ||
}); | ||
} | ||
// reusing connection when using keepAlive agent | ||
if (socket.authorized) write(); | ||
// TODO: should also check if authorized here and throw if not | ||
else socket.on('secureConnect', function() { write(); }) | ||
}); | ||
@@ -96,5 +112,6 @@ } | ||
try { | ||
payload = payload ? JSON.parse(payload) : null; | ||
payload = JSON.parse(payload); | ||
} catch(e) { | ||
return callback(new APIError('Error parsing JSON payload', e, payload)); | ||
// We can safely ignore this. If not JSON it's probably a string. | ||
utils.debug('Error parsing JSON payload: ' + e); | ||
} | ||
@@ -101,0 +118,0 @@ |
@@ -62,3 +62,3 @@ 'use strict'; | ||
request.get({ | ||
path: '/users/' + querystring.escape(userId) + '/conversations' + (conversationUUID ? '/' + conversationId : '') | ||
path: '/users/' + querystring.escape(userId) + '/conversations' + (conversationUUID ? '/' + conversationUUID : '') | ||
}, callback); | ||
@@ -65,0 +65,0 @@ }, |
@@ -65,3 +65,4 @@ 'use strict'; | ||
token: 'You need to pass a valid `token` to constructor', | ||
appId: 'You need to pass a valid `appId` to constructor' | ||
appId: 'You need to pass a valid `appId` to constructor', | ||
agent: 'Constructor option `agent` needs to be an instance of https.Agent' | ||
}, | ||
@@ -68,0 +69,0 @@ conversations: { |
{ | ||
"name": "layer-api", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Node.js library, which provides a wrapper for the Layer Platform API", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -56,2 +56,4 @@ # Layer API for node.js | ||
- `timeout` - Request timeout in milliseconds (default: `10000` milliseconds) | ||
- `agent` - Instance of [https.Agent](https://nodejs.org/api/https.html#https_class_https_agent) to use HTTPS KeepAlive | ||
- `agentOptions` - Or use [Agent options](https://nodejs.org/api/http.html#http_new_agent_options) hash directly | ||
- `debug` - Enable debugging (default: `false`) | ||
@@ -385,4 +387,8 @@ | ||
## Changelog | ||
For a list of version changes please refer to [Github releases](https://github.com/layerhq/node-layer-api/releases) page. | ||
## Author | ||
[Nil Gradisnik](https://github.com/nilgradisnik) |
77319
1522
393