-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreq.js
679 lines (573 loc) · 20.6 KB
/
req.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/**
* @author jboeselt
* simple xapi http requests via XHR or NODE (request module)
*/
var req = (function() {
'use strict';
var settings = {
ASYNC: 'callback'
};
// check node or browser
// - note that bundler like webpack might have both window and commonJS module
var NODE = (typeof module !== 'undefined' && module.exports);
if (typeof window !== 'undefined' && window.XMLHttpRequest) {
/* global window */
/* global XMLHttpRequest */
NODE = false;
}
// if node then load npm.request module
if (NODE) {
var http = require('http');
var https = require('https');
var Url = require('url');
}
// checks ES6 promise support and attaches native Promise, can be substituted
settings.Promise = (typeof Promise !== 'function') ? function() {
throw new Error('"promise" flag was set in request config but the system doesn\'t support ES6 Promise, use callbacks instead.');
} : Promise;
////
// Utils
////
var isScalar = function(v) {
var type = typeof v;
return v === null || ['string', 'number', 'boolean'].indexOf(type) > -1;
};
var _baseExtendArray = function(targ, arr) {
var l = arr.length;
for (var k = 0; k < l; k++) {
targ[k] = arr[k];
}
return targ;
};
/**
* extends targObj with one object or more objects.
* mutates first object (targObj)
* extend(targObj, obj1, [obj2...])
* extend(deepFlag, targObj, obj1, [obj2...])
*
* if deepFlag === true:
* [object Object] properties are deeply merged
* [object Array] properties are flat merged (simply adding/replacing indexes)
*/
var extend = function() {
var s = 0;
var deep = false;
var obj;
var val;
var src;
var length = arguments.length;
if (typeof arguments[0] === 'boolean') {
s = 1;
deep = arguments[0];
}
src = arguments[s];
s += 1;
for (var i = s; i < length; i++) {
obj = arguments[i];
for (var key in obj) {
val = obj[key];
if (!obj.hasOwnProperty(key)) {
continue;
}
if (val === undefined) {
continue;
}
if (!deep) {
src[key] = val;
continue;
}
if (isScalar(val)) {
src[key] = val;
continue;
}
var pType = Object.prototype.toString.call(val);
if (pType === '[object Object]') {
// force new assignment
src[key] = extend(deep, src[key] || {}, val);
continue;
}
if (pType === '[object Array]') {
// force new assignment
src[key] = _baseExtendArray(src[key] || [], val);
continue;
}
src[key] = val;
}
}
return src;
};
var extendDefaults = function(defaults, config) {
defaults.data = undefined;
return extend(true, defaults, config);
};
////
// headers
////
var normalizeHeaders = function(config) {
var headers = config.headers;
var normalized = {};
var parts;
var k;
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
k = '';
parts = key.toLowerCase().split('-');
for (var i = 0; i < parts.length; i++) {
k += (k) ? '-' : '';
k += parts[i].charAt(0).toUpperCase() + parts[i].substr(1);
}
normalized[k] = headers[key];
}
}
config.headers = normalized;
};
////
// Data serialization
////
// parse urelencoded request body
var sendableRawDataFormats = [];
if (!NODE) {
sendableRawDataFormats = [
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send
'[object Blob]',
'[object FormData]',
'[object String]', //DOMString === string
'[object HTMLDocument]',
// https://developer.mozilla.sorg/en-US/docs/Web/API/ArrayBufferView
'[object ArrayBuffer]',
'[object DataView]',
'[object Int8Array]',
'[object Uint8Array]',
'[object Uint8ClampedArray]',
'[object Int16Array]',
'[object Uint16Array]',
'[object Int32Array]',
'[object Uint32Array]',
'[object Float32Array]',
'[object Float64Array]'
];
} else {
// https://nodejs.org/api/http.html#http_request_write_chunk_encoding_callback
// https://nodejs.org/api/buffer.html
sendableRawDataFormats = [
'[object String]',
'[object Uint8Array]'
];
}
// encode a javascript object into a query string
// - TODO merge, review, see https://github.com/angular/angular.js/blob/master/src/ng/http.js#L60 and https://stackoverflow.com/a/30970229
var encodeData = function(obj, prefix) {
prefix = prefix || '';
if (Object.prototype.toString.call(obj) === '[object Array]') {
obj = obj.reduce(function(o, v, i) {// TODO >= ie9
o[i] = v;
return o;
}, {});
}
var str = [];
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
var k = prefix ? prefix + '[' + p + ']' : p, v = obj[p];
str.push(typeof v === 'object' ? encodeData(v, k) : encodeURIComponent(k) + '=' + encodeURIComponent(v));
}
}
return str.join('&');
};
// encode a javascript object into a query string
// - TODO merge with above
var _encodeQuery = function(config) {
var str = [];
var obj = config.query;
var val;
if (config.type !== 'json') {
return encodeData(obj);// if deep nested obj php-like query
}
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
val = (isScalar(obj[key])) ? obj[key] : JSON.stringify(obj[key]);
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(val));
}
}
return str.join('&');
};
var Serializer = {
json: function(data) {
try {
return JSON.stringify(data);
} catch (e) {
//@TODO DEBUG, log error, production throw
throw Error('Serializer.json: Failed to stringify JSON: ' + e.message);
}
},
form: function(data) {
// scalars
if (isScalar(data)) {
return encodeURI(data);
}
// TODO optimise
var _type = Object.prototype.toString.call(data);
if (sendableRawDataFormats.indexOf(_type) > -1) {
throw Error('Serializer.form: Cannot encode data of type ' + _type);
}
return encodeData(data);
},
plain: function(data) {
if (typeof data !== 'string') {
throw Error('Serializer.plain: String required');
}
return data;
},
raw: function(data) {
// nothing, use at own risk
return data;
}
};
// parse JSON
var _parseResponseBody = function(body, config) {
if (!body || body === undefined) {
return body;
}
if (config.type === 'json') {
try {
return JSON.parse(body);
} catch (e) {
//@TODO thow? Response.error? leave Response.data=null?
console.error('_parseResponseBody: Failed to parse response JSON: ' + e.message);
}
}
// everything else than json
return body;
};
////
// Response
////
var Response = function() {
this.data = null;
this.status = null;
this.statusText = null;
this.error = false;
this.headers = null;
};
/**
* @see https://gist.github.com/monsur/706839
* @courtesy https://gist.github.com/monsur
*/
var _xhrResponseHeaders = function(headerStr) {
var headers = {};
if (!headerStr) {
return headers;
}
var headerPairs = headerStr.split('\u000d\u000a');
for (var i = 0; i < headerPairs.length; i++) {
var headerPair = headerPairs[i];
// Can't use split() here because it does the wrong thing
// if the header value has the string ": " in it.
var index = headerPair.indexOf('\u003a\u0020');
if (index > 0) {
var key = headerPair.substring(0, index);
var val = headerPair.substring(index + 2);
headers[key] = val;
}
}
return headers;
};
////
// request
////
// XHR request
var _xhrRequest = function(url, config) {
var xhr = new XMLHttpRequest();
xhr.open(config.method, url, true);// TODO catch exception (legacy IE CORS throws exception and does not trigger onerror)
// headers
for (var key in config.headers) {
if (config.headers.hasOwnProperty(key)) {
xhr.setRequestHeader(key, config.headers[key]);
}
}
var result = new Response();
// core
xhr.onreadystatechange = function() {
if (xhr.readyState === 2) {
result.headers = _xhrResponseHeaders(xhr.getAllResponseHeaders());
}
if (xhr.readyState === 4) {
result.status = xhr.status;
result.statusText = xhr.statusText;
result.data = _parseResponseBody(xhr.responseText, config);
if (typeof config.transformResponse === 'function') {
config.transformResponse(result);
}
if (xhr.status < 299) {
config.success(result, xhr);
} else {
config.error(result, xhr);
}
config.always(result, xhr, config);
}
};
// catches cors requests
xhr.onerror = function(error) {
result.error = error;
var message = (error && error.message) ? error.message : error;
result.statusText = 'Network error or CORS conflict: ' + message;
config.error(result, request);
config.always(result, error, config);
};
var data = null;
if (config.data) {
data = config.serialize(config.data);
//TODO options.headers['Content-Length'] = bytelength;
}
if (typeof config.beforeSend === 'function') {
config.beforeSend(config, data, xhr);
}
xhr.send(data);
return xhr;
};
/// node/http
var _httpRequest = function(url, config) {
var _url = Url.parse(url);
var module = (_url.protocol === 'https:') ? https : http;
var data = null; // parsed request data
var _data = ''; // the incoming data stream
var options = {
host: (_url.port) ? _url.host.replace(':' + _url.port, '') : _url.host,
path: _url.path,
port: _url.port,
query: _url.query,
method: config.method,
headers: config.headers
};
if (config.data) {
data = config.serialize(config.data);
options.headers['Content-Length'] = Buffer.byteLength(data);
}
if (typeof config.beforeSend === 'function') {
config.beforeSend(config, data, options);
}
var result = new Response();
var request = module.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
// @var res: http.IncomingMessage
_data += chunk;
});
res.on('end', function() {
result.status = res.statusCode;
result.statusText = res.statusMessage;
result.data = _parseResponseBody(_data, config);
result.headers = res.headers;
if (typeof config.transformResponse === 'function') {
config.transformResponse(result);
}
if (res.statusCode < 299) {
config.success(result, res);
} else {
config.error(result, res);
}
config.always(result, res, config);
});
});
if (data) {
request.write(data);
}
request.on('error', function(error) {
result.error = error;
var message = (error && error.message) ? error.message : error;
result.statusText = 'Network error' + message;
config.error(result, request);
config.always(result, error, config);
});
request.end();
return null;
};
/////
// config controller
// TODO charset global option
// - https://xhr.spec.whatwg.org/
////
var applyConfig = function(config, isHttp) {
var preset = config.preset;
var type = (preset) ? preset : 'none';
var serializer;
// if preset, set (and overwrite!) headers header and choose default serializer callback
switch (type) {
case 'json': {
config.headers['Content-Type'] = 'application/json; charset=utf-8';
config.headers['Accept'] = 'application/json, text/plain, */*';
if (isHttp) {
config.headers['Accept-Charset'] = 'utf-8';
}
serializer = Serializer.json;
break;
}
case 'form': {
config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
config.headers['Accept'] = 'application/x-www-form-urlencoded, text/plain, */*';
if (isHttp) {
config.headers['Accept-Charset'] = 'utf-8';
}
serializer = Serializer.form;
break;
}
case 'plain': {
config.headers['Content-Type'] = 'text/plain; charset=utf-8';
config.headers['Accept'] = 'text/plain, */*';
if (isHttp) {
config.headers['Accept-Charset'] = 'utf-8';
}
serializer = Serializer.plain;
break;
}
case 'raw': {
serializer = Serializer.raw;
break;
}
// if no preset, inspect content-type header and set serializer callback
default: {
var header = config.headers['Content-Type'] || '';
if (/text\/plain/i.test(header)) {
type = 'plain';
serializer = Serializer.plain;
break;
}
if (/application\/x-www-form-urlencoded/i.test(header)) {
type = 'form';
serializer = Serializer.form;
break;
}
if (/application\/json/i.test(header)) {
type = 'json';
serializer = Serializer.json;
break;
}
// if no or empty header is set it defaults to inimal config "form"
config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
type = 'form';
serializer = Serializer.form;
}
}
// add type as a shorthand lookup
config.type = type;
// set chosen serializer only if no custom serializer was set in config
if (typeof config.serialize !== 'function') {
config.serialize = serializer;
}
return config;
};
////
// request api
////
var request = function(url, config) {
// 'raw' in comments below: xhr or http.ClientRequest
var defaults = {
method: 'GET', // request method //TODO normalize uppercase
promise: false, // return promise or use "success" and "error" calback options. if set to true these callbacks will be ignored
query: {}, // query params object (key, value)
headers: {}, // header object (key, value), maybe overwritten by "preset" option
preset: '', // 'json', 'form', 'plain', 'raw'
serialize: false, // custom serializer function. taking data and returning string
beforeSend: false, // inspect a request who is about to be sent. function(mergedConfig, parsedData, xhrInstance|httpRequestOptions) note that config changes will have no effect
transformResponse: false, // function(raw)
success: function() {}, // (Response, raw)
error: function() {}, // (Response, raw)
always: function() {} // (Response, raw, config)
};
// merge config with defaults
config = extendDefaults(defaults, config);
// normalize headers
normalizeHeaders(config);
// apply request type, presets, set serializer
applyConfig(config, NODE);
// encode query params
var query = _encodeQuery(config);
if (query) {
var glue = (url.indexOf('?') > -1) ? '&' : '?';
url = url + glue + query;
}
var fn = (NODE) ? _httpRequest : _xhrRequest;
var p = (config.promise === true || settings.ASYNC === 'promise');
if (p) {
return new settings.Promise(function(resolve, reject) {
config.success = function(data) {
resolve(data);
};
config.error = function(data) {
reject(data);
};
fn(url, config);
});
}
return fn(url, config);
};
////
// export
////
var exports = settings;
exports.extend = extend;
exports.extendDefaults = extendDefaults;
exports.serializeParams = encodeData;
exports.isScalar = isScalar;
exports.request = request;
// application/json request
exports.json = function(url, config) {
config = config || {};
config.preset = 'json';
return request(url, config);// note the order of merge. default overwrites are allowed
};
// application/x-www-form-urlencoded request
exports.form = function(url, config) {
config = config || {};
config.preset = 'form';
return request(url, config);// note the order of merge. default overwrites are allowed
};
// text/plain request
exports.plain = function(url, config) {
config = config || {};
config.preset = 'plain';
return request(url, config);// note the order of merge. default overwrites are allowed
};
// raw request (everything goes, no serialization)
exports.raw = function(url, config) {
config = config || {};
config.preset = 'raw';
return request(url, config);// note the order of merge. default overwrites are allowed
};
exports.get = function(url, config) {
config = config || {};
return request(url, config);
};
exports.head = function(url, config) {
config = config || {};
config.method = 'HEAD';
return request(url, config);
};
exports['delete'] = function(url, config) {
config = config || {};
config.method = 'DELETE';
return request(url, config);
};
exports.post = function(url, data, config) {
config = config || {};
config.method = 'POST';
config.data = data;
return request(url, config);
};
exports.put = function(url, data, config) {
config = config || {};
config.method = 'PUT';
config.data = data;
return request(url, config);
};
exports.patch = function(url, data, config) {
config = config || {};
config.method = 'PATCH';
config.data = data;
return request(url, config);
};
return exports;
})();
// node
if (typeof module !== 'undefined' && module.exports) {
module.exports = req;
}