node-fetch
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -8,4 +8,8 @@ | ||
## v1.2.1 (master) | ||
## v1.3.0 (master) | ||
- Enhance: now fetch.Request is exposed as well. | ||
## v1.2.1 | ||
- Enhance: `Headers` now normalized `Number` value to `String`, prevent common mistakes | ||
@@ -12,0 +16,0 @@ |
60
index.js
@@ -17,2 +17,3 @@ | ||
var Headers = require('./lib/headers'); | ||
var Request = require('./lib/request'); | ||
@@ -24,3 +25,3 @@ module.exports = Fetch; | ||
* | ||
* @param String url Absolute url | ||
* @param Mixed url Absolute url or Request instance | ||
* @param Object opts Fetch options | ||
@@ -46,40 +47,18 @@ * @return Promise | ||
return new Fetch.Promise(function(resolve, reject) { | ||
var uri = parse_url(url); | ||
if (!uri.protocol || !uri.hostname) { | ||
reject(new Error('only absolute urls are supported')); | ||
// build request object | ||
var options; | ||
try { | ||
options = new Request(url, opts); | ||
} catch (err) { | ||
reject(err); | ||
return; | ||
} | ||
if (uri.protocol !== 'http:' && uri.protocol !== 'https:') { | ||
reject(new Error('only http(s) protocols are supported')); | ||
return; | ||
} | ||
var request; | ||
if (uri.protocol === 'https:') { | ||
request = https.request; | ||
var send; | ||
if (options.protocol === 'https:') { | ||
send = https.request; | ||
} else { | ||
request = http.request; | ||
send = http.request; | ||
} | ||
opts = opts || {}; | ||
// avoid side-effect on input options | ||
var options = { | ||
hostname: uri.hostname | ||
, port: uri.port | ||
, path: uri.path | ||
, auth: uri.auth | ||
, method: opts.method || 'GET' | ||
, headers: opts.headers || {} | ||
, follow: opts.follow !== undefined ? opts.follow : 20 | ||
, counter: opts.counter || 0 | ||
, timeout: opts.timeout || 0 | ||
, compress: opts.compress !== false | ||
, size: opts.size || 0 | ||
, body: opts.body | ||
, agent: opts.agent | ||
}; | ||
// normalize headers | ||
@@ -107,3 +86,3 @@ var headers = new Headers(options.headers); | ||
// send request | ||
var req = request(options); | ||
var req = send(options); | ||
var reqTimeout; | ||
@@ -115,3 +94,3 @@ | ||
req.abort(); | ||
reject(new Error('network timeout at: ' + uri.href)); | ||
reject(new Error('network timeout at: ' + options.url)); | ||
}, options.timeout); | ||
@@ -123,3 +102,3 @@ }); | ||
clearTimeout(reqTimeout); | ||
reject(new Error('request to ' + uri.href + ' failed, reason: ' + err.message)); | ||
reject(new Error('request to ' + options.url + ' failed, reason: ' + err.message)); | ||
}); | ||
@@ -133,3 +112,3 @@ | ||
if (options.counter >= options.follow) { | ||
reject(new Error('maximum redirect reached at: ' + uri.href)); | ||
reject(new Error('maximum redirect reached at: ' + options.url)); | ||
return; | ||
@@ -139,3 +118,3 @@ } | ||
if (!res.headers.location) { | ||
reject(new Error('redirect location header missing at: ' + uri.href)); | ||
reject(new Error('redirect location header missing at: ' + options.url)); | ||
return; | ||
@@ -146,3 +125,3 @@ } | ||
resolve(Fetch(resolve_url(uri.href, res.headers.location), options)); | ||
resolve(Fetch(resolve_url(options.url, res.headers.location), options)); | ||
return; | ||
@@ -167,3 +146,3 @@ } | ||
var output = new Response(body, { | ||
url: uri.href | ||
url: options.url | ||
, status: res.statusCode | ||
@@ -205,1 +184,2 @@ , headers: headers | ||
Fetch.Headers = Headers; | ||
Fetch.Request = Request; |
@@ -19,4 +19,2 @@ | ||
- Only expose `Response` and `Headers` constructors at the moment, we don't see a good use-case for `Request` interface yet. | ||
- There is currently no built-in caching, as server-side caching varies by use-cases. |
{ | ||
"name": "node-fetch", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "A light-weight module that brings window.fetch to node.js and io.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,2 +19,3 @@ | ||
var Response = require('../lib/response.js'); | ||
var Request = require('../lib/request.js'); | ||
// test with native promise on node 0.11, and bluebird for node 0.10 | ||
@@ -63,5 +64,6 @@ fetch.Promise = fetch.Promise || bluebird; | ||
it('should expose Headers and Response constructors', function() { | ||
it('should expose Headers, Response and Request constructors', function() { | ||
expect(fetch.Headers).to.equal(Headers); | ||
expect(fetch.Response).to.equal(Response); | ||
expect(fetch.Request).to.equal(Request); | ||
}); | ||
@@ -479,3 +481,2 @@ | ||
method: 'HEAD' | ||
}; | ||
@@ -699,2 +700,48 @@ return fetch(url, opts).then(function(res) { | ||
it('should support fetch with Request instance', function() { | ||
url = base + '/hello'; | ||
var req = new Request(url); | ||
return fetch(req).then(function(res) { | ||
expect(res.url).to.equal(url); | ||
expect(res.ok).to.be.true; | ||
expect(res.status).to.equal(200); | ||
}); | ||
}); | ||
it('should support wrapping Request instance', function() { | ||
url = base + '/hello'; | ||
var r1 = new Request(url, { | ||
method: 'POST' | ||
, follow: 1 | ||
}); | ||
var r2 = new Request(r1, { | ||
follow: 2 | ||
}) | ||
expect(r2.url).to.equal(url); | ||
expect(r2.method).to.equal('POST'); | ||
expect(r1.follow).to.equal(1); | ||
expect(r2.follow).to.equal(2); | ||
}); | ||
it('should support overwrite Request instance', function() { | ||
url = base + '/inspect'; | ||
var req = new Request(url, { | ||
method: 'POST' | ||
, headers: { | ||
a: '1' | ||
} | ||
}); | ||
return fetch(req, { | ||
method: 'GET' | ||
, headers: { | ||
a: '2' | ||
} | ||
}).then(function(res) { | ||
return res.json(); | ||
}).then(function(body) { | ||
expect(body.method).to.equal('GET'); | ||
expect(body.headers.a).to.equal('2'); | ||
}); | ||
}); | ||
it('should support https request', function() { | ||
@@ -711,3 +758,2 @@ this.timeout(5000); | ||
}); | ||
}); |
47620
13
1321
75