Comparing version 0.0.1 to 0.0.2
@@ -26,2 +26,4 @@ | ||
var xml; | ||
var type = res.headers.get('Content-Type'); | ||
var mime = type ? type.split(';').unshift() : 'text/xml' ; | ||
var text = res.text(); | ||
@@ -32,3 +34,3 @@ if (window) { | ||
try { | ||
xml = ( new window.DOMParser() ).parseFromString( text, "text/xml" ); | ||
xml = ( new window.DOMParser() ).parseFromString( text, mime ); | ||
} catch ( e ) { | ||
@@ -77,3 +79,3 @@ xml = undefined; | ||
var normalizeContentType = (contentType = 'application/x-www-form-urlencoded; charset=UTF-8') => { | ||
var normalizeContentType = (contentType) => { | ||
var normalized = shortContentType[contentType]; | ||
@@ -101,3 +103,3 @@ return normalized || contentType; | ||
if (!options.mode) { | ||
options.mode = isCORS(url) ? 'cors' : 'no-cors' | ||
options.mode = isCORS(url) ? 'cors' : 'no-cors'; | ||
} | ||
@@ -107,25 +109,25 @@ | ||
if (rnoContent.test(options.method)) { | ||
var urldata = this.param(data); | ||
if (urldata) { | ||
url = url + (/\?/.test(url) ? '&' : '?') + urldata; | ||
var query = this.param(data); | ||
if (query) { | ||
url = url + (/\?/.test(url) ? '&' : '?') + query; | ||
} | ||
} | ||
// set Content-Type header | ||
if (!rnoContent.test(options.method)) { | ||
headers.set("Content-Type", normalizeContentType(headers.get('Content-Type'))); | ||
} | ||
// grab and delete Content-Type header | ||
// fetch will set Content-Type for common cases | ||
var contentType = normalizeContentType(headers.get('Content-Type')); | ||
headers.delete("Content-Type"); | ||
// set body | ||
if (!rnoContent.test(options.method)) { | ||
// set body | ||
if (typeof data === 'string' | ||
|| (support.formdata && FormData.prototype.isPrototypeOf(data)) | ||
|| (support.blob && Blob.prototype.isPrototypeOf(data)) ) { | ||
if (contentType) { headers.set("Content-Type", contentType); } | ||
options.body = data; | ||
} else { | ||
if (headers.get("Content-Type") === 'application/json') { | ||
options.body = JSON.stringify(data); | ||
} else { | ||
options.body = this.param(data); | ||
} | ||
} else if (contentType === 'application/json') { | ||
headers.set("Content-Type", contentType); | ||
options.body = JSON.stringify(data); | ||
} else if (data) { | ||
options.body = this.param(data); | ||
} | ||
@@ -149,3 +151,3 @@ } | ||
if (!extractor) { | ||
var mimeType = res.headers.get('Content-Type').split(';')[0]; | ||
var mimeType = res.headers.get('Content-Type').split(';').unshift(); | ||
var dataType = mimeType.split(/[\/+]/).pop(); | ||
@@ -152,0 +154,0 @@ |
@@ -46,2 +46,4 @@ (function (global, factory) { | ||
var xml; | ||
var type = res.headers.get('Content-Type'); | ||
var mime = type ? type.split(';').unshift() : 'text/xml'; | ||
var text = res.text(); | ||
@@ -52,3 +54,3 @@ if (window) { | ||
try { | ||
xml = new window.DOMParser().parseFromString(text, 'text/xml'); | ||
xml = new window.DOMParser().parseFromString(text, mime); | ||
} catch (e) { | ||
@@ -109,5 +111,3 @@ xml = undefined; | ||
var normalizeContentType = function normalizeContentType() { | ||
var contentType = arguments[0] === undefined ? 'application/x-www-form-urlencoded; charset=UTF-8' : arguments[0]; | ||
var normalizeContentType = function normalizeContentType(contentType) { | ||
var normalized = shortContentType[contentType]; | ||
@@ -154,23 +154,25 @@ return normalized || contentType; | ||
if (rnoContent.test(options.method)) { | ||
var urldata = this.param(data); | ||
if (urldata) { | ||
url = url + (/\?/.test(url) ? '&' : '?') + urldata; | ||
var query = this.param(data); | ||
if (query) { | ||
url = url + (/\?/.test(url) ? '&' : '?') + query; | ||
} | ||
} | ||
// set Content-Type header | ||
if (!rnoContent.test(options.method)) { | ||
headers.set('Content-Type', normalizeContentType(headers.get('Content-Type'))); | ||
} | ||
// grab and delete Content-Type header | ||
// fetch will set Content-Type for common cases | ||
var contentType = normalizeContentType(headers.get('Content-Type')); | ||
headers['delete']('Content-Type'); | ||
// set body | ||
if (!rnoContent.test(options.method)) { | ||
// set body | ||
if (typeof data === 'string' || support.formdata && FormData.prototype.isPrototypeOf(data) || support.blob && Blob.prototype.isPrototypeOf(data)) { | ||
if (contentType) { | ||
headers.set('Content-Type', contentType); | ||
} | ||
options.body = data; | ||
} else { | ||
if (headers.get('Content-Type') === 'application/json') { | ||
options.body = JSON.stringify(data); | ||
} else { | ||
options.body = this.param(data); | ||
} | ||
} else if (contentType === 'application/json') { | ||
headers.set('Content-Type', contentType); | ||
options.body = JSON.stringify(data); | ||
} else if (data) { | ||
options.body = this.param(data); | ||
} | ||
@@ -194,3 +196,3 @@ } | ||
if (!extractor) { | ||
var mimeType = res.headers.get('Content-Type').split(';')[0]; | ||
var mimeType = res.headers.get('Content-Type').split(';').unshift(); | ||
var dataType = mimeType.split(/[\/+]/).pop(); | ||
@@ -197,0 +199,0 @@ |
{ | ||
"name": "fetch-er", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "WHATWG fetch helper", | ||
@@ -22,9 +22,10 @@ "homepage": "https://github.com/othree/fetcher", | ||
"devDependencies": { | ||
"es6-promise": "*", | ||
"expect": "*", | ||
"istanbul": "*", | ||
"mocha": "*", | ||
"sinon": "*", | ||
"node-fetch": "*", | ||
"should": "*", | ||
"expect": "*", | ||
"istanbul": "*", | ||
"es6-promise": "*", | ||
"node-fetch": "*" | ||
"should-promised": "^0.2.1", | ||
"sinon": "*" | ||
}, | ||
@@ -31,0 +32,0 @@ "scripts": { |
@@ -8,3 +8,3 @@ fetch-er | ||
Developers have to deal with some detail. ex: Post parameter serialize, transform response JSON to JavaScript object. | ||
So here is the fetcher to help you deal with these stuff. Inspired by jQuery.ajax. | ||
So here is the fetch-er to help you deal with these stuff. Inspired by jQuery.ajax. | ||
@@ -11,0 +11,0 @@ Constraint |
122
test/test.js
require('mocha'); | ||
var should = require('should'); | ||
var stream = require('stream'); | ||
var should = require('should-promised'); | ||
var expect = require('expect'); | ||
var sinon = require('sinon'); | ||
var Promise = require('es6-promise').Promise; | ||
var fetcher = require('../'); | ||
global.Promise = require('es6-promise').Promise; | ||
global.fetcher = require('../'); | ||
global.Headers = require('node-fetch/lib/headers'); | ||
global.Response = require('node-fetch/lib/response'); | ||
@@ -40,3 +42,3 @@ | ||
it("POST request parameter", function () { | ||
it("POST request parameter, use 'application/json'", function () { | ||
var callback = sinon.stub().returns(new Promise(function (resolver) { | ||
@@ -58,1 +60,113 @@ resolve('{"bcd": 234}'); | ||
}); | ||
it("POST request parameter, use 'json'", function () { | ||
var callback = sinon.stub().returns(new Promise(function (resolver) { | ||
resolve('{"bcd": 234}'); | ||
})); | ||
global.fetch = once(callback); | ||
fetcher.post('/', {abc: 123}, {headers: {"Content-Type": "json"}}); | ||
callback.called.should.be.true; | ||
var args = callback.getCall(0).args; | ||
var url = args[0]; | ||
url.should.equal('/'); | ||
var body = args[1].body; | ||
body.should.equal('{"abc":123}'); | ||
}); | ||
it("POST request parameter, use json string", function () { | ||
var callback = sinon.stub().returns(new Promise(function (resolver) { | ||
resolve('{"bcd": 234}'); | ||
})); | ||
global.fetch = once(callback); | ||
fetcher.post('/', JSON.stringify({abc: 123}), {headers: {"Content-Type": "application/json"}}); | ||
callback.called.should.be.true; | ||
var args = callback.getCall(0).args; | ||
var url = args[0]; | ||
url.should.equal('/'); | ||
var body = args[1].body; | ||
body.should.equal('{"abc":123}'); | ||
}); | ||
it("POST request without Content-Type", function () { | ||
var callback = sinon.stub().returns(new Promise(function (resolver) { | ||
resolve('{"bcd": 234}'); | ||
})); | ||
global.fetch = once(callback); | ||
fetcher.post('/', {abc: 123, def: 456}); | ||
callback.called.should.be.true; | ||
var args = callback.getCall(0).args; | ||
var url = args[0]; | ||
url.should.equal('/'); | ||
var body = args[1].body; | ||
body.should.equal('abc=123&def=456'); | ||
}); | ||
it("Response JSON with dataType option", function () { | ||
var data = {bcd: 456}; | ||
var body = new stream.PassThrough(); | ||
var res = new Response( | ||
body, | ||
{ | ||
url: '/', | ||
status: '200', | ||
headers: (new Headers()), | ||
size: 12, | ||
timeout: 5000 | ||
} | ||
); | ||
var callback = sinon.stub().returns(Promise.resolve(res)); | ||
global.fetch = once(callback); | ||
var r = fetcher.post('/', {abc: 123, def: 456}, {dataType: 'json'}); | ||
body.end(JSON.stringify(data)); | ||
callback.called.should.be.true; | ||
r.should.be.fulfilledWith([data, res]); | ||
}); | ||
it("Response JSON with Content-Type", function () { | ||
var data = {bcd: 456}; | ||
var body = new stream.PassThrough(); | ||
var res = new Response( | ||
body, | ||
{ | ||
url: '/', | ||
status: '200', | ||
headers: (new Headers({ | ||
"Content-Type": 'application/json' | ||
})), | ||
size: 12, | ||
timeout: 5000 | ||
} | ||
); | ||
var callback = sinon.stub().returns(Promise.resolve(res)); | ||
global.fetch = once(callback); | ||
var r = fetcher.post('/', {abc: 123, def: 456}); | ||
body.end(JSON.stringify(data)); | ||
callback.called.should.be.true; | ||
r.should.be.fulfilledWith([data, res]); | ||
}); | ||
/* | ||
* Cases | ||
* response text | ||
* response form-data encoded string | ||
*/ |
20831
514
8