UNPKG

4.02 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
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 === 'del' ? 'DELETE' : 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('del')
74
75request.jar = function (store) {
76 return cookies.jar(store)
77}
78
79request.cookie = function (str) {
80 return cookies.parse(str)
81}
82
83function 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 === 'del' ? 'DELETE' : verb.toUpperCase())
95 }
96
97 if (isFunction(requester)) {
98 method = requester
99 }
100
101 return method(target, target.callback)
102 }
103}
104
105request.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']
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
128request.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
143module.exports = request
144request.Request = require('./request')
145request.initParams = initParams
146
147// Backwards compatibility for request.debug
148Object.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})