From e7024b70f88a2601895608476ce916c610c08ce8 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Thu, 11 Apr 2024 17:01:12 +0800 Subject: [PATCH] feat: enable to use custom DeepL API URL --- package.json | 15 ++++++++++++--- src/preferences.ts | 5 +++-- src/translation/deepL.ts | 8 +++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 223e03e..6db04ab 100644 --- a/package.json +++ b/package.json @@ -669,7 +669,7 @@ "default": false }, { - "title": "Display Order", + "title": "Service List Order", "name": "servicesOrder", "type": "textfield", "required": false, @@ -690,8 +690,8 @@ "name": "openAIAPIURL", "type": "textfield", "required": false, - "description": "Your OpenAI API URL", - "default": "https://api.openai.com/v1/chat/completions" + "description": "Custom OpenAI API URL", + "placeholder": "https://api.openai.com/v1/chat/completions" }, { "title": "DeepL Auth Key", @@ -701,6 +701,15 @@ "description": "Your DeepL Auth Key", "default": "" }, + { + "title": "DeepL API URL", + "name": "deepLEndpoint", + "type": "textfield", + "required": false, + "description": "Custom DeepL API URL", + "placeholder": "https://api-free.deepl.com/v2/translate", + "default": "" + }, { "title": "Baidu App ID", "name": "baiduAppId", diff --git a/src/preferences.ts b/src/preferences.ts index 1ccfc2f..5706006 100644 --- a/src/preferences.ts +++ b/src/preferences.ts @@ -40,6 +40,7 @@ export interface MyPreferences { enableDeepLTranslate: boolean; deepLAuthKey: string; + deepLEndpoint: string; enableGoogleTranslate: boolean; @@ -74,6 +75,7 @@ export interface MyPreferences { */ export class AppKeyStore { static deepLAuthKey = myPreferences.deepLAuthKey.trim(); + static deepLEndpoint = myPreferences.deepLEndpoint.trim(); // This is a official test token from https://open.caiyunapp.com/%E4%BA%94%E5%88%86%E9%92%9F%E5%AD%A6%E4%BC%9A%E5%BD%A9%E4%BA%91%E5%B0%8F%E8%AF%91_API private static defaultEncryptedCaiyunToken = "U2FsdGVkX1+RTgfMmgZgkD1Phn4FyvzMiMed5BvxnjoqS8QIJ/AFjUJdfC7OqjU3"; @@ -95,9 +97,8 @@ export class AppKeyStore { static volcanoSecretId = myPreferences.volcanoAccessKeyId.trim(); static volcanoSecretKey = myPreferences.volcanoAccessKeySecret.trim(); - private static defaultOpenAIAPIURL = "https://api.openai.com/v1/chat/completions"; static openAIAPIKey = myPreferences.openAIAPIKey.trim(); - static openAIAPIURL = myPreferences.openAIAPIURL.trim() || this.defaultOpenAIAPIURL; + static openAIAPIURL = myPreferences.openAIAPIURL.trim() || "https://api.openai.com/v1/chat/completions"; } // Test AES online: https://www.sojson.com/encrypt_aes.html diff --git a/src/translation/deepL.ts b/src/translation/deepL.ts index a7b5467..6f6e03a 100644 --- a/src/translation/deepL.ts +++ b/src/translation/deepL.ts @@ -56,9 +56,15 @@ export async function requestDeepLTranslate(queryWordInfo: QueryWordInfo): Promi } // * deepL api free and deepL pro api use different url host. - const url = deepLAuthKey.endsWith(":fx") + let url = deepLAuthKey.endsWith(":fx") ? "https://api-free.deepl.com/v2/translate" : "https://api.deepl.com/v2/translate"; + + const deepLEndpoint = AppKeyStore.deepLEndpoint; + if (deepLEndpoint.length > 0) { + url = deepLEndpoint; + } + const params = { auth_key: deepLAuthKey, text: word,