eh-api-client
Advanced tools
Comparing version 0.1.0 to 0.2.0
34
index.js
@@ -28,3 +28,3 @@ var | ||
Client.prototype.request = function(method, url, body, cb) { | ||
Client.prototype.request = function(method, options, body, cb) { | ||
if(typeof body === "function") { | ||
@@ -34,6 +34,12 @@ cb = body; | ||
} | ||
if(typeof options === "string") { | ||
options = { | ||
url: options | ||
}; | ||
} | ||
var reqParams = { | ||
url: this.apiURL + url, | ||
url: this.apiURL + options.url, | ||
method: method, | ||
json: true | ||
json: true, | ||
qs: {} | ||
}; | ||
@@ -46,2 +52,24 @@ if(body) { | ||
} | ||
if(options.filter) { | ||
var filterFields = []; | ||
for(var i = 0; i != options.filter.length; i++) { | ||
var filter = options.filter[i]; | ||
if(!filter.field) { | ||
var key = Object.keys(filter)[0]; | ||
if(!key) { | ||
continue; | ||
} | ||
filter.field = key; | ||
filter.value = filter[key]; | ||
filter.type = "eq"; | ||
} | ||
filterFields.push(filter.field); | ||
reqParams.qs["filterValue_" + filter.field] = filter.value; | ||
reqParams.qs["filterType_" + filter.field] = filter.type; | ||
} | ||
reqParams.qs["filterFields"] = filterFields; | ||
} | ||
if(options.test) { | ||
return cb(null, reqParams); | ||
} | ||
request(reqParams, function(err, res, data) { | ||
@@ -48,0 +76,0 @@ if(err) { |
{ | ||
"name": "eh-api-client", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Node.js rest client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -58,2 +58,25 @@ var | ||
}); | ||
it("should make correct filter request", function(done) { | ||
client.get({ | ||
test: true, | ||
url: "test", | ||
filter: [ | ||
{key: 1}, | ||
{ | ||
field: "id", | ||
type: "gt", | ||
value: 500 | ||
} | ||
] | ||
}, function(err, req) { | ||
should.not.exists(err); | ||
req.qs.filterFields.should.eql(["key", "id"]); | ||
req.qs.filterType_key.should.equal("eq"); | ||
req.qs.filterValue_key.should.equal(1); | ||
req.qs.filterType_id.should.equal("gt"); | ||
req.qs.filterValue_id.should.equal(500); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -60,0 +83,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
5546
208