jefflix-website/app/api/music/search/route.ts

58 lines
1.2 KiB
TypeScript

import { NextResponse } from 'next/server'
import { navidromeGet } from '@/lib/navidrome'
interface SubsonicSong {
id: string
title: string
artist: string
album: string
albumId: string
duration: number
track: number
year: number
coverArt: string
suffix: string
}
interface SearchResult {
searchResult3?: {
song?: SubsonicSong[]
}
}
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const q = searchParams.get('q')
if (!q || q.length < 2) {
return NextResponse.json({ songs: [] })
}
try {
const data = await navidromeGet<SearchResult>('search3.view', {
query: q,
songCount: '30',
albumCount: '0',
artistCount: '0',
})
const songs = (data.searchResult3?.song || []).map((s) => ({
id: s.id,
title: s.title,
artist: s.artist,
album: s.album,
albumId: s.albumId,
duration: s.duration,
track: s.track,
year: s.year,
coverArt: s.coverArt,
suffix: s.suffix,
}))
return NextResponse.json({ songs })
} catch (error) {
console.error('Music search error:', error)
return NextResponse.json({ error: 'Search failed' }, { status: 502 })
}
}