Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
SleeplessOne1917 committed May 29, 2023
1 parent 52b7e36 commit d2644e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class HttpClient {
options: SendRequestOptions,
): AxiosRequestConfig {
const headers = Object.assign({}, this.headers, options.headers);
console.log(`isDeepL: ${isDeepL}`);
logDebug(`isDeepL: ${isDeepL}`);

const axiosRequestConfig: AxiosRequestConfig = {
url: isDeepL ? url : undefined,
Expand Down Expand Up @@ -152,7 +152,7 @@ export class HttpClient {
* @param url Path to endpoint, excluding base server URL if DeepL API request, including base server URL if a webpage.
* @param options Additional options controlling request.
* @param responseAsStream Set to true if the return type is IncomingMessage.
* @return Fulfills with status code and response (as text or stream).
* @return Fulfills with status code, content type, and response (as text or stream).
*/
async sendRequestWithBackoff<TContent extends string | IncomingMessage>(
method: HttpMethod,
Expand Down Expand Up @@ -182,6 +182,11 @@ export class HttpClient {
isDeepLUrl,
options,
);

if (!isDeepLUrl && axiosRequestConfig.headers) {
delete axiosRequestConfig.headers.Authorization;
}

try {
response = await HttpClient.sendAxiosRequest<TContent>(axiosRequestConfig);
error = undefined;
Expand Down Expand Up @@ -251,7 +256,11 @@ export class HttpClient {
}
}

return { statusCode: response.status, content: response.data, contentType };
return {
statusCode: response.status,
content: response.data,
contentType: contentType,
};
} catch (axios_error_raw) {
const axiosError = axios_error_raw as AxiosError;
const message: string = axiosError.message || '';
Expand Down
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ export class DocumentTranslationError extends DeepLError {
export class GlossaryNotFoundError extends DeepLError {}

export class DocumentNotReadyError extends DeepLError {}

export class WebsiteDownloadError extends DeepLError {}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GlossaryNotFoundError,
QuotaExceededError,
TooManyRequestsError,
WebsiteDownloadError,
} from './errors';
import { GlossaryEntries } from './glossaryEntries';
import {
Expand Down Expand Up @@ -1062,7 +1063,7 @@ export class Translator {
await checkStatusCode(statusCode, content);

if (!contentType?.includes('text/html')) {
throw new Error('URL to translate must return HTML');
throw new WebsiteDownloadError('URL to translate must return HTML');
}

return content;
Expand Down

0 comments on commit d2644e2

Please sign in to comment.