Skip to content

Commit

Permalink
Closes #4.
Browse files Browse the repository at this point in the history
Closes #5.

Closes #6.

Stable Version 1.0.0-alpha.2.
  • Loading branch information
jmdobry committed Nov 2, 2014
1 parent 7807b99 commit d8d52d4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
##### 1.0.0-alpha.2 - 01 November 2014

###### Backwards compatible API changes
- #4 - Log failures. See also #5 and #6

##### 1.0.0-alpha.1 - 31 October 2014

###### Backwards compatible bug fixes
Expand Down
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "js-data-http",
"description": "http adapter for js-data.",
"version": "1.0.0-alpha.1",
"homepage": "http://www.js-data.io/js-data-http",
"version": "1.0.0-alpha.2",
"homepage": "http://www.js-data.io/docs/dshttpadapter",
"repository": {
"type": "git",
"url": "git://github.com/js-data/js-data-http.git"
"url": "https://github.com/js-data/js-data-http.git"
},
"author": {
"name": "Jason Dobry",
Expand Down
34 changes: 25 additions & 9 deletions dist/js-data-http.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @author Jason Dobry <[email protected]>
* @file js-data-http.js
* @version 1.0.0-alpha.1 - Homepage <http://www.js-data.iojs-data-http/>
* @version 1.0.0-alpha.2 - Homepage <http://www.js-data.iojs-data-http/>
* @copyright (c) 2014 Jason Dobry
* @license MIT <https://github.com/js-data/js-data-http/blob/master/LICENSE>
*
Expand Down Expand Up @@ -1418,8 +1418,13 @@ defaultsPrototype.forceTrailingSlash = '';

defaultsPrototype.httpConfig = {};

defaultsPrototype.log = console ? function (a, b, c, d, e) {
console.log(a, b, c, d, e);
defaultsPrototype.log = console ? function (a, b) {
console[typeof console.info === 'function' ? 'info' : 'log'](a, b);
} : function () {
};

defaultsPrototype.error = console ? function (a, b) {
console[typeof console.error === 'function' ? 'error' : 'log'](a, b);
} : function () {
};

Expand Down Expand Up @@ -1448,17 +1453,28 @@ dsHttpAdapterPrototype.getAllPath = function (resourceConfig, options) {

dsHttpAdapterPrototype.HTTP = function (config) {
var _this = this;
var start = new Date().getTime();
var start = new Date();
config = deepMixIn(config, _this.defaults.httpConfig);
if (_this.defaults.forceTrailingSlash && config.url[config.url.length] !== '/') {
config.url += '/';
}
return http(config).then(function (data) {
if (_this.defaults.log) {
_this.defaults.log(data.config.method.toUpperCase() + ' request: ' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms', data);

function logResponse(data) {
var str = start.toUTCString() + ' - ' + data.config.method.toUpperCase() + ' ' + data.config.url + ' - ' + data.status + ' ' + (new Date().getTime() - start.getTime()) + 'ms';
if (data.status >= 200 && data.status < 300) {
if (_this.defaults.log) {
_this.defaults.log(str, data);
}
return data;
} else {
if (_this.defaults.error) {
_this.defaults.error('FAILED: ' + str, data);
}
throw data;
}
return data;
});
}

return http(config).then(logResponse, logResponse);
};

dsHttpAdapterPrototype.GET = function (url, config) {
Expand Down
4 changes: 2 additions & 2 deletions dist/js-data-http.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "js-data-http",
"description": "http adapter for js-data.",
"version": "1.0.0-alpha.1",
"homepage": "http://www.js-data.io/js-data-http",
"version": "1.0.0-alpha.2",
"homepage": "http://www.js-data.io/docs/dshttpadapter",
"repository": {
"type": "git",
"url": "https://github.com/js-data/js-data-http.git"
Expand Down Expand Up @@ -46,6 +46,6 @@
"dependencies": {
"axios": "0.4.x",
"mout": "0.10.0",
"js-data": "1.0.x"
"js-data": "~1.0.x"
}
}
32 changes: 24 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ defaultsPrototype.forceTrailingSlash = '';

defaultsPrototype.httpConfig = {};

defaultsPrototype.log = console ? function (a, b, c, d, e) {
console.log(a, b, c, d, e);
defaultsPrototype.log = console ? function (a, b) {
console[typeof console.info === 'function' ? 'info' : 'log'](a, b);
} : function () {
};

defaultsPrototype.error = console ? function (a, b) {
console[typeof console.error === 'function' ? 'error' : 'log'](a, b);
} : function () {
};

Expand Down Expand Up @@ -66,17 +71,28 @@ dsHttpAdapterPrototype.getAllPath = function (resourceConfig, options) {

dsHttpAdapterPrototype.HTTP = function (config) {
var _this = this;
var start = new Date().getTime();
var start = new Date();
config = deepMixIn(config, _this.defaults.httpConfig);
if (_this.defaults.forceTrailingSlash && config.url[config.url.length] !== '/') {
config.url += '/';
}
return http(config).then(function (data) {
if (_this.defaults.log) {
_this.defaults.log(data.config.method.toUpperCase() + ' request: ' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms', data);

function logResponse(data) {
var str = start.toUTCString() + ' - ' + data.config.method.toUpperCase() + ' ' + data.config.url + ' - ' + data.status + ' ' + (new Date().getTime() - start.getTime()) + 'ms';
if (data.status >= 200 && data.status < 300) {
if (_this.defaults.log) {
_this.defaults.log(str, data);
}
return data;
} else {
if (_this.defaults.error) {
_this.defaults.error('FAILED: ' + str, data);
}
throw data;
}
return data;
});
}

return http(config).then(logResponse, logResponse);
};

dsHttpAdapterPrototype.GET = function (url, config) {
Expand Down
28 changes: 28 additions & 0 deletions test/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,32 @@ describe('DSHttpAdapter.find(resourceConfig, id, options)', function () {
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1));
}, 10);
});

it('should log errors', function (done) {
var _this = this;
var loggedError;

dsHttpAdapter.defaults.error = function (err) {
loggedError = err;
};

dsHttpAdapter.find(Post, 1).then(function () {
done('Should not have succeeded!');
}, function () {
assert.isString(loggedError);
assert.isTrue(loggedError.indexOf('api/posts/1') !== -1);
done();
});

setTimeout(function () {
try {
assert.equal(1, _this.requests.length);
assert.equal(_this.requests[0].url, 'api/posts/1');
assert.equal(_this.requests[0].method, 'get');
} catch (err) {
done(err);
}
_this.requests[0].respond(404, { 'Content-Type': 'text/plain' }, 'Not Found');
}, 10);
});
});

0 comments on commit d8d52d4

Please sign in to comment.