eh-api-client
Advanced tools
Comparing version 0.7.4 to 0.8.0
19
index.js
var | ||
request = require("request"); | ||
request = require("request"), | ||
Q = require("q"); | ||
@@ -53,2 +54,3 @@ function buildError(res, data) { | ||
Client.prototype.request = function(method, options, body, cb) { | ||
var deferred = Q.defer(); | ||
if(typeof body === "function") { | ||
@@ -107,14 +109,19 @@ cb = body; | ||
} | ||
deferred.promise.nodeify(cb); | ||
if(options.test) { | ||
return cb(null, reqParams); | ||
process.nextTick(function() { | ||
deferred.resolve(reqParams); | ||
}); | ||
return deferred.promise; | ||
} | ||
request(reqParams, function(err, res, data) { | ||
if(err) { | ||
return cb(err); | ||
deferred.reject(err); | ||
} | ||
if(res.statusCode < 200 || res.statusCode >= 300) { | ||
return cb(buildError(res, data), null, res); | ||
deferred.reject(buildError(res, data), null, res); | ||
} | ||
cb(null, data, res); | ||
deferred.resolve(data, res); | ||
}); | ||
return deferred.promise; | ||
}; | ||
@@ -135,3 +142,3 @@ | ||
args.unshift(method); | ||
this.request.apply(this, args); | ||
return this.request.apply(this, args); | ||
}; | ||
@@ -138,0 +145,0 @@ }); |
{ | ||
"name": "eh-api-client", | ||
"version": "0.7.4", | ||
"version": "0.8.0", | ||
"description": "Node.js rest client", | ||
@@ -16,2 +16,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"q": "^1.4.1", | ||
"request": "^2.55.0" | ||
@@ -18,0 +19,0 @@ }, |
@@ -40,2 +40,10 @@ var | ||
it("get note(promise)", function(done) { | ||
client.get("/notes/" + noteGlobalId).then(function(data) { | ||
data.title.should.equal("test note"); | ||
data.globalId.should.equal(noteGlobalId); | ||
done(); | ||
}); | ||
}); | ||
it("should make correct filter request", function(done) { | ||
@@ -76,2 +84,13 @@ client.get({ | ||
it("should make a range request(promise)", function(done) { | ||
client.get("/test", { | ||
range: "items 0-4", | ||
test: true | ||
}).then(function(req) { | ||
req.url.should.equal("http://localhost:3000/test"); | ||
req.headers.Range.should.equal("items 0-4"); | ||
done(); | ||
}); | ||
}); | ||
it("should fork client", function() { | ||
@@ -78,0 +97,0 @@ var fork = client.fork("/sub/url"); |
8080
2
4
282