Skip to content

Commit c8bd2f4

Browse files
committed
fix issues according to copilot review
1 parent 3c448d4 commit c8bd2f4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

backend/src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ async function checkRateLimit(ip: string, env: Env) {
4141
}
4242

4343
async function updateAnalytics(source: string, dest: string, env: Env) {
44-
const key = `${source}-${dest}`;
44+
const normalizedSource = source.trim().toLowerCase();
45+
const normalizedDest = dest.trim().toLowerCase();
46+
const key = `${normalizedSource}-${normalizedDest}`;
4547
let value = await env.LANG_TRANSLATION_ANALYTICS.get(key);
4648

4749
let data = { count: 0 };
@@ -78,7 +80,7 @@ ${code}`;
7880
const result = await model.generateContent(prompt);
7981
const translatedCode = result.response.text();
8082
await updateAnalytics(sourceLanguage, targetLanguage, env);
81-
return new Response(JSON.stringify({ translation: translatedCode, sourceLanguage }), {
83+
return new Response(JSON.stringify({ translation: translatedCode}), {
8284
status: 200,
8385
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
8486
});
@@ -137,7 +139,12 @@ export default {
137139
const stats: Record<string, any> = {};
138140
for (const key of list.keys) {
139141
const val = await env.LANG_TRANSLATION_ANALYTICS.get(key.name);
140-
stats[key.name] = JSON.parse(val || '{}');
142+
try {
143+
stats[key.name] = JSON.parse(val || '{}');
144+
} catch (e) {
145+
console.error(`Failed to parse analytics value for key "${key.name}":`, e);
146+
stats[key.name] = {};
147+
}
141148
}
142149
return new Response(JSON.stringify(stats, null, 2), {
143150
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
@@ -177,5 +184,5 @@ Code:
177184
${code}`;
178185

179186
const result = await model.generateContent(prompt);
180-
return result.response.text().trim();
187+
return result.response.text().trim().toLowerCase();
181188
}

0 commit comments

Comments
 (0)