Skip to content

Commit

Permalink
feat!: migrate to node-fetch + x-fetch
Browse files Browse the repository at this point in the history
migrate to Vercel Edge Runtime
  • Loading branch information
JounQin committed Dec 15, 2023
1 parent f35e9d4 commit 4bfb4f1
Show file tree
Hide file tree
Showing 19 changed files with 6,573 additions and 5,351 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
coverage
lib
node_modules
/api/_deeplx.js
/public/index.html
/auto-imports.d.ts
235 changes: 0 additions & 235 deletions api/_deeplx.js

This file was deleted.

3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "deepl-api",
"type": "commonjs",
"dependencies": {
"got": "^11.8.5"
"node-fetch": "^3.3.2",
"x-fetch": "^0.1.3"
}
}
47 changes: 27 additions & 20 deletions api/translate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VercelRequest, VercelResponse } from '@vercel/node'
import type { SourceLanguage, TargetLanguage } from 'deeplx'
import { ApiMethod } from 'x-fetch'

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

export interface RequestParams {
Expand All @@ -14,52 +14,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

if (!body || req.method !== 'POST') {
res.end(`DeepL Translate Api
try {
body = (await req.json()) as RequestParams
} catch {}

if (!body || req.method !== ApiMethod.POST) {
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

if (!abbreviateLanguage(targetLang)) {
res.status(NOT_ALLOWED)
res.end(
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(
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,
},
)
}
}
22 changes: 0 additions & 22 deletions jest.config.ts

This file was deleted.

Loading

0 comments on commit 4bfb4f1

Please sign in to comment.