|
| 1 | +// Copyright 2010-2012 Mikeal Rogers |
| 2 | +// |
| 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 |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 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. |
| 14 | + |
| 15 | +var extend = require('extend') |
| 16 | +var cookies = require('./lib/cookies') |
| 17 | +var helpers = require('./lib/helpers') |
| 18 | + |
| 19 | +var paramsHaveRequestBody = helpers.paramsHaveRequestBody |
| 20 | + |
| 21 | +// organize params for patch, post, put, head, del |
| 22 | +function initParams(uri, options, callback) { |
| 23 | + if (typeof options === 'function') { |
| 24 | + callback = options |
| 25 | + } |
| 26 | + |
| 27 | + var params = {} |
| 28 | + if (options !== null && typeof options === 'object') { |
| 29 | + extend(params, options, { uri: uri }) |
| 30 | + } else if (typeof uri === 'string') { |
| 31 | + extend(params, { uri: uri }) |
| 32 | + } else { |
| 33 | + extend(params, uri) |
| 34 | + } |
| 35 | + |
| 36 | + params.callback = callback || params.callback |
| 37 | + return params |
| 38 | +} |
| 39 | + |
| 40 | +function request(uri, options, callback) { |
| 41 | + if (typeof uri === 'undefined') { |
| 42 | + throw new Error('undefined is not a valid uri or options object.') |
| 43 | + } |
| 44 | + |
| 45 | + var params = initParams(uri, options, callback) |
| 46 | + |
| 47 | + if (params.method === 'HEAD' && paramsHaveRequestBody(params)) { |
| 48 | + throw new Error('HTTP HEAD requests MUST NOT include a request body.') |
| 49 | + } |
| 50 | + |
| 51 | + return new request.Request(params) |
| 52 | +} |
| 53 | + |
| 54 | +function verbFunc(verb) { |
| 55 | + var method = verb.toUpperCase() |
| 56 | + return function(uri, options, callback) { |
| 57 | + var params = initParams(uri, options, callback) |
| 58 | + params.method = method |
| 59 | + return request(params, params.callback) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// define like this to please codeintel/intellisense IDEs |
| 64 | +request.get = verbFunc('get') |
| 65 | +request.head = verbFunc('head') |
| 66 | +request.options = verbFunc('options') |
| 67 | +request.post = verbFunc('post') |
| 68 | +request.put = verbFunc('put') |
| 69 | +request.patch = verbFunc('patch') |
| 70 | +request.del = verbFunc('delete') |
| 71 | +request['delete'] = verbFunc('delete') |
| 72 | + |
| 73 | +request.jar = function(store) { |
| 74 | + return cookies.jar(store) |
| 75 | +} |
| 76 | + |
| 77 | +request.cookie = function(str) { |
| 78 | + return cookies.parse(str) |
| 79 | +} |
| 80 | + |
| 81 | +function wrapRequestMethod(method, options, requester, verb) { |
| 82 | + return function(uri, opts, callback) { |
| 83 | + var params = initParams(uri, opts, callback) |
| 84 | + |
| 85 | + var target = {} |
| 86 | + extend(true, target, options, params) |
| 87 | + |
| 88 | + target.pool = params.pool || options.pool |
| 89 | + |
| 90 | + if (verb) { |
| 91 | + target.method = verb.toUpperCase() |
| 92 | + } |
| 93 | + |
| 94 | + if (typeof requester === 'function') { |
| 95 | + method = requester |
| 96 | + } |
| 97 | + |
| 98 | + return method(target, target.callback) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +request.defaults = function(options, requester) { |
| 103 | + var self = this |
| 104 | + |
| 105 | + options = options || {} |
| 106 | + |
| 107 | + if (typeof options === 'function') { |
| 108 | + requester = options |
| 109 | + options = {} |
| 110 | + } |
| 111 | + |
| 112 | + var defaults = wrapRequestMethod(self, options, requester) |
| 113 | + |
| 114 | + var verbs = ['get', 'head', 'post', 'put', 'patch', 'del', 'delete'] |
| 115 | + verbs.forEach(function(verb) { |
| 116 | + defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb) |
| 117 | + }) |
| 118 | + |
| 119 | + defaults.cookie = wrapRequestMethod(self.cookie, options, requester) |
| 120 | + defaults.jar = self.jar |
| 121 | + defaults.defaults = self.defaults |
| 122 | + return defaults |
| 123 | +} |
| 124 | + |
| 125 | +request.forever = function(agentOptions, optionsArg) { |
| 126 | + var options = {} |
| 127 | + if (optionsArg) { |
| 128 | + extend(options, optionsArg) |
| 129 | + } |
| 130 | + if (agentOptions) { |
| 131 | + options.agentOptions = agentOptions |
| 132 | + } |
| 133 | + |
| 134 | + options.forever = true |
| 135 | + return request.defaults(options) |
| 136 | +} |
| 137 | + |
| 138 | +// Exports |
| 139 | + |
| 140 | +module.exports = request |
| 141 | +request.Request = require('./request') |
| 142 | +request.initParams = initParams |
| 143 | + |
| 144 | +// Backwards compatibility for request.debug |
| 145 | +Object.defineProperty(request, 'debug', { |
| 146 | + enumerable: true, |
| 147 | + get: function() { |
| 148 | + return request.Request.debug |
| 149 | + }, |
| 150 | + set: function(debug) { |
| 151 | + request.Request.debug = debug |
| 152 | + } |
| 153 | +}) |
0 commit comments