Spaces:
Runtime error
Runtime error
| import axios from 'axios'; | |
| export default async function handler(req, res) { | |
| const { accessToken, type } = req.query; | |
| if (!accessToken) { | |
| return res.status(401).json({ error: 'Missing access token' }); | |
| } | |
| try { | |
| let url; | |
| if (type === 'profile') { | |
| url = 'https://api.spotify.com/v1/me'; | |
| } else if (type === 'tracks') { | |
| url = 'https://api.spotify.com/v1/me/top/tracks?limit=10&time_range=medium_term'; | |
| } else if (type === 'artists') { | |
| url = 'https://api.spotify.com/v1/me/top/artists?limit=10&time_range=medium_term'; | |
| } else { | |
| return res.status(400).json({ error: 'Invalid type' }); | |
| } | |
| const response = await axios.get(url, { | |
| headers: { | |
| Authorization: `Bearer ${accessToken}`, | |
| }, | |
| }); | |
| res.status(200).json(response.data); | |
| } catch (error) { | |
| console.error('Spotify API error:', error.response?.data || error.message); | |
| res.status(error.response?.status || 500).json({ | |
| error: 'Failed to fetch Spotify data', | |
| details: error.response?.data | |
| }); | |
| } | |
| } |