Skip to content

Commit

Permalink
feat!: bump got to ESM only v14, migrate to Vercel Edge Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 15, 2023
1 parent f35e9d4 commit 35ab464
Show file tree
Hide file tree
Showing 21 changed files with 6,503 additions and 5,205 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ lib
CHANGELOG.md
/api/_deeplx.js
/public/index.html
/auto-imports.d.ts
!/.*.cjs
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: latest

- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: pnpm
Expand All @@ -44,7 +42,6 @@ jobs:

- name: Test
run: pnpm test
continue-on-error: true

- name: Codecov
uses: codecov/codecov-action@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
coverage
lib
node_modules
/api/_deeplx.js
/public/index.html
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/api/_deeplx.js
/auto-imports.d.ts
/pnpm-lock.yaml
235 changes: 0 additions & 235 deletions api/_deeplx.js

This file was deleted.

4 changes: 2 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "deepl-api",
"type": "commonjs",
"type": "module",
"dependencies": {
"got": "^11.8.5"
"got": "^14.0.0"
}
}
53 changes: 29 additions & 24 deletions api/translate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { VercelRequest, VercelResponse } from '@vercel/node'
import type { SourceLanguage, TargetLanguage } from 'deeplx'

// Workaround for Vercel `Cannot find module 'deeplx'`
import { abbreviateLanguage, translate } from './_deeplx'
import type { SourceLanguage, TargetLanguage } from './_deeplx.js'
import { abbreviateLanguage, translate } from './_deeplx.js'

export interface RequestParams {
text?: string
Expand All @@ -14,52 +12,59 @@ const OK = 200
const NOT_ALLOWED = 405
const INTERNAL_ERROR = 500

export default async (
req: VercelRequest,
res: VercelResponse,
): Promise<void> => {
// type-coverage:ignore-next-line
const body = req.body as RequestParams | undefined
export const config = {
runtime: 'edge',
}

export default async (req: Request): Promise<Response> => {
let body: RequestParams | undefined

try {
body = (await req.json()) as RequestParams
} catch {}

if (!body || req.method !== 'POST') {
res.end(`DeepL Translate Api
return new Response(`DeepL Translate Api
POST {"text": "have a try", "source_lang": "auto", "target_lang": "ZH"} to /translate
https://github.com/un-ts/deeplx`)
return
}

res.setHeader('Content-Type', 'application/json')

const { text, source_lang: sourceLang, target_lang: targetLang } = body
const { text, source_lang, target_lang } = body

if (!abbreviateLanguage(targetLang)) {
res.status(NOT_ALLOWED)
res.end(
if (!abbreviateLanguage(target_lang)) {
return new Response(
JSON.stringify({
code: NOT_ALLOWED,
data: 'Invalid target language',
}),
{
headers: {
'Content-Type': 'application/json',
},
status: NOT_ALLOWED,
},
)
return
}

try {
const translation = await translate(text, targetLang, sourceLang)
res.end(
const translation = await translate(text, target_lang, source_lang)
return new Response(
JSON.stringify({
code: OK,
data: translation,
}),
)
} catch (err) {
res.status(INTERNAL_ERROR)
res.end(
return new Response(
JSON.stringify({
code: INTERNAL_ERROR,
data: String(err),
data: err,
}),
{
status: INTERNAL_ERROR,
},
)
}
}

0 comments on commit 35ab464

Please sign in to comment.