import withSession from 'hocs/withSession' import ytdl from 'ytdl-core' const getId = url => { const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/ const match = url.match(regExp) return (match && match[7].length === 11) ? match[7] : false } export default withSession(async (req, res) => { switch (req.method) { case 'POST': try { const user = req.session.get('user') const { url } = req.body if (!user || !user?.isVerified || !url) { throw new Error('Something went wrong') } const info = await ytdl.getInfo(getId(url)) res.status(200).json(info) } catch (error) { res.status(400).json([]) } break default: res.status(400).send() break } })