File size: 1,062 Bytes
0dec4f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 
    });
  }
}