-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdeeplClient.ts
71 lines (62 loc) · 2.22 KB
/
deeplClient.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2025 DeepL SE (https://www.deepl.com)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
import { Translator, checkStatusCode } from './translator';
import { SourceLanguageCode, DeepLClientOptions, WriteResult } from './types';
import { parseWriteResultArray } from './parsing';
import { appendTextsAndReturnIsSingular } from './utils';
export enum WritingStyle {
ACADEMIC = 'academic',
BUSINESS = 'business',
CASUAL = 'casual',
DEFAULT = 'default',
PREFER_ACADEMIC = 'prefer_academic',
PREFER_BUSINESS = 'prefer_business',
PREFER_CASUAL = 'prefer_casual',
PREFER_SIMPLE = 'prefer_simple',
SIMPLE = 'simple',
}
export enum WritingTone {
CONFIDENT = 'confident',
DEFAULT = 'default',
DIPLOMATIC = 'diplomatic',
ENTHUSIASTIC = 'enthusiastic',
FRIENDLY = 'friendly',
PREFER_CONFIDENT = 'prefer_confident',
PREFER_DIPLOMATIC = 'prefer_diplomatic',
PREFER_ENTHUSIASTIC = 'prefer_enthusiastic',
PREFER_FRIENDLY = 'prefer_friendly',
}
export class DeepLClient extends Translator {
constructor(authKey: string, options: DeepLClientOptions = {}) {
super(authKey, options);
}
async rephraseText<T extends string | string[]>(
texts: T,
targetLang: SourceLanguageCode | null,
writingStyle?: string | null,
tone?: string | null,
): Promise<T extends string ? WriteResult : WriteResult[]> {
const data = new URLSearchParams();
if (targetLang) {
data.append('target_lang', targetLang);
}
if (writingStyle) {
data.append('writing_style', writingStyle);
}
if (tone) {
data.append('tone', tone);
}
const singular = appendTextsAndReturnIsSingular(data, texts);
const { statusCode, content } = await this.httpClient.sendRequestWithBackoff<string>(
'POST',
'/v2/write/rephrase',
{ data },
);
await checkStatusCode(statusCode, content);
const writeResults = parseWriteResultArray(content);
return (singular ? writeResults[0] : writeResults) as T extends string
? WriteResult
: WriteResult[];
}
}