import { Hono } from "npm:hono@3";
const app = new Hono();
function analyzeJSON(jsonData) {
const images = jsonData.images;
const allIds = new Set(images.map(img => img.id));
const referencedIds = new Set();
const errors = [];
const warnings = [];
images.forEach(image => {
image.related.forEach(ref => {
if (!allIds.has(ref)) {
errors.push(`Error: '${ref}' in '${image.id}' related list doesn't have a matching ID`);
}
referencedIds.add(ref);
});
});
allIds.forEach(id => {
if (!referencedIds.has(id)) {
warnings.push(`Warning: '${id}' is never referenced in any related list`);
}
});
return { errors, warnings };
}
app.get("/", (c) =>
c.html(
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" accept=".json,.txt,application/json,text/plain" name="file" />
<button type="submit">Analyze File</button>
</form>,
));
app.post("/", async (c) => {
const formData = await c.req.formData();
const file = formData.get("file");
if (!file || !(file instanceof File)) {
return c.html(<p>No file uploaded or invalid file.</p>);
}
try {
const jsonContent = await file.text();
const jsonData = JSON.parse(jsonContent);
const { errors, warnings } = analyzeJSON(jsonData);
return c.html(
<div>
<h2>Analysis Results</h2>
<h3>Errors:</h3>
<ul>
{errors.map(error => <li>{error}</li>)}
</ul>
<h3>Warnings:</h3>
<ul>
{warnings.map(warning => <li>{warning}</li>)}
</ul>
<a href="/">Analyze Another File</a>
</div>,
);
} catch (error) {
return c.html(<p>Error processing file: {error.message}</p>);
}
});
export default app.fetch;