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