import { Router, Request, Response } from "express"; import { searchSwitches, getSwitchById, getCompatibleTransceivers, getFlexoptixSuggestions, getSwitchDocuments, getSwitchIssues } from "../db/queries"; export const switchRouter = Router(); // GET /api/switches — Search/list switches // Filters: ?q=&category=&whitebox=true&sonic=true&asic_vendor=Broadcom&nos=SONiC&ocp=true switchRouter.get("/", async (req: Request, res: Response) => { try { const result = await searchSwitches({ q: String(req.query.q || ""), category: req.query.category ? String(req.query.category) : undefined, whitebox: req.query.whitebox === "true" ? true : undefined, sonic_compatible: req.query.sonic === "true" ? true : undefined, asic_vendor: req.query.asic_vendor ? String(req.query.asic_vendor) : undefined, nos: req.query.nos ? String(req.query.nos) : undefined, ocp: req.query.ocp === "true" ? true : undefined, max_speed_gbps: req.query.max_speed_gbps ? parseFloat(String(req.query.max_speed_gbps)) : undefined, limit: req.query.limit ? parseInt(String(req.query.limit)) : 50, offset: req.query.offset ? parseInt(String(req.query.offset)) : 0, }); res.json({ success: true, ...result }); } catch (err) { console.error("Search switches error:", err); res.status(500).json({ success: false, error: "Internal server error" }); } }); // GET /api/switches/:id — Get single switch switchRouter.get("/:id", async (req: Request, res: Response) => { try { const sw = await getSwitchById(String(req.params.id)); if (!sw) { res.status(404).json({ success: false, error: "Switch not found" }); return; } res.json({ success: true, data: sw }); } catch (err) { console.error("Get switch error:", err); res.status(500).json({ success: false, error: "Internal server error" }); } }); // GET /api/switches/:id/documents — Datasheets, manuals, guides for a switch switchRouter.get("/:id/documents", async (req: Request, res: Response) => { try { const docs = await getSwitchDocuments(String(req.params.id)); res.json({ success: true, data: docs, total: docs.length }); } catch (err) { console.error("Get switch documents error:", err); res.status(500).json({ success: false, error: "Internal server error" }); } }); // GET /api/switches/:id/issues — Known bugs, incompatibilities from community sources switchRouter.get("/:id/issues", async (req: Request, res: Response) => { try { const issues = await getSwitchIssues(String(req.params.id)); res.json({ success: true, data: issues, total: issues.length }); } catch (err) { console.error("Get switch issues error:", err); res.status(500).json({ success: false, error: "Internal server error" }); } }); // GET /api/switches/:id/compatibility — Compatible transceivers for a switch switchRouter.get("/:id/compatibility", async (req: Request, res: Response) => { try { const transceivers = await getCompatibleTransceivers(String(req.params.id)); res.json({ success: true, data: transceivers, total: transceivers.length }); } catch (err) { console.error("Get compatibility error:", err); res.status(500).json({ success: false, error: "Internal server error" }); } }); // GET /api/switches/:id/flexoptix — Flexoptix transceivers by form factor (always available) // Returns Flexoptix catalog items that physically fit the switch's port slots, // derived from ports_config keys — works before the compat scraper has run. switchRouter.get("/:id/flexoptix", async (req: Request, res: Response) => { try { const suggestions = await getFlexoptixSuggestions(String(req.params.id)); res.json({ success: true, data: suggestions, total: suggestions.length }); } catch (err) { console.error("Get Flexoptix suggestions error:", err); res.status(500).json({ success: false, error: "Internal server error" }); } });