shopify-node
Advanced tools
Comparing version 0.1.9 to 0.2.0
@@ -29,2 +29,52 @@ function ShopifyConnect(params) { | ||
}; | ||
function doReq(opts, d, callback) { | ||
var q = JSON.stringify(d); | ||
if(!opts.method) { | ||
opts.method = 'get'; | ||
} | ||
opts.method = opts.method.toLowerCase(); | ||
if(opts.method === 'post' || opts.method === 'put' || opts.method === 'delete') { | ||
opts.headers['Content-Length'] = q.length; | ||
} | ||
var req = https.request(opts, function(sock) { | ||
sock.setEncoding('utf-8'); | ||
}); | ||
req.on('response', function(res) { | ||
var responseData = ''; | ||
res.on('data', function(chunk) { | ||
responseData += chunk; | ||
}); | ||
res.on('end', function() { | ||
try { | ||
var j = JSON.parse(responseData); | ||
if(j.errors || j.error) { | ||
j.arguments = opts; | ||
return callback(j, null); | ||
} | ||
callback(null, j); | ||
} catch(e) { | ||
callback({ | ||
error: true, | ||
message: 'Error on: [' + opts.method + '] ' + opts.path, | ||
details: responseData, | ||
arguments: opts | ||
}, null); | ||
} | ||
}); | ||
}); | ||
req.on('error', function(e) { | ||
callback(e, null); | ||
}); | ||
if(opts.method === 'post' || opts.method === 'put' || opts.method === 'delete') { | ||
req.write(q); | ||
} | ||
req.end(); | ||
} | ||
@@ -39,7 +89,7 @@ $scope.getAccessToken = function(code, callback) { | ||
var json = JSON.stringify({ | ||
var d = { | ||
client_id: $scope.config.id, | ||
client_secret: $scope.config.secret, | ||
code: code | ||
}); | ||
}; | ||
@@ -52,26 +102,14 @@ var opts = { | ||
headers: { | ||
'Content-Length': '' + json.length, | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'node-shopify (www.typefoo.com)', | ||
} | ||
}; | ||
var shReq = https.request(opts, function(res) { | ||
var data = ''; | ||
res.on('data', function(chunk) { | ||
data += chunk; | ||
var j = JSON.parse(data); | ||
if(j.error) { | ||
return callback(j, null); | ||
} | ||
$scope.config.access_token = j.access_token; | ||
callback(null, j.access_token); | ||
}); | ||
doReq(opts, d, function(err, json) { | ||
if(err) { | ||
return callback(err, null); | ||
} | ||
callback(null, json.access_token); | ||
}); | ||
shReq.on('error', function(e) { | ||
callback(e, null); | ||
}); | ||
shReq.write(json); | ||
shReq.end(); | ||
}; | ||
@@ -87,4 +125,2 @@ | ||
} | ||
var q = JSON.stringify(data); | ||
@@ -100,3 +136,3 @@ method = method.toLowerCase(); | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'Vern.io (http://www.typefoo.com)', | ||
'User-Agent': 'node-shopify (www.typefoo.com)', | ||
'X-Shopify-Access-Token': access_token | ||
@@ -106,41 +142,9 @@ } | ||
if(method === 'post' || method === 'put' || method === 'delete') { | ||
opts.headers['Content-Length'] = q.length; | ||
} | ||
var req = https.request(opts, function(sock) { | ||
var fullData = ''; | ||
sock.setEncoding('utf-8'); | ||
sock.on('data', function(chunk) { | ||
fullData += chunk; | ||
}); | ||
sock.on('end', function() { | ||
try { | ||
var j = JSON.parse(fullData); | ||
if(j.errors || j.error) { | ||
j.arguments = used_args; | ||
return callback(j, null); | ||
} | ||
callback(null, j); | ||
} catch(e) { | ||
callback({ | ||
error: true, | ||
message: 'Error on: [' + method + '] ' + path, | ||
details: fullData, | ||
arguments: used_args | ||
}, null); | ||
} | ||
}); | ||
doReq(opts, data, function(err, json) { | ||
if(err) { | ||
return callback(err, null); | ||
} | ||
callback(null, json); | ||
}); | ||
req.on('error', function(e) { | ||
callback(e, null); | ||
}); | ||
if(method === 'post' || method === 'put' || method === 'delete') { | ||
req.write(q); | ||
} | ||
req.end(); | ||
}; | ||
@@ -147,0 +151,0 @@ |
{ | ||
"name": "shopify-node", | ||
"description": "A NodeJS connector for Shopify OAuth2", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -30,4 +30,4 @@ "name": "uh-sem-blee, Co. | typefoo", | ||
}, | ||
"_id": "shopify-node@0.1.8", | ||
"_id": "shopify-node@0.2.0", | ||
"_from": "shopify-node@" | ||
} |
10406
254