Comparing version 0.1.1 to 0.1.2
@@ -1,18 +0,32 @@ | ||
module.exports = { | ||
endpoints: { | ||
data: { | ||
protocol: 'https', | ||
host: 'data.gosquared.com', | ||
port: 443, | ||
method: 'GET' | ||
}, | ||
var URL = require('url'), | ||
h = require('./lib/helpers'); | ||
api: { | ||
protocol: 'https', | ||
host: 'api.gosquared.com', | ||
port: 443, | ||
method: 'GET' | ||
} | ||
var endpoints = { | ||
data: { | ||
protocol: 'https:', | ||
hostname: 'data.gosquared.com', | ||
port: 443, | ||
method: 'POST' | ||
}, | ||
api: { | ||
protocol: 'https:', | ||
hostname: 'api.gosquared.com', | ||
port: 443, | ||
method: 'GET' | ||
} | ||
}; | ||
Object.keys(endpoints).forEach(function(l){ | ||
var url; | ||
if((url = process.env[l+'Endpoint'])){ | ||
var pUrl = URL.parse(url); | ||
if(!pUrl) return; | ||
h.extend(endpoints[l], pUrl); | ||
} | ||
}); | ||
module.exports = { | ||
endpoints: endpoints, | ||
gsEvent: { | ||
@@ -19,0 +33,0 @@ route: '/event' |
@@ -1,2 +0,3 @@ | ||
var https = require('https'), | ||
var http = require('http'), | ||
https = require('https'), | ||
config = require('../config'), | ||
@@ -22,3 +23,6 @@ h = require('./helpers'), | ||
missingEventName: 'The event must have a name. No name was given.', | ||
missingSiteToken: 'A site token must be given.' | ||
missingSiteToken: 'A site token must be given.', | ||
paramsTooLarge: 'The event parameters were too large. Send fewer / smaller parameters.', | ||
non200Code: 'The HTTP request completed with a non-200 status code.', | ||
errorEncountered: 'The GoSquared server did\'t like something, so it gave us an error.' | ||
}; | ||
@@ -34,3 +38,6 @@ | ||
6: messages.missingSiteToken, | ||
7: messages.requestFailed | ||
7: messages.requestFailed, | ||
8: messages.paramsTooLarge, | ||
9: messages.non200Code, | ||
10: messages.errorEncountered | ||
}; | ||
@@ -41,2 +48,6 @@ | ||
var GoSquared = module.exports = function(opts){ | ||
if(!(this instanceof GoSquared)){ | ||
return new GoSquared(opts); | ||
} | ||
if(!opts.siteToken){ | ||
@@ -48,3 +59,3 @@ return this._debug(6); | ||
version: 'latest', | ||
debugLevel: 'FATAL' | ||
debugLevel: 'WARNING' | ||
}, opts); | ||
@@ -56,5 +67,5 @@ this.config = config; | ||
var self = this; | ||
var requestPath = path + '?' + queryString.stringify(params); | ||
var requestPath = path; | ||
var requestOpts = { | ||
host: endpoint.host, | ||
host: endpoint.hostname, | ||
port: endpoint.port, | ||
@@ -64,6 +75,20 @@ method: endpoint.method, | ||
}; | ||
var haveParams = !!Object.keys(params).length; | ||
var d = '', dLength = 0; | ||
this._debug(0, 'TRACE', requestOpts); | ||
if(requestOpts.method == 'POST' && haveParams){ | ||
d = JSON.stringify(params); | ||
dLength = d.length; | ||
requestOpts.headers = { | ||
'Content-Type': 'application/json' | ||
// No need to specify Content-Length because this is raw HTTP body | ||
}; | ||
} | ||
var request = https.request(requestOpts, function(res){ | ||
this._debug(0, 'TRACE', {requestOpts: requestOpts, bodySize: dLength}); | ||
var request = (endpoint.protocol == 'https:' ? https : http).request(requestOpts, function(res){ | ||
if(res.statusCode != 200){ | ||
self._debug(9, 'WARNING', 'The status code received was ' + res.statusCode); | ||
} | ||
h.bufferResponse(res, cb); | ||
@@ -73,6 +98,9 @@ }); | ||
request.on('error', function(e){ | ||
self._debug(1, 'WARNING', e); | ||
return cb(1); | ||
self._debug(1, 'WARNING', {error: e, bodySize: dLength}); | ||
return cb(self._makeError(1)); | ||
}); | ||
if(haveParams){ | ||
request.write(d); | ||
} | ||
request.end(); | ||
@@ -98,3 +126,3 @@ | ||
err = 3; | ||
this._debug(err, 'WARNING', e); | ||
this._debug(err, 'WARNING', {error: e, responseData: responseData}); | ||
return err; | ||
@@ -104,5 +132,18 @@ } | ||
err = 4; | ||
this._debug(err, 'WARNING'); | ||
this._debug(err, 'WARNING', {responseData: responseData}); | ||
return err; | ||
} | ||
if(!parsed.success && parsed.error){ | ||
var errObj = parsed.error; | ||
switch(errObj.code){ | ||
case 1011: | ||
err = 8; | ||
this._debug(err, 'WARNING', errObj); | ||
return err; | ||
default: | ||
err = 10; | ||
this._debug(err, 'WARNING', errObj); | ||
return err; | ||
} | ||
} | ||
return parsed; | ||
@@ -124,12 +165,23 @@ }; | ||
GoSquared.prototype._makeError = function(code){ | ||
var err = new Error(errors[code]); | ||
err.code = code; | ||
return err; | ||
}; | ||
GoSquared.prototype.storeEvent = function(name, params, done){ | ||
if(typeof params == 'function'){ | ||
done = params, params = {}; | ||
} | ||
else if(typeof params != 'object') params = {}; | ||
if(!name){ | ||
return this._debug(5, 'WARNING'); | ||
this._debug(5, 'WARNING'); | ||
return done(this._makeError(5)); | ||
} | ||
if(typeof params == 'function') done = params, params = {}; | ||
params._name = name; | ||
params.a = this.opts.siteToken; | ||
this._exec(this.config.endpoints.data, '/event', params, this._responseCompleted.bind(this, done)); | ||
var qsParams = {}; | ||
qsParams.name = name; | ||
qsParams.site_token = this.opts.siteToken; | ||
this._exec(this.config.endpoints.data, '/event?' + queryString.stringify(qsParams), params, this._responseCompleted.bind(this, done)); | ||
}; | ||
@@ -144,3 +196,6 @@ | ||
if(typeof validated != "object"){ | ||
return cb(validated); | ||
if(typeof validated == "number"){ | ||
err = this._makeError(validated); | ||
} | ||
return cb(err); | ||
} | ||
@@ -163,2 +218,2 @@ | ||
})(func); | ||
} | ||
} |
@@ -10,2 +10,10 @@ var should = require('should'); | ||
done(); | ||
}; | ||
module.exports.testError = function(done, err, res){ | ||
should.exist(err); | ||
err.should.be.a('object'); | ||
err.should.have.property('code'); | ||
err.should.have.property('message'); | ||
done(); | ||
}; |
{ | ||
"name": "gosquared", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "GoSquared for your Node.JS application", | ||
@@ -21,2 +21,7 @@ "main": "lib/gosquared.js", | ||
"url": "http://simontabor.com" | ||
}, | ||
{ | ||
"name": "JT", | ||
"email": "jt@gosquared.com", | ||
"url": "http://floopily.com" | ||
} | ||
@@ -23,0 +28,0 @@ ], |
# node-gosquared | ||
This node module works with the [GoSquared API](api-docs), making it really easy to integrate GoSquared with your node app. | ||
This node module works with the [GoSquared API][api-docs], making it really easy to integrate GoSquared with your node app. | ||
@@ -20,6 +20,6 @@ It can be used as an easy proxy for frontend JavaScript so you don't publically expose your API Key. | ||
* api_key: API key from your [account](casa). Required for API functions, not required for tracking functions. | ||
* api_key: API key from your [account][casa]. Required for API functions, not required for tracking functions. | ||
* site_token: Token for the registered site you're working with. Required. | ||
* requestTimeout: Maximum time in ms an API request can be pending. Default 2000ms | ||
* debugLevel: One of 'TRACE', 'NOTICE', 'WARNING', 'ALL'. Default 'ALL' | ||
* debugLevel: One of 'TRACE', 'NOTICE', 'WARNING', 'ALL'. Default 'WARNING' | ||
@@ -41,3 +41,3 @@ ### API | ||
All functions listed in the [API documentation](api-docs) are methods you can call on the ```gosquared``` object. | ||
All functions listed in the [API documentation][api-docs] are methods you can call on the ```gosquared``` object. | ||
@@ -51,3 +51,13 @@ | ||
```javascript | ||
gosquared.storeEvent('Test Event', {its: true, 'you can': 'store', any: 'event', properties: 'You Like' }); | ||
gosquared.storeEvent('Test Event', { | ||
its: true, | ||
'you can': 'store', | ||
any: 'event', | ||
properties: 'You Like' | ||
}, | ||
function(e, res){ | ||
if(e) return console.log(e); | ||
console.log(res); | ||
} | ||
); | ||
``` | ||
@@ -54,0 +64,0 @@ |
@@ -20,5 +20,31 @@ var GoSquared = require('../lib/gosquared'), | ||
describe('Events', function(){ | ||
it('can be stored using storeEvent', function(done){ | ||
it('cannot be stored without a name', function(done){ | ||
GS.storeEvent(null, th.testError.bind(this, done)); | ||
}); | ||
it('can be stored without parameters', function(done){ | ||
GS.storeEvent('Test Event', th.testResponse.bind(this, done)); | ||
}); | ||
it('can be stored with parameters', function(done){ | ||
var params = { | ||
some: 'test', | ||
parameters: 'which', | ||
are: 'really', | ||
cool: true | ||
}; | ||
GS.storeEvent('Test Event', params, th.testResponse.bind(this, done)); | ||
}); | ||
it('errors if trying to store a mahoosive parameters object', function(done){ | ||
var params = {}; | ||
var gen = function(o, max){ | ||
var oSize = Object.keys(o).length; | ||
if(oSize > max) return o; | ||
o[oSize] = new Array(~~(Math.random()*100)).join('-'); | ||
return gen(o, max); | ||
}; | ||
gen(params, 25); | ||
GS.storeEvent('Test Event', params, th.testError.bind(this, done)); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13299
336
77
9
2