backbone.fetch
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -1,5 +0,5 @@ | ||
// Backbone.Fetch.js 0.2.1 | ||
// Backbone.Fetch.js 0.2.2 | ||
// --------------- | ||
// (c) 2015 Adam Krebs | ||
// (c) 2016 Adam Krebs | ||
// Backbone.Fetch may be freely distributed under the MIT license. | ||
@@ -32,3 +32,3 @@ // For all details and documentation: | ||
var checkStatus = function(response) { | ||
if (response.status >= 200 && response.status < 300) { | ||
if (response.ok) { | ||
return response; | ||
@@ -48,10 +48,12 @@ } else { | ||
return fetch(options.url, defaults(options, { | ||
method: options.type, | ||
headers: defaults(options.headers || {}, { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
}), | ||
body: options.data | ||
})) | ||
defaults(options, { | ||
method: options.type, | ||
headers: defaults(options.headers || {}, { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
}), | ||
body: options.data | ||
}) | ||
return fetch(options.url, options) | ||
.then(checkStatus) | ||
@@ -63,3 +65,3 @@ .then(function(response) { | ||
.catch(function(e) { | ||
options.error(e); | ||
if (options.error) options.error(e); | ||
throw e; | ||
@@ -66,0 +68,0 @@ }); |
{ | ||
"name": "backbone.fetch", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"author": "Adam Krebs <amk528@cs.nyu.edu>", | ||
@@ -20,10 +20,10 @@ "description": "A drop-in Backbone.ajax replacement powered by window.fetch", | ||
"devDependencies": { | ||
"backbone": "1.2.0", | ||
"chai": "^1.9.1", | ||
"mocha": "^1.21.4", | ||
"backbone": "1.3.1", | ||
"chai": "^3.5.0", | ||
"mocha": "^2.4.5", | ||
"sinon": "^1.10.3", | ||
"underscore": "^1.8.0", | ||
"whatwg-fetch": "^0.9.0", | ||
"whatwg-fetch": "^0.11.0", | ||
"xmlhttprequest": "^1.6.0" | ||
} | ||
} |
@@ -96,3 +96,3 @@ global.XMLHttpRequest = function() { | ||
headers: { | ||
Accept: "custom", 'Content-Type': "custom" | ||
Accept: "custom", 'Content-Type': "custom", "X-MyApp-Header": 'present' | ||
} | ||
@@ -102,3 +102,3 @@ }); | ||
sinon.assert.calledWith(fetch, 'test', sinon.match({headers: { | ||
Accept: "custom", 'Content-Type': "custom" | ||
Accept: "custom", 'Content-Type': "custom", "X-MyApp-Header": 'present' | ||
}})); | ||
@@ -113,3 +113,3 @@ }); | ||
type: 'GET', | ||
success: function(response) { | ||
success: function(response) { | ||
expect(response).to.equal('ok'); | ||
@@ -127,3 +127,3 @@ } | ||
type: 'GET', | ||
success: function(response) { | ||
success: function(response) { | ||
expect(response).to.deep.equal({status: 'ok'}); | ||
@@ -141,3 +141,3 @@ } | ||
success: function(response) { | ||
throw new Error('this request should be failed'); | ||
throw new Error('this request should fail'); | ||
}, | ||
@@ -150,8 +150,33 @@ error: function(error) { | ||
promise.then(function() { | ||
throw new Error('this request should be failed'); | ||
throw new Error('this request should fail'); | ||
}).catch(function(error) { | ||
if (error.response) { | ||
expect(error.response.status).to.equal(400); | ||
} else { | ||
throw error; | ||
} | ||
else { | ||
done(); | ||
}).catch(function(error) { | ||
done(error); | ||
}); | ||
server.respond([400, {}, 'Server error']); | ||
return promise; | ||
}); | ||
it('should not fail without error callback', function(done) { | ||
var promise = ajax({ | ||
url: 'test', | ||
type: 'GET', | ||
success: function(response) { | ||
throw new Error('this request should fail'); | ||
} | ||
}); | ||
promise.then(function() { | ||
throw new Error('this request should fail'); | ||
}).catch(function(error) { | ||
if (error.response) { | ||
expect(error.response.status).to.equal(400); | ||
} else { | ||
throw error; | ||
@@ -167,3 +192,2 @@ } | ||
}); | ||
}); | ||
@@ -170,0 +194,0 @@ |
10188
248