Rename translation variables from _ENDPOINT to _BASE_URL

This commit is contained in:
Alex Gleason 2024-10-10 13:48:50 -05:00
parent b6f9fe5770
commit 9469fff6ac
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
7 changed files with 66 additions and 77 deletions

View file

@ -276,19 +276,19 @@ class Conf {
return Deno.env.get('TRANSLATION_PROVIDER');
}
/** DeepL URL endpoint. */
static get deepLendpoint(): string | undefined {
return Deno.env.get('DEEPL_ENDPOINT');
static get deeplBaseUrl(): string | undefined {
return Deno.env.get('DEEPL_BASE_URL');
}
/** DeepL API KEY. */
static get deepLapiKey(): string | undefined {
static get deeplApiKey(): string | undefined {
return Deno.env.get('DEEPL_API_KEY');
}
/** LibreTranslate URL endpoint. */
static get libreTranslateEndpoint(): string | undefined {
return Deno.env.get('LIBRETRANSLATE_ENDPOINT');
static get libretranslateBaseUrl(): string | undefined {
return Deno.env.get('LIBRETRANSLATE_BASE_URL');
}
/** LibreTranslate API KEY. */
static get libreTranslateApiKey(): string | undefined {
static get libretranslateApiKey(): string | undefined {
return Deno.env.get('LIBRETRANSLATE_API_KEY');
}
/** Cache settings. */

View file

@ -6,33 +6,22 @@ import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator
/** Set the translator used for translating posts. */
export const translatorMiddleware: AppMiddleware = async (c, next) => {
const deepLendpoint = Conf.deepLendpoint;
const deepLapiKey = Conf.deepLapiKey;
const libreTranslateEndpoint = Conf.libreTranslateEndpoint;
const libreTranslateApiKey = Conf.libreTranslateApiKey;
const translationProvider = Conf.translationProvider;
switch (Conf.translationProvider) {
case 'deepl': {
const { deeplApiKey: apiKey, deeplBaseUrl: baseUrl } = Conf;
if (apiKey) {
c.set('translator', new DeepLTranslator({ baseUrl, apiKey, fetch: fetchWorker }));
}
break;
}
switch (translationProvider) {
case 'deepl':
if (deepLapiKey) {
c.set(
'translator',
new DeepLTranslator({ endpoint: deepLendpoint, apiKey: deepLapiKey, fetch: fetchWorker }),
);
case 'libretranslate': {
const { libretranslateApiKey: apiKey, libretranslateBaseUrl: baseUrl } = Conf;
if (apiKey) {
c.set('translator', new LibreTranslateTranslator({ baseUrl, apiKey, fetch: fetchWorker }));
}
break;
case 'libretranslate':
if (libreTranslateApiKey) {
c.set(
'translator',
new LibreTranslateTranslator({
endpoint: libreTranslateEndpoint,
apiKey: libreTranslateApiKey,
fetch: fetchWorker,
}),
);
}
break;
}
await next();

View file

@ -4,15 +4,18 @@ import { Conf } from '@/config.ts';
import { DeepLTranslator } from '@/translators/DeepLTranslator.ts';
import { getLanguage } from '@/test.ts';
const endpoint = Conf.deepLendpoint;
const apiKey = Conf.deepLapiKey;
const translationProvider = Conf.translationProvider;
const deepL = 'deepl';
const {
deeplBaseUrl: baseUrl,
deeplApiKey: apiKey,
translationProvider,
} = Conf;
const deepl = 'deepl';
Deno.test('DeepL translation with source language omitted', {
ignore: !(translationProvider === deepL && apiKey),
ignore: !(translationProvider === deepl && apiKey),
}, async () => {
const translator = new DeepLTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string });
const translator = new DeepLTranslator({ fetch: fetch, baseUrl, apiKey: apiKey! });
const data = await translator.translate(
[
@ -31,9 +34,9 @@ Deno.test('DeepL translation with source language omitted', {
});
Deno.test('DeepL translation with source language set', {
ignore: !(translationProvider === deepL && apiKey),
ignore: !(translationProvider === deepl && apiKey),
}, async () => {
const translator = new DeepLTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string });
const translator = new DeepLTranslator({ fetch: fetch, baseUrl, apiKey: apiKey as string });
const data = await translator.translate(
[

View file

@ -5,8 +5,8 @@ import { DittoTranslator, SourceLanguage, TargetLanguage } from '@/translators/t
import { languageSchema } from '@/schema.ts';
interface DeepLTranslatorOpts {
/** DeepL endpoint to use. Default: 'https://api.deepl.com' */
endpoint?: string;
/** DeepL base URL to use. Default: 'https://api.deepl.com' */
baseUrl?: string;
/** DeepL API key. */
apiKey: string;
/** Custom fetch implementation. */
@ -14,13 +14,14 @@ interface DeepLTranslatorOpts {
}
export class DeepLTranslator implements DittoTranslator {
private readonly endpoint: string;
private readonly baseUrl: string;
private readonly apiKey: string;
private readonly fetch: typeof fetch;
private static provider = 'DeepL.com';
readonly provider = 'DeepL.com';
constructor(opts: DeepLTranslatorOpts) {
this.endpoint = opts.endpoint ?? 'https://api.deepl.com';
this.baseUrl = opts.baseUrl ?? 'https://api.deepl.com';
this.fetch = opts.fetch ?? globalThis.fetch;
this.apiKey = opts.apiKey;
}
@ -31,11 +32,11 @@ export class DeepLTranslator implements DittoTranslator {
dest: TargetLanguage,
opts?: { signal?: AbortSignal },
) {
const data = (await this.translateMany(texts, source, dest, opts)).translations;
const { translations } = await this.translateMany(texts, source, dest, opts);
return {
results: data.map((value) => value.text),
source_lang: data[0].detected_source_language as LanguageCode,
results: translations.map((value) => value.text),
source_lang: translations[0]?.detected_source_language as LanguageCode,
};
}
@ -56,25 +57,26 @@ export class DeepLTranslator implements DittoTranslator {
body.source_lang = source.toUpperCase();
}
const headers = new Headers();
headers.append('Authorization', 'DeepL-Auth-Key' + ' ' + this.apiKey);
headers.append('Content-Type', 'application/json');
const url = new URL('/v2/translate', this.baseUrl);
const request = new Request(this.endpoint + '/v2/translate', {
const request = new Request(url, {
method: 'POST',
body: JSON.stringify(body),
headers,
headers: {
'Authorization': `DeepL-Auth-Key ${this.apiKey}`,
'Content-Type': 'application/json',
},
signal: opts?.signal,
});
const response = await this.fetch(request);
const json = await response.json();
if (!response.ok) {
throw new Error(json['message']);
}
const data = DeepLTranslator.schema().parse(json);
return data;
return DeepLTranslator.schema().parse(json);
}
/** DeepL response schema.
@ -89,9 +91,4 @@ export class DeepLTranslator implements DittoTranslator {
),
});
}
/** DeepL provider. */
getProvider(): string {
return DeepLTranslator.provider;
}
}

View file

@ -4,15 +4,18 @@ import { Conf } from '@/config.ts';
import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator.ts';
import { getLanguage } from '@/test.ts';
const endpoint = Conf.libreTranslateEndpoint;
const apiKey = Conf.libreTranslateApiKey;
const translationProvider = Conf.translationProvider;
const libreTranslate = 'libretranslate';
const {
libretranslateBaseUrl: baseUrl,
libretranslateApiKey: apiKey,
translationProvider,
} = Conf;
const libretranslate = 'libretranslate';
Deno.test('LibreTranslate translation with source language omitted', {
ignore: !(translationProvider === libreTranslate && apiKey),
ignore: !(translationProvider === libretranslate && apiKey),
}, async () => {
const translator = new LibreTranslateTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string });
const translator = new LibreTranslateTranslator({ fetch: fetch, baseUrl, apiKey: apiKey! });
const data = await translator.translate(
[
@ -31,9 +34,9 @@ Deno.test('LibreTranslate translation with source language omitted', {
});
Deno.test('LibreTranslate translation with source language set', {
ignore: !(translationProvider === libreTranslate && apiKey),
ignore: !(translationProvider === libretranslate && apiKey),
}, async () => {
const translator = new LibreTranslateTranslator({ fetch: fetch, endpoint, apiKey: apiKey as string });
const translator = new LibreTranslateTranslator({ fetch: fetch, baseUrl, apiKey: apiKey! });
const data = await translator.translate(
[

View file

@ -6,7 +6,7 @@ import { languageSchema } from '@/schema.ts';
interface LibreTranslateTranslatorOpts {
/** Libretranslate endpoint to use. Default: 'https://libretranslate.com' */
endpoint?: string;
baseUrl?: string;
/** Libretranslate API key. */
apiKey: string;
/** Custom fetch implementation. */
@ -14,13 +14,14 @@ interface LibreTranslateTranslatorOpts {
}
export class LibreTranslateTranslator implements DittoTranslator {
private readonly endpoint: string;
private readonly baseUrl: string;
private readonly apiKey: string;
private readonly fetch: typeof fetch;
private static provider = 'libretranslate.com';
readonly provider = 'libretranslate.com';
constructor(opts: LibreTranslateTranslatorOpts) {
this.endpoint = opts.endpoint ?? 'https://libretranslate.com';
this.baseUrl = opts.baseUrl ?? 'https://libretranslate.com';
this.fetch = opts.fetch ?? globalThis.fetch;
this.apiKey = opts.apiKey;
}
@ -59,7 +60,7 @@ export class LibreTranslateTranslator implements DittoTranslator {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
const request = new Request(this.endpoint + '/translate', {
const request = new Request(new URL('/translate', this.baseUrl), {
method: 'POST',
body: JSON.stringify(body),
headers,
@ -87,9 +88,4 @@ export class LibreTranslateTranslator implements DittoTranslator {
}).optional(),
});
}
/** LibreTranslate provider. */
getProvider(): string {
return LibreTranslateTranslator.provider;
}
}

View file

@ -41,7 +41,8 @@ export interface DittoTranslator {
/** Custom options. */
opts?: { signal?: AbortSignal },
): Promise<{ results: string[]; source_lang: SourceLanguage }>;
getProvider(): string;
/** Provider name, eg `DeepL.com` */
provider: string;
}
/** Includes the TARGET language and the status id.