From 01156d44bf169b037709995f23036c8e8778183d Mon Sep 17 00:00:00 2001 From: ABHISHEK FADAKE <31249309+fadkeabhi@users.noreply.github.com> Date: Tue, 29 Oct 2024 20:54:39 +0530 Subject: [PATCH] fix: Support both DeepL Free and Pro API URLs (#81) * 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 --- README.md | 4 ++-- src/modules/functions.ts | 6 +++++- src/modules/modules.ts | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dc176ab..17fb2f2 100644 --- a/README.md +++ b/README.md @@ -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)`
`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)` | @@ -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)`
`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)` | diff --git a/src/modules/functions.ts b/src/modules/functions.ts index bac7c1b..9a5d04d 100644 --- a/src/modules/functions.ts +++ b/src/modules/functions.ts @@ -144,9 +144,13 @@ export async function translateWithDeepL( to: string ): Promise { 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)], @@ -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: { diff --git a/src/modules/modules.ts b/src/modules/modules.ts index df3cffd..1c56903 100644 --- a/src/modules/modules.ts +++ b/src/modules/modules.ts @@ -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, },