Skip to content

Commit

Permalink
Added multiple args to get
Browse files Browse the repository at this point in the history
  • Loading branch information
Amareis committed Dec 18, 2017
1 parent ee622c4 commit 7793e4f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Now we can query our resources using methods `get` (optionally gets query args),
```js
api.cookies.get(); //GET http://example.com/cookies
api.cookies.get({fresh: true}); //GET http://example.com/cookies?fresh=true
api.cookies.get({'filter[]': 'fresh'}, {'filter[]': 'taste'}); //GET http://example.com/cookies?filter%5B%5D=fresh&filter%5B%5D=taste
//POST http://example.com/cows, body="{"color":"white","name":"Moo"}"
api.cows.post({color: 'white', name: 'Moo'}).then(function(cow) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "another-rest-client",
"version": "0.3.4",
"version": "0.3.5",
"description": "Simple REST API client that makes your code lesser and more beautiful than without it.",
"main": "rest-client.js",
"scripts": {
Expand Down
31 changes: 18 additions & 13 deletions rest-client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rest-client.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rest-client.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rest-client.min.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ function resource(client, parent, name, id, ctx) {
return url;
};

self.get = (args) => {
self.get = (...args) => {
let url = self.url();
if (args)
url += '?' + encodeUrl(args);
const query = args.map(encodeUrl).join('&')
if (query)
url += '?' + query;
return client._request('GET', url);
};

Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ describe('resource', () => {
api.res('cookies');
});

it('should correct form query args when get one instance', () => {
var req;
xhr.onCreate = r => req = r;

api.cookies(4).get();
req.url.should.be.equal(host + '/cookies/4');
});

it('should correct form query args when get multiply instances', () => {
var req;
xhr.onCreate = r => req = r;
Expand All @@ -156,6 +164,14 @@ describe('resource', () => {
req.url.should.be.equal(host + '/cookies?fresh=true');
});

it('should correct form query args when get multiply args', () => {
var req;
xhr.onCreate = r => req = r;

api.cookies.get({'filter[]': 'fresh'}, {'filter[]': 'taste'});
req.url.should.be.equal(host + '/cookies?filter%5B%5D=fresh&filter%5B%5D=taste');
});

it('should work correctly with an undefined content type', (done) => {
var req;
xhr.onCreate = r => req = r;
Expand Down

0 comments on commit 7793e4f

Please sign in to comment.