Comparing version 0.3.4 to 1.0.0
@@ -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
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
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 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
24674
626
1
0
1
+ Addedrequest@^2.44.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)