Skip to content

Commit

Permalink
Added per-request-events, closes #7, #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Amareis committed Jul 26, 2017
1 parent 679a164 commit 048c820
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 27 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ api.on('request', function(xhr) {
});
```
Also, returns by `get`, `post`, `put`, `patch` and `delete` `Promise` objects also emit these events, but only for current request.
```js
api.dogs(1337).toys.get().on('success', console.log.bind.(console)).then((toys) => "..."); //in log will be xhr instance
api.dogs(1337).toys.get().then((toys) => "..."); //log is clear
```
## Configuration
All the examples given above are based on the default settings. If for some reason you are not satisfied, read this section.
Expand Down Expand Up @@ -216,7 +222,7 @@ var api = new RestClient('http://example.com', opts);
//or by conf
api.conf(opts);
```
If there is no suitable decoder (or server given't `Content-Type` header, gotten string will be passed to Promise.resolve without changes.
If there is no suitable decoder (or server given't `Content-Type` header), gotten string will be passed to Promise.resolve without changes.
Of course, you can combine encoders and decoders for single MIME:
```js
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "another-rest-client",
"version": "0.3.3",
"version": "0.3.4",
"description": "Simple REST API client that makes your code lesser and more beautiful than without it.",
"main": "rest-client.js",
"scripts": {
Expand All @@ -26,7 +26,7 @@
"babel-plugin-transform-object-assign": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"chai": "^3.5.0",
"minivents": "^2.0.2",
"minivents": "github:Amareis/minivents",
"mocha": "^2.5.1",
"sinon": "^1.17.4",
"webpack": "^1.13.1"
Expand Down
26 changes: 16 additions & 10 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.

28 changes: 17 additions & 11 deletions src/rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RestClient {
}

_request(method, url, data=null, contentType=null) {
if (url.indexOf('?') == -1)
if (url.indexOf('?') === -1)
url += this._opts.trailing;
else
url = url.replace('?', this._opts.trailing + '?');
Expand All @@ -65,14 +65,14 @@ class RestClient {
xhr.setRequestHeader('Content-Type', contentType);
}

this.emit('request', xhr);

let p = new Promise((resolve, reject) => {
let p = new Promise((resolve, reject) =>
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
if (xhr.readyState === 4) {
this.emit('response', xhr);
if (xhr.status == 200 || xhr.status == 201 || xhr.status == 204) {
p.emit('response', xhr);
if (xhr.status === 200 || xhr.status === 201 || xhr.status === 204) {
this.emit('success', xhr);
p.emit('success', xhr);

let res = xhr.responseText;
let responseHeader = xhr.getResponseHeader('Content-Type');
Expand All @@ -82,22 +82,28 @@ class RestClient {
if (mime && mime.decode)
res = safe(mime.decode, res);
}
p.off();
resolve(res);
} else {
this.emit('error', xhr);
p.emit('error', xhr);
p.off();
reject(xhr);
}
}
};
});
}
);
new Events(p);
this.emit('request', xhr);
p.emit('request', xhr);
xhr.send(data);
return p;
}
}

function resource(client, parent, name, id, ctx) {
let self = ctx ? ctx : (newId) => {
if (newId == undefined)
if (newId === undefined)
return self;
return self._clone(parent, newId);
};
Expand Down Expand Up @@ -132,7 +138,7 @@ function resource(client, parent, name, id, ctx) {
};

// (resources instanceof String) don't work. Fuck you, javascript.
if (resources.constructor == String)
if (resources.constructor === String)
return makeRes(resources);

if (resources instanceof Array)
Expand All @@ -154,7 +160,7 @@ function resource(client, parent, name, id, ctx) {
let url = parent ? parent.url() : '';
if (name)
url += '/' + name;
if (id != undefined)
if (id !== undefined)
url += '/' + id;
return url;
};
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,24 @@ describe('resource', () => {
done();
}).catch(done);
});


it('should emit once event', (done) => {
var req;
xhr.onCreate = r => req = r;

var respTText;

var p = api.cookies.get({fresh: true}).on('success', xhr => respText = xhr.responseText);

req.respond(200, [], '{a:1}');

req.url.should.be.equal(host + '/cookies?fresh=true');
p.then(r => {
r.should.be.equal('{a:1}');
respText.should.be.equal('{a:1}');
done();
}).catch(done);
});
});
});

0 comments on commit 048c820

Please sign in to comment.