@@ -79,6 +79,10 @@ export interface CommonHttpClientOptions {
7979 * Process the error before throwing it. Can be used to add additional information to the error.
8080 */
8181 processError ?: ( error : Error ) => Error ;
82+ /**
83+ * Whether to follow redirects. Default is true.
84+ */
85+ followRedirects ?: boolean ;
8286}
8387
8488/**
@@ -119,14 +123,14 @@ export interface CommonHttpClientFetchRequest {
119123 * Credentials mode.
120124 */
121125 credentials : 'include' | 'omit' | 'same-origin' ;
122- /**
123- * Redirect mode.
124- */
125- redirect : 'error' | 'follow' | 'manual' ;
126126 /**
127127 * Custom request properties. Can be used to pass metadata to the fetch function.
128128 */
129129 customRequestProps ?: Record < string , unknown > ;
130+ /**
131+ * Redirect mode.
132+ */
133+ redirect : 'error' | 'follow' | 'manual' ;
130134}
131135
132136/**
@@ -198,7 +202,7 @@ export type CommonHttpClientRequest = Omit<
198202 * Request parameters serialization information.
199203 */
200204 parameters ?: CommonHttpClientRequestParameters ;
201- } & Partial < Pick < CommonHttpClientFetchRequest , 'cache' | 'credentials' | 'redirect' > > ;
205+ } & Partial < Pick < CommonHttpClientFetchRequest , 'cache' | 'credentials' > > ;
202206
203207/**
204208 * Response of the fetch function.
@@ -780,6 +784,25 @@ export class CommonHttpClient {
780784 try {
781785 return await this . performRequest ( request ) ;
782786 } catch ( e ) {
787+ const error = e as CommonHttpClientError ;
788+ if ( error . response ) {
789+ if (
790+ error . response . status > 300 &&
791+ error . response . status < 400 &&
792+ error . response . headers [ 'location' ] &&
793+ this . options . followRedirects !== false
794+ ) {
795+ const redirectUrl = new URL ( error . response . headers [ 'location' ] , error . url ) ;
796+ return this . request ( {
797+ method : error . response . status === 307 || error . response . status === 308 ? request . method : 'GET' ,
798+ path : redirectUrl . pathname ,
799+ query :
800+ redirectUrl . searchParams . size > 0
801+ ? Object . fromEntries ( redirectUrl . searchParams . entries ( ) )
802+ : undefined
803+ } ) ;
804+ }
805+ }
783806 if ( this . options . processError ) {
784807 throw this . options . processError ( e instanceof Error ? e : new Error ( String ( e ) ) ) ;
785808 }
@@ -835,7 +858,6 @@ export class CommonHttpClient {
835858 headers : requestHeaders ,
836859 cache,
837860 credentials,
838- redirect,
839861 ...otherRequestProps
840862 } = request ;
841863 const headers = this . cleanupHeaders ( requestHeaders ) ;
@@ -844,7 +866,7 @@ export class CommonHttpClient {
844866 headers,
845867 cache : cache ?? 'default' ,
846868 credentials : credentials ?? 'same-origin' ,
847- redirect : redirect ?? 'follow ',
869+ redirect : 'error ',
848870 body : this . getRequestBody ( request )
849871 } ;
850872 let attemptNumber = 1 ;
0 commit comments