-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
client.js
1065 lines (906 loc) · 22.6 KB
/
client.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Root reference for iframes.
*/
let root;
if (typeof window !== 'undefined') {
// Browser window
root = window;
} else if (typeof self === 'undefined') {
// Other environments
console.warn(
'Using browser-only version of superagent in non-browser environment'
);
root = this;
} else {
// Web Worker
root = self;
}
const Emitter = require('component-emitter');
const safeStringify = require('fast-safe-stringify');
const qs = require('qs');
const RequestBase = require('./request-base');
const isObject = require('./is-object');
const ResponseBase = require('./response-base');
const Agent = require('./agent-base');
/**
* Noop.
*/
function noop() {}
/**
* Expose `request`.
*/
module.exports = function (method, url) {
// callback
if (typeof url === 'function') {
return new exports.Request('GET', method).end(url);
}
// url first
if (arguments.length === 1) {
return new exports.Request('GET', method);
}
return new exports.Request(method, url);
};
exports = module.exports;
const request = exports;
exports.Request = Request;
/**
* Determine XHR.
*/
request.getXHR = () => {
if (
root.XMLHttpRequest &&
(!root.location ||
root.location.protocol !== 'file:' ||
!root.ActiveXObject)
) {
return new XMLHttpRequest();
}
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch {}
try {
return new ActiveXObject('Msxml2.XMLHTTP.6.0');
} catch {}
try {
return new ActiveXObject('Msxml2.XMLHTTP.3.0');
} catch {}
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch {}
throw new Error('Browser-only version of superagent could not find XHR');
};
/**
* Removes leading and trailing whitespace, added to support IE.
*
* @param {String} s
* @return {String}
* @api private
*/
const trim = ''.trim ? (s) => s.trim() : (s) => s.replace(/(^\s*|\s*$)/g, '');
/**
* Serialize the given `obj`.
*
* @param {Object} obj
* @return {String}
* @api private
*/
function serialize(obj) {
if (!isObject(obj)) return obj;
const pairs = [];
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key))
pushEncodedKeyValuePair(pairs, key, obj[key]);
}
return pairs.join('&');
}
/**
* Helps 'serialize' with serializing arrays.
* Mutates the pairs array.
*
* @param {Array} pairs
* @param {String} key
* @param {Mixed} val
*/
function pushEncodedKeyValuePair(pairs, key, val) {
if (val === undefined) return;
if (val === null) {
pairs.push(encodeURI(key));
return;
}
if (Array.isArray(val)) {
val.forEach((v) => {
pushEncodedKeyValuePair(pairs, key, v);
});
} else if (isObject(val)) {
for (const subkey in val) {
if (Object.prototype.hasOwnProperty.call(val, subkey))
pushEncodedKeyValuePair(pairs, `${key}[${subkey}]`, val[subkey]);
}
} else {
pairs.push(encodeURI(key) + '=' + encodeURIComponent(val));
}
}
/**
* Expose serialization method.
*/
request.serializeObject = serialize;
/**
* Parse the given x-www-form-urlencoded `str`.
*
* @param {String} str
* @return {Object}
* @api private
*/
function parseString(str) {
const obj = {};
const pairs = str.split('&');
let pair;
let pos;
for (let i = 0, len = pairs.length; i < len; ++i) {
pair = pairs[i];
pos = pair.indexOf('=');
if (pos === -1) {
obj[decodeURIComponent(pair)] = '';
} else {
obj[decodeURIComponent(pair.slice(0, pos))] = decodeURIComponent(
pair.slice(pos + 1)
);
}
}
return obj;
}
/**
* Expose parser.
*/
request.parseString = parseString;
/**
* Default MIME type map.
*
* superagent.types.xml = 'application/xml';
*
*/
request.types = {
html: 'text/html',
json: 'application/json',
xml: 'text/xml',
urlencoded: 'application/x-www-form-urlencoded',
form: 'application/x-www-form-urlencoded',
'form-data': 'application/x-www-form-urlencoded'
};
/**
* Default serialization map.
*
* superagent.serialize['application/xml'] = function(obj){
* return 'generated xml here';
* };
*
*/
request.serialize = {
'application/x-www-form-urlencoded': qs.stringify,
'application/json': safeStringify
};
/**
* Default parsers.
*
* superagent.parse['application/xml'] = function(str){
* return { object parsed from str };
* };
*
*/
request.parse = {
'application/x-www-form-urlencoded': parseString,
'application/json': JSON.parse
};
/**
* Parse the given header `str` into
* an object containing the mapped fields.
*
* @param {String} str
* @return {Object}
* @api private
*/
function parseHeader(str) {
const lines = str.split(/\r?\n/);
const fields = {};
let index;
let line;
let field;
let val;
for (let i = 0, len = lines.length; i < len; ++i) {
line = lines[i];
index = line.indexOf(':');
if (index === -1) {
// could be empty line, just skip it
continue;
}
field = line.slice(0, index).toLowerCase();
val = trim(line.slice(index + 1));
fields[field] = val;
}
return fields;
}
/**
* Check if `mime` is json or has +json structured syntax suffix.
*
* @param {String} mime
* @return {Boolean}
* @api private
*/
function isJSON(mime) {
// should match /json or +json
// but not /json-seq
return /[/+]json($|[^-\w])/i.test(mime);
}
/**
* Initialize a new `Response` with the given `xhr`.
*
* - set flags (.ok, .error, etc)
* - parse header
*
* Examples:
*
* Aliasing `superagent` as `request` is nice:
*
* request = superagent;
*
* We can use the promise-like API, or pass callbacks:
*
* request.get('/').end(function(res){});
* request.get('/', function(res){});
*
* Sending data can be chained:
*
* request
* .post('/user')
* .send({ name: 'tj' })
* .end(function(res){});
*
* Or passed to `.send()`:
*
* request
* .post('/user')
* .send({ name: 'tj' }, function(res){});
*
* Or passed to `.post()`:
*
* request
* .post('/user', { name: 'tj' })
* .end(function(res){});
*
* Or further reduced to a single call for simple cases:
*
* request
* .post('/user', { name: 'tj' }, function(res){});
*
* @param {XMLHTTPRequest} xhr
* @param {Object} options
* @api private
*/
function Response(req) {
this.req = req;
this.xhr = this.req.xhr;
// responseText is accessible only if responseType is '' or 'text' and on older browsers
this.text =
(this.req.method !== 'HEAD' &&
(this.xhr.responseType === '' || this.xhr.responseType === 'text')) ||
typeof this.xhr.responseType === 'undefined'
? this.xhr.responseText
: null;
this.statusText = this.req.xhr.statusText;
let { status } = this.xhr;
// handle IE9 bug: http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
if (status === 1223) {
status = 204;
}
this._setStatusProperties(status);
this.headers = parseHeader(this.xhr.getAllResponseHeaders());
this.header = this.headers;
// getAllResponseHeaders sometimes falsely returns "" for CORS requests, but
// getResponseHeader still works. so we get content-type even if getting
// other headers fails.
this.header['content-type'] = this.xhr.getResponseHeader('content-type');
this._setHeaderProperties(this.header);
if (this.text === null && req._responseType) {
this.body = this.xhr.response;
} else {
this.body =
this.req.method === 'HEAD'
? null
: this._parseBody(this.text ? this.text : this.xhr.response);
}
}
// eslint-disable-next-line new-cap
ResponseBase(Response.prototype);
/**
* Parse the given body `str`.
*
* Used for auto-parsing of bodies. Parsers
* are defined on the `superagent.parse` object.
*
* @param {String} str
* @return {Mixed}
* @api private
*/
Response.prototype._parseBody = function (str) {
let parse = request.parse[this.type];
if (this.req._parser) {
return this.req._parser(this, str);
}
if (!parse && isJSON(this.type)) {
parse = request.parse['application/json'];
}
return parse && str && (str.length > 0 || str instanceof Object)
? parse(str)
: null;
};
/**
* Return an `Error` representative of this response.
*
* @return {Error}
* @api public
*/
Response.prototype.toError = function () {
const { req } = this;
const { method } = req;
const { url } = req;
const msg = `cannot ${method} ${url} (${this.status})`;
const err = new Error(msg);
err.status = this.status;
err.method = method;
err.url = url;
return err;
};
/**
* Expose `Response`.
*/
request.Response = Response;
/**
* Initialize a new `Request` with the given `method` and `url`.
*
* @param {String} method
* @param {String} url
* @api public
*/
function Request(method, url) {
const self = this;
this._query = this._query || [];
this.method = method;
this.url = url;
this.header = {}; // preserves header name case
this._header = {}; // coerces header names to lowercase
this.on('end', () => {
let err = null;
let res = null;
try {
res = new Response(self);
} catch (err_) {
err = new Error('Parser is unable to parse the response');
err.parse = true;
err.original = err_;
// issue #675: return the raw response if the response parsing fails
if (self.xhr) {
// ie9 doesn't have 'response' property
err.rawResponse =
typeof self.xhr.responseType === 'undefined'
? self.xhr.responseText
: self.xhr.response;
// issue #876: return the http status code if the response parsing fails
err.status = self.xhr.status ? self.xhr.status : null;
err.statusCode = err.status; // backwards-compat only
} else {
err.rawResponse = null;
err.status = null;
}
return self.callback(err);
}
self.emit('response', res);
let new_err;
try {
if (!self._isResponseOK(res)) {
new_err = new Error(
res.statusText || res.text || 'Unsuccessful HTTP response'
);
}
} catch (err_) {
new_err = err_; // ok() callback can throw
}
// #1000 don't catch errors from the callback to avoid double calling it
if (new_err) {
new_err.original = err;
new_err.response = res;
new_err.status = res.status;
self.callback(new_err, res);
} else {
self.callback(null, res);
}
});
}
/**
* Mixin `Emitter` and `RequestBase`.
*/
// eslint-disable-next-line new-cap
Emitter(Request.prototype);
// eslint-disable-next-line new-cap
RequestBase(Request.prototype);
/**
* Set Content-Type to `type`, mapping values from `request.types`.
*
* Examples:
*
* superagent.types.xml = 'application/xml';
*
* request.post('/')
* .type('xml')
* .send(xmlstring)
* .end(callback);
*
* request.post('/')
* .type('application/xml')
* .send(xmlstring)
* .end(callback);
*
* @param {String} type
* @return {Request} for chaining
* @api public
*/
Request.prototype.type = function (type) {
this.set('Content-Type', request.types[type] || type);
return this;
};
/**
* Set Accept to `type`, mapping values from `request.types`.
*
* Examples:
*
* superagent.types.json = 'application/json';
*
* request.get('/agent')
* .accept('json')
* .end(callback);
*
* request.get('/agent')
* .accept('application/json')
* .end(callback);
*
* @param {String} accept
* @return {Request} for chaining
* @api public
*/
Request.prototype.accept = function (type) {
this.set('Accept', request.types[type] || type);
return this;
};
/**
* Set Authorization field value with `user` and `pass`.
*
* @param {String} user
* @param {String} [pass] optional in case of using 'bearer' as type
* @param {Object} options with 'type' property 'auto', 'basic' or 'bearer' (default 'basic')
* @return {Request} for chaining
* @api public
*/
Request.prototype.auth = function (user, pass, options) {
if (arguments.length === 1) pass = '';
if (typeof pass === 'object' && pass !== null) {
// pass is optional and can be replaced with options
options = pass;
pass = '';
}
if (!options) {
options = {
type: typeof btoa === 'function' ? 'basic' : 'auto'
};
}
const encoder = (string) => {
if (typeof btoa === 'function') {
return btoa(string);
}
throw new Error('Cannot use basic auth, btoa is not a function');
};
return this._auth(user, pass, options, encoder);
};
/**
* Add query-string `val`.
*
* Examples:
*
* request.get('/shoes')
* .query('size=10')
* .query({ color: 'blue' })
*
* @param {Object|String} val
* @return {Request} for chaining
* @api public
*/
Request.prototype.query = function (val) {
if (typeof val !== 'string') val = serialize(val);
if (val) this._query.push(val);
return this;
};
/**
* Queue the given `file` as an attachment to the specified `field`,
* with optional `options` (or filename).
*
* ``` js
* request.post('/upload')
* .attach('content', new Blob(['<a id="a"><b id="b">hey!</b></a>'], { type: "text/html"}))
* .end(callback);
* ```
*
* @param {String} field
* @param {Blob|File} file
* @param {String|Object} options
* @return {Request} for chaining
* @api public
*/
Request.prototype.attach = function (field, file, options) {
if (file) {
if (this._data) {
throw new Error("superagent can't mix .send() and .attach()");
}
this._getFormData().append(field, file, options || file.name);
}
return this;
};
Request.prototype._getFormData = function () {
if (!this._formData) {
this._formData = new root.FormData();
}
return this._formData;
};
/**
* Invoke the callback with `err` and `res`
* and handle arity check.
*
* @param {Error} err
* @param {Response} res
* @api private
*/
Request.prototype.callback = function (err, res) {
if (this._shouldRetry(err, res)) {
return this._retry();
}
const fn = this._callback;
this.clearTimeout();
if (err) {
if (this._maxRetries) err.retries = this._retries - 1;
this.emit('error', err);
}
fn(err, res);
};
/**
* Invoke callback with x-domain error.
*
* @api private
*/
Request.prototype.crossDomainError = function () {
const err = new Error(
'Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.'
);
err.crossDomain = true;
err.status = this.status;
err.method = this.method;
err.url = this.url;
this.callback(err);
};
// This only warns, because the request is still likely to work
Request.prototype.agent = function () {
console.warn('This is not supported in browser version of superagent');
return this;
};
Request.prototype.ca = Request.prototype.agent;
Request.prototype.buffer = Request.prototype.ca;
// This throws, because it can't send/receive data as expected
Request.prototype.write = () => {
throw new Error(
'Streaming is not supported in browser version of superagent'
);
};
Request.prototype.pipe = Request.prototype.write;
/**
* Check if `obj` is a host object,
* we don't want to serialize these :)
*
* @param {Object} obj host object
* @return {Boolean} is a host object
* @api private
*/
Request.prototype._isHost = function (obj) {
// Native objects stringify to [object File], [object Blob], [object FormData], etc.
return (
obj &&
typeof obj === 'object' &&
!Array.isArray(obj) &&
Object.prototype.toString.call(obj) !== '[object Object]'
);
};
/**
* Initiate request, invoking callback `fn(res)`
* with an instanceof `Response`.
*
* @param {Function} fn
* @return {Request} for chaining
* @api public
*/
Request.prototype.end = function (fn) {
if (this._endCalled) {
console.warn(
'Warning: .end() was called twice. This is not supported in superagent'
);
}
this._endCalled = true;
// store callback
this._callback = fn || noop;
// querystring
this._finalizeQueryString();
this._end();
};
Request.prototype._setUploadTimeout = function () {
const self = this;
// upload timeout it's wokrs only if deadline timeout is off
if (this._uploadTimeout && !this._uploadTimeoutTimer) {
this._uploadTimeoutTimer = setTimeout(() => {
self._timeoutError(
'Upload timeout of ',
self._uploadTimeout,
'ETIMEDOUT'
);
}, this._uploadTimeout);
}
};
// eslint-disable-next-line complexity
Request.prototype._end = function () {
if (this._aborted)
return this.callback(
new Error('The request has been aborted even before .end() was called')
);
const self = this;
this.xhr = request.getXHR();
const { xhr } = this;
let data = this._formData || this._data;
this._setTimeouts();
// state change
xhr.onreadystatechange = () => {
const { readyState } = xhr;
if (readyState >= 2 && self._responseTimeoutTimer) {
clearTimeout(self._responseTimeoutTimer);
}
if (readyState !== 4) {
return;
}
// In IE9, reads to any property (e.g. status) off of an aborted XHR will
// result in the error "Could not complete the operation due to error c00c023f"
let status;
try {
status = xhr.status;
} catch {
status = 0;
}
if (!status) {
if (self.timedout || self._aborted) return;
return self.crossDomainError();
}
self.emit('end');
};
// progress
const handleProgress = (direction, e) => {
if (e.total > 0) {
e.percent = (e.loaded / e.total) * 100;
if (e.percent === 100) {
clearTimeout(self._uploadTimeoutTimer);
}
}
e.direction = direction;
self.emit('progress', e);
};
if (this.hasListeners('progress')) {
try {
xhr.addEventListener('progress', handleProgress.bind(null, 'download'));
if (xhr.upload) {
xhr.upload.addEventListener(
'progress',
handleProgress.bind(null, 'upload')
);
}
} catch {
// Accessing xhr.upload fails in IE from a web worker, so just pretend it doesn't exist.
// Reported here:
// https://connect.microsoft.com/IE/feedback/details/837245/xmlhttprequest-upload-throws-invalid-argument-when-used-from-web-worker-context
}
}
if (xhr.upload) {
this._setUploadTimeout();
}
// initiate request
try {
if (this.username && this.password) {
xhr.open(this.method, this.url, true, this.username, this.password);
} else {
xhr.open(this.method, this.url, true);
}
} catch (err) {
// see #1149
return this.callback(err);
}
// CORS
if (this._withCredentials) xhr.withCredentials = true;
// body
if (
!this._formData &&
this.method !== 'GET' &&
this.method !== 'HEAD' &&
typeof data !== 'string' &&
!this._isHost(data)
) {
// serialize stuff
const contentType = this._header['content-type'];
let serialize =
this._serializer ||
request.serialize[contentType ? contentType.split(';')[0] : ''];
if (!serialize && isJSON(contentType)) {
serialize = request.serialize['application/json'];
}
if (serialize) data = serialize(data);
}
// set header fields
for (const field in this.header) {
if (this.header[field] === null) continue;
if (Object.prototype.hasOwnProperty.call(this.header, field))
xhr.setRequestHeader(field, this.header[field]);
}
if (this._responseType) {
xhr.responseType = this._responseType;
}
// send stuff
this.emit('request', this);
// IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing)
// We need null here if data is undefined
xhr.send(typeof data === 'undefined' ? null : data);
};
request.agent = () => new Agent();
['GET', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE'].forEach((method) => {
Agent.prototype[method.toLowerCase()] = function (url, fn) {
const req = new request.Request(method, url);
this._setDefaults(req);
if (fn) {
req.end(fn);
}
return req;
};
});
Agent.prototype.del = Agent.prototype.delete;
/**
* GET `url` with optional callback `fn(res)`.
*
* @param {String} url
* @param {Mixed|Function} [data] or fn
* @param {Function} [fn]
* @return {Request}
* @api public
*/
request.get = (url, data, fn) => {
const req = request('GET', url);
if (typeof data === 'function') {
fn = data;
data = null;
}
if (data) req.query(data);
if (fn) req.end(fn);
return req;
};
/**
* HEAD `url` with optional callback `fn(res)`.
*
* @param {String} url
* @param {Mixed|Function} [data] or fn
* @param {Function} [fn]
* @return {Request}
* @api public
*/
request.head = (url, data, fn) => {
const req = request('HEAD', url);
if (typeof data === 'function') {
fn = data;
data = null;
}
if (data) req.query(data);
if (fn) req.end(fn);
return req;
};
/**
* OPTIONS query to `url` with optional callback `fn(res)`.
*
* @param {String} url
* @param {Mixed|Function} [data] or fn
* @param {Function} [fn]
* @return {Request}
* @api public
*/
request.options = (url, data, fn) => {
const req = request('OPTIONS', url);
if (typeof data === 'function') {
fn = data;
data = null;
}
if (data) req.send(data);
if (fn) req.end(fn);
return req;
};
/**
* DELETE `url` with optional `data` and callback `fn(res)`.
*
* @param {String} url
* @param {Mixed} [data]
* @param {Function} [fn]
* @return {Request}
* @api public
*/
function del(url, data, fn) {
const req = request('DELETE', url);
if (typeof data === 'function') {
fn = data;
data = null;
}
if (data) req.send(data);
if (fn) req.end(fn);
return req;
}
request.del = del;
request.delete = del;