deno.land / x / at_test@v8.1.0 / src / externalInterface / translator.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
type MakeUrl = (p: { txt: string; targetLang: string;}) => string;const makeUrl: MakeUrl = (p) => `https://api-free.deepl.com/v2/translate?text=${ encodeURI(p.txt) }&target_lang=${p.targetLang}`;
type TranslateResponse = | null | { translations: Array<{ detected_source_language: "EN"; text: string; }>; };
type Translate = (p: { txt: string; targetLang: string; translateAuthKey: string;}) => Promise<string | void | undefined>;/** * @see https://www.deepl.com/docs-api/translate-text/translate-text/ */export const translate: Translate = (p) => fetch(makeUrl(p), { method: "POST", headers: { Authorization: `DeepL-Auth-Key ${p.translateAuthKey}`, }, }) .then<TranslateResponse>((v) => v.ok ? v.json() : null) .then((v) => v?.translations.at(0)?.text) .catch(console.error);
at_test

Version Info

Tagged at
a year ago