export async function midi(req) { const { searchParams: sp } = new URL(req.url); const q = sp.get('q'); const page = sp.get('page') || 0; const id = sp.get('id'); const mode = sp.get('mode') || 'search'; if (mode === 'download' && id) { return Response.redirect(`https://bitmidi.com/uploads/${id}.mid`, 302); } if (!q) { return { error: "missing query", usage: { search: "/midi?q=tokyo", page: "/midi?q=tokyo&page=1", download: "/midi?mode=download&id=104639" } }; } try { const res = await fetch(`https://bitmidi.com/api/midi/search?q=${encodeURIComponent(q)}&page=${page}`, { headers: { "Accept": "application/json" } }); const data = await res.json(); const results = (data.result?.results || []).map(r => ({ id: r.id, name: r.name, slug: r.slug, views: r.views, plays: r.plays, url: `https://bitmidi.com${r.url}`, download: `https://bitmidi.com${r.downloadUrl}` })); return { status: true, query: q, page: Number(page), total: data.result?.total || 0, results }; } catch (err) { return { error: err.message }; } }