feat(moderation): normalize JSON response handling and enhance test coverage for moderation analysis

This commit is contained in:
MythEclipse
2026-05-21 03:52:51 +07:00
parent 419018ab0c
commit 5588ece6c7
3 changed files with 78 additions and 4 deletions
+10 -1
View File
@@ -19,7 +19,16 @@ const openai = new OpenAI({
? await response.text()
: JSON.stringify(await response.json());
return new Response(body, {
let normalizedBody = body;
if (response.ok !== false) {
try {
JSON.parse(body);
} catch {
normalizedBody = JSON.stringify(extractJson(body));
}
}
return new Response(normalizedBody, {
status: response.status ?? 200,
headers: { "Content-Type": "application/json" },
});