|
| 1 | +import { |
| 2 | + abbreviateLanguage, |
| 3 | + HTTP_STATUS_BAD_REQUEST, |
| 4 | + HTTP_STATUS_INTERNAL_ERROR, |
| 5 | + HTTP_STATUS_NOT_ALLOWED, |
| 6 | + HTTP_STATUS_NOT_FOUND, |
| 7 | + HTTP_STATUS_OK, |
| 8 | + type SourceLanguage, |
| 9 | + type TargetLanguage, |
| 10 | + translate as translate_, |
| 11 | +} from 'npm:@deeplx/core' |
| 12 | + |
| 13 | +export interface RequestBody { |
| 14 | + text: string |
| 15 | + source_lang?: SourceLanguage |
| 16 | + target_lang: TargetLanguage |
| 17 | +} |
| 18 | + |
| 19 | +async function extractBodyText(body: ReadableStream) { |
| 20 | + const reader = body.getReader() |
| 21 | + const decoder = new TextDecoder() |
| 22 | + |
| 23 | + let text = '' |
| 24 | + |
| 25 | + while (true) { |
| 26 | + const { done, value } = await reader.read() |
| 27 | + if (done) { |
| 28 | + return text |
| 29 | + } |
| 30 | + text += decoder.decode(value) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export const translate = async ( |
| 35 | + req: Request, |
| 36 | +): Promise<Response> => { |
| 37 | + let url = new URL(req.url).pathname |
| 38 | + |
| 39 | + if (!url || url === '/') { |
| 40 | + url = '/index.html' |
| 41 | + } |
| 42 | + |
| 43 | + if (/\.[a-z]+[a-z\d]*$/.test(url)) { |
| 44 | + if (req.method !== 'GET') { |
| 45 | + return Response.json({ |
| 46 | + code: HTTP_STATUS_NOT_ALLOWED, |
| 47 | + message: 'Not Allowed', |
| 48 | + }, { status: HTTP_STATUS_NOT_ALLOWED }) |
| 49 | + } |
| 50 | + return new Response(await Deno.readFile(`./public${url}`)) |
| 51 | + } |
| 52 | + |
| 53 | + if (url !== '/translate') { |
| 54 | + return Response.json({ |
| 55 | + code: HTTP_STATUS_NOT_FOUND, |
| 56 | + message: 'Not Found', |
| 57 | + }, { |
| 58 | + status: HTTP_STATUS_NOT_FOUND, |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + const body = req.body |
| 63 | + |
| 64 | + if (!body || req.method !== 'POST') { |
| 65 | + return new Response(`DeepL Translate Api |
| 66 | +
|
| 67 | +POST {"text": "have a try", "source_lang": "auto", "target_lang": "ZH"} to /translate |
| 68 | +
|
| 69 | +https://github.com/devno-js/deeplx |
| 70 | +
|
| 71 | +powered by https://github.com/un-ts/deeplx`) |
| 72 | + } |
| 73 | + |
| 74 | + const bodyText = await extractBodyText(body) |
| 75 | + |
| 76 | + const { text, source_lang: sourceLang, target_lang: targetLang } = JSON.parse( |
| 77 | + bodyText, |
| 78 | + ) as RequestBody |
| 79 | + |
| 80 | + if (!text) { |
| 81 | + return Response.json({ |
| 82 | + code: HTTP_STATUS_BAD_REQUEST, |
| 83 | + data: 'Text is required', |
| 84 | + }, { |
| 85 | + status: HTTP_STATUS_BAD_REQUEST, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + if (!abbreviateLanguage(targetLang)) { |
| 90 | + return Response.json({ |
| 91 | + code: HTTP_STATUS_BAD_REQUEST, |
| 92 | + data: 'Invalid target language', |
| 93 | + }, { |
| 94 | + status: HTTP_STATUS_BAD_REQUEST, |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + const translation = await translate_(text, targetLang, sourceLang) |
| 100 | + return Response.json({ |
| 101 | + code: HTTP_STATUS_OK, |
| 102 | + data: translation, |
| 103 | + }) |
| 104 | + } catch (err) { |
| 105 | + return Response.json({ |
| 106 | + code: HTTP_STATUS_INTERNAL_ERROR, |
| 107 | + data: err instanceof Error ? err.message : String(err), |
| 108 | + }, { |
| 109 | + status: HTTP_STATUS_INTERNAL_ERROR, |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments