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 paramsHaveRequestBody = helpers.paramsHaveRequestBody
24 // organize params for patch, post, put, head, del
25 function initParams(uri, options, callback) {
26 if (typeof options === 'function') {
31 if (typeof options === 'object') {
32 extend(params, options, {uri: uri})
33 } else if (typeof uri === 'string') {
34 extend(params, {uri: uri})
39 params.callback = callback || params.callback
43 function request (uri, options, callback) {
44 if (typeof uri === 'undefined') {
45 throw new Error('undefined is not a valid uri or options object.')
48 var params = initParams(uri, options, callback)
50 if (params.method === 'HEAD' && paramsHaveRequestBody(params)) {
51 throw new Error('HTTP HEAD requests MUST NOT include a request body.')
54 return new request.Request(params)
57 function verbFunc (verb) {
58 var method = verb.toUpperCase()
59 return function (uri, options, callback) {
60 var params = initParams(uri, options, callback)
61 params.method = method
62 return request(params, params.callback)
66 // define like this to please codeintel/intellisense IDEs
67 request.get = verbFunc('get')
68 request.head = verbFunc('head')
69 request.post = verbFunc('post')
70 request.put = verbFunc('put')
71 request.patch = verbFunc('patch')
72 request.del = verbFunc('delete')
73 request['delete'] = verbFunc('delete')
75 request.jar = function (store) {
76 return cookies.jar(store)
79 request.cookie = function (str) {
80 return cookies.parse(str)
83 function wrapRequestMethod (method, options, requester, verb) {
85 return function (uri, opts, callback) {
86 var params = initParams(uri, opts, callback)
89 extend(true, target, options, params)
91 target.pool = params.pool || options.pool
94 target.method = verb.toUpperCase()
97 if (typeof requester === 'function') {
101 return method(target, target.callback)
105 request.defaults = function (options, requester) {
108 options = options || {}
110 if (typeof options === 'function') {
115 var defaults = wrapRequestMethod(self, options, requester)
117 var verbs = ['get', 'head', 'post', 'put', 'patch', 'del', 'delete']
118 verbs.forEach(function(verb) {
119 defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb)
122 defaults.cookie = wrapRequestMethod(self.cookie, options, requester)
123 defaults.jar = self.jar
124 defaults.defaults = self.defaults
128 request.forever = function (agentOptions, optionsArg) {
131 extend(options, optionsArg)
134 options.agentOptions = agentOptions
137 options.forever = true
138 return request.defaults(options)
143 module.exports = request
144 request.Request = require('./request')
145 request.initParams = initParams
147 // Backwards compatibility for request.debug
148 Object.defineProperty(request, 'debug', {
151 return request.Request.debug
153 set : function(debug) {
154 request.Request.debug = debug