forked from ptrstn/deepl-translate
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: support
auto
for source language
- Loading branch information
Showing
29 changed files
with
773 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'deepl-translate': minor | ||
--- | ||
|
||
refactor: support `auto` for source language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
coverage | ||
lib | ||
CHANGELOG.md | ||
/public/index.html | ||
!/.*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
.*cache | ||
.type-coverage | ||
.vercel | ||
coverage | ||
lib | ||
node_modules | ||
/public/index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { VercelRequest, VercelResponse } from '@vercel/node' | ||
import { SourceLanguage, TargetLanguage, translate } from 'deepl-translate' | ||
|
||
export interface RequestParams { | ||
text: string | ||
source_lang?: SourceLanguage | ||
target_lang: TargetLanguage | ||
} | ||
|
||
const NO_CONTENT = 204 | ||
const NOT_ALLOWED = 405 | ||
|
||
export default async ( | ||
req: VercelRequest, | ||
res: VercelResponse, | ||
): Promise<void> => { | ||
if (req.method !== 'POST') { | ||
res.status(NOT_ALLOWED) | ||
res.end() | ||
return | ||
} | ||
|
||
const { | ||
text, | ||
source_lang: sourceLang, | ||
target_lang: targetLang, | ||
// type-coverage:ignore-next-line | ||
} = req.body as RequestParams | ||
|
||
if (!text) { | ||
res.status(NO_CONTENT) | ||
res.end() | ||
return | ||
} | ||
|
||
try { | ||
const translated = await translate(text, targetLang, sourceLang) | ||
res.end(translated) | ||
} catch (err) { | ||
res.status(500) | ||
res.end(String(err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Config } from '@jest/types' | ||
|
||
const config: Config.InitialOptions = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
collectCoverage: true, | ||
extensionsToTreatAsEsm: ['.ts'], | ||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
'^deepl-translate$': '<rootDir>/src/index.ts', | ||
}, | ||
globals: { | ||
'ts-jest': { | ||
useESM: true, | ||
tsconfig: { | ||
importHelpers: false, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.