Edge function reports 525 error.
ifyour opened this issue · 2 comments
ifyour commented
Hello, thank you for sharing this implementation method. It has inspired me to deploy a deeplx API on edge functions using a similar approach. However, I keep getting error 525 and would like to seek your help.
// src/pages/api/translate.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
interface RequestParams {
text: string;
source_lang: string;
target_lang: string;
}
interface ResponseParams {
id: number;
code: number;
data: string;
}
async function queryAPI(data: RequestParams): Promise<ResponseParams> {
const res = await fetch("https://www2.deepl.com/jsonrpc", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
method: "POST",
body: buildBodyString(data),
});
if (res.ok) {
const result = (await res.json()) as {
jsonrpc: string;
id: number;
result: {
texts: {
text: string;
}[];
};
};
return {
id: result.id,
code: 200,
data: result?.result?.texts?.[0]?.text,
};
}
return {
id: 42,
code: res.status,
data:
res.status === 429
? "Too many requests, please try again later."
: "Unknown error.",
};
}
function buildRequestParams(sourceLang: string, targetLang: string) {
return {
jsonrpc: "2.0",
method: "LMT_handle_texts",
id: Math.floor(Math.random() * 100000 + 100000) * 1000,
params: {
texts: [{ text: "", requestAlternatives: 3 }],
timestamp: 0,
splitting: "newlines",
lang: {
source_lang_user_selected: sourceLang,
target_lang: targetLang,
},
},
};
}
function getCountOfI(translateText: string) {
return translateText.split("i").length - 1;
}
function getTimestamp(iCount: number) {
let ts = new Date().getTime();
if (iCount !== 0) {
iCount = iCount + 1;
return ts - (ts % iCount) + iCount;
} else {
return ts;
}
}
function buildBodyString(data: RequestParams) {
const post_data = buildRequestParams(
data.source_lang || "AUTO",
data.target_lang || "AUTO"
);
post_data.params.texts = [{ text: data.text, requestAlternatives: 3 }];
post_data.params.timestamp = getTimestamp(getCountOfI(data.text));
let post_str = JSON.stringify(post_data);
if (
[0, 3].includes((post_data["id"] + 5) % 29) ||
(post_data["id"] + 3) % 13 === 0
) {
post_str = post_str.replace('"method":"', '"method" : "');
} else {
post_str = post_str.replace('"method":"', '"method": "');
}
return post_str;
}
export const config = {
runtime: "edge",
};
export default async function MyEdgeFunction(request: NextRequest) {
const req = (await request.json()) as RequestParams;
const res = await queryAPI(req);
return NextResponse.json(res);
}
After debugging locally and confirming normal operation, I encountered a persistent 525 error after deploying to the edge function. Can you advise me on how to solve this problem?
curl --location 'https://deeplx-edge-api.vercel.app/api/translate' \
--header 'Content-Type: application/json' \
--data '{
"text": "你好呀,请问你来自哪里",
"source_lang": "auto",
"target_lang": "en"
}'
{"id":42,"code":525,"data":"Unknown error."}
curl --location '127.0.0.1:3000/api/translate' \
--header 'Content-Type: application/json' \
--data '{
"text": "请给我一个冰淇淋",
"source_lang": "zh",
"target_lang": "en"
}'
{"id":115601000,"code":200,"data":"Please give me an ice cream"}
x-dr commented
I tried deploying with Vercel and Cloudflare Worker, but encountered the same error. It's likely that Deepl has some limitations.
ifyour commented
Thank you for your response. I also tried both methods, but they ultimately failed. In the end, I used server deployment and ran a Go service on my virtual server, which worked properly.