whatwg-fetch
Advanced tools
Comparing version 0.7.0 to 0.8.0
140
fetch.js
@@ -8,2 +8,19 @@ (function() { | ||
function normalizeName(name) { | ||
if (typeof name !== 'string') { | ||
name = name.toString(); | ||
} | ||
if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) { | ||
throw new TypeError('Invalid character in header field name') | ||
} | ||
return name.toLowerCase() | ||
} | ||
function normalizeValue(value) { | ||
if (typeof value !== 'string') { | ||
value = value.toString(); | ||
} | ||
return value | ||
} | ||
function Headers(headers) { | ||
@@ -28,3 +45,4 @@ this.map = {} | ||
Headers.prototype.append = function(name, value) { | ||
name = name.toLowerCase() | ||
name = normalizeName(name) | ||
value = normalizeValue(value) | ||
var list = this.map[name] | ||
@@ -39,7 +57,7 @@ if (!list) { | ||
Headers.prototype['delete'] = function(name) { | ||
delete this.map[name.toLowerCase()] | ||
delete this.map[normalizeName(name)] | ||
} | ||
Headers.prototype.get = function(name) { | ||
var values = this.map[name.toLowerCase()] | ||
var values = this.map[normalizeName(name)] | ||
return values ? values[0] : null | ||
@@ -49,11 +67,11 @@ } | ||
Headers.prototype.getAll = function(name) { | ||
return this.map[name.toLowerCase()] || [] | ||
return this.map[normalizeName(name)] || [] | ||
} | ||
Headers.prototype.has = function(name) { | ||
return this.map.hasOwnProperty(name.toLowerCase()) | ||
return this.map.hasOwnProperty(normalizeName(name)) | ||
} | ||
Headers.prototype.set = function(name, value) { | ||
this.map[name.toLowerCase()] = [value] | ||
this.map[normalizeName(name)] = [normalizeValue(value)] | ||
} | ||
@@ -114,18 +132,19 @@ | ||
if (support.blob) { | ||
this._initBody = function(body) { | ||
this._bodyInit = body | ||
if (typeof body === 'string') { | ||
this._bodyText = body | ||
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) { | ||
this._bodyBlob = body | ||
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) { | ||
this._bodyFormData = body | ||
} else if (!body) { | ||
this._bodyText = '' | ||
} else { | ||
throw new Error('unsupported BodyInit type') | ||
} | ||
this._initBody = function(body) { | ||
this._bodyInit = body | ||
if (typeof body === 'string') { | ||
this._bodyText = body | ||
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) { | ||
this._bodyBlob = body | ||
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) { | ||
this._bodyFormData = body | ||
} else if (!body) { | ||
this._bodyText = '' | ||
} else { | ||
throw new Error('unsupported BodyInit type') | ||
} | ||
} | ||
if (support.blob) { | ||
this.blob = function() { | ||
@@ -165,15 +184,2 @@ var rejected = consumed(this) | ||
} else { | ||
this._initBody = function(body) { | ||
this._bodyInit = body | ||
if (typeof body === 'string') { | ||
this._bodyText = body | ||
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) { | ||
this._bodyFormData = body | ||
} else if (!body) { | ||
this._bodyText = '' | ||
} else { | ||
throw new Error('unsupported BodyInit type') | ||
} | ||
} | ||
this.text = function() { | ||
@@ -247,8 +253,37 @@ var rejected = consumed(this) | ||
Request.prototype.fetch = function() { | ||
var self = this | ||
Body.call(Request.prototype) | ||
function Response(bodyInit, options) { | ||
if (!options) { | ||
options = {} | ||
} | ||
this._initBody(bodyInit) | ||
this.type = 'default' | ||
this.url = null | ||
this.status = options.status | ||
this.ok = this.status >= 200 && this.status < 300 | ||
this.statusText = options.statusText | ||
this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers) | ||
this.url = options.url || '' | ||
} | ||
Body.call(Response.prototype) | ||
self.Headers = Headers; | ||
self.Request = Request; | ||
self.Response = Response; | ||
self.fetch = function(input, init) { | ||
// TODO: Request constructor should accept input, init | ||
var request | ||
if (Request.prototype.isPrototypeOf(input) && !init) { | ||
request = input | ||
} else { | ||
request = new Request(input, init) | ||
} | ||
return new Promise(function(resolve, reject) { | ||
var xhr = new XMLHttpRequest() | ||
if (self.credentials === 'cors') { | ||
if (request.credentials === 'cors') { | ||
xhr.withCredentials = true; | ||
@@ -290,3 +325,4 @@ } | ||
xhr.open(self.method, self.url, true) | ||
xhr.open(request.method, request.url, true) | ||
if ('responseType' in xhr && support.blob) { | ||
@@ -296,3 +332,3 @@ xhr.responseType = 'blob' | ||
self.headers.forEach(function(name, values) { | ||
request.headers.forEach(function(name, values) { | ||
values.forEach(function(value) { | ||
@@ -303,32 +339,6 @@ xhr.setRequestHeader(name, value) | ||
xhr.send(typeof self._bodyInit === 'undefined' ? null : self._bodyInit) | ||
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit) | ||
}) | ||
} | ||
Body.call(Request.prototype) | ||
function Response(bodyInit, options) { | ||
if (!options) { | ||
options = {} | ||
} | ||
this._initBody(bodyInit) | ||
this.type = 'default' | ||
this.url = null | ||
this.status = options.status | ||
this.statusText = options.statusText | ||
this.headers = options.headers | ||
this.url = options.url || '' | ||
} | ||
Body.call(Response.prototype) | ||
self.Headers = Headers; | ||
self.Request = Request; | ||
self.Response = Response; | ||
self.fetch = function (url, options) { | ||
return new Request(url, options).fetch() | ||
} | ||
self.fetch.polyfill = true | ||
})(); |
{ | ||
"name": "whatwg-fetch", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"main": "fetch.js", | ||
@@ -19,3 +19,7 @@ "repository": "github/fetch", | ||
"phantomjs": "1.9.13" | ||
} | ||
}, | ||
"files" : [ | ||
"LICENSE", | ||
"fetch.js" | ||
] | ||
} |
@@ -27,2 +27,4 @@ # window.fetch polyfill | ||
(For a node.js implementation, try [node-fetch](https://github.com/bitinn/node-fetch)) | ||
## Usage | ||
@@ -119,6 +121,5 @@ | ||
if (response.status >= 200 && response.status < 300) { | ||
return Promise.resolve(response) | ||
} else { | ||
return Promise.reject(new Error(response.statusText)) | ||
return response | ||
} | ||
throw new Error(response.statusText) | ||
} | ||
@@ -125,0 +126,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
161
1
1
13978
4
282