forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·153 lines (121 loc) · 3.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2010-2012 Mikeal Rogers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict'
var extend = require('util')._extend
, cookies = require('./lib/cookies')
, helpers = require('./lib/helpers')
var isFunction = helpers.isFunction
, paramsHaveRequestBody = helpers.paramsHaveRequestBody
// organize params for patch, post, put, head, del
function initParams(uri, options, callback) {
if (typeof options === 'function') {
callback = options
}
var params = {}
if (typeof options === 'object') {
params = extend({}, options)
params = extend(params, {uri: uri})
} else if (typeof uri === 'string') {
params = extend({}, {uri: uri})
} else {
params = extend({}, uri)
}
params.callback = callback
return params
}
function request (uri, options, callback) {
if (typeof uri === 'undefined') {
throw new Error('undefined is not a valid uri or options object.')
}
var params = initParams(uri, options, callback)
if (params.method === 'HEAD' && paramsHaveRequestBody(params)) {
throw new Error('HTTP HEAD requests MUST NOT include a request body.')
}
return new request.Request(params)
}
var verbs = ['get', 'head', 'post', 'put', 'patch', 'del']
verbs.forEach(function(verb) {
var method = verb === 'del' ? 'DELETE' : verb.toUpperCase()
request[verb] = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.method = method
return request(params, params.callback)
}
})
request.jar = function (store) {
return cookies.jar(store)
}
request.cookie = function (str) {
return cookies.parse(str)
}
function wrapRequestMethod (method, options, requester, verb) {
return function (uri, opts, callback) {
var params = initParams(uri, opts, callback)
var headerlessOptions = extend({}, options)
delete headerlessOptions.headers
params = extend(headerlessOptions, params)
if (options.headers) {
var headers = extend({}, options.headers)
params.headers = extend(headers, params.headers)
}
if (verb) {
params.method = (verb === 'del' ? 'DELETE' : verb.toUpperCase())
}
if (isFunction(requester)) {
method = requester
}
return method(params, params.callback)
}
}
request.defaults = function (options, requester) {
var self = this
if (typeof options === 'function') {
requester = options
options = {}
}
var defaults = wrapRequestMethod(self, options, requester)
var verbs = ['get', 'head', 'post', 'put', 'patch', 'del']
verbs.forEach(function(verb) {
defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb)
})
defaults.cookie = wrapRequestMethod(self.cookie, options, requester)
defaults.jar = self.jar
defaults.defaults = self.defaults
return defaults
}
request.forever = function (agentOptions, optionsArg) {
var options = {}
if (optionsArg) {
options = extend({}, optionsArg)
}
if (agentOptions) {
options.agentOptions = agentOptions
}
options.forever = true
return request.defaults(options)
}
// Exports
module.exports = request
request.Request = require('./request')
request.initParams = initParams
// Backwards compatibility for request.debug
Object.defineProperty(request, 'debug', {
enumerable : true,
get : function() {
return request.Request.debug
},
set : function(debug) {
request.Request.debug = debug
}
})