Skip to content

Commit

Permalink
feat: enable to use custom DeepL API URL
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Apr 11, 2024
1 parent 93ceeec commit e7024b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@
"default": false
},
{
"title": "Display Order",
"title": "Service List Order",
"name": "servicesOrder",
"type": "textfield",
"required": false,
Expand All @@ -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",
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface MyPreferences {

enableDeepLTranslate: boolean;
deepLAuthKey: string;
deepLEndpoint: string;

enableGoogleTranslate: boolean;

Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/translation/deepL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e7024b7

Please sign in to comment.