node/deps/npm/node_modules/request/index.js

187 lines
5.3 KiB
JavaScript
Raw Normal View History

// Copyright 2010-2012 Mikeal Rogers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-11-05 07:08:12 +08:00
'use strict'
2014-09-17 06:38:50 +08:00
var extend = require('util')._extend
, cookies = require('./lib/cookies')
, helpers = require('./lib/helpers')
2014-11-05 07:08:12 +08:00
var isFunction = helpers.isFunction
2014-09-17 06:38:50 +08:00
, constructObject = helpers.constructObject
, filterForCallback = helpers.filterForCallback
, constructOptionsFrom = helpers.constructOptionsFrom
, paramsHaveRequestBody = helpers.paramsHaveRequestBody
2014-11-05 07:08:12 +08:00
2013-04-12 00:16:47 +08:00
// organize params for patch, post, put, head, del
function initParams(uri, options, callback) {
2014-09-17 06:38:50 +08:00
callback = filterForCallback([options, callback])
options = constructOptionsFrom(uri, options)
return constructObject()
.extend({callback: callback})
.extend({options: options})
.extend({uri: options.uri})
.done()
}
function request (uri, options, callback) {
2014-11-05 07:08:12 +08:00
if (typeof uri === 'undefined') {
2014-09-17 06:38:50 +08:00
throw new Error('undefined is not a valid uri or options object.')
2014-11-05 07:08:12 +08:00
}
2014-09-17 06:38:50 +08:00
var params = initParams(uri, options, callback)
options = params.options
options.callback = params.callback
options.uri = params.uri
2013-04-12 00:16:47 +08:00
2014-09-17 06:38:50 +08:00
return new request.Request(options)
}
2013-12-12 02:20:26 +08:00
function requester(params) {
2014-11-05 07:08:12 +08:00
if(typeof params.options._requester === 'function') {
2013-12-12 02:20:26 +08:00
return params.options._requester
2014-11-05 07:08:12 +08:00
}
2014-09-17 06:38:50 +08:00
return request
2013-12-12 02:20:26 +08:00
}
2014-09-17 06:38:50 +08:00
request.get = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'GET'
return requester(params)(params.uri || null, params.options, params.callback)
}
request.head = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'HEAD'
2014-11-05 07:08:12 +08:00
if (paramsHaveRequestBody(params)) {
throw new Error('HTTP HEAD requests MUST NOT include a request body.')
}
2014-09-17 06:38:50 +08:00
return requester(params)(params.uri || null, params.options, params.callback)
2012-01-17 07:06:16 +08:00
}
request.post = function (uri, options, callback) {
2013-04-12 00:16:47 +08:00
var params = initParams(uri, options, callback)
params.options.method = 'POST'
2013-12-12 02:20:26 +08:00
return requester(params)(params.uri || null, params.options, params.callback)
}
2014-09-17 06:38:50 +08:00
request.put = function (uri, options, callback) {
2013-04-12 00:16:47 +08:00
var params = initParams(uri, options, callback)
params.options.method = 'PUT'
2013-12-12 02:20:26 +08:00
return requester(params)(params.uri || null, params.options, params.callback)
}
2014-09-17 06:38:50 +08:00
2013-04-12 00:16:47 +08:00
request.patch = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'PATCH'
2013-12-12 02:20:26 +08:00
return requester(params)(params.uri || null, params.options, params.callback)
2013-04-12 00:16:47 +08:00
}
2013-12-12 02:20:26 +08:00
request.del = function (uri, options, callback) {
2013-04-12 00:16:47 +08:00
var params = initParams(uri, options, callback)
params.options.method = 'DELETE'
2013-12-12 02:20:26 +08:00
return requester(params)(params.uri || null, params.options, params.callback)
}
2014-09-17 06:38:50 +08:00
request.jar = function (store) {
return cookies.jar(store)
2011-11-23 08:56:59 +08:00
}
2014-09-17 06:38:50 +08:00
2011-11-23 08:56:59 +08:00
request.cookie = function (str) {
2014-09-17 06:38:50 +08:00
return cookies.parse(str)
}
request.defaults = function (options, requester) {
2014-11-05 07:08:12 +08:00
var self = this
2014-09-17 06:38:50 +08:00
var wrap = function (method) {
var headerlessOptions = function (options) {
options = extend({}, options)
delete options.headers
return options
}
var getHeaders = function (params, options) {
return constructObject()
.extend(options.headers)
.extend(params.options.headers)
.done()
}
return function (uri, opts, callback) {
var params = initParams(uri, opts, callback)
2014-11-05 07:08:12 +08:00
params.options = extend(headerlessOptions(options), params.options)
2014-09-17 06:38:50 +08:00
2014-11-05 07:08:12 +08:00
if (options.headers) {
2014-09-17 06:38:50 +08:00
params.options.headers = getHeaders(params, options)
2014-11-05 07:08:12 +08:00
}
2014-09-17 06:38:50 +08:00
if (isFunction(requester)) {
2014-11-05 07:08:12 +08:00
if (method === self) {
2014-09-17 06:38:50 +08:00
method = requester
} else {
params.options._requester = requester
}
}
return method(params.options, params.callback)
}
}
2014-11-05 07:08:12 +08:00
var defaults = wrap(self)
defaults.get = wrap(self.get)
defaults.patch = wrap(self.patch)
defaults.post = wrap(self.post)
defaults.put = wrap(self.put)
defaults.head = wrap(self.head)
defaults.del = wrap(self.del)
defaults.cookie = wrap(self.cookie)
defaults.jar = self.jar
defaults.defaults = self.defaults
2014-09-17 06:38:50 +08:00
return defaults
}
request.forever = function (agentOptions, optionsArg) {
var options = constructObject()
2014-11-05 07:08:12 +08:00
if (optionsArg) {
options.extend(optionsArg)
}
if (agentOptions) {
options.agentOptions = agentOptions
}
2014-09-17 06:38:50 +08:00
options.extend({forever: true})
return request.defaults(options.done())
2011-11-23 08:56:59 +08:00
}
2014-09-17 06:38:50 +08:00
// Exports
module.exports = request
request.Request = require('./request')
request.initParams = initParams
// Backwards compatibility for request.debug
Object.defineProperty(request, 'debug', {
enumerable : true,
get : function() {
return request.Request.debug
},
set : function(debug) {
request.Request.debug = debug
}
})