From e86b08fe3bf1b5525e5eaef990676a589696c857 Mon Sep 17 00:00:00 2001 From: coulsontl <149155411+coulsontl@users.noreply.github.com> Date: Sun, 15 Dec 2024 21:43:43 +0800 Subject: [PATCH] chore: update openai request method --- src/services/translate/openai/index.jsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/translate/openai/index.jsx b/src/services/translate/openai/index.jsx index b001716d87..4677d042e7 100644 --- a/src/services/translate/openai/index.jsx +++ b/src/services/translate/openai/index.jsx @@ -118,13 +118,13 @@ export async function translate(text, from, to, options) { throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`; } } else { - let res = await fetch(apiUrl.href, { + let res = await window.fetch(apiUrl.href, { method: 'POST', headers: headers, - body: Body.json(body), + body: JSON.stringify(body), }); if (res.ok) { - let result = res.data; + let result = await res.json(); const { choices } = result; if (choices) { let target = choices[0].message.content.trim(); @@ -143,7 +143,8 @@ export async function translate(text, from, to, options) { throw JSON.stringify(result); } } else { - throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`; + const errorData = await res.json().catch(() => null); + throw `Http Request Error\nHttp Status: ${res.status}\n${errorData ? JSON.stringify(errorData) : 'No error details available'}`; } } }