backbone.fetch
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,2 +0,2 @@ | ||
// Backbone.Fetch.js 0.2.2 | ||
// Backbone.Fetch.js 0.2.3 | ||
// --------------- | ||
@@ -31,10 +31,4 @@ | ||
var checkStatus = function(response) { | ||
if (response.ok) { | ||
return response; | ||
} else { | ||
var error = new Error(response.statusText); | ||
error.response = response; | ||
throw error; | ||
} | ||
var getData = function(response, dataType) { | ||
return dataType === 'json' ? response.json() : response.text(); | ||
}; | ||
@@ -58,11 +52,16 @@ | ||
return fetch(options.url, options) | ||
.then(checkStatus) | ||
.then(function(response) { | ||
return options.dataType === 'json' ? response.json(): response.text(); | ||
var promise = getData(response, options.dataType) | ||
if (response.ok) return promise; | ||
var error = new Error(response.statusText); | ||
promise.then(function(responseData) { | ||
error.response = response; | ||
error.responseData = responseData; | ||
if (options.error) options.error(error); | ||
throw error; | ||
}); | ||
}) | ||
.then(options.success) | ||
.catch(function(e) { | ||
if (options.error) options.error(e); | ||
throw e; | ||
}); | ||
.then(options.success); | ||
}; | ||
@@ -69,0 +68,0 @@ |
{ | ||
"name": "backbone.fetch", | ||
"version": "0.2.1", | ||
"version": "0.2.3", | ||
"homepage": "https://github.com/akre54/backbone.fetch", | ||
@@ -5,0 +5,0 @@ "author": "Adam Krebs <amk528@cs.nyu.edu>", |
{ | ||
"name": "backbone.fetch", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"author": "Adam Krebs <amk528@cs.nyu.edu>", | ||
@@ -20,10 +20,7 @@ "description": "A drop-in Backbone.ajax replacement powered by window.fetch", | ||
"devDependencies": { | ||
"backbone": "1.3.1", | ||
"chai": "^3.5.0", | ||
"mocha": "^2.4.5", | ||
"sinon": "^1.10.3", | ||
"underscore": "^1.8.0", | ||
"whatwg-fetch": "^0.11.0", | ||
"xmlhttprequest": "^1.6.0" | ||
"whatwg-fetch": "^0.11.0" | ||
} | ||
} |
@@ -147,7 +147,3 @@ global.XMLHttpRequest = function() { | ||
}).catch(function(error) { | ||
if (error.response) { | ||
expect(error.response.status).to.equal(400); | ||
} else { | ||
throw error; | ||
} | ||
expect(error.response.status).to.equal(400); | ||
done(); | ||
@@ -174,7 +170,3 @@ }).catch(function(error) { | ||
}).catch(function(error) { | ||
if (error.response) { | ||
expect(error.response.status).to.equal(400); | ||
} else { | ||
throw error; | ||
} | ||
expect(error.response.status).to.equal(400); | ||
done(); | ||
@@ -188,4 +180,66 @@ }).catch(function(error) { | ||
}); | ||
it('should parse json as property of Error on failing request', function(done) { | ||
var promise = ajax({ | ||
dataType: 'json', | ||
url: 'test', | ||
type: 'GET', | ||
}); | ||
promise.then(function() { | ||
throw new Error('this request should fail'); | ||
}).catch(function(error) { | ||
expect(error.responseData).to.deep.equal({ code: 'INVALID_HORSE' }); | ||
done(); | ||
}).catch(function(error) { | ||
done(error); | ||
}); | ||
server.respond([400, {}, JSON.stringify({ code: 'INVALID_HORSE' })]); | ||
return promise; | ||
}); | ||
it('should parse text as property of Error on failing request', function(done) { | ||
var promise = ajax({ | ||
dataType: 'text', | ||
url: 'test', | ||
type: 'GET', | ||
}); | ||
promise.then(function() { | ||
throw new Error('this request should fail'); | ||
}).catch(function(error) { | ||
expect(error.responseData).to.equal('Nope'); | ||
done(); | ||
}).catch(function(error) { | ||
done(error); | ||
}); | ||
server.respond([400, {}, 'Nope']); | ||
return promise; | ||
}); | ||
}); | ||
it('should pass through network errors', function(done) { | ||
var promise = ajax({ | ||
dataType: 'text', | ||
url: 'test', | ||
type: 'GET', | ||
}); | ||
promise.then(function() { | ||
throw new Error('this request should fail'); | ||
}).catch(function(error) { | ||
expect(error).to.be.an.instanceof(TypeError); | ||
expect(error).not.to.have.property('response'); | ||
expect(error.message).to.equal('Network request failed'); | ||
done(); | ||
}).catch(function(error) { | ||
done(error); | ||
}); | ||
server.respond([600, {}, 'Network error']); | ||
return promise; | ||
}); | ||
describe('Promise', function() { | ||
@@ -192,0 +246,0 @@ it('should return a Promise', function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11743
4
290