1
1
"use strict"
2
2
3
3
var buildQueryString = require ( "../querystring/build" )
4
+ var Promise = require ( "../promise/promise" )
4
5
5
6
module . exports = function ( $window , Stream ) {
6
7
var callbackCount = 0
7
8
8
9
var oncompletion
9
10
function setCompletionCallback ( callback ) { oncompletion = callback }
10
-
11
- function request ( args ) {
11
+
12
+ function request ( args , extra ) {
13
+ if ( typeof args === "string" ) {
14
+ var url = args
15
+
16
+ if ( typeof extra === "object" ) args = extra
17
+ else args = { }
18
+
19
+ if ( typeof args . url === "undefined" ) args . url = url
20
+ }
21
+
22
+ if ( typeof args . method === "undefined" ) args . method = "GET"
23
+
12
24
var stream = Stream ( )
13
25
if ( args . initialValue !== undefined ) stream ( args . initialValue )
14
26
args . method = args . method . toUpperCase ( )
15
-
27
+
16
28
var useBody = typeof args . useBody === "boolean" ? args . useBody : args . method !== "GET" && args . method !== "TRACE"
17
-
29
+
18
30
if ( typeof args . serialize !== "function" ) args . serialize = typeof FormData !== "undefined" && args . data instanceof FormData ? function ( value ) { return value } : JSON . stringify
19
31
if ( typeof args . deserialize !== "function" ) args . deserialize = deserialize
20
32
if ( typeof args . extract !== "function" ) args . extract = extract
21
-
33
+
22
34
args . url = interpolate ( args . url , args . data )
23
35
if ( useBody ) args . data = args . serialize ( args . data )
24
36
else args . url = assemble ( args . url , args . data )
25
-
37
+
26
38
var xhr = new $window . XMLHttpRequest ( )
27
39
xhr . open ( args . method , args . url , typeof args . async === "boolean" ? args . async : true , typeof args . user === "string" ? args . user : undefined , typeof args . password === "string" ? args . password : undefined )
28
-
40
+
29
41
if ( args . serialize === JSON . stringify && useBody ) {
30
42
xhr . setRequestHeader ( "Content-Type" , "application/json; charset=utf-8" )
31
43
}
32
44
if ( args . deserialize === deserialize ) {
33
45
xhr . setRequestHeader ( "Accept" , "application/json, text/*" )
34
46
}
35
-
47
+
36
48
if ( typeof args . config === "function" ) xhr = args . config ( xhr , args ) || xhr
37
-
49
+
38
50
xhr . onreadystatechange = function ( ) {
39
51
if ( xhr . readyState === 4 ) {
40
52
try {
@@ -54,17 +66,17 @@ module.exports = function($window, Stream) {
54
66
if ( typeof oncompletion === "function" ) oncompletion ( )
55
67
}
56
68
}
57
-
69
+
58
70
if ( useBody && ( args . data != null ) ) xhr . send ( args . data )
59
71
else xhr . send ( )
60
-
72
+
61
73
return stream
62
74
}
63
75
64
76
function jsonp ( args ) {
65
77
var stream = Stream ( )
66
78
if ( args . initialValue !== undefined ) stream ( args . initialValue )
67
-
79
+
68
80
var callbackName = args . callbackName || "_mithril_" + Math . round ( Math . random ( ) * 1e16 ) + "_" + callbackCount ++
69
81
var script = $window . document . createElement ( "script" )
70
82
$window [ callbackName ] = function ( data ) {
@@ -116,7 +128,7 @@ module.exports = function($window, Stream) {
116
128
}
117
129
118
130
function extract ( xhr ) { return xhr . responseText }
119
-
131
+
120
132
function cast ( type , data ) {
121
133
if ( typeof type === "function" ) {
122
134
if ( data instanceof Array ) {
@@ -128,6 +140,6 @@ module.exports = function($window, Stream) {
128
140
}
129
141
return data
130
142
}
131
-
143
+
132
144
return { request : request , jsonp : jsonp , setCompletionCallback : setCompletionCallback }
133
145
}
0 commit comments