@@ -106,14 +106,16 @@ export class HttpClient {
106
106
url : string ,
107
107
timeoutMs : number ,
108
108
responseAsStream : boolean ,
109
+ isDeepL : boolean ,
109
110
options : SendRequestOptions ,
110
111
) : AxiosRequestConfig {
111
112
const headers = Object . assign ( { } , this . headers , options . headers ) ;
113
+ console . log ( `isDeepL: ${ isDeepL } ` ) ;
112
114
113
115
const axiosRequestConfig : AxiosRequestConfig = {
114
- url,
116
+ url : isDeepL ? url : undefined ,
115
117
method,
116
- baseURL : this . serverUrl ,
118
+ baseURL : isDeepL ? this . serverUrl : url ,
117
119
headers,
118
120
responseType : responseAsStream ? 'stream' : 'text' ,
119
121
timeout : timeoutMs ,
@@ -147,7 +149,7 @@ export class HttpClient {
147
149
/**
148
150
* Makes API request retrying if necessary, and returns (as Promise) response.
149
151
* @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 .
151
153
* @param options Additional options controlling request.
152
154
* @param responseAsStream Set to true if the return type is IncomingMessage.
153
155
* @return Fulfills with status code and response (as text or stream).
@@ -157,9 +159,16 @@ export class HttpClient {
157
159
url : string ,
158
160
options ?: SendRequestOptions ,
159
161
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
+
161
170
options = options === undefined ? { } : options ;
162
- logInfo ( `Request to DeepL API ${ method } ${ url } ` ) ;
171
+ logInfo ( `${ isDeepLUrl ? ' Request to DeepL API' : 'Request to webpage' } ${ method } ${ url } ` ) ;
163
172
logDebug ( `Request details: ${ options . data } ` ) ;
164
173
const backoff = new BackoffTimer ( ) ;
165
174
let response , error ;
@@ -170,6 +179,7 @@ export class HttpClient {
170
179
url ,
171
180
timeoutMs ,
172
181
responseAsStream ,
182
+ isDeepLUrl ,
173
183
options ,
174
184
) ;
175
185
try {
@@ -199,8 +209,12 @@ export class HttpClient {
199
209
}
200
210
201
211
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
+ ) ;
204
218
if ( ! responseAsStream ) {
205
219
logDebug ( 'Response details:' , { content : content } ) ;
206
220
}
@@ -217,7 +231,7 @@ export class HttpClient {
217
231
*/
218
232
private static async sendAxiosRequest < TContent extends string | IncomingMessage > (
219
233
axiosRequestConfig : AxiosRequestConfig ,
220
- ) : Promise < { statusCode : number ; content : TContent } > {
234
+ ) : Promise < { statusCode : number ; content : TContent ; contentType ?: string } > {
221
235
try {
222
236
const response = await axios . request ( axiosRequestConfig ) ;
223
237
@@ -227,7 +241,17 @@ export class HttpClient {
227
241
response . data = JSON . stringify ( response . data ) ;
228
242
}
229
243
}
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 } ;
231
255
} catch ( axios_error_raw ) {
232
256
const axiosError = axios_error_raw as AxiosError ;
233
257
const message : string = axiosError . message || '' ;
0 commit comments