Comparing version 0.0.2 to 0.0.3
@@ -6,14 +6,54 @@ var request = require('superagent'), | ||
function toError(status, message) { | ||
var json; | ||
try { json = JSON.parse(message); message = undefined; } catch(ex) { } | ||
return { | ||
status: status, | ||
message: message, | ||
json: json | ||
} | ||
} | ||
function exceptionToError(ex) { | ||
return toError(null, ex.message); | ||
} | ||
function promisify(callback) { | ||
var deferred = q.defer(); | ||
callback(function(err, value) { | ||
if (err) deferred.reject(err); | ||
else deferred.resolve(value); | ||
}); | ||
callback(function(err, value) { | ||
if (err) deferred.reject(err); | ||
else deferred.resolve(value); | ||
}); | ||
return deferred.promise; | ||
} | ||
function completable(callback, promised) { | ||
return function(error, user) { | ||
// call the callback if it was provided | ||
if (typeof callback === 'function') { | ||
if (error) callback(error); | ||
else callback(undefined, user); | ||
} | ||
// fulfill the promise | ||
if (error) promised(error); | ||
else promised(undefined, user); | ||
} | ||
} | ||
function responseHandler(complete) { | ||
return function(err, res) { | ||
try { | ||
if (err) complete(exceptionToError(err)); | ||
else if (res.error) complete(toError(res.error.status, res.error.text)); | ||
else complete(undefined, res.body); | ||
} catch(ex) { | ||
complete(exceptionToError(ex)); | ||
} | ||
} | ||
} | ||
function api(method, endpoint) { | ||
return function(user, callback) { | ||
return promisify(function(done) { | ||
return promisify(function(promised) { | ||
method(AuthBase.url + endpoint) | ||
@@ -23,16 +63,4 @@ .set('X-App-Id', appId) | ||
.send(user) | ||
.end(function(err, res) { | ||
var error = err || res.error; | ||
// call the callback if it was provided | ||
if (typeof callback === 'function') { | ||
if (error) callback(JSON.parse(error.text)); | ||
else callback(undefined, res.body); | ||
} | ||
// fulfill the promise | ||
if (error) done(error.text); | ||
else done(undefined, res.body); | ||
}); | ||
.buffer() | ||
.end(responseHandler(completable(callback, promised))); | ||
}); | ||
@@ -39,0 +67,0 @@ } |
{ | ||
"name": "authbase", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "AuthBase API Client", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -15,4 +15,3 @@ var expect = require('expect.js'), | ||
return function() { | ||
return MockApiFactory() | ||
.get(endpoint, {username: 'foo', password: 'bar'}); | ||
return MockApiFactory().get(endpoint, {username: 'foo', password: 'bar'}); | ||
} | ||
@@ -23,4 +22,3 @@ } | ||
return function() { | ||
return MockApiFactory() | ||
.post(endpoint, {username: 'foo', password: 'bar'}); | ||
return MockApiFactory().post(endpoint, {username: 'foo', password: 'bar'}); | ||
} | ||
@@ -66,3 +64,3 @@ } | ||
it('should pass the user to the callback as the first argument on failure', function(done) { | ||
it('should pass a json error if JSON object to the callback as the first argument on failure', function(done) { | ||
var mock = request().reply(400, { errors: [] }); | ||
@@ -75,3 +73,5 @@ | ||
expect(user).to.be(undefined); | ||
expect(err).to.eql({ errors: [] }); | ||
expect(err.status).to.be(400); | ||
expect(err.message).to.be(undefined); | ||
expect(err.json).to.eql({ errors: [] }); | ||
mock.done(); | ||
@@ -82,2 +82,18 @@ done(); | ||
it('should pass a message if not a JSON object to the callback as the first argument on failure', function(done) { | ||
var mock = request().reply(401, 'You are unauthorized'); | ||
proc({ | ||
username: 'foo', | ||
password: 'bar' | ||
}, function(err, user) { | ||
expect(user).to.be(undefined); | ||
expect(err.status).to.be(401); | ||
expect(err.message).to.be('You are unauthorized'); | ||
expect(err.json).to.be(undefined); | ||
mock.done(); | ||
done(); | ||
}); | ||
}); | ||
it('should pass the user to the promise on failure', function(done) { | ||
@@ -93,3 +109,4 @@ var mock = request().reply(400, { errors: [] }); | ||
}).catch(function(err) { | ||
expect(err).to.eql({ errors: [] }); | ||
expect(err.status).to.be(400); | ||
expect(err.body).to.eql({ errors: [] }); | ||
}).finally(function() { | ||
@@ -100,2 +117,40 @@ mock.done(); | ||
}); | ||
it('should pass a json error if JSON object to the promise on failure', function(done) { | ||
var mock = request().reply(400, { errors: [] }); | ||
proc({ | ||
username: 'foo', | ||
password: 'bar' | ||
}).then(function(user) { | ||
// should never hit this on error, so automatically fail | ||
expect(false).to.be(true); | ||
}).catch(function(err) { | ||
expect(err.status).to.be(400); | ||
expect(err.message).to.be(undefined); | ||
expect(err.json).to.eql({ errors: [] }); | ||
}).finally(function() { | ||
mock.done(); | ||
done(); | ||
}); | ||
}); | ||
it('should pass a message if not a JSON object to the promise on failure', function(done) { | ||
var mock = request().reply(401, 'You are unauthorized'); | ||
proc({ | ||
username: 'foo', | ||
password: 'bar' | ||
}).then(function(user) { | ||
// should never hit this on error, so automatically fail | ||
expect(false).to.be(true); | ||
}).catch(function(err) { | ||
expect(err.status).to.be(401); | ||
expect(err.message).to.be('You are unauthorized'); | ||
expect(err.json).to.be(undefined); | ||
}).finally(function() { | ||
mock.done(); | ||
done(); | ||
}); | ||
}); | ||
} | ||
@@ -102,0 +157,0 @@ } |
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
7540
205