1 // Copyright 2010-2012 Mikeal Rogers
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
17 var extend = require('extend')
18 , cookies = require('./lib/cookies')
19 , helpers = require('./lib/helpers')
21 var isFunction = helpers.isFunction
22 , paramsHaveRequestBody = helpers.paramsHaveRequestBody
25 // organize params for patch, post, put, head, del
26 function initParams(uri, options, callback) {
27 if (typeof options === 'function') {
32 if (typeof options === 'object') {
33 extend(params, options, {uri: uri})
34 } else if (typeof uri === 'string') {
35 extend(params, {uri: uri})
40 params.callback = callback || params.callback
44 function request (uri, options, callback) {
45 if (typeof uri === 'undefined') {
46 throw new Error('undefined is not a valid uri or options object.')
49 var params = initParams(uri, options, callback)
51 if (params.method === 'HEAD' && paramsHaveRequestBody(params)) {
52 throw new Error('HTTP HEAD requests MUST NOT include a request body.')
55 return new request.Request(params)
58 function verbFunc (verb) {
59 var method = verb.toUpperCase()
60 return function (uri, options, callback) {
61 var params = initParams(uri, options, callback)
62 params.method = method
63 return request(params, params.callback)
67 // define like this to please codeintel/intellisense IDEs
68 request.get = verbFunc('get')
69 request.head = verbFunc('head')
70 request.post = verbFunc('post')
71 request.put = verbFunc('put')
72 request.patch = verbFunc('patch')
73 request.del = verbFunc('delete')
74 request['delete'] = verbFunc('delete')
76 request.jar = function (store) {
77 return cookies.jar(store)
80 request.cookie = function (str) {
81 return cookies.parse(str)
84 function wrapRequestMethod (method, options, requester, verb) {
86 return function (uri, opts, callback) {
87 var params = initParams(uri, opts, callback)
90 extend(true, target, options, params)
92 target.pool = params.pool || options.pool
95 target.method = verb.toUpperCase()
98 if (isFunction(requester)) {
102 return method(target, target.callback)
106 request.defaults = function (options, requester) {
109 options = options || {}
111 if (typeof options === 'function') {
116 var defaults = wrapRequestMethod(self, options, requester)
118 var verbs = ['get', 'head', 'post', 'put', 'patch', 'del', 'delete']
119 verbs.forEach(function(verb) {
120 defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb)
123 defaults.cookie = wrapRequestMethod(self.cookie, options, requester)
124 defaults.jar = self.jar
125 defaults.defaults = self.defaults
129 request.forever = function (agentOptions, optionsArg) {
132 extend(options, optionsArg)
135 options.agentOptions = agentOptions
138 options.forever = true
139 return request.defaults(options)
144 module.exports = request
145 request.Request = require('./request')
146 request.initParams = initParams
148 // Backwards compatibility for request.debug
149 Object.defineProperty(request, 'debug', {
152 return request.Request.debug
154 set : function(debug) {
155 request.Request.debug = debug