UNPKG

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