]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - frontend/js/superagent.js
1 ! function ( e
){ if ( "object" == typeof exports
&& "undefined" != typeof module
) module
. exports
= e (); else if ( "function" == typeof define
&& define
. amd
) define ([], e
); else { var f
; "undefined" != typeof window
? f
= window : "undefined" != typeof global
? f
= global : "undefined" != typeof self
&&( f
= self
), f
. superagent
= e ()}}( function (){ var define
, module
, exports
; return ( function e ( t
, n
, r
){ function s ( o
, u
){ if (! n
[ o
]){ if (! t
[ o
]){ var a
= typeof require
== "function" && require
; if (! u
&& a
) return a ( o
,! 0 ); if ( i
) return i ( o
,! 0 ); var f
= new Error ( "Cannot find module '" + o
+ "'" ); throw f
. code
= "MODULE_NOT_FOUND" , f
} var l
= n
[ o
]={ exports :{}}; t
[ o
][ 0 ]. call ( l
. exports
, function ( e
){ var n
= t
[ o
][ 1 ][ e
]; return s ( n
? n : e
)}, l
, l
. exports
, e
, t
, n
, r
)} return n
[ o
]. exports
} var i
= typeof require
== "function" && require
; for ( var o
= 0 ; o
< r
. length
; o
++) s ( r
[ o
]); return s
})({ 1 :[ function ( require
, module
, exports
){
7 module
. exports
= Emitter
;
10 * Initialize a new `Emitter`.
15 function Emitter ( obj
) {
16 if ( obj
) return mixin ( obj
);
20 * Mixin the emitter properties.
28 for ( var key
in Emitter
. prototype ) {
29 obj
[ key
] = Emitter
. prototype [ key
];
35 * Listen on the given `event` with `fn`.
37 * @param {String} event
38 * @param {Function} fn
43 Emitter
. prototype . on
=
44 Emitter
. prototype . addEventListener = function ( event
, fn
){
45 this . _callbacks
= this . _callbacks
|| {};
46 ( this . _callbacks
[ '$' + event
] = this . _callbacks
[ '$' + event
] || [])
52 * Adds an `event` listener that will be invoked a single
53 * time then automatically removed.
55 * @param {String} event
56 * @param {Function} fn
61 Emitter
. prototype . once = function ( event
, fn
){
64 fn
. apply ( this , arguments
);
73 * Remove the given callback for `event` or all
74 * registered callbacks.
76 * @param {String} event
77 * @param {Function} fn
82 Emitter
. prototype . off
=
83 Emitter
. prototype . removeListener
=
84 Emitter
. prototype . removeAllListeners
=
85 Emitter
. prototype . removeEventListener = function ( event
, fn
){
86 this . _callbacks
= this . _callbacks
|| {};
89 if ( 0 == arguments
. length
) {
95 var callbacks
= this . _callbacks
[ '$' + event
];
96 if (! callbacks
) return this ;
98 // remove all handlers
99 if ( 1 == arguments
. length
) {
100 delete this . _callbacks
[ '$' + event
];
104 // remove specific handler
106 for ( var i
= 0 ; i
< callbacks
. length
; i
++) {
108 if ( cb
=== fn
|| cb
. fn
=== fn
) {
109 callbacks
. splice ( i
, 1 );
117 * Emit `event` with the given args.
119 * @param {String} event
124 Emitter
. prototype . emit = function ( event
){
125 this . _callbacks
= this . _callbacks
|| {};
126 var args
= []. slice
. call ( arguments
, 1 )
127 , callbacks
= this . _callbacks
[ '$' + event
];
130 callbacks
= callbacks
. slice ( 0 );
131 for ( var i
= 0 , len
= callbacks
. length
; i
< len
; ++ i
) {
132 callbacks
[ i
]. apply ( this , args
);
140 * Return array of callbacks for `event`.
142 * @param {String} event
147 Emitter
. prototype . listeners = function ( event
){
148 this . _callbacks
= this . _callbacks
|| {};
149 return this . _callbacks
[ '$' + event
] || [];
153 * Check if this emitter has `event` handlers.
155 * @param {String} event
160 Emitter
. prototype . hasListeners = function ( event
){
161 return !! this . listeners ( event
). length
;
164 },{}], 2 :[ function ( require
, module
, exports
){
167 * Reduce `arr` with `fn`.
170 * @param {Function} fn
171 * @param {Mixed} initial
173 * TODO: combatible error handling?
176 module
. exports = function ( arr
, fn
, initial
){
178 var len
= arr
. length
;
179 var curr
= arguments
. length
== 3
184 curr
= fn
. call ( null , curr
, arr
[ idx
], ++ idx
, arr
);
189 },{}], 3 :[ function ( require
, module
, exports
){
191 * Module dependencies.
194 var Emitter
= require ( 'emitter' );
195 var reduce
= require ( 'reduce' );
198 * Root reference for iframes.
202 if ( typeof window
!== 'undefined' ) { // Browser window
204 } else if ( typeof self
!== 'undefined' ) { // Web Worker
206 } else { // Other environments
217 * Check if `obj` is a host object,
218 * we don't want to serialize these :)
220 * TODO: future proof, move to compoent land
222 * @param {Object} obj
227 function isHost ( obj
) {
228 var str
= {}. toString
. call ( obj
);
231 case '[object File]' :
232 case '[object Blob]' :
233 case '[object FormData]' :
244 request
. getXHR = function () {
245 if ( root
. XMLHttpRequest
246 && (! root
. location
|| 'file:' != root
. location
. protocol
247 || ! root
. ActiveXObject
)) {
248 return new XMLHttpRequest
;
250 try { return new ActiveXObject ( 'Microsoft.XMLHTTP' ); } catch ( e
) {}
251 try { return new ActiveXObject ( 'Msxml2.XMLHTTP.6.0' ); } catch ( e
) {}
252 try { return new ActiveXObject ( 'Msxml2.XMLHTTP.3.0' ); } catch ( e
) {}
253 try { return new ActiveXObject ( 'Msxml2.XMLHTTP' ); } catch ( e
) {}
259 * Removes leading and trailing whitespace, added to support IE.
267 ? function ( s
) { return s
. trim (); }
268 : function ( s
) { return s
. replace ( /(^\s*|\s*$)/g , '' ); };
271 * Check if `obj` is an object.
273 * @param {Object} obj
278 function isObject ( obj
) {
279 return obj
=== Object ( obj
);
283 * Serialize the given `obj`.
285 * @param {Object} obj
290 function serialize ( obj
) {
291 if (! isObject ( obj
)) return obj
;
293 for ( var key
in obj
) {
294 if ( null != obj
[ key
]) {
295 pushEncodedKeyValuePair ( pairs
, key
, obj
[ key
]);
298 return pairs
. join ( '&' );
302 * Helps 'serialize' with serializing arrays.
303 * Mutates the pairs array.
305 * @param {Array} pairs
306 * @param {String} key
310 function pushEncodedKeyValuePair ( pairs
, key
, val
) {
311 if ( Array
. isArray ( val
)) {
312 return val
. forEach ( function ( v
) {
313 pushEncodedKeyValuePair ( pairs
, key
, v
);
316 pairs
. push ( encodeURIComponent ( key
)
317 + '=' + encodeURIComponent ( val
));
321 * Expose serialization method.
324 request
. serializeObject
= serialize
;
327 * Parse the given x-www-form-urlencoded `str`.
329 * @param {String} str
334 function parseString ( str
) {
336 var pairs
= str
. split ( '&' );
340 for ( var i
= 0 , len
= pairs
. length
; i
< len
; ++ i
) {
342 parts
= pair
. split ( '=' );
343 obj
[ decodeURIComponent ( parts
[ 0 ])] = decodeURIComponent ( parts
[ 1 ]);
353 request
. parseString
= parseString
;
356 * Default MIME type map.
358 * superagent.types.xml = 'application/xml';
364 json : 'application/json' ,
365 xml : 'application/xml' ,
366 urlencoded : 'application/x-www-form-urlencoded' ,
367 'form' : 'application/x-www-form-urlencoded' ,
368 'form-data' : 'application/x-www-form-urlencoded'
372 * Default serialization map.
374 * superagent.serialize['application/xml'] = function(obj){
375 * return 'generated xml here';
380 request
. serialize
= {
381 'application/x-www-form-urlencoded' : serialize
,
382 'application/json' : JSON
. stringify
388 * superagent.parse['application/xml'] = function(str){
389 * return { object parsed from str };
395 'application/x-www-form-urlencoded' : parseString
,
396 'application/json' : JSON
. parse
400 * Parse the given header `str` into
401 * an object containing the mapped fields.
403 * @param {String} str
408 function parseHeader ( str
) {
409 var lines
= str
. split ( /\r?\n/ );
416 lines
. pop (); // trailing CRLF
418 for ( var i
= 0 , len
= lines
. length
; i
< len
; ++ i
) {
420 index
= line
. indexOf ( ':' );
421 field
= line
. slice ( 0 , index
). toLowerCase ();
422 val
= trim ( line
. slice ( index
+ 1 ));
430 * Check if `mime` is json or has +json structured syntax suffix.
432 * @param {String} mime
437 function isJSON ( mime
) {
438 return /[\/+]json\b/ . test ( mime
);
442 * Return the mime type for the given `str`.
444 * @param {String} str
450 return str
. split (/ *; */
). shift ();
454 * Return header field parameters.
456 * @param {String} str
461 function params ( str
){
462 return reduce ( str
. split (/ *; */
), function ( obj
, str
){
463 var parts
= str
. split (/ *= */
)
464 , key
= parts
. shift ()
465 , val
= parts
. shift ();
467 if ( key
&& val
) obj
[ key
] = val
;
473 * Initialize a new `Response` with the given `xhr`.
475 * - set flags (.ok, .error, etc)
480 * Aliasing `superagent` as `request` is nice:
482 * request = superagent;
484 * We can use the promise-like API, or pass callbacks:
486 * request.get('/').end(function(res){});
487 * request.get('/', function(res){});
489 * Sending data can be chained:
493 * .send({ name: 'tj' })
494 * .end(function(res){});
496 * Or passed to `.send()`:
500 * .send({ name: 'tj' }, function(res){});
502 * Or passed to `.post()`:
505 * .post('/user', { name: 'tj' })
506 * .end(function(res){});
508 * Or further reduced to a single call for simple cases:
511 * .post('/user', { name: 'tj' }, function(res){});
513 * @param {XMLHTTPRequest} xhr
514 * @param {Object} options
518 function Response ( req
, options
) {
519 options
= options
|| {};
521 this . xhr
= this . req
. xhr
;
522 // responseText is accessible only if responseType is '' or 'text' and on older browsers
523 this . text
= (( this . req
. method
!= 'HEAD' && ( this . xhr
. responseType
=== '' || this . xhr
. responseType
=== 'text' )) || typeof this . xhr
. responseType
=== 'undefined' )
524 ? this . xhr
. responseText
526 this . statusText
= this . req
. xhr
. statusText
;
527 this . setStatusProperties ( this . xhr
. status
);
528 this . header
= this . headers
= parseHeader ( this . xhr
. getAllResponseHeaders ());
529 // getAllResponseHeaders sometimes falsely returns "" for CORS requests, but
530 // getResponseHeader still works. so we get content-type even if getting
531 // other headers fails.
532 this . header
[ 'content-type' ] = this . xhr
. getResponseHeader ( 'content-type' );
533 this . setHeaderProperties ( this . header
);
534 this . body
= this . req
. method
!= 'HEAD'
535 ? this . parseBody ( this . text
? this . text : this . xhr
. response
)
540 * Get case-insensitive `field` value.
542 * @param {String} field
547 Response
. prototype . get = function ( field
){
548 return this . header
[ field
. toLowerCase ()];
552 * Set header related properties:
554 * - `.type` the content type without params
556 * A response of "Content-Type: text/plain; charset=utf-8"
557 * will provide you with a `.type` of "text/plain".
559 * @param {Object} header
563 Response
. prototype . setHeaderProperties = function ( header
){
565 var ct
= this . header
[ 'content-type' ] || '' ;
566 this . type
= type ( ct
);
569 var obj
= params ( ct
);
570 for ( var key
in obj
) this [ key
] = obj
[ key
];
574 * Parse the given body `str`.
576 * Used for auto-parsing of bodies. Parsers
577 * are defined on the `superagent.parse` object.
579 * @param {String} str
584 Response
. prototype . parseBody = function ( str
){
585 var parse
= request
. parse
[ this . type
];
586 return parse
&& str
&& ( str
. length
|| str
instanceof Object
)
592 * Set flags such as `.ok` based on `status`.
594 * For example a 2xx response will give you a `.ok` of __true__
595 * whereas 5xx will be __false__ and `.error` will be __true__. The
596 * `.clientError` and `.serverError` are also available to be more
597 * specific, and `.statusType` is the class of error ranging from 1..5
598 * sometimes useful for mapping respond colors etc.
600 * "sugar" properties are also defined for common cases. Currently providing:
608 * @param {Number} status
612 Response
. prototype . setStatusProperties = function ( status
){
613 // handle IE9 bug: http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
614 if ( status
=== 1223 ) {
618 var type
= status
/ 100 | 0 ;
621 this . status
= this . statusCode
= status
;
622 this . statusType
= type
;
625 this . info
= 1 == type
;
627 this . clientError
= 4 == type
;
628 this . serverError
= 5 == type
;
629 this . error
= ( 4 == type
|| 5 == type
)
634 this . accepted
= 202 == status
;
635 this . noContent
= 204 == status
;
636 this . badRequest
= 400 == status
;
637 this . unauthorized
= 401 == status
;
638 this . notAcceptable
= 406 == status
;
639 this . notFound
= 404 == status
;
640 this . forbidden
= 403 == status
;
644 * Return an `Error` representative of this response.
650 Response
. prototype . toError = function (){
652 var method
= req
. method
;
655 var msg
= 'cannot ' + method
+ ' ' + url
+ ' (' + this . status
+ ')' ;
656 var err
= new Error ( msg
);
657 err
. status
= this . status
;
668 request
. Response
= Response
;
671 * Initialize a new `Request` with the given `method` and `url`.
673 * @param {String} method
674 * @param {String} url
678 function Request ( method
, url
) {
681 this . _query
= this . _query
|| [];
682 this . method
= method
;
686 this . on ( 'end' , function (){
691 res
= new Response ( self
);
693 err
= new Error ( 'Parser is unable to parse the response' );
696 // issue #675: return the raw response if the response parsing fails
697 err
. rawResponse
= self
. xhr
&& self
. xhr
. responseText
? self
. xhr
. responseText : null ;
698 return self
. callback ( err
);
701 self
. emit ( 'response' , res
);
704 return self
. callback ( err
, res
);
707 if ( res
. status
>= 200 && res
. status
< 300 ) {
708 return self
. callback ( err
, res
);
711 var new_err
= new Error ( res
. statusText
|| 'Unsuccessful HTTP response' );
712 new_err
. original
= err
;
713 new_err
. response
= res
;
714 new_err
. status
= res
. status
;
716 self
. callback ( new_err
, res
);
724 Emitter ( Request
. prototype );
727 * Allow for extension
730 Request
. prototype . use = function ( fn
) {
736 * Set timeout to `ms`.
739 * @return {Request} for chaining
743 Request
. prototype . timeout = function ( ms
){
749 * Clear previous timeout.
751 * @return {Request} for chaining
755 Request
. prototype . clearTimeout = function (){
757 clearTimeout ( this . _timer
);
762 * Abort the request, and clear potential timeout.
768 Request
. prototype . abort = function (){
769 if ( this . aborted
) return ;
778 * Set header `field` to `val`, or multiple fields with one object.
783 * .set('Accept', 'application/json')
784 * .set('X-API-Key', 'foobar')
788 * .set({ Accept: 'application/json', 'X-API-Key': 'foobar' })
791 * @param {String|Object} field
792 * @param {String} val
793 * @return {Request} for chaining
797 Request
. prototype . set = function ( field
, val
){
798 if ( isObject ( field
)) {
799 for ( var key
in field
) {
800 this . set ( key
, field
[ key
]);
804 this . _header
[ field
. toLowerCase ()] = val
;
805 this . header
[ field
] = val
;
810 * Remove header `field`.
815 * .unset('User-Agent')
818 * @param {String} field
819 * @return {Request} for chaining
823 Request
. prototype . unset = function ( field
){
824 delete this . _header
[ field
. toLowerCase ()];
825 delete this . header
[ field
];
830 * Get case-insensitive header `field` value.
832 * @param {String} field
837 Request
. prototype . getHeader = function ( field
){
838 return this . _header
[ field
. toLowerCase ()];
842 * Set Content-Type to `type`, mapping values from `request.types`.
846 * superagent.types.xml = 'application/xml';
854 * .type('application/xml')
858 * @param {String} type
859 * @return {Request} for chaining
863 Request
. prototype . type = function ( type
){
864 this . set ( 'Content-Type' , request
. types
[ type
] || type
);
871 * Sets the body parser no matter type.
877 Request
. prototype . parse = function ( fn
){
883 * Set Accept to `type`, mapping values from `request.types`.
887 * superagent.types.json = 'application/json';
889 * request.get('/agent')
893 * request.get('/agent')
894 * .accept('application/json')
897 * @param {String} accept
898 * @return {Request} for chaining
902 Request
. prototype . accept = function ( type
){
903 this . set ( 'Accept' , request
. types
[ type
] || type
);
908 * Set Authorization field value with `user` and `pass`.
910 * @param {String} user
911 * @param {String} pass
912 * @return {Request} for chaining
916 Request
. prototype . auth = function ( user
, pass
){
917 var str
= btoa ( user
+ ':' + pass
);
918 this . set ( 'Authorization' , 'Basic ' + str
);
923 * Add query-string `val`.
927 * request.get('/shoes')
929 * .query({ color: 'blue' })
931 * @param {Object|String} val
932 * @return {Request} for chaining
936 Request
. prototype . query = function ( val
){
937 if ( 'string' != typeof val
) val
= serialize ( val
);
938 if ( val
) this . _query
. push ( val
);
943 * Write the field `name` and `val` for "multipart/form-data"
947 * request.post('/upload')
948 * .field('foo', 'bar')
952 * @param {String} name
953 * @param {String|Blob|File} val
954 * @return {Request} for chaining
958 Request
. prototype . field = function ( name
, val
){
959 if (! this . _formData
) this . _formData
= new root
. FormData ();
960 this . _formData
. append ( name
, val
);
965 * Queue the given `file` as an attachment to the specified `field`,
966 * with optional `filename`.
969 * request.post('/upload')
970 * .attach(new Blob(['<a id="a"><b id="b">hey!</b></a>'], { type: "text/html"}))
974 * @param {String} field
975 * @param {Blob|File} file
976 * @param {String} filename
977 * @return {Request} for chaining
981 Request
. prototype . attach = function ( field
, file
, filename
){
982 if (! this . _formData
) this . _formData
= new root
. FormData ();
983 this . _formData
. append ( field
, file
, filename
|| file
. name
);
988 * Send `data` as the request body, defaulting the `.type()` to "json" when
989 * an object is given.
994 * request.post('/user')
996 * .send('{"name":"tj"}')
1000 * request.post('/user')
1001 * .send({ name: 'tj' })
1004 * // manual x-www-form-urlencoded
1005 * request.post('/user')
1010 * // auto x-www-form-urlencoded
1011 * request.post('/user')
1013 * .send({ name: 'tj' })
1016 * // defaults to x-www-form-urlencoded
1017 * request.post('/user')
1018 * .send('name=tobi')
1019 * .send('species=ferret')
1022 * @param {String|Object} data
1023 * @return {Request} for chaining
1027 Request
. prototype . send = function ( data
){
1028 var obj
= isObject ( data
);
1029 var type
= this . getHeader ( 'Content-Type' );
1032 if ( obj
&& isObject ( this . _data
)) {
1033 for ( var key
in data
) {
1034 this . _data
[ key
] = data
[ key
];
1036 } else if ( 'string' == typeof data
) {
1037 if (! type
) this . type ( 'form' );
1038 type
= this . getHeader ( 'Content-Type' );
1039 if ( 'application/x-www-form-urlencoded' == type
) {
1040 this . _data
= this . _data
1041 ? this . _data
+ '&' + data
1044 this . _data
= ( this . _data
|| '' ) + data
;
1050 if (! obj
|| isHost ( data
)) return this ;
1051 if (! type
) this . type ( 'json' );
1056 * Invoke the callback with `err` and `res`
1057 * and handle arity check.
1059 * @param {Error} err
1060 * @param {Response} res
1064 Request
. prototype . callback = function ( err
, res
){
1065 var fn
= this . _callback
;
1066 this . clearTimeout ();
1071 * Invoke callback with x-domain error.
1076 Request
. prototype . crossDomainError = function (){
1077 var err
= new Error ( 'Request has been terminated \n Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.' );
1078 err
. crossDomain
= true ;
1080 err
. status
= this . status
;
1081 err
. method
= this . method
;
1088 * Invoke callback with timeout error.
1093 Request
. prototype . timeoutError = function (){
1094 var timeout
= this . _timeout
;
1095 var err
= new Error ( 'timeout of ' + timeout
+ 'ms exceeded' );
1096 err
. timeout
= timeout
;
1101 * Enable transmission of cookies with x-domain requests.
1103 * Note that for this to work the origin must not be
1104 * using "Access-Control-Allow-Origin" with a wildcard,
1105 * and also must set "Access-Control-Allow-Credentials"
1111 Request
. prototype . withCredentials = function (){
1112 this . _withCredentials
= true ;
1117 * Initiate request, invoking callback `fn(res)`
1118 * with an instanceof `Response`.
1120 * @param {Function} fn
1121 * @return {Request} for chaining
1125 Request
. prototype . end = function ( fn
){
1127 var xhr
= this . xhr
= request
. getXHR ();
1128 var query
= this . _query
. join ( '&' );
1129 var timeout
= this . _timeout
;
1130 var data
= this . _formData
|| this . _data
;
1133 this . _callback
= fn
|| noop
;
1136 xhr
. onreadystatechange = function (){
1137 if ( 4 != xhr
. readyState
) return ;
1139 // In IE9, reads to any property (e.g. status) off of an aborted XHR will
1140 // result in the error "Could not complete the operation due to error c00c023f"
1142 try { status
= xhr
. status
} catch ( e
) { status
= 0 ; }
1145 if ( self
. timedout
) return self
. timeoutError ();
1146 if ( self
. aborted
) return ;
1147 return self
. crossDomainError ();
1153 var handleProgress = function ( e
){
1155 e
. percent
= e
. loaded
/ e
. total
* 100 ;
1157 e
. direction
= 'download' ;
1158 self
. emit ( 'progress' , e
);
1160 if ( this . hasListeners ( 'progress' )) {
1161 xhr
. onprogress
= handleProgress
;
1164 if ( xhr
. upload
&& this . hasListeners ( 'progress' )) {
1165 xhr
. upload
. onprogress
= handleProgress
;
1168 // Accessing xhr.upload fails in IE from a web worker, so just pretend it doesn't exist.
1170 // https://connect.microsoft.com/IE/feedback/details/837245/xmlhttprequest-upload-throws-invalid-argument-when-used-from-web-worker-context
1174 if ( timeout
&& ! this . _timer
) {
1175 this . _timer
= setTimeout ( function (){
1176 self
. timedout
= true ;
1183 query
= request
. serializeObject ( query
);
1184 this . url
+= ~ this . url
. indexOf ( '?' )
1190 xhr
. open ( this . method
, this . url
, true );
1193 if ( this . _withCredentials
) xhr
. withCredentials
= true ;
1196 if ( 'GET' != this . method
&& 'HEAD' != this . method
&& 'string' != typeof data
&& ! isHost ( data
)) {
1198 var contentType
= this . getHeader ( 'Content-Type' );
1199 var serialize
= this . _parser
|| request
. serialize
[ contentType
? contentType
. split ( ';' )[ 0 ] : '' ];
1200 if (! serialize
&& isJSON ( contentType
)) serialize
= request
. serialize
[ 'application/json' ];
1201 if ( serialize
) data
= serialize ( data
);
1204 // set header fields
1205 for ( var field
in this . header
) {
1206 if ( null == this . header
[ field
]) continue ;
1207 xhr
. setRequestHeader ( field
, this . header
[ field
]);
1211 this . emit ( 'request' , this );
1213 // IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing)
1214 // We need null here if data is undefined
1215 xhr
. send ( typeof data
!== 'undefined' ? data : null );
1220 * Faux promise support
1222 * @param {Function} fulfill
1223 * @param {Function} reject
1227 Request
. prototype . then = function ( fulfill
, reject
) {
1228 return this . end ( function ( err
, res
) {
1229 err
? reject ( err
) : fulfill ( res
);
1237 request
. Request
= Request
;
1244 * request('GET', '/users').end(callback)
1245 * request('/users').end(callback)
1246 * request('/users', callback)
1248 * @param {String} method
1249 * @param {String|Function} url or callback
1254 function request ( method
, url
) {
1256 if ( 'function' == typeof url
) {
1257 return new Request ( 'GET' , method
). end ( url
);
1261 if ( 1 == arguments
. length
) {
1262 return new Request ( 'GET' , method
);
1265 return new Request ( method
, url
);
1269 * GET `url` with optional callback `fn(res)`.
1271 * @param {String} url
1272 * @param {Mixed|Function} data or fn
1273 * @param {Function} fn
1278 request
. get = function ( url
, data
, fn
){
1279 var req
= request ( 'GET' , url
);
1280 if ( 'function' == typeof data
) fn
= data
, data
= null ;
1281 if ( data
) req
. query ( data
);
1282 if ( fn
) req
. end ( fn
);
1287 * HEAD `url` with optional callback `fn(res)`.
1289 * @param {String} url
1290 * @param {Mixed|Function} data or fn
1291 * @param {Function} fn
1296 request
. head = function ( url
, data
, fn
){
1297 var req
= request ( 'HEAD' , url
);
1298 if ( 'function' == typeof data
) fn
= data
, data
= null ;
1299 if ( data
) req
. send ( data
);
1300 if ( fn
) req
. end ( fn
);
1305 * DELETE `url` with optional callback `fn(res)`.
1307 * @param {String} url
1308 * @param {Function} fn
1313 function del ( url
, fn
){
1314 var req
= request ( 'DELETE' , url
);
1315 if ( fn
) req
. end ( fn
);
1319 request
[ 'del' ] = del
;
1320 request
[ 'delete' ] = del
;
1323 * PATCH `url` with optional `data` and callback `fn(res)`.
1325 * @param {String} url
1326 * @param {Mixed} data
1327 * @param {Function} fn
1332 request
. patch = function ( url
, data
, fn
){
1333 var req
= request ( 'PATCH' , url
);
1334 if ( 'function' == typeof data
) fn
= data
, data
= null ;
1335 if ( data
) req
. send ( data
);
1336 if ( fn
) req
. end ( fn
);
1341 * POST `url` with optional `data` and callback `fn(res)`.
1343 * @param {String} url
1344 * @param {Mixed} data
1345 * @param {Function} fn
1350 request
. post = function ( url
, data
, fn
){
1351 var req
= request ( 'POST' , url
);
1352 if ( 'function' == typeof data
) fn
= data
, data
= null ;
1353 if ( data
) req
. send ( data
);
1354 if ( fn
) req
. end ( fn
);
1359 * PUT `url` with optional `data` and callback `fn(res)`.
1361 * @param {String} url
1362 * @param {Mixed|Function} data or fn
1363 * @param {Function} fn
1368 request
. put = function ( url
, data
, fn
){
1369 var req
= request ( 'PUT' , url
);
1370 if ( 'function' == typeof data
) fn
= data
, data
= null ;
1371 if ( data
) req
. send ( data
);
1372 if ( fn
) req
. end ( fn
);
1380 module
. exports
= request
;
1382 },{ "emitter" : 1 , "reduce" : 2 }]},{},[ 3 ])( 3 )