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