@@ -106,14 +106,16 @@ export class HttpClient {
106106 url : string ,
107107 timeoutMs : number ,
108108 responseAsStream : boolean ,
109+ isDeepL : boolean ,
109110 options : SendRequestOptions ,
110111 ) : AxiosRequestConfig {
111112 const headers = Object . assign ( { } , this . headers , options . headers ) ;
113+ console . log ( `isDeepL: ${ isDeepL } ` ) ;
112114
113115 const axiosRequestConfig : AxiosRequestConfig = {
114- url,
116+ url : isDeepL ? url : undefined ,
115117 method,
116- baseURL : this . serverUrl ,
118+ baseURL : isDeepL ? this . serverUrl : url ,
117119 headers,
118120 responseType : responseAsStream ? 'stream' : 'text' ,
119121 timeout : timeoutMs ,
@@ -147,7 +149,7 @@ export class HttpClient {
147149 /**
148150 * Makes API request retrying if necessary, and returns (as Promise) response.
149151 * @param method HTTP method, for example 'GET'
150- * @param url Path to endpoint, excluding base server URL.
152+ * @param url Path to endpoint, excluding base server URL if DeepL API request, including base server URL if a webpage .
151153 * @param options Additional options controlling request.
152154 * @param responseAsStream Set to true if the return type is IncomingMessage.
153155 * @return Fulfills with status code and response (as text or stream).
@@ -157,9 +159,16 @@ export class HttpClient {
157159 url : string ,
158160 options ?: SendRequestOptions ,
159161 responseAsStream = false ,
160- ) : Promise < { statusCode : number ; content : TContent } > {
162+ ) : Promise < { statusCode : number ; content : TContent ; contentType ?: string } > {
163+ let isDeepLUrl : boolean ;
164+ try {
165+ isDeepLUrl = ! ! new URL ( url ) ;
166+ } catch {
167+ isDeepLUrl = true ;
168+ }
169+
161170 options = options === undefined ? { } : options ;
162- logInfo ( `Request to DeepL API ${ method } ${ url } ` ) ;
171+ logInfo ( `${ isDeepLUrl ? ' Request to DeepL API' : 'Request to webpage' } ${ method } ${ url } ` ) ;
163172 logDebug ( `Request details: ${ options . data } ` ) ;
164173 const backoff = new BackoffTimer ( ) ;
165174 let response , error ;
@@ -170,6 +179,7 @@ export class HttpClient {
170179 url ,
171180 timeoutMs ,
172181 responseAsStream ,
182+ isDeepLUrl ,
173183 options ,
174184 ) ;
175185 try {
@@ -199,8 +209,12 @@ export class HttpClient {
199209 }
200210
201211 if ( response !== undefined ) {
202- const { statusCode, content } = response ;
203- logInfo ( `DeepL API response ${ method } ${ url } ${ statusCode } ` ) ;
212+ const { statusCode, content, contentType } = response ;
213+ logInfo (
214+ `${
215+ isDeepLUrl ? 'DeepL API response' : 'Webpage response'
216+ } ${ method } ${ url } ${ statusCode } ${ ! isDeepLUrl ? ` ${ contentType } ` : '' } `,
217+ ) ;
204218 if ( ! responseAsStream ) {
205219 logDebug ( 'Response details:' , { content : content } ) ;
206220 }
@@ -217,7 +231,7 @@ export class HttpClient {
217231 */
218232 private static async sendAxiosRequest < TContent extends string | IncomingMessage > (
219233 axiosRequestConfig : AxiosRequestConfig ,
220- ) : Promise < { statusCode : number ; content : TContent } > {
234+ ) : Promise < { statusCode : number ; content : TContent ; contentType ?: string } > {
221235 try {
222236 const response = await axios . request ( axiosRequestConfig ) ;
223237
@@ -227,7 +241,17 @@ export class HttpClient {
227241 response . data = JSON . stringify ( response . data ) ;
228242 }
229243 }
230- return { statusCode : response . status , content : response . data } ;
244+
245+ let contentType : string | undefined = undefined ;
246+ if ( response . headers . getContentType ) {
247+ if ( typeof response . headers . getContentType === 'string' ) {
248+ contentType = response . headers . getContentType ;
249+ } else {
250+ contentType = response . headers . getContentType ( ) ?. toString ( ) ?? undefined ;
251+ }
252+ }
253+
254+ return { statusCode : response . status , content : response . data , contentType } ;
231255 } catch ( axios_error_raw ) {
232256 const axiosError = axios_error_raw as AxiosError ;
233257 const message : string = axiosError . message || '' ;
0 commit comments