Skip to content

Commit

Permalink
fix: Support both DeepL Free and Pro API URLs (#81)
Browse files Browse the repository at this point in the history
* fix: Support both DeepL Free and Pro API URLs

Add DEEPL_API_URL env var to override default api-free.deepl.com
Fixes #78

* Remove Console log
  • Loading branch information
fadkeabhi authored Oct 29, 2024
1 parent 12d0a68 commit 01156d4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This package will provide you to translate your JSON/YAML files or JSON objects
| Microsoft Bing Translate || `✅ FREE` |
| Libre Translate || `✅ FREE` |
| Argos Translate || `✅ FREE` |
| DeepL Translate || `require API KEY (DEEPL_API_KEY as env)` |
| DeepL Translate || `require API KEY (DEEPL_API_KEY as env)` </br> `optional API URL (DEEPL_API_URL as env)` |
| gpt-4o || `require API KEY (OPENAI_API_KEY as env)` |
| gpt-3.5-turbo || `require API KEY (OPENAI_API_KEY as env)` |
| gpt-4 || `require API KEY (OPENAI_API_KEY as env)` |
Expand All @@ -60,7 +60,7 @@ This package will provide you to translate your JSON/YAML files or JSON objects
| Microsoft Bing Translate || `✅ FREE` |
| Libre Translate || `✅ FREE` |
| Argos Translate || `✅ FREE` |
| DeepL Translate || `require API KEY (DEEPL_API_KEY as env)` |
| DeepL Translate || `require API KEY (DEEPL_API_KEY as env)` </br> `optional API URL (DEEPL_API_URL as env)` |
| gpt-4o || `require API KEY (OPENAI_API_KEY as env)` |
| gpt-3.5-turbo || `require API KEY (OPENAI_API_KEY as env)` |
| gpt-4 || `require API KEY (OPENAI_API_KEY as env)` |
Expand Down
6 changes: 5 additions & 1 deletion src/modules/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ export async function translateWithDeepL(
to: string
): Promise<string> {
const DEEPL_API_KEY = process.env.DEEPL_API_KEY;
const DEEPL_API_URL = process.env.DEEPL_API_URL || "api-free.deepl.com";
if (!DEEPL_API_KEY) {
warn('process.env.DEEPL_API_KEY is not defined');
}
if (!process.env.DEEPL_API_URL) {
warn('process.env.DEEPL_API_URL is not defined, using api-free.deepl.com as default');
}

const body = {
text: [safeValueTransition(str)],
Expand All @@ -155,7 +159,7 @@ export async function translateWithDeepL(
};

const { data } = await axios.post(
'https://api-free.deepl.com/v2/translate',
`https://${DEEPL_API_URL}/v2/translate`,
body,
{
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const TranslationModules: TranslationModulesType = {
deepl: {
name: 'DeepL Translate',
altName: 'DeepL Translate (29 languages)',
requirements: ['"DEEPL_API_KEY" as env'],
requirements: ['"DEEPL_API_KEY" and "DEEPL_API_URL" as env'],
languages: DeepLTranslateLanguages,
translate: translateWithDeepL,
},
Expand Down

0 comments on commit 01156d4

Please sign in to comment.