plumbing.js
5.68 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
var errors = require('./errors.js'),
isFunction = require('lodash/isFunction'),
isObjectLike = require('lodash/isObjectLike'),
isString = require('lodash/isString'),
isUndefined = require('lodash/isUndefined');
module.exports = function (options) {
var errorText = 'Please verify options'; // For better minification because this string is repeating
if (!isObjectLike(options)) {
throw new TypeError(errorText);
}
if (!isFunction(options.PromiseImpl)) {
throw new TypeError(errorText + '.PromiseImpl');
}
if (!isUndefined(options.constructorMixin) && !isFunction(options.constructorMixin)) {
throw new TypeError(errorText + '.PromiseImpl');
}
var PromiseImpl = options.PromiseImpl;
var constructorMixin = options.constructorMixin;
var plumbing = {};
plumbing.init = function (requestOptions) {
var self = this;
self._rp_promise = new PromiseImpl(function (resolve, reject) {
self._rp_resolve = resolve;
self._rp_reject = reject;
if (constructorMixin) {
constructorMixin.apply(self, arguments); // Using arguments since specific Promise libraries may pass additional parameters
}
});
self._rp_callbackOrig = requestOptions.callback;
requestOptions.callback = self.callback = function RP$callback(err, response, body) {
plumbing.callback.call(self, err, response, body);
};
if (isString(requestOptions.method)) {
requestOptions.method = requestOptions.method.toUpperCase();
}
requestOptions.transform = requestOptions.transform || plumbing.defaultTransformations[requestOptions.method];
self._rp_options = requestOptions;
self._rp_options.simple = requestOptions.simple !== false;
self._rp_options.resolveWithFullResponse = requestOptions.resolveWithFullResponse === true;
self._rp_options.transform2xxOnly = requestOptions.transform2xxOnly === true;
};
plumbing.defaultTransformations = {
HEAD: function (body, response, resolveWithFullResponse) {
return resolveWithFullResponse ? response : response.headers;
}
};
plumbing.callback = function (err, response, body) {
var self = this;
var origCallbackThrewException = false, thrownException = null;
if (isFunction(self._rp_callbackOrig)) {
try {
self._rp_callbackOrig.apply(self, arguments); // TODO: Apply to self mimics behavior of request@2. Is that also right for request@next?
} catch (e) {
origCallbackThrewException = true;
thrownException = e;
}
}
var is2xx = !err && /^2/.test('' + response.statusCode);
if (err) {
self._rp_reject(new errors.RequestError(err, self._rp_options, response));
} else if (self._rp_options.simple && !is2xx) {
if (isFunction(self._rp_options.transform) && self._rp_options.transform2xxOnly === false) {
(new PromiseImpl(function (resolve) {
resolve(self._rp_options.transform(body, response, self._rp_options.resolveWithFullResponse)); // transform may return a Promise
}))
.then(function (transformedResponse) {
self._rp_reject(new errors.StatusCodeError(response.statusCode, body, self._rp_options, transformedResponse));
})
.catch(function (transformErr) {
self._rp_reject(new errors.TransformError(transformErr, self._rp_options, response));
});
} else {
self._rp_reject(new errors.StatusCodeError(response.statusCode, body, self._rp_options, response));
}
} else {
if (isFunction(self._rp_options.transform) && (is2xx || self._rp_options.transform2xxOnly === false)) {
(new PromiseImpl(function (resolve) {
resolve(self._rp_options.transform(body, response, self._rp_options.resolveWithFullResponse)); // transform may return a Promise
}))
.then(function (transformedResponse) {
self._rp_resolve(transformedResponse);
})
.catch(function (transformErr) {
self._rp_reject(new errors.TransformError(transformErr, self._rp_options, response));
});
} else if (self._rp_options.resolveWithFullResponse) {
self._rp_resolve(response);
} else {
self._rp_resolve(body);
}
}
if (origCallbackThrewException) {
throw thrownException;
}
};
plumbing.exposePromiseMethod = function (exposeTo, bindTo, promisePropertyKey, methodToExpose, exposeAs) {
exposeAs = exposeAs || methodToExpose;
if (exposeAs in exposeTo) {
throw new Error('Unable to expose method "' + exposeAs + '"');
}
exposeTo[exposeAs] = function RP$exposed() {
var self = bindTo || this;
return self[promisePropertyKey][methodToExpose].apply(self[promisePropertyKey], arguments);
};
};
plumbing.exposePromise = function (exposeTo, bindTo, promisePropertyKey, exposeAs) {
exposeAs = exposeAs || 'promise';
if (exposeAs in exposeTo) {
throw new Error('Unable to expose method "' + exposeAs + '"');
}
exposeTo[exposeAs] = function RP$promise() {
var self = bindTo || this;
return self[promisePropertyKey];
};
};
return plumbing;
};