@@ -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
+ logDebug ( `isDeepL: ${ isDeepL } ` ) ;
112
114
113
115
const axiosRequestConfig : AxiosRequestConfig = {
114
116
url,
115
117
method,
116
- baseURL : this . serverUrl ,
118
+ baseURL : isDeepL ? this . serverUrl : undefined ,
117
119
headers,
118
120
responseType : responseAsStream ? 'stream' : 'text' ,
119
121
timeout : timeoutMs ,
@@ -147,19 +149,26 @@ 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
- * @return Fulfills with status code and response (as text or stream).
155
+ * @return Fulfills with status code, content type, and response (as text or stream).
154
156
*/
155
157
async sendRequestWithBackoff < TContent extends string | IncomingMessage > (
156
158
method : HttpMethod ,
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,8 +179,14 @@ export class HttpClient {
170
179
url ,
171
180
timeoutMs ,
172
181
responseAsStream ,
182
+ isDeepLUrl ,
173
183
options ,
174
184
) ;
185
+
186
+ if ( ! isDeepLUrl && axiosRequestConfig . headers ) {
187
+ delete axiosRequestConfig . headers . Authorization ;
188
+ }
189
+
175
190
try {
176
191
response = await HttpClient . sendAxiosRequest < TContent > ( axiosRequestConfig ) ;
177
192
error = undefined ;
@@ -199,8 +214,12 @@ export class HttpClient {
199
214
}
200
215
201
216
if ( response !== undefined ) {
202
- const { statusCode, content } = response ;
203
- logInfo ( `DeepL API response ${ method } ${ url } ${ statusCode } ` ) ;
217
+ const { statusCode, content, contentType } = response ;
218
+ logInfo (
219
+ `${
220
+ isDeepLUrl ? 'DeepL API response' : 'Webpage response'
221
+ } ${ method } ${ url } ${ statusCode } ${ ! isDeepLUrl ? ` ${ contentType } ` : '' } `,
222
+ ) ;
204
223
if ( ! responseAsStream ) {
205
224
logDebug ( 'Response details:' , { content : content } ) ;
206
225
}
@@ -217,7 +236,7 @@ export class HttpClient {
217
236
*/
218
237
private static async sendAxiosRequest < TContent extends string | IncomingMessage > (
219
238
axiosRequestConfig : AxiosRequestConfig ,
220
- ) : Promise < { statusCode : number ; content : TContent } > {
239
+ ) : Promise < { statusCode : number ; content : TContent ; contentType ?: string } > {
221
240
try {
222
241
const response = await axios . request ( axiosRequestConfig ) ;
223
242
@@ -227,7 +246,12 @@ export class HttpClient {
227
246
response . data = JSON . stringify ( response . data ) ;
228
247
}
229
248
}
230
- return { statusCode : response . status , content : response . data } ;
249
+
250
+ return {
251
+ statusCode : response . status ,
252
+ content : response . data ,
253
+ contentType : response . headers [ 'content-type' ] ,
254
+ } ;
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