-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.promise.js
144 lines (135 loc) · 4.33 KB
/
http.promise.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
'use strict';
var Promise = require('bluebird'),
onPossiblyUnhandledRejection = Promise.onPossiblyUnhandledRejection.bind(Promise),
onUnhandledRejectionHandled = Promise.onUnhandledRejectionHandled.bind(Promise),
longStackTraces = Promise.longStackTraces.bind(Promise),
HTTP_METHODS = {
post : 'POST',
put : 'PUT',
patch : 'PATCH',
head : 'HEAD',
del : 'DELETE',
get : 'GET'
},
reqMethods = [
'get', 'head', 'post', 'put',
'patch', 'del', 'jar', 'cookie'
],
defaultOptions = {
error: true,
method: 'GET'
};
module.exports = (function wrapRequest(request, defaultOpts){
reqMethods.forEach(function (method){
HTTP[method] = HTTP_METHODS.hasOwnProperty(method)
? wrapMethod(HTTP_METHODS[method])
: request[method].bind(request);
});
HTTP.defaults = setDefaults;
HTTP.onPossiblyUnhandledRejection = onPossiblyUnhandledRejection;
HTTP.onUnhandledRejectionHandled = onUnhandledRejectionHandled;
HTTP.longStackTraces = longStackTraces;
Object.defineProperties(HTTP, {
error: {
value: require('./http.error'),
enumerable: false,
configurable: false,
writable: false
},
request: {
value: request,
enumerable: false,
configurable: false,
writable: false
},
debug: {
get: function() { return request.debug },
set: function(v){ return request.debug = v },
enumerable: true
}
});
function HTTP(options, extra){
var opts = setOptions(options, extra);
opts.method = opts.method.toUpperCase();
return new Promise(function HTTP_PROMISE(resolve, reject) {
request(opts, function HTTP_RESPONSE(error, response, body) {
if (error) {
error.options = opts;
error.statusCode = 0;
error.title = 'Invalid Request';
error.summary = 'failed to perform HTTP request';
reject(error);
} else if (opts.error && (response.statusCode === 0 || response.statusCode >= 400)) {
var statusCode = response.statusCode;
var HTTPErr = HTTP.error.hasOwnProperty(statusCode)
? HTTP.error[statusCode]
: statusCode < 500
? HTTP.error.client
: statusCode < 600
? HTTP.error.server
: HTTP.error;
var httpErr = new HTTPErr;
httpErr.body = body;
httpErr.options = opts;
httpErr.response = response;
httpErr.statusCode = statusCode;
Object.defineProperty(httpErr, 'response', {
value: response,
enumerable: false,
configurable: false,
writable: false
});
reject(httpErr);
} else {
if (typeof opts.transform === 'function') {
body = opts.transform(response, body);
}
if (!opts.resolve) {
resolve([response, body]);
} else if (opts.resolve === 'body') {
resolve(body);
} else if (opts.resolve === 'response') {
resolve(response);
} else if (Array.isArray(opts.resolve)
&& opts.resolve.length === 2
&& opts.resolve[0] === 'body'
&& opts.resolve[1] === 'response') {
resolve([body, response]);
} else {
resolve([response, body]);
}
}
});
});
}
function wrapMethod(method){
return function HTTP_METHOD(options, extra){
var opts = setOptions(options, extra, method);
return HTTP(opts);
}
}
function setDefaults(defaults){
var current = assign({}, defaultOpts);
return wrapRequest(
request.defaults(defaults),
assign(current, defaults)
);
}
function setOptions(options, extra, method){
var opts = assign(assign({}, defaultOpts), options);
if (typeof options === 'string') {
opts = assign(opts, extra);
opts.uri = options;
}
opts.method = method || opts.method;
return opts;
}
return HTTP;
})(require('request'), defaultOptions);
function assign(target, extension){ /*eslint-disable curly*/
if (typeof extension === 'object' && extension !== null)
for (var k in extension)
if (extension.hasOwnProperty(k))
target[k] = extension[k];
return target;
}