aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/youtube/search.js
blob: e06818c66076845618438b7abb5ee681041f5bb5 (plain) (blame)
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
import withSession from 'hocs/withSession'
import { youtube } from 'scrape-youtube'

export default withSession(async (req, res) => {
  switch (req.method) {
    case 'POST':
      try {
        const user = req.session.get('user')
        const { quote } = req.body

        if (!user || !user?.isVerified || !quote) {
          throw new Error('Something went wrong')
        }

        const results = await youtube.search(quote[0], {
          ...quote[1],
          requestOptions: {
            headers: { 'Accept-Language': user.language || 'en' }
          }
        })

        res.status(200).json(results)
      } catch (error) {
        res.status(400).json([])
      }
      break
    default:
      res.status(400).send()
      break
  }
})