@@ -18,6 +18,7 @@ export type RequestOptions = {
18
18
installationId ? : string ;
19
19
batchSize ? : number ;
20
20
include ? : any ;
21
+ progress ? : any ;
21
22
} ;
22
23
23
24
export type FullOptions = {
@@ -26,6 +27,7 @@ export type FullOptions = {
26
27
useMasterKey ? : boolean ;
27
28
sessionToken ? : string ;
28
29
installationId ? : string ;
30
+ progress ? : any ;
29
31
} ;
30
32
31
33
var XHR = null ;
@@ -42,7 +44,7 @@ if (typeof XDomainRequest !== 'undefined' &&
42
44
useXDomainRequest = true ;
43
45
}
44
46
45
- function ajaxIE9 ( method : string , url : string , data : any ) {
47
+ function ajaxIE9 ( method : string , url : string , data : any , options ?: FullOptions ) {
46
48
return new Promise ( ( resolve , reject ) => {
47
49
var xdr = new XDomainRequest ( ) ;
48
50
xdr . onload = function ( ) {
@@ -66,16 +68,20 @@ function ajaxIE9(method: string, url: string, data: any) {
66
68
} ;
67
69
reject ( fakeResponse ) ;
68
70
} ;
69
- xdr . onprogress = function ( ) { } ;
71
+ xdr . onprogress = function ( ) {
72
+ if ( options && typeof options . progress === 'function' ) {
73
+ options . progress ( xdr . responseText ) ;
74
+ }
75
+ } ;
70
76
xdr . open ( method , url ) ;
71
77
xdr . send ( data ) ;
72
78
} ) ;
73
79
}
74
80
75
81
const RESTController = {
76
- ajax ( method : string , url : string , data : any , headers ?: any ) {
82
+ ajax ( method : string , url : string , data : any , headers ?: any , options ?: FullOptions ) {
77
83
if ( useXDomainRequest ) {
78
- return ajaxIE9 ( method , url , data , headers ) ;
84
+ return ajaxIE9 ( method , url , data , headers , options ) ;
79
85
}
80
86
81
87
var res , rej ;
@@ -141,7 +147,28 @@ const RESTController = {
141
147
' (NodeJS ' + process . versions . node + ')' ;
142
148
}
143
149
150
+ if ( options && typeof options . progress === 'function' ) {
151
+ if ( xhr . upload ) {
152
+ xhr . upload . addEventListener ( 'progress' , ( oEvent ) => {
153
+ if ( oEvent . lengthComputable ) {
154
+ options . progress ( oEvent . loaded / oEvent . total ) ;
155
+ } else {
156
+ options . progress ( null ) ;
157
+ }
158
+ } ) ;
159
+ } else if ( xhr . addEventListener ) {
160
+ xhr . addEventListener ( 'progress' , ( oEvent ) => {
161
+ if ( oEvent . lengthComputable ) {
162
+ options . progress ( oEvent . loaded / oEvent . total ) ;
163
+ } else {
164
+ options . progress ( null ) ;
165
+ }
166
+ } ) ;
167
+ }
168
+ }
169
+
144
170
xhr . open ( method , url , true ) ;
171
+
145
172
for ( var h in headers ) {
146
173
xhr . setRequestHeader ( h , headers [ h ] ) ;
147
174
}
@@ -225,8 +252,7 @@ const RESTController = {
225
252
}
226
253
227
254
var payloadString = JSON . stringify ( payload ) ;
228
-
229
- return RESTController . ajax ( method , url , payloadString ) . then ( ( { response } ) => {
255
+ return RESTController . ajax ( method , url , payloadString , { } , options ) . then ( ( { response } ) => {
230
256
return response ;
231
257
} ) ;
232
258
} ) . catch ( function ( response : { responseText : string } ) {
0 commit comments