Skip to content

Commit

Permalink
Use xhr.response instead responseText
Browse files Browse the repository at this point in the history
  • Loading branch information
Amareis committed Jan 12, 2018
1 parent 7793e4f commit 3414ac1
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 10 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ 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
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
```
You can use events to set `responseType` XMLHttpRequest property, to handle binary files (and you can compose it with custom decoders, as described below, to automatically convert blob to File object):
```js
api.files('presentation.pdf').get().on('request', xhr => xhr.responseType = 'blob').then(blobObj => "...");
```
## Configuration
Expand Down Expand Up @@ -223,7 +228,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 `XMLHttpRequest.response` will be passed to Promise.resolve without changes.
Of course, you can combine encoders and decoders for single MIME:
```js
Expand Down
105 changes: 105 additions & 0 deletions npm-debug.log

Large diffs are not rendered by default.

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.5",
"version": "0.4.0",
"description": "Simple REST API client that makes your code lesser and more beautiful than without it.",
"main": "rest-client.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion 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.

2 changes: 1 addition & 1 deletion src/rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class RestClient {
this.emit('success', xhr);
p.emit('success', xhr);

let res = xhr.responseText;
let res = xhr.response;
let responseHeader = xhr.getResponseHeader('Content-Type');
if (responseHeader) {
let responseContentType = responseHeader.split(';')[0];
Expand Down
15 changes: 14 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ describe('resource', () => {
}).catch(done);
});

it('should correctly parse response', (done) => {
var req;
xhr.onCreate = r => req = r;

var p = api.cookies.get({fresh: true});

req.respond(200, {'Content-Type': 'application/json'}, '{"a":"1df"}');

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

it('should correctly handle exception with wrong encoded response body', (done) => {
var req;
Expand All @@ -210,7 +224,6 @@ describe('resource', () => {
}).catch(done);
});


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

0 comments on commit 3414ac1

Please sign in to comment.