import type { APIRoute } from "astro"; import templatesData from "../../data/templates.json"; const typeMap: Record = { agents: "agent", commands: "command", mcps: "mcp", settings: "setting", hooks: "hook", skills: "skill", }; const types = [ "agents", "commands", "mcps", "settings", "hooks", "skills", ] as const; export const GET: APIRoute = async ({ url }) => { const filter = url.searchParams.get("filter") || "all"; const page = parseInt(url.searchParams.get("page") || "1", 10); const limit = parseInt(url.searchParams.get("limit") || "50", 10); const search = url.searchParams.get("search") || ""; // Build templates array let templates: any[] = []; if (filter === "all") { types.forEach((type) => { const items = (templatesData as any)[type] || []; items.forEach((item: any) => { templates.push({ name: item.name, type: typeMap[type], description: item.description || "", content: item.content || "", downloadUrl: item.downloadUrl, rawUrl: item.rawUrl, size: item.size, }); }); }); } else { // Filter by specific type (agents, commands, etc.) const items = (templatesData as any)[filter] || []; items.forEach((item: any) => { templates.push({ name: item.name, type: typeMap[filter] || filter, description: item.description || "", content: item.content || "", downloadUrl: item.downloadUrl, rawUrl: item.rawUrl, size: item.size, }); }); } // Search filter if (search) { const searchLower = search.toLowerCase(); templates = templates.filter( (t) => t.name.toLowerCase().includes(searchLower) || t.description.toLowerCase().includes(searchLower), ); } // Sort by name templates.sort((a, b) => a.name.localeCompare(b.name)); // Pagination const total = templates.length; const totalPages = Math.ceil(total / limit); const offset = (page - 1) * limit; const paginatedTemplates = templates.slice(offset, offset + limit); return new Response( JSON.stringify({ templates: paginatedTemplates, pagination: { page, limit, total, totalPages, hasMore: page < totalPages, }, }), { status: 200, headers: { "Content-Type": "application/json", "Cache-Control": "public, max-age=60", }, }, ); }; export const prerender = false;