// ------------------------------------------------------------ // Imports & Initialization // ------------------------------------------------------------ import { Hono } from "hono"; import { Service } from "$lib/schemas/Service"; import { userAuth } from "$lib/middlewares/userAuth"; const app = new Hono(); // ------------------------------------------------------------ // Endpoints // ------------------------------------------------------------ app.get("/", userAuth, async (c) => { const services = await Service.find(); // Handle service not found if (!services) { return c.json({ message: "No services found" }, 404); } // Obfuscate sensitive information (token) for (const service of services) { service.token = "********"; } // Omit sensitive information (token) return c.json(services); }); app.post("/", userAuth, async (c) => { const { name, description } = await c.req.json(); const token = crypto.getRandomValues(new Uint8Array(32)).toHex(); console.log(token); const service = await Service.create({ name, description, token }); return c.json(service); }); app.patch("/:id", userAuth, async (c) => { const id = c.req.param("id"); const { name, description } = await c.req.json(); const service = await Service.findById(id); // Handle service not found if (!service) { return c.json({ message: "Service not found" }, 404); } service.name = name; service.description = description; await service.save(); // Obfuscate sensitive information (token) service.token = "******"; return c.json(service); }); app.post("/:id/reset-token", userAuth, async (c) => { const id = c.req.param("id"); const service = await Service.findById(id); // Handle service not found if (!service) { return c.json({ message: "Service not found" }, 404); } const token = crypto.getRandomValues(new Uint8Array(32)).toHex(); service.token = token; await service.save(); return c.json(service); }); app.delete("/:id", userAuth, async (c) => { const id = c.req.param("id"); const service = await Service.findById(id); // Handle service not found if (!service) { return c.json({ message: "Service not found" }, 404); } await service.deleteOne(); return c.json({ message: "Service deleted" }); }); // ------------------------------------------------------------ // Exports // ------------------------------------------------------------ export default app;