UNPKG

3.97 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')
18var cookies = require('./lib/cookies')
19var helpers = require('./lib/helpers')
20
21var paramsHaveRequestBody = helpers.paramsHaveRequestBody
22
23// organize params for patch, post, put, head, del
24function initParams (uri, options, callback) {
25 if (typeof options === 'function') {
26 callback = options
27 }
28
29 var params = {}
30 if (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
42function 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
56function 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
66request.get = verbFunc('get')
67request.head = verbFunc('head')
68request.options = verbFunc('options')
69request.post = verbFunc('post')
70request.put = verbFunc('put')
71request.patch = verbFunc('patch')
72request.del = verbFunc('delete')
73request['delete'] = verbFunc('delete')
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 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
104request.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
127request.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
142module.exports = request
143request.Request = require('./request')
144request.initParams = initParams
145
146// Backwards compatibility for request.debug
147Object.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})