Monorepo for Aesthetic.Computer aesthetic.computer

m4l: fix plugins API to return Response objects (Netlify v2 format)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+21 -18
+21 -18
system/netlify/functions/m4l-plugins.mjs
··· 14 14 incrementViews, 15 15 getAllVersions 16 16 } from '../../backend/plugins.mjs'; 17 - import { respond } from '../../backend/http.mjs'; 17 + 18 + const json = (status, data) => 19 + new Response(JSON.stringify(data), { 20 + status, 21 + headers: { 22 + "Content-Type": "application/json", 23 + "Access-Control-Allow-Origin": "*", 24 + }, 25 + }); 18 26 19 27 export default async function handler(req, context) { 20 28 const { db, disconnect } = await connect(); 21 29 22 30 try { 23 - const url = new URL(req.url, `https://${req.headers.host}`); 31 + const url = new URL(req.url); 24 32 const pathParts = url.pathname.split('/').filter(Boolean); 25 33 26 34 // GET /m4l-plugins - List all latest plugins 27 35 if (req.method === 'GET' && pathParts.length === 1) { 28 36 const plugins = await getLatestPlugins(db); 29 - return respond(200, plugins); 37 + return json(200, plugins); 30 38 } 31 39 32 40 // GET /m4l-plugins/category/:category - List plugins by category 33 41 if (req.method === 'GET' && pathParts.length === 3 && pathParts[1] === 'category') { 34 42 const category = pathParts[2]; 35 43 const plugins = await getPluginsByCategory(db, category); 36 - return respond(200, plugins); 44 + return json(200, plugins); 37 45 } 38 46 39 47 // GET /m4l-plugins/versions/:deviceName - Get all versions of a device 40 48 if (req.method === 'GET' && pathParts.length === 3 && pathParts[1] === 'versions') { 41 49 const deviceName = pathParts[2]; 42 50 const versions = await getAllVersions(db, deviceName); 43 - return respond(200, versions); 51 + return json(200, versions); 44 52 } 45 53 46 54 // GET /m4l-plugins/:code - Get specific plugin ··· 49 57 const plugin = await getPluginByCode(db, code); 50 58 51 59 if (!plugin) { 52 - return respond(404, { error: 'Plugin not found' }); 60 + return json(404, { error: 'Plugin not found' }); 53 61 } 54 62 55 - return respond(200, plugin); 63 + return json(200, plugin); 56 64 } 57 65 58 66 // POST /m4l-plugins/:code/download - Track download 59 67 if (req.method === 'POST' && pathParts.length === 3 && pathParts[2] === 'download') { 60 68 const code = pathParts[1]; 61 69 62 - // Get plugin first to ensure it exists 63 70 const plugin = await getPluginByCode(db, code); 64 71 65 72 if (!plugin) { 66 - return respond(404, { error: 'Plugin not found' }); 73 + return json(404, { error: 'Plugin not found' }); 67 74 } 68 75 69 - // Increment download counter 70 76 await incrementDownloads(db, code); 71 77 72 - // Return download URL 73 - return respond(200, { 78 + return json(200, { 74 79 downloadUrl: plugin.m4l.downloadUrl, 75 80 fileName: plugin.m4l.fileName, 76 81 fileSize: plugin.m4l.fileSize ··· 81 86 if (req.method === 'POST' && pathParts.length === 3 && pathParts[2] === 'view') { 82 87 const code = pathParts[1]; 83 88 84 - // Increment view counter 85 89 const result = await incrementViews(db, code); 86 90 87 91 if (result.matchedCount === 0) { 88 - return respond(404, { error: 'Plugin not found' }); 92 + return json(404, { error: 'Plugin not found' }); 89 93 } 90 94 91 - return respond(200, { success: true }); 95 + return json(200, { success: true }); 92 96 } 93 97 94 - // Method not allowed 95 - return respond(405, { error: 'Method not allowed' }); 98 + return json(405, { error: 'Method not allowed' }); 96 99 97 100 } catch (error) { 98 101 console.error('Error in m4l-plugins API:', error); 99 - return respond(500, { error: 'Internal server error', message: error.message }); 102 + return json(500, { error: 'Internal server error', message: error.message }); 100 103 } finally { 101 104 await disconnect(); 102 105 }