@@ -54,6 +54,13 @@ if (typeof XDomainRequest !== 'undefined' && !('withCredentials' in new XMLHttpR
5454 useXDomainRequest = true ;
5555}
5656
57+ function getPath ( url : string , path : string ) {
58+ if ( url [ url . length - 1 ] !== '/' ) {
59+ url += '/' ;
60+ }
61+ return url + path ;
62+ }
63+
5764function ajaxIE9 ( method : string , url : string , data : any , _headers ?: any , options ?: FullOptions ) {
5865 return new Promise ( ( resolve , reject ) => {
5966 // @ts -ignore
@@ -140,6 +147,7 @@ const RESTController = {
140147 method,
141148 headers,
142149 signal,
150+ redirect : 'manual' ,
143151 } ;
144152 if ( data ) {
145153 fetchOptions . body = data ;
@@ -189,6 +197,9 @@ const RESTController = {
189197 } else if ( status >= 400 && status < 500 ) {
190198 const error = await response . json ( ) ;
191199 promise . reject ( error ) ;
200+ } else if ( status === 301 || status === 302 || status === 303 || status === 307 ) {
201+ const location = response . headers . get ( 'location' ) ;
202+ promise . resolve ( { status, location, method : status === 303 ? 'GET' : method } ) ;
192203 } else if ( status >= 500 || status === 0 ) {
193204 // retry on 5XX or library error
194205 if ( ++ attempts < CoreManager . get ( 'REQUEST_ATTEMPT_LIMIT' ) ) {
@@ -221,12 +232,7 @@ const RESTController = {
221232
222233 request ( method : string , path : string , data : any , options ?: RequestOptions ) {
223234 options = options || { } ;
224- let url = CoreManager . get ( 'SERVER_URL' ) ;
225- if ( url [ url . length - 1 ] !== '/' ) {
226- url += '/' ;
227- }
228- url += path ;
229-
235+ const url = getPath ( CoreManager . get ( 'SERVER_URL' ) , path ) ;
230236 const payload : Partial < PayloadType > = { } ;
231237 if ( data && typeof data === 'object' ) {
232238 for ( const k in data ) {
@@ -302,15 +308,18 @@ const RESTController = {
302308 }
303309
304310 const payloadString = JSON . stringify ( payload ) ;
305- return RESTController . ajax ( method , url , payloadString , { } , options ) . then (
306- ( { response, status, headers } ) => {
307- if ( options . returnStatus ) {
308- return { ...response , _status : status , _headers : headers } ;
309- } else {
310- return response ;
311- }
311+ return RESTController . ajax ( method , url , payloadString , { } , options ) . then ( async ( result ) => {
312+ if ( result . location ) {
313+ const newURL = getPath ( result . location , path ) ;
314+ result = await RESTController . ajax ( result . method , newURL , payloadString , { } , options ) ;
315+ }
316+ const { response, status, headers } = result ;
317+ if ( options . returnStatus ) {
318+ return { ...response , _status : status , _headers : headers } ;
319+ } else {
320+ return response ;
312321 }
313- ) ;
322+ } ) ;
314323 } )
315324 . catch ( RESTController . handleError ) ;
316325 } ,
0 commit comments