Skip to content

Commit

Permalink
feat: upgrade to got v12
Browse files Browse the repository at this point in the history
close #2
  • Loading branch information
JounQin committed May 30, 2022
1 parent b65e4d7 commit 157b849
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 85 deletions.
67 changes: 38 additions & 29 deletions api/_deepl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

Object.defineProperty(exports, '__esModule', { value: true });

var _got = require('got');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _got__default = /*#__PURE__*/_interopDefaultLegacy(_got);

function extractTranslatedSentences(response) {
return response.result.translations.reduce((sentences, translation) => {
sentences.push(translation.beams[0].postprocessed_sentence);
Expand Down Expand Up @@ -135,6 +129,9 @@ function createAbbreviationsDictionary(languages = SUPPORTED_LANGUAGES) {
function abbreviateLanguage(language) {
return createAbbreviationsDictionary()[language.toLowerCase()];
}
function importEsm(modulePath) {
return new Function("modulePath", "return import(modulePath)")(modulePath);
}

var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
Expand All @@ -156,32 +153,42 @@ var __async = (__this, __arguments, generator) => {
step((generator = generator.apply(__this, __arguments)).next());
});
};
const got = _got__default["default"].extend({
headers: {
accept: "*/*",
"accept-language": "en-US;q=0.8,en;q=0.7",
authority: "www2.deepl.com",
"content-type": "application/json",
origin: "https://www.deepl.com",
referer: "https://www.deepl.com/translator",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36"
},
stringifyJson(object) {
return JSON.stringify(object).replace('"method":"', () => {
const self = object;
if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) {
return '"method" : "';
}
return '"method": "';
});
}
});
let _got;
function getGot() {
return __async(this, null, function* () {
if (!_got) {
const got = yield importEsm("got");
_got = got.default.extend({
headers: {
accept: "*/*",
"accept-language": "en-US;q=0.8,en;q=0.7",
authority: "www2.deepl.com",
"content-type": "application/json",
origin: "https://www.deepl.com",
referer: "https://www.deepl.com/translator",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36"
},
stringifyJson(object) {
return JSON.stringify(object).replace('"method":"', () => {
const self = object;
if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) {
return '"method" : "';
}
return '"method": "';
});
}
});
}
return _got;
});
}
function splitSentences(text, sourceLanguage, identifier) {
return __async(this, null, function* () {
const data = generateSplitSentencesRequestData(text, sourceLanguage, identifier);
const got = yield getGot();
return yield got.post(API_URL, {
json: data
}).json();
Expand All @@ -196,6 +203,7 @@ function requestTranslation(text, targetLanguage, sourceLanguage, identifier, al
return __async(this, null, function* () {
const res = yield splitSentences(text, sourceLanguage, identifier);
const data = generateTranslationRequestData(sourceLanguage === "auto" ? res.result.lang : sourceLanguage, targetLanguage, extractSplitSentences(res), identifier, alternatives, formalityTone);
const got = yield getGot();
return yield got.post(API_URL, {
json: data
}).json();
Expand Down Expand Up @@ -223,6 +231,7 @@ exports.generateJobs = generateJobs;
exports.generateSplitSentencesRequestData = generateSplitSentencesRequestData;
exports.generateTimestamp = generateTimestamp;
exports.generateTranslationRequestData = generateTranslationRequestData;
exports.importEsm = importEsm;
exports.randRange = randRange;
exports.requestTranslation = requestTranslation;
exports.splitIntoSentences = splitIntoSentences;
Expand Down
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "deepl-api",
"type": "commonjs",
"dependencies": {
"got": "^11.8.5"
"got": "^12.1.0"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"dependencies": {
"commander": "^9.3.0",
"got": "^11.8.5",
"got": "^12.1.0",
"tslib": "^2.4.0"
},
"devDependencies": {
Expand Down
52 changes: 31 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 43 additions & 28 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,56 @@
import _got from 'got'
import type { Got } from 'got'

import {
extractTranslatedSentences,
extractSplitSentences,
SplittedSentences,
TranslatedSentences,
type TranslatedSentences,
} from './extractors.js'
import {
generateTranslationRequestData,
generateSplitSentencesRequestData,
} from './generators.js'
import { API_URL, AUTO, SourceLanguage, TargetLanguage } from './settings.js'
import { abbreviateLanguage } from './utils.js'
import {
API_URL,
AUTO,
type SourceLanguage,
type TargetLanguage,
} from './settings.js'
import { abbreviateLanguage, importEsm } from './utils.js'

let _got: Got | undefined

const got = _got.extend({
headers: {
accept: '*/*',
'accept-language': 'en-US;q=0.8,en;q=0.7',
authority: 'www2.deepl.com',
'content-type': 'application/json',
origin: 'https://www.deepl.com',
referer: 'https://www.deepl.com/translator',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent':
'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36',
},
stringifyJson(object: unknown) {
return JSON.stringify(object).replace('"method":"', () => {
const self = object as { id: number }
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) {
return '"method" : "'
}
return '"method": "'
async function getGot() {
if (!_got) {
const got = await importEsm<typeof import('got')>('got')
_got = got.default.extend({
headers: {
accept: '*/*',
'accept-language': 'en-US;q=0.8,en;q=0.7',
authority: 'www2.deepl.com',
'content-type': 'application/json',
origin: 'https://www.deepl.com',
referer: 'https://www.deepl.com/translator',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent':
'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36',
},
stringifyJson(object: unknown) {
return JSON.stringify(object).replace('"method":"', () => {
const self = object as { id: number }
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
if ((self.id + 3) % 13 === 0 || (self.id + 5) % 29 === 0) {
return '"method" : "'
}
return '"method": "'
})
},
})
},
})
}
return _got
}

export async function splitSentences(
text: string,
Expand All @@ -49,6 +62,7 @@ export async function splitSentences(
sourceLanguage,
identifier,
)
const got = await getGot()
return await got
.post(API_URL, {
json: data,
Expand Down Expand Up @@ -83,6 +97,7 @@ export async function requestTranslation(
alternatives,
formalityTone,
)
const got = await getGot()
return await got
.post(API_URL, {
json: data,
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { URL } from 'node:url'
import { program } from 'commander'

import { translate } from './api.js'
import { SourceLanguage, TargetLanguage } from './settings.js'
import { type SourceLanguage, type TargetLanguage } from './settings.js'

const __dirname = new URL('.', import.meta.url).pathname

Expand Down
2 changes: 1 addition & 1 deletion src/extractors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SourceLanguage } from './settings.js'
import { type SourceLanguage } from './settings.js'

export interface TranslatedSentences {
result: {
Expand Down
4 changes: 2 additions & 2 deletions src/generators.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { generateId, generateTimestamp } from './hacks.js'
import {
AUTO,
SourceLanguage,
type SourceLanguage,
SUPPORTED_FORMALITY_TONES,
TargetLanguage,
type TargetLanguage,
} from './settings.js'

export function generateSplitSentencesRequestData(
Expand Down
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SUPPORTED_LANGUAGES, TargetLanguage } from './settings.js'
import { SUPPORTED_LANGUAGES, type TargetLanguage } from './settings.js'

export function createAbbreviationsDictionary(languages = SUPPORTED_LANGUAGES) {
return languages.reduce<Record<string, string>>((acc, lang) => {
Expand All @@ -13,3 +13,10 @@ export function abbreviateLanguage(language: string) {
| TargetLanguage
| undefined
}

export function importEsm<T>(modulePath: string) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
return new Function('modulePath', 'return import(modulePath)')(
modulePath,
) as Promise<T>
}

0 comments on commit 157b849

Please sign in to comment.