-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleAgent.js
234 lines (198 loc) · 6.34 KB
/
SimpleAgent.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict';
const MODULE_REQUIRE = 1
/* built-in */
, http = require('http')
/* NPM */
, cloneObject = require('jinang/cloneObject')
, jointString = require('jinang/jointString')
, modifyUrl = require('jinang/modifyUrl')
/* in-package */
, htp = require('./htp')
, METHODS_WITHOUT_PAYLOAD = require('./methods-without-payload')
, once = require('./lib/once')
;
/**
*
* @param {object} options
* @param {Function} options.beforeRequest
* @param {string} options.endPoint
* @param {object} options.headers
* @param {object} options.query
* @param {object} options.settings
*/
function SimpleAgent(options) {
// Clone and uniform the input options.
this.options = cloneObject(options, (key, value) => [ key.toLowerCase(), value ]);
if (this.options.settings) {
this.htp = new htp(this.options.settings);
}
else {
this.htp = htp;
}
}
SimpleAgent.prototype.request = function(method, urlname, headers, body, callback) {
// ---------------------------
// Uniform arguments.
// Will be re-assigned if exist.
headers = null;
body = null;
callback = null;
if (arguments.length < 2) {
throw new Error('too less arguments: (method, urlname) is necessary');
}
if (arguments.length > 5) {
throw new Error('too may arguments: no more than (method, urlname [, headers] [, body] [, callback])');
}
// method & urlname are necessary.
// So, args.length <= 5 - 2
let args = Array.from(arguments).slice(2);
for (var i = args.length - 1; i >= 0; i--) {
if (args[i] === null || args[i] === undefined) {
args.pop();
}
else if (typeof args[i] == 'function') {
callback = args.pop();
}
else {
break;
}
}
// N == null, U == undefined, T == type
let isNUT = (index, type) => {
let value = args[index];
return value === null || value === undefined || typeof value == type;
}
const METHOD = method.toUpperCase();
if (!http.METHODS.includes(METHOD)) {
throw new Error(`invalid method name: ${method}`);
}
if (typeof urlname != 'string') {
throw new Error(`invalid url: ${urlname}`);
}
if (METHODS_WITHOUT_PAYLOAD.includes(METHOD)) {
body = null;
let argumentsError = new Error(`invalid arguments, ("${method}", urlname [, headers] [,callback]) needed`);
switch(args.length) {
case 3:
case 2:
throw argumentsError;
case 1:
if (typeof args[0] == 'object') {
[ headers ] = args;
}
else {
throw argumentsError;
}
break;
case 0:
break;
}
}
else {
let argumentsError = new Error(`invalid arguments, ("${method}", urlname [, headers], body [,callback]) needed`);
switch (args.length) {
case 3:
throw argumentsError;
case 2:
if (isNUT(0, 'object')) {
[ headers, body ] = args;
}
else {
throw argumentsError;
}
break;
case 1:
[ body ] = args;
break;
}
}
// ---------------------------
// Main process.
if (this.options.endpoint) {
urlname = jointString('/', this.options.endpoint, urlname);
}
if (this.options.query) {
urlname = modifyUrl.query(urlname, this.options.query);
}
if (this.options.beforerequest) {
let ret = this.options.beforerequest({
method,
urlname,
url: urlname,
headers,
body,
callback
});
if (ret) {
if (ret.hasOwnProperty('method')) method = ret.method;
if (ret.hasOwnProperty('url')) urlname = ret.url;
if (ret.hasOwnProperty('urlname')) urlname = ret.urlname;
if (ret.hasOwnProperty('headers')) headers = ret.headers;
if (ret.hasOwnProperty('body')) body = ret.body;
if (ret.hasOwnProperty('callback')) callback = ret.callback;
}
}
// ---------------------------
let agentCallback = callback, beforeCallback = this.options.beforecallback;
if (callback && beforeCallback) {
agentCallback = (err, response) => {
try {
let ret = beforeCallback(err, response);
callback(null, ret);
} catch(ex) {
callback(ex);
}
};
}
let ret = null;
if (1) {
let args = [ method, urlname ];
if (this.options.headers || headers) {
headers = Object.assign({}, headers, this.options.headers);
args.push(headers);
}
if (body !== null) {
args.push(body);
}
if (agentCallback) {
args.push(agentCallback);
}
ret = this.htp.request.apply(this.htp, args);
}
if (ret instanceof Promise && beforeCallback) {
ret = ret
.then(response => {
return beforeCallback(null, response);
})
.catch(err => {
beforeCallback(err);
});
}
return ret;
};
// According HTTP methods' names, add homonymous method to the prototype.
http.METHODS.forEach((METHOD_NAME) => {
let name = METHOD_NAME.toLowerCase();
if (METHODS_WITHOUT_PAYLOAD.includes(METHOD_NAME)) {
/**
* @param {string} pathname
* @param {object} [headers]
* @param {function} [callback]
*/
SimpleAgent.prototype[name] = function(pathname, headers, callback) {
return this.request(METHOD_NAME, pathname, headers, callback);
};
}
else {
/**
* @param {string} pathname
* @param {object} [headers]
* @param {*} body
* @param {function} [callback]
*/
SimpleAgent.prototype[name] = function(pathname, headers, body, callback) {
return this.request(METHOD_NAME, pathname, headers, body, callback);
}
}
});
module.exports = SimpleAgent;