Comparing version
@@ -6,5 +6,3 @@ var URL = require('url'); | ||
data: { | ||
protocol: 'https:', | ||
hostname: 'data.gosquared.com', | ||
port: 443, | ||
url: 'https://data.gosquared.com', | ||
method: 'POST' | ||
@@ -14,5 +12,3 @@ }, | ||
api: { | ||
protocol: 'https:', | ||
hostname: 'api.gosquared.com', | ||
port: 443, | ||
url: 'https://api.gosquared.com', | ||
method: 'GET' | ||
@@ -19,0 +15,0 @@ } |
@@ -1,3 +0,2 @@ | ||
var http = require('http'); | ||
var https = require('https'); | ||
var request = require('request'); | ||
var config = require('../config'); | ||
@@ -9,2 +8,3 @@ var h = require('./helpers'); | ||
var Transaction = require('./Transaction'); | ||
var Person = require('./Person'); | ||
@@ -18,2 +18,3 @@ var debugLevels = { | ||
}; | ||
debugLevels.ALL = Math.pow(2, Object.keys(debugLevels).length) -1; | ||
@@ -49,3 +50,2 @@ var messages = { | ||
debugLevels.ALL = Math.pow(2, Object.keys(debugLevels).length) -1; | ||
@@ -55,4 +55,4 @@ var GoSquared = module.exports = function(opts){ | ||
this.opts = h.extend({ | ||
requestTimeout: 2000, | ||
debugLevel: 'WARNING' | ||
requestTimeout: 10000, | ||
debugLevel: 'NONE' | ||
}, opts); | ||
@@ -69,47 +69,25 @@ | ||
GoSquared.prototype._exec = function(endpoint, path, params, cb){ | ||
GoSquared.prototype._exec = function(endpoint, path, params, data, cb){ | ||
var self = this; | ||
var requestPath = path; | ||
var requestOpts = { | ||
host: endpoint.hostname, | ||
port: endpoint.port, | ||
method: endpoint.method, | ||
path: requestPath + '?' + queryString.stringify(params) | ||
}; | ||
var haveParams = !!Object.keys(params).length; | ||
var d = '', dLength = 0; | ||
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 | ||
}; | ||
if (!cb && typeof data === 'function') { | ||
cb = data; | ||
data = {}; | ||
} | ||
this._debug(0, 'TRACE', {requestOpts: requestOpts, bodySize: dLength, body: d}); | ||
var req = { | ||
url: endpoint.url + path, | ||
qs: params, | ||
method: endpoint.method, | ||
json: true, | ||
data: data, | ||
timeout: this.opts.requestTimeout | ||
}; | ||
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); | ||
} | ||
clearTimeout(abortTimeout); | ||
h.bufferResponse(res, cb); | ||
}); | ||
this._debug(0, 'TRACE', req); | ||
request.on('error', function(e){ | ||
self._debug(1, 'WARNING', {error: e, bodySize: dLength}); | ||
clearTimeout(abortTimeout); | ||
return cb(self._makeError(1)); | ||
request(req, function(err, res, body) { | ||
if (err) self._debug(1, 'WARNING', { error: err, req: req }); | ||
cb(err, body); | ||
}); | ||
if(haveParams){ | ||
request.write(d); | ||
} | ||
request.end(); | ||
var abortTimeout = setTimeout(function(){ | ||
request.abort(); | ||
}, this.opts.requestTimeout); | ||
}; | ||
@@ -119,2 +97,3 @@ | ||
var err; | ||
if(!responseData){ | ||
@@ -125,18 +104,5 @@ err = 2; | ||
} | ||
var parsed = ''; | ||
try{ | ||
parsed = JSON.parse(responseData); | ||
} | ||
catch(e){ | ||
err = 3; | ||
this._debug(err, 'WARNING', {error: e, responseData: responseData}); | ||
return err; | ||
} | ||
if(!parsed){ | ||
err = 4; | ||
this._debug(err, 'WARNING', {responseData: responseData}); | ||
return err; | ||
} | ||
if(!parsed.success && parsed.error){ | ||
var errObj = parsed.error; | ||
if (!responseData.success && responseData.error) { | ||
var errObj = responseData.error; | ||
switch(errObj.code){ | ||
@@ -153,3 +119,3 @@ case 1011: | ||
} | ||
return parsed; | ||
return responseData; | ||
}; | ||
@@ -180,9 +146,27 @@ | ||
GoSquared.prototype.event = require('./Event'); | ||
GoSquared.prototype.createTransaction = function(transactionID, opts){ | ||
var self = this; | ||
return new Transaction(self, transactionID, opts); | ||
GoSquared.prototype.event = function(name, data, cb) { | ||
if(typeof data === 'function'){ | ||
cb = data; | ||
data = {}; | ||
} | ||
if(!name){ | ||
this._debug(5, 'WARNING'); | ||
return cb && cb(this._makeError(5)); | ||
} | ||
this._exec(this.config.endpoints.data, '/' + this.opts.site_token + '/v1/event', { name: name }, data, this._responseCompleted.bind(this, cb)); | ||
}; | ||
GoSquared.prototype.createTransaction = GoSquared.prototype.Transaction = function(transactionID, opts){ | ||
return new Transaction(this, transactionID, opts); | ||
}; | ||
GoSquared.prototype.createPerson = GoSquared.prototype.Person = function(id, opts){ | ||
return new Person(this, id); | ||
}; | ||
GoSquared.prototype._responseCompleted = function(cb, err, responseData){ | ||
if (!cb) cb = function(){}; | ||
if(err){ | ||
@@ -195,4 +179,4 @@ this._debug(7, 'WARNING'); | ||
if(typeof validated != "object"){ | ||
if(typeof validated == "number"){ | ||
if (typeof validated !== "object") { | ||
if (typeof validated === "number") { | ||
err = this._makeError(validated); | ||
@@ -199,0 +183,0 @@ } |
@@ -1,13 +0,1 @@ | ||
module.exports.bufferResponse = function(res, cb){ | ||
res.setEncoding('utf8'); | ||
var response = ''; | ||
res.on('data', function(chunk){ | ||
response += chunk; | ||
}); | ||
res.on('end', function(){ | ||
cb(null, response); | ||
}); | ||
}; | ||
/** | ||
@@ -29,1 +17,21 @@ * From https://github.com/Raynos/xtend | ||
}; | ||
module.exports.clone = function(obj) { | ||
function copy(a){ | ||
if(!a || typeof a !== 'object') return a; | ||
if(Array.isArray(a)){ | ||
return a.concat([]).map(copy); | ||
} | ||
var out = {}; | ||
for(var i in a){ | ||
if (!a.hasOwnProperty(i)) continue; | ||
out[i] = copy(a[i]); | ||
} | ||
return out; | ||
} | ||
return copy(obj); | ||
}; |
@@ -13,7 +13,13 @@ var util = require('util'); | ||
if (typeof this.id === 'undefined') this.GS._debug(101, 'WARNING'); | ||
else this.id = '' + this.id; | ||
if (typeof this.id === 'undefined') { | ||
this.GS._debug(101, 'WARNING'); | ||
} else { | ||
this.id = '' + this.id; | ||
} | ||
if (typeof opts === 'object') this.opts = opts; | ||
else this.opts = {}; | ||
if (typeof opts === 'object') { | ||
this.opts = opts; | ||
} else { | ||
this.opts = {}; | ||
} | ||
}; | ||
@@ -37,12 +43,12 @@ | ||
Transaction.prototype.track = function(done){ | ||
Transaction.prototype.track = function(cb){ | ||
var GS = this.GS; | ||
if(typeof done !== 'function'){ | ||
done = function(){}; | ||
if(typeof cb !== 'function'){ | ||
cb = function(){}; | ||
} | ||
if (typeof this.id === 'undefined') return done('transactionID not given'); | ||
if (typeof this.id === 'undefined') return cb('transactionID not given'); | ||
var params = { | ||
var data = { | ||
id: this.id, | ||
@@ -53,3 +59,6 @@ items: this.items, | ||
GS._exec(GS.config.endpoints.data, '/' + GS.opts.site_token + '/transaction', params, GS._responseCompleted.bind(GS, done)); | ||
// not the tidiest | ||
var params = this.params || {}; | ||
GS._exec(GS.config.endpoints.data, '/' + GS.opts.site_token + '/v1/transaction', params, data, GS._responseCompleted.bind(GS, cb)); | ||
}; |
{ | ||
"name": "gosquared", | ||
"version": "0.3.4", | ||
"version": "1.0.0", | ||
"description": "GoSquared for your Node.JS application", | ||
@@ -30,2 +30,5 @@ "main": "lib/GoSquared.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"request": "^2.44.0" | ||
}, | ||
"devDependencies": { | ||
@@ -32,0 +35,0 @@ "mocha": "~1.8.1", |
# node-gosquared | ||
This lightweight, zero-dependency module allows you to integrate the [GoSquared API][api-docs] into your Node.JS app with ease. | ||
This lightweight module allows you to integrate the [GoSquared API][api-docs] into your Node.JS app with ease. | ||
@@ -105,4 +105,4 @@ Commonly, you'll use this module to retrieve analytics data collected by GoSquared, but it can also be used to manage accounts, store events, and record transactions for GoSquared Ecommerce. | ||
var opts = { | ||
customRevenue: 10, | ||
customQuantity: 5 | ||
revenue: 10, | ||
quantity: 5 | ||
}; | ||
@@ -109,0 +109,0 @@ |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
24674
5.82%626
8.12%1
-50%0
-100%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added