backbone.fetch
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,5 +0,5 @@ | ||
// Backbone.Fetch.js 0.1.0 | ||
// Backbone.Fetch.js 0.2.0 | ||
// --------------- | ||
// (c) 2014 Adam Krebs | ||
// (c) 2015 Adam Krebs | ||
// Backbone.Fetch may be freely distributed under the MIT license. | ||
@@ -17,3 +17,3 @@ // For all details and documentation: | ||
return obj; | ||
} | ||
}; | ||
@@ -30,17 +30,33 @@ var stringifyGETParams = function(url, data) { | ||
return url; | ||
} | ||
}; | ||
var checkStatus = function(response) { | ||
if (response.status >= 200 && response.status < 300) { | ||
return response; | ||
} else { | ||
var error = new Error(response.statusText); | ||
error.response = response; | ||
throw error; | ||
} | ||
}; | ||
var ajax = function(options) { | ||
if (options.type === 'GET' && typeof options.data === 'object') { | ||
options.url = stringifyGETParams(options.url, options.data); | ||
delete options.data; | ||
} | ||
return fetch(options.url, defaults(options, { | ||
method: options.type, | ||
headers: defaults(options.headers || {}, { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
}), | ||
body: options.data | ||
})).then(options.success, options.error); | ||
method: options.type, | ||
headers: defaults(options.headers || {}, { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
}), | ||
body: options.data | ||
})) | ||
.then(checkStatus) | ||
.then(function(response) { | ||
return options.dataType === 'json' ? response.json(): response.text(); | ||
}) | ||
.then(options.success, options.error); | ||
}; | ||
@@ -47,0 +63,0 @@ |
{ | ||
"name": "backbone.fetch", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/akre54/backbone.fetch", | ||
@@ -5,0 +5,0 @@ "author": "Adam Krebs <amk528@cs.nyu.edu>", |
{ | ||
"name": "backbone.fetch", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Adam Krebs <amk528@cs.nyu.edu>", | ||
"description": "A drop-in Backbone.ajax replacement powered by window.fetch", | ||
"licence": "MIT", | ||
"license": "MIT", | ||
"main": "backbone.fetch", | ||
@@ -20,10 +20,10 @@ "repository": { | ||
"devDependencies": { | ||
"backbone": "git://github.com/jashkenas/backbone#cbaa8d144b7560d2b509c1ffbaf6ddb1e3829e6c", | ||
"backbone": "1.2.0", | ||
"chai": "^1.9.1", | ||
"fetch": "git://github.com/github/fetch", | ||
"mocha": "^1.21.4", | ||
"sinon": "^1.10.3", | ||
"underscore": "^1.6.0", | ||
"underscore": "^1.8.0", | ||
"whatwg-fetch": "^0.9.0", | ||
"xmlhttprequest": "^1.6.0" | ||
} | ||
} |
@@ -18,8 +18,2 @@ Backbone.Fetch | ||
```js | ||
// AMD | ||
define(['backbone', 'backbone.fetch'], function(Backbone, fetch) { | ||
Backbone.ajax = fetch; | ||
}); | ||
// CommonJS | ||
var Backbone = require('backbone'); | ||
@@ -26,0 +20,0 @@ Backbone.ajax = require('backbone.fetch'); |
@@ -0,21 +1,28 @@ | ||
global.XMLHttpRequest = function() { | ||
this.withCredentials = true; | ||
}; | ||
var sinon = require('sinon'); | ||
var expect = require('chai').expect; | ||
// Bleh. There has to be a better way to test this | ||
sinon = require('sinon'); | ||
require('sinon/lib/sinon/util/event'); | ||
require('sinon/lib/sinon/util/fake_xml_http_request'); | ||
(function() { | ||
if (typeof window === 'undefined') global.window = {}; | ||
require('fetch'); | ||
global.fetch = sinon.spy(window.fetch); | ||
if (typeof window === 'undefined') global.self = {}; | ||
require('whatwg-fetch'); | ||
global.fetch = sinon.spy(self.fetch); | ||
})(); | ||
XMLHttpRequest = function() {} | ||
XMLHttpRequest.prototype = sinon.useFakeXMLHttpRequest().prototype; | ||
var ajax = require('../backbone.fetch'); | ||
describe('backbone.fetch', function() { | ||
var server; | ||
beforeEach(function() { | ||
server = sinon.fakeServer.create(); | ||
}); | ||
afterEach(function() { | ||
server.restore(); | ||
}); | ||
describe('creating a request', function() { | ||
@@ -100,21 +107,42 @@ it('should pass the method and url to fetch', function() { | ||
describe('finishing a request', function() { | ||
it('should invoke the success callback on complete', function(done) { | ||
done(); | ||
// ajax({ | ||
// url: 'test', | ||
// type: 'GET', | ||
// success: function() { done(); }, | ||
// error: function() { throw new Error(); } | ||
// }); | ||
it('should invoke the success callback on complete', function() { | ||
var promise = ajax({ | ||
url: 'test', | ||
type: 'GET', | ||
success: function(response) { | ||
expect(response).to.equal('ok'); | ||
} | ||
}); | ||
server.respond('ok'); | ||
return promise; | ||
}); | ||
it('should invoke the error callback on error', function(done) { | ||
done(); | ||
// ajax({ | ||
// url: 'test', | ||
// type: 'GET', | ||
// success: function() { throw new Error; }, | ||
// error: function(e) { console.log(arguments); done(); } | ||
// }); | ||
it('should parse response as json if dataType option is provided', function() { | ||
var promise = ajax({ | ||
url: 'test', | ||
dataType: 'json', | ||
type: 'GET', | ||
success: function(response) { | ||
expect(response).to.deep.equal({status: 'ok'}); | ||
} | ||
}); | ||
server.respond('{"status": "ok"}'); | ||
return promise; | ||
}); | ||
it('should invoke the error callback on error', function() { | ||
var promise = ajax({ | ||
url: 'test', | ||
type: 'GET', | ||
success: function(response) { | ||
throw new Error('this reqest should be failed'); | ||
}, | ||
error: function(error) { | ||
expect(error.response.status).to.equal(400); | ||
} | ||
}); | ||
server.respond([400, {}, 'Server error']); | ||
return promise; | ||
}); | ||
}); | ||
@@ -121,0 +149,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
9089
207
27