Skip to content

Commit

Permalink
Fixed once request error
Browse files Browse the repository at this point in the history
  • Loading branch information
Amareis committed Jan 13, 2018
1 parent 3414ac1 commit 808c9ef
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bower_components
node_modules
.idea
npm-debug.log

105 changes: 0 additions & 105 deletions npm-debug.log

This file was deleted.

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.4.0",
"version": "0.4.1",
"description": "Simple REST API client that makes your code lesser and more beautiful than without it.",
"main": "rest-client.js",
"scripts": {
Expand Down
8 changes: 5 additions & 3 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.

8 changes: 5 additions & 3 deletions src/rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ class RestClient {
}
);
new Events(p);
this.emit('request', xhr);
p.emit('request', xhr);
xhr.send(data);
setTimeout(() => {
this.emit('request', xhr);
p.emit('request', xhr);
xhr.send(data);
}, 0);
return p;
}
}
Expand Down
35 changes: 34 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ describe('RestClient', () => {
new RestClient(host, {trailing: '/'}).res('cookies').get({fresh: true});
req.url.should.be.equal(host + '/cookies/?fresh=true');
});

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

var p = new RestClient(host, {trailing: '/'}).on('request', xhr => bool = true).res('cookies').get({fresh: true});
req.url.should.be.equal(host + '/cookies/?fresh=true');

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

p.then(() => {
bool.should.be.equal(true)
done();
}).catch(done);
});

});
});

Expand Down Expand Up @@ -228,7 +244,7 @@ describe('resource', () => {
var req;
xhr.onCreate = r => req = r;

var respTText;
var respText;

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

Expand All @@ -241,5 +257,22 @@ describe('resource', () => {
done();
}).catch(done);
});

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

var bool;

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

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

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

0 comments on commit 808c9ef

Please sign in to comment.