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'); return Deno.env.get('TRANSLATION_PROVIDER');
} }
/** DeepL URL endpoint. */ /** DeepL URL endpoint. */
static get deepLendpoint(): string | undefined { static get deeplBaseUrl(): string | undefined {
return Deno.env.get('DEEPL_ENDPOINT'); return Deno.env.get('DEEPL_BASE_URL');
} }
/** DeepL API KEY. */ /** DeepL API KEY. */
static get deepLapiKey(): string | undefined { static get deeplApiKey(): string | undefined {
return Deno.env.get('DEEPL_API_KEY'); return Deno.env.get('DEEPL_API_KEY');
} }
/** LibreTranslate URL endpoint. */ /** LibreTranslate URL endpoint. */
static get libreTranslateEndpoint(): string | undefined { static get libretranslateBaseUrl(): string | undefined {
return Deno.env.get('LIBRETRANSLATE_ENDPOINT'); return Deno.env.get('LIBRETRANSLATE_BASE_URL');
} }
/** LibreTranslate API KEY. */ /** LibreTranslate API KEY. */
static get libreTranslateApiKey(): string | undefined { static get libretranslateApiKey(): string | undefined {
return Deno.env.get('LIBRETRANSLATE_API_KEY'); return Deno.env.get('LIBRETRANSLATE_API_KEY');
} }
/** Cache settings. */ /** Cache settings. */

View file

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

View file

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

View file

@ -5,8 +5,8 @@ import { DittoTranslator, SourceLanguage, TargetLanguage } from '@/translators/t
import { languageSchema } from '@/schema.ts'; import { languageSchema } from '@/schema.ts';
interface DeepLTranslatorOpts { interface DeepLTranslatorOpts {
/** DeepL endpoint to use. Default: 'https://api.deepl.com' */ /** DeepL base URL to use. Default: 'https://api.deepl.com' */
endpoint?: string; baseUrl?: string;
/** DeepL API key. */ /** DeepL API key. */
apiKey: string; apiKey: string;
/** Custom fetch implementation. */ /** Custom fetch implementation. */
@ -14,13 +14,14 @@ interface DeepLTranslatorOpts {
} }
export class DeepLTranslator implements DittoTranslator { export class DeepLTranslator implements DittoTranslator {
private readonly endpoint: string; private readonly baseUrl: string;
private readonly apiKey: string; private readonly apiKey: string;
private readonly fetch: typeof fetch; private readonly fetch: typeof fetch;
private static provider = 'DeepL.com';
readonly provider = 'DeepL.com';
constructor(opts: DeepLTranslatorOpts) { 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.fetch = opts.fetch ?? globalThis.fetch;
this.apiKey = opts.apiKey; this.apiKey = opts.apiKey;
} }
@ -31,11 +32,11 @@ export class DeepLTranslator implements DittoTranslator {
dest: TargetLanguage, dest: TargetLanguage,
opts?: { signal?: AbortSignal }, opts?: { signal?: AbortSignal },
) { ) {
const data = (await this.translateMany(texts, source, dest, opts)).translations; const { translations } = await this.translateMany(texts, source, dest, opts);
return { return {
results: data.map((value) => value.text), results: translations.map((value) => value.text),
source_lang: data[0].detected_source_language as LanguageCode, source_lang: translations[0]?.detected_source_language as LanguageCode,
}; };
} }
@ -56,25 +57,26 @@ export class DeepLTranslator implements DittoTranslator {
body.source_lang = source.toUpperCase(); body.source_lang = source.toUpperCase();
} }
const headers = new Headers(); const url = new URL('/v2/translate', this.baseUrl);
headers.append('Authorization', 'DeepL-Auth-Key' + ' ' + this.apiKey);
headers.append('Content-Type', 'application/json');
const request = new Request(this.endpoint + '/v2/translate', { const request = new Request(url, {
method: 'POST', method: 'POST',
body: JSON.stringify(body), body: JSON.stringify(body),
headers, headers: {
'Authorization': `DeepL-Auth-Key ${this.apiKey}`,
'Content-Type': 'application/json',
},
signal: opts?.signal, signal: opts?.signal,
}); });
const response = await this.fetch(request); const response = await this.fetch(request);
const json = await response.json(); const json = await response.json();
if (!response.ok) { if (!response.ok) {
throw new Error(json['message']); throw new Error(json['message']);
} }
const data = DeepLTranslator.schema().parse(json);
return data; return DeepLTranslator.schema().parse(json);
} }
/** DeepL response schema. /** 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 { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator.ts';
import { getLanguage } from '@/test.ts'; import { getLanguage } from '@/test.ts';
const endpoint = Conf.libreTranslateEndpoint; const {
const apiKey = Conf.libreTranslateApiKey; libretranslateBaseUrl: baseUrl,
const translationProvider = Conf.translationProvider; libretranslateApiKey: apiKey,
const libreTranslate = 'libretranslate'; translationProvider,
} = Conf;
const libretranslate = 'libretranslate';
Deno.test('LibreTranslate translation with source language omitted', { Deno.test('LibreTranslate translation with source language omitted', {
ignore: !(translationProvider === libreTranslate && apiKey), ignore: !(translationProvider === libretranslate && apiKey),
}, async () => { }, 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( 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', { Deno.test('LibreTranslate translation with source language set', {
ignore: !(translationProvider === libreTranslate && apiKey), ignore: !(translationProvider === libretranslate && apiKey),
}, async () => { }, 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( const data = await translator.translate(
[ [

View file

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

View file

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