@deeplx/core 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -3
- package/lib/api.d.ts +5 -1
- package/lib/api.js +2 -2
- package/lib/api.js.map +1 -1
- package/lib/constants.d.ts +64 -22
- package/lib/constants.js +72 -26
- package/lib/constants.js.map +1 -1
- package/lib/index.cjs +269 -191
- package/lib/translate.d.ts +1 -1
- package/lib/translate.js +155 -129
- package/lib/translate.js.map +1 -1
- package/lib/types.d.ts +17 -60
- package/lib/utils.d.ts +0 -5
- package/lib/utils.js +0 -20
- package/lib/utils.js.map +1 -1
- package/package.json +3 -7
package/lib/translate.js
CHANGED
|
@@ -1,148 +1,174 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export const translateByDeepLX = async (sourceLang, targetLang, text, formal, tagHandling = 'plaintext', proxyUrl, dlSession) => {
|
|
16
|
-
if (!text) {
|
|
17
|
-
return { code: HTTP_STATUS_NOT_FOUND, message: 'No text to translate' };
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { createProxy } from 'node-fetch-native/proxy';
|
|
3
|
+
import { ResponseError, xfetch } from 'x-fetch';
|
|
4
|
+
import { ONESHOT_FREE_ENDPOINT, ONESHOT_PRO_ENDPOINT, CHROME_EXTENSION_ID, CHROME_EXTENSION_VERSION, IMPERSONATED_CHROME_MAJOR, MAX_FREE_TEXT_LENGTH, HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK, HTTP_STATUS_SERVICE_UNAVAILABLE, HTTP_STATUS_BAD_REQUEST, HTTP_STATUS_PAYLOAD_TOO_LARGE, HTTP_STATUS_TOO_MANY_REQUESTS, TARGET_LANG_MAP, SOURCE_LANG_MAP, } from "./constants.js";
|
|
5
|
+
import { abbreviateLanguage } from "./utils.js";
|
|
6
|
+
let instanceID_;
|
|
7
|
+
function getInstanceID() {
|
|
8
|
+
return (instanceID_ ??= randomUUID());
|
|
9
|
+
}
|
|
10
|
+
let sharedCookies = '';
|
|
11
|
+
let warmupPromise = null;
|
|
12
|
+
async function warmCookies(proxyUrl) {
|
|
13
|
+
if (warmupPromise !== null) {
|
|
14
|
+
return warmupPromise;
|
|
18
15
|
}
|
|
19
|
-
|
|
20
|
-
const translatedParts = [];
|
|
21
|
-
const allAlternatives = [];
|
|
22
|
-
for (const part of textParts) {
|
|
23
|
-
if (!part.trim()) {
|
|
24
|
-
translatedParts.push('');
|
|
25
|
-
allAlternatives.push(['']);
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
if (!sourceLang || sourceLang === 'auto') {
|
|
29
|
-
sourceLang = detectLang(part, true);
|
|
30
|
-
}
|
|
31
|
-
const sourceLangCode = abbreviateLanguage(sourceLang) ??
|
|
32
|
-
sourceLang.toUpperCase();
|
|
33
|
-
const jobs = [
|
|
34
|
-
{
|
|
35
|
-
kind: 'default',
|
|
36
|
-
preferred_num_beams: 4,
|
|
37
|
-
raw_en_context_before: [],
|
|
38
|
-
raw_en_context_after: [],
|
|
39
|
-
sentences: [{ prefix: '', text: part, id: 0 }],
|
|
40
|
-
},
|
|
41
|
-
];
|
|
42
|
-
let hasRegionalVariant = false;
|
|
43
|
-
let targetLangCode = abbreviateLanguage(targetLang) ??
|
|
44
|
-
targetLang.toUpperCase();
|
|
45
|
-
const targetLangParts = targetLang.split('-');
|
|
46
|
-
if (targetLangParts.length > 1) {
|
|
47
|
-
targetLangCode = targetLangParts[0];
|
|
48
|
-
hasRegionalVariant = true;
|
|
49
|
-
}
|
|
50
|
-
const id = getRandomNumber();
|
|
51
|
-
const postData = {
|
|
52
|
-
jsonrpc: '2.0',
|
|
53
|
-
method: 'LMT_handle_jobs',
|
|
54
|
-
id,
|
|
55
|
-
params: {
|
|
56
|
-
commonJobParams: {
|
|
57
|
-
mode: 'translate',
|
|
58
|
-
formality: formal == null ? 'undefined' : formal ? 'formal' : 'informal',
|
|
59
|
-
transcribe_as: 'romanize',
|
|
60
|
-
advancedMode: false,
|
|
61
|
-
textType: tagHandling,
|
|
62
|
-
wasSpoken: false,
|
|
63
|
-
...(hasRegionalVariant && { regionalVariant: targetLang }),
|
|
64
|
-
},
|
|
65
|
-
lang: {
|
|
66
|
-
source_lang_user_selected: 'auto',
|
|
67
|
-
target_lang: targetLangCode,
|
|
68
|
-
source_lang_computed: sourceLangCode,
|
|
69
|
-
},
|
|
70
|
-
jobs,
|
|
71
|
-
timestamp: getTimeStamp(getICount(part)),
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
let translations;
|
|
16
|
+
warmupPromise = (async () => {
|
|
75
17
|
try {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
partTranslation = partTranslation.trim();
|
|
92
|
-
const numBeams = translations[0].beams.length;
|
|
93
|
-
for (let i = 1; i < numBeams; i++) {
|
|
94
|
-
let altText = '';
|
|
95
|
-
for (const translation of translations) {
|
|
96
|
-
const beams = translation.beams;
|
|
97
|
-
if (i < beams.length) {
|
|
98
|
-
altText += beams[i].sentences[0].text + ' ';
|
|
99
|
-
}
|
|
18
|
+
const res = await xfetch('https://www.deepl.com/translator', {
|
|
19
|
+
type: null,
|
|
20
|
+
...createProxy({ url: proxyUrl }),
|
|
21
|
+
});
|
|
22
|
+
const setCookie = res.headers.get('set-cookie');
|
|
23
|
+
if (setCookie) {
|
|
24
|
+
const cookies = [];
|
|
25
|
+
const userCountryMatch = /userCountry=[^;]+/.exec(setCookie);
|
|
26
|
+
if (userCountryMatch) {
|
|
27
|
+
cookies.push(userCountryMatch[0]);
|
|
28
|
+
}
|
|
29
|
+
const verifiedBotMatch = /verifiedBot=[^;]+/.exec(setCookie);
|
|
30
|
+
if (verifiedBotMatch) {
|
|
31
|
+
cookies.push(verifiedBotMatch[0]);
|
|
100
32
|
}
|
|
101
|
-
if (
|
|
102
|
-
|
|
33
|
+
if (cookies.length > 0) {
|
|
34
|
+
sharedCookies = cookies.join('; ');
|
|
103
35
|
}
|
|
104
36
|
}
|
|
105
37
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
38
|
+
catch {
|
|
39
|
+
warmupPromise = null;
|
|
40
|
+
}
|
|
41
|
+
})();
|
|
42
|
+
return warmupPromise;
|
|
43
|
+
}
|
|
44
|
+
function resolveLang(code, kind) {
|
|
45
|
+
if (!code || code.toLowerCase() === 'auto') {
|
|
46
|
+
if (kind === 'target') {
|
|
47
|
+
return { success: false, error: 'target_lang cannot be "auto" or empty' };
|
|
111
48
|
}
|
|
112
|
-
|
|
113
|
-
allAlternatives.push(partAlternatives);
|
|
49
|
+
return { success: true, value: undefined };
|
|
114
50
|
}
|
|
115
|
-
const
|
|
116
|
-
const
|
|
117
|
-
let
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
51
|
+
const langMap = kind === 'target' ? TARGET_LANG_MAP : SOURCE_LANG_MAP;
|
|
52
|
+
const upperCode = code.toUpperCase();
|
|
53
|
+
let mapped = langMap[upperCode];
|
|
54
|
+
if (!mapped) {
|
|
55
|
+
const abbreviated = abbreviateLanguage(code);
|
|
56
|
+
if (abbreviated) {
|
|
57
|
+
mapped = langMap[abbreviated.toUpperCase()];
|
|
121
58
|
}
|
|
122
59
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
altParts.push(translatedParts[j]);
|
|
134
|
-
}
|
|
60
|
+
if (!mapped) {
|
|
61
|
+
return { success: false, error: `unsupported ${kind}_lang "${code}"` };
|
|
62
|
+
}
|
|
63
|
+
return { success: true, value: mapped };
|
|
64
|
+
}
|
|
65
|
+
function parseTranslationError(error, reqId) {
|
|
66
|
+
if (error instanceof ResponseError) {
|
|
67
|
+
const status = error.response.status;
|
|
68
|
+
if (status === HTTP_STATUS_TOO_MANY_REQUESTS) {
|
|
69
|
+
return { code: status, id: reqId, message: 'too many requests, ...' };
|
|
135
70
|
}
|
|
136
|
-
|
|
71
|
+
return { code: status, id: reqId, message: error.message };
|
|
137
72
|
}
|
|
73
|
+
return {
|
|
74
|
+
code: HTTP_STATUS_SERVICE_UNAVAILABLE,
|
|
75
|
+
id: reqId,
|
|
76
|
+
message: String(error),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function buildHeaders(dlSession) {
|
|
80
|
+
const authValue = dlSession ? `Bearer ${dlSession}` : 'None';
|
|
81
|
+
const headers = {
|
|
82
|
+
'Content-Type': 'application/json',
|
|
83
|
+
Accept: '*/*',
|
|
84
|
+
Authorization: authValue,
|
|
85
|
+
Origin: `chrome-extension://${CHROME_EXTENSION_ID}`,
|
|
86
|
+
'Sec-Fetch-Site': 'cross-site',
|
|
87
|
+
'Sec-Fetch-Mode': 'cors',
|
|
88
|
+
'Sec-Fetch-Dest': 'empty',
|
|
89
|
+
'Accept-Encoding': 'gzip, deflate, br',
|
|
90
|
+
'User-Agent': `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${IMPERSONATED_CHROME_MAJOR}.0.0.0 Safari/537.36`,
|
|
91
|
+
};
|
|
92
|
+
if (sharedCookies) {
|
|
93
|
+
headers['Cookie'] = sharedCookies;
|
|
94
|
+
}
|
|
95
|
+
return headers;
|
|
96
|
+
}
|
|
97
|
+
function processTranslationResponse(response, reqId, sourceLang, targetLang, dlSession) {
|
|
98
|
+
if (!response?.translations || response.translations.length === 0) {
|
|
99
|
+
return {
|
|
100
|
+
code: HTTP_STATUS_SERVICE_UNAVAILABLE,
|
|
101
|
+
id: reqId,
|
|
102
|
+
message: 'Translation failed',
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const mainTranslation = response.translations[0];
|
|
106
|
+
if (!mainTranslation.text) {
|
|
107
|
+
return {
|
|
108
|
+
code: HTTP_STATUS_SERVICE_UNAVAILABLE,
|
|
109
|
+
id: reqId,
|
|
110
|
+
message: 'Translation failed',
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const detectedLang = mainTranslation.detected_source_language
|
|
114
|
+
? mainTranslation.detected_source_language.toUpperCase()
|
|
115
|
+
: sourceLang || 'auto';
|
|
138
116
|
return {
|
|
139
117
|
code: HTTP_STATUS_OK,
|
|
140
|
-
id:
|
|
141
|
-
data:
|
|
142
|
-
alternatives:
|
|
143
|
-
sourceLang:
|
|
118
|
+
id: reqId,
|
|
119
|
+
data: mainTranslation.text,
|
|
120
|
+
alternatives: [],
|
|
121
|
+
sourceLang: detectedLang,
|
|
144
122
|
targetLang,
|
|
145
123
|
method: dlSession ? 'Pro' : 'Free',
|
|
146
124
|
};
|
|
125
|
+
}
|
|
126
|
+
export const translateByDeepLX = async (sourceLang, targetLang, text, proxyUrl, dlSession) => {
|
|
127
|
+
if (!text) {
|
|
128
|
+
return { code: HTTP_STATUS_NOT_FOUND, message: 'No text to translate' };
|
|
129
|
+
}
|
|
130
|
+
if ([...text].length > MAX_FREE_TEXT_LENGTH) {
|
|
131
|
+
return {
|
|
132
|
+
code: HTTP_STATUS_PAYLOAD_TOO_LARGE,
|
|
133
|
+
message: `text exceeds maximum length: ${[...text].length} characters (anonymous oneshot limit is ${MAX_FREE_TEXT_LENGTH})`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (!dlSession) {
|
|
137
|
+
await warmCookies(proxyUrl);
|
|
138
|
+
}
|
|
139
|
+
const targetResult = resolveLang(targetLang, 'target');
|
|
140
|
+
if (!targetResult.success) {
|
|
141
|
+
return { code: HTTP_STATUS_BAD_REQUEST, message: targetResult.error };
|
|
142
|
+
}
|
|
143
|
+
const sourceResult = resolveLang(sourceLang, 'source');
|
|
144
|
+
if (!sourceResult.success) {
|
|
145
|
+
return { code: HTTP_STATUS_BAD_REQUEST, message: sourceResult.error };
|
|
146
|
+
}
|
|
147
|
+
const reqData = {
|
|
148
|
+
text: [text],
|
|
149
|
+
target_lang: targetResult.value,
|
|
150
|
+
source_lang: sourceResult.value,
|
|
151
|
+
usage_type: 'Translate',
|
|
152
|
+
app_information: {
|
|
153
|
+
os: 'brex_macOS',
|
|
154
|
+
os_version: `brex_chrome_${IMPERSONATED_CHROME_MAJOR}.0.0.0`,
|
|
155
|
+
app_version: CHROME_EXTENSION_VERSION,
|
|
156
|
+
app_build: 'chrome_web_store',
|
|
157
|
+
instance_id: getInstanceID(),
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
const reqId = Date.now();
|
|
161
|
+
try {
|
|
162
|
+
const response = await xfetch(dlSession ? ONESHOT_PRO_ENDPOINT : ONESHOT_FREE_ENDPOINT, {
|
|
163
|
+
method: 'POST',
|
|
164
|
+
body: reqData,
|
|
165
|
+
headers: buildHeaders(dlSession),
|
|
166
|
+
...createProxy({ url: proxyUrl }),
|
|
167
|
+
});
|
|
168
|
+
return processTranslationResponse(response, reqId, sourceLang, targetLang, dlSession);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
return parseTranslationError(error, reqId);
|
|
172
|
+
}
|
|
147
173
|
};
|
|
148
174
|
//# sourceMappingURL=translate.js.map
|
package/lib/translate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translate.js","sourceRoot":"","sources":["../src/translate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"translate.js","sourceRoot":"","sources":["../src/transl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C,GAAG,EAAE,CAAA;IAExB,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,MAAM,CAC3B,SAAS,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,qBAAqB,EACxD;YACE,MAAM,EAAE,MAAM;YACd,IAAI,EAAE,OAAO;YACb,OAAO,EAAE,YAAY,CAAC,SAAS,CAAC;YAChC,GAAG,WAAW,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;SAClC,CACF,CAAA;QAED,OAAO,0BAA0B,CAC/B,QAAQ,EACR,KAAK,EACL,UAAU,EACV,UAAU,EACV,SAAS,CACV,CAAA;IACH,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,OAAO,qBAAqB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IAC5C,CAAC;AACH,CAAC,CAAA"}
|
package/lib/types.d.ts
CHANGED
|
@@ -1,68 +1,25 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SourceLanguage, TargetLanguage } from './constants.ts';
|
|
2
2
|
export type ValueOf<T> = T extends ReadonlyArray<infer R> ? R : T extends Set<infer R> ? R : T[keyof T];
|
|
3
|
-
export interface
|
|
4
|
-
|
|
3
|
+
export interface AppInformation {
|
|
4
|
+
os: string;
|
|
5
|
+
os_version: string;
|
|
6
|
+
app_version: string;
|
|
7
|
+
app_build: string;
|
|
8
|
+
instance_id: string;
|
|
9
|
+
}
|
|
10
|
+
export interface OneshotRequest {
|
|
11
|
+
text: string[];
|
|
5
12
|
target_lang: string;
|
|
6
|
-
|
|
13
|
+
source_lang?: string;
|
|
14
|
+
usage_type: string;
|
|
15
|
+
app_information: AppInformation;
|
|
7
16
|
}
|
|
8
|
-
export interface
|
|
9
|
-
formality: FormalityTone;
|
|
10
|
-
transcribe_as: string;
|
|
11
|
-
mode: string;
|
|
12
|
-
wasSpoken: boolean;
|
|
13
|
-
advancedMode: boolean;
|
|
14
|
-
textType: string;
|
|
15
|
-
regionalVariant?: string;
|
|
16
|
-
}
|
|
17
|
-
export interface Sentence {
|
|
18
|
-
prefix: string;
|
|
17
|
+
export interface OneshotTranslation {
|
|
19
18
|
text: string;
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
export interface Job {
|
|
23
|
-
kind: string;
|
|
24
|
-
preferred_num_beams: number;
|
|
25
|
-
raw_en_context_before: string[];
|
|
26
|
-
raw_en_context_after: string[];
|
|
27
|
-
sentences: Sentence[];
|
|
28
|
-
}
|
|
29
|
-
export interface Params {
|
|
30
|
-
commonJobParams: CommonJobParams;
|
|
31
|
-
lang: Lang;
|
|
32
|
-
jobs: Job[];
|
|
33
|
-
timestamp: number;
|
|
34
|
-
}
|
|
35
|
-
export interface PostData {
|
|
36
|
-
jsonrpc: string;
|
|
37
|
-
method: string;
|
|
38
|
-
id: number;
|
|
39
|
-
params: Params;
|
|
19
|
+
detected_source_language?: string;
|
|
40
20
|
}
|
|
41
|
-
export interface
|
|
42
|
-
|
|
43
|
-
num_symbols: number;
|
|
44
|
-
rephrase_variant: {
|
|
45
|
-
name: string;
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
export interface Translation {
|
|
49
|
-
beams: TranslationBeam[];
|
|
50
|
-
quality: string;
|
|
51
|
-
}
|
|
52
|
-
export interface TranslationResponse {
|
|
53
|
-
jsonrpc: string;
|
|
54
|
-
id: number;
|
|
55
|
-
result: {
|
|
56
|
-
translations: Translation[];
|
|
57
|
-
target_lang: string;
|
|
58
|
-
source_lang: string;
|
|
59
|
-
source_lang_is_confident: boolean;
|
|
60
|
-
detectedLanguages: Record<string, string>;
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
export interface SentenceResponse {
|
|
64
|
-
text: string;
|
|
65
|
-
ids: number[];
|
|
21
|
+
export interface OneshotResponse {
|
|
22
|
+
translations: OneshotTranslation[];
|
|
66
23
|
}
|
|
67
24
|
export interface DeepLXTranslationErrorResult {
|
|
68
25
|
code: number;
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
1
|
import { type SupportedCode } from './constants.ts';
|
|
2
|
-
import type { PostData } from './types.ts';
|
|
3
|
-
export declare const getICount: (translateText: string) => number;
|
|
4
|
-
export declare const getRandomNumber: () => number;
|
|
5
|
-
export declare const getTimeStamp: (iCount: number) => number;
|
|
6
|
-
export declare const formatPostString: (postData: PostData) => string;
|
|
7
2
|
export declare function abbreviateLanguage(language: string): SupportedCode | undefined;
|
package/lib/utils.js
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
1
|
import { SUPPORTED_LANGUAGES, } from "./constants.js";
|
|
2
|
-
export const getICount = (translateText) => {
|
|
3
|
-
return (translateText.match(/i/g) || []).length;
|
|
4
|
-
};
|
|
5
|
-
export const getRandomNumber = () => {
|
|
6
|
-
const base = Math.floor(Math.random() * 99_999) + 8_300_000;
|
|
7
|
-
return base * 1000;
|
|
8
|
-
};
|
|
9
|
-
export const getTimeStamp = (iCount) => {
|
|
10
|
-
const ts = Date.now();
|
|
11
|
-
if (iCount !== 0) {
|
|
12
|
-
const adjustedCount = iCount + 1;
|
|
13
|
-
return ts - (ts % adjustedCount) + adjustedCount;
|
|
14
|
-
}
|
|
15
|
-
return ts;
|
|
16
|
-
};
|
|
17
|
-
export const formatPostString = (postData) => {
|
|
18
|
-
const postStr = JSON.stringify(postData);
|
|
19
|
-
const shouldAddSpace = (postData.id + 5) % 29 === 0 || (postData.id + 3) % 13 === 0;
|
|
20
|
-
return postStr.replaceAll('"method":"', shouldAddSpace ? `"method" : "` : `"method": "`);
|
|
21
|
-
};
|
|
22
2
|
let abbreviateLanguageDictionary;
|
|
23
3
|
function getAbbreviateLanguages() {
|
|
24
4
|
return (abbreviateLanguageDictionary ??= SUPPORTED_LANGUAGES.reduce((acc, lang) => {
|
package/lib/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,GAGpB,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,GAGpB,MAAM,gBAAgB,CAAA;AAEvB,IAAI,4BAES,CAAA;AAEb,SAAS,sBAAsB;IAE7B,OAAO,CAAC,4BAA4B,KAAK,mBAAmB,CAAC,MAAM,CACjE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QACZ,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAyB,CAAC,GAAG,IAAI,CAAC,IAAI,CAAA;QAC/D,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAyB,CAAC,GAAG,IAAI,CAAC,IAAI,CAAA;QACnE,OAAO,GAAG,CAAA;IACZ,CAAC,EACD,EAAgD,CACjD,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAChC,QAAgB;IAEhB,OAAO,sBAAsB,EAAE,CAC7B,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAyB,CAC5D,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deeplx/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "An unofficial but powerful and easy-to-use yet free DeepL API client for Node.js using [DeepL](https://www.deepl.com) by porting [OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX).",
|
|
6
6
|
"repository": "git+https://github.com/un-ts/deeplx.git",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"funding": "https://opencollective.com/deeplx",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"engines": {
|
|
12
|
-
"node": "^12.20 || ^14.18.0 || >=16.0.0"
|
|
12
|
+
"node": "^12.20.0 || ^14.18.0 || >=16.0.0"
|
|
13
13
|
},
|
|
14
14
|
"main": "./lib/index.cjs",
|
|
15
15
|
"types": "./index.d.cts",
|
|
@@ -20,10 +20,6 @@
|
|
|
20
20
|
"types": "./lib/index.d.ts",
|
|
21
21
|
"default": "./lib/index.js"
|
|
22
22
|
},
|
|
23
|
-
"module-sync": {
|
|
24
|
-
"types": "./lib/index.d.ts",
|
|
25
|
-
"default": "./lib/index.js"
|
|
26
|
-
},
|
|
27
23
|
"require": {
|
|
28
24
|
"types": "./index.d.cts",
|
|
29
25
|
"default": "./lib/index.cjs"
|
|
@@ -48,7 +44,7 @@
|
|
|
48
44
|
"translator"
|
|
49
45
|
],
|
|
50
46
|
"dependencies": {
|
|
51
|
-
"
|
|
47
|
+
"node-fetch-native": "^1.6.7",
|
|
52
48
|
"x-fetch": "^0.2.6"
|
|
53
49
|
},
|
|
54
50
|
"publishConfig": {
|