node-fetch/lib/request.js

76 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-04-05 10:43:12 -07:00
2015-06-03 21:05:01 -07:00
/**
* request.js
*
* Request class contains server only options
*/
var parse_url = require('url').parse;
var Headers = require('./headers');
var Body = require('./body');
2015-06-03 21:05:01 -07:00
module.exports = Request;
/**
* Request class
*
* @param Mixed input Url or Request instance
* @param Object init Custom options
* @return Void
*/
function Request(input, init) {
var url, url_parsed;
// normalize input
if (!(input instanceof Request)) {
url = input;
url_parsed = parse_url(url);
input = {};
} else {
url = input.url;
url_parsed = parse_url(url);
}
// normalize init
init = init || {};
// fetch spec options
this.method = init.method || input.method || 'GET';
2016-04-05 11:47:23 -07:00
this.redirect = init.redirect || input.redirect || 'follow';
this.headers = new Headers(init.headers || input.headers || {});
2015-06-03 21:05:01 -07:00
this.url = url;
2015-06-03 21:40:01 -07:00
// server only options
this.follow = init.follow !== undefined ?
init.follow : input.follow !== undefined ?
input.follow : 20;
this.compress = init.compress !== undefined ?
init.compress : input.compress !== undefined ?
input.compress : true;
2016-03-19 03:06:33 -07:00
this.counter = init.counter || input.counter || input.follow || 0;
this.agent = init.agent || input.agent;
2015-06-03 21:40:01 -07:00
2016-03-19 03:06:33 -07:00
Body.call(this, init.body || this._clone(input), {
timeout: init.timeout || input.timeout || 0,
size: init.size || input.size || 0
});
2015-06-03 21:40:01 -07:00
// server request options
2015-06-03 21:05:01 -07:00
this.protocol = url_parsed.protocol;
this.hostname = url_parsed.hostname;
this.port = url_parsed.port;
this.path = url_parsed.path;
this.auth = url_parsed.auth;
}
Request.prototype = Object.create(Body.prototype);
2016-03-19 03:06:33 -07:00
/**
* Clone this request
*
* @return Request
*/
Request.prototype.clone = function() {
return new Request(this);
};