feat(server): analyzeLink endpoint; test(server): analyzeLink unit tests; chore(server): linting

This commit is contained in:
Roberto Tonino
2021-06-01 22:35:49 +02:00
parent ffedd67a11
commit cb77745776
12 changed files with 283 additions and 67 deletions

View File

@@ -0,0 +1,30 @@
import { appSendGet } from '../../../../tests/utils'
describe('analyzeLink requests', () => {
it('should respond 200 to calls with supported term', async () => {
const res = await appSendGet('/api/analyzeLink/?term=https://www.deezer.com/en/album/100896762')
expect(res.status).toBe(200)
})
it('should respond with an error to calls with not supported term', async () => {
const res = await appSendGet('/api/analyzeLink/?term=https://www.deezer.com/en/artist/15166511')
expect(res.status).toBe(400)
expect(res.body.errorMessage).toBe('Not supported')
})
it('should respond album analyzed data', async () => {
const res = await appSendGet('/api/analyzeLink/?term=https://www.deezer.com/en/album/100896762')
expect(res.body.type).toBe('album')
expect(res.body.artist.name).toBe('Lil Nas X')
})
it('should respond track analyzed data', async () => {
const res = await appSendGet('/api/analyzeLink/?term=https://www.deezer.com/en/track/1283264142')
expect(res.body.type).toBe('track')
expect(res.body.artist.name).toBe('Lil Nas X')
})
})

View File

@@ -0,0 +1,47 @@
import type { RequestHandler } from 'express'
// @ts-expect-error
import deemix from 'deemix'
// @ts-expect-error
import { Deezer } from 'deezer-js'
import type { ApiHandler, GetTrackResponse, GetAlbumResponse } from '../../../types'
import { sessionDZ } from '../../../main'
export interface AnalyzeQuery {
term?: string
}
type ResBody = GetAlbumResponse | GetTrackResponse
const path: ApiHandler['path'] = '/analyzeLink'
const handler: RequestHandler<ResBody, {}, {}, AnalyzeQuery> = async (req, res) => {
try {
if (!req.query || !req.query.term) {
return res.status(400).send({ errorMessage: 'No term specified', errorCode: 'AL01' })
}
const { term: linkToAnalyze } = req.query
const [, linkType, linkId] = await deemix.parseLink(linkToAnalyze)
const isTrackOrAlbum = ['track', 'album'].includes(linkType)
if (isTrackOrAlbum) {
if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer()
const dz = sessionDZ[req.session.id]
const apiMethod = linkType === 'track' ? 'get_track' : 'get_album'
const resBody: ResBody = await dz.api[apiMethod](linkId)
return res.status(200).send(resBody)
}
return res.status(400).send({ errorMessage: 'Not supported', errorCode: 'AL02' })
} catch (error) {
return res
.status(500)
.send({ errorMessage: 'The server had a problem. Please try again', errorObject: error, errorCode: 'AL03' })
}
}
const apiHandler: ApiHandler = { path, handler }
export default apiHandler

View File

@@ -7,7 +7,7 @@ const path: ApiHandler['path'] = '/getQueue'
// let homeCache: any
const handler: ApiHandler['handler'] = (_, res) => {
const result:any = {
const result: any = {
queue,
order: queueOrder
}

View File

@@ -20,42 +20,42 @@ const handler: ApiHandler['handler'] = async (req, res) => {
}
case 'spotifyplaylist':
case 'spotify_playlist': {
if (!plugins.spotify.enabled){
if (!plugins.spotify.enabled) {
res.send({
collaborative: false,
description: "",
external_urls: {spotify: null},
followers: {total: 0, href: null},
description: '',
external_urls: { spotify: null },
followers: { total: 0, href: null },
id: null,
images: [],
name: "Something went wrong",
name: 'Something went wrong',
owner: {
display_name: "Error",
display_name: 'Error',
id: null
},
public: true,
tracks : [],
tracks: [],
type: 'playlist',
uri: null
})
break
}
let sp = plugins.spotify.sp
const sp = plugins.spotify.sp
let playlist = await sp.getPlaylist(list_id)
playlist = playlist.body
let tracklist = playlist.tracks.items
while (playlist.tracks.next) {
let regExec = /offset=(\d+)&limit=(\d+)/g.exec(playlist.tracks.next)
let offset = regExec![1]
let limit = regExec![2]
let playlistTracks = await sp.getPlaylistTracks(list_id, { offset, limit })
playlist.tracks = playlistTracks.body
tracklist = tracklist.concat(playlist.tracks.items)
}
tracklist.forEach((item:any, i:number) => {
const regExec = /offset=(\d+)&limit=(\d+)/g.exec(playlist.tracks.next)
const offset = regExec![1]
const limit = regExec![2]
const playlistTracks = await sp.getPlaylistTracks(list_id, { offset, limit })
playlist.tracks = playlistTracks.body
tracklist = tracklist.concat(playlist.tracks.items)
}
tracklist.forEach((item: any, i: number) => {
tracklist[i] = item.track
tracklist[i].selected = false
});
})
playlist.tracks = tracklist
res.send(playlist)
break

View File

@@ -6,25 +6,25 @@ const path: ApiHandler['path'] = '/getUserSpotifyPlaylists'
const handler: ApiHandler['handler'] = async (req, res) => {
let data
if (plugins.spotify.enabled){
let sp = plugins.spotify.sp
if (plugins.spotify.enabled) {
const sp = plugins.spotify.sp
const username = req.query.spotifyUser
data = []
let playlists = await sp.getUserPlaylists(username)
let playlistList = playlists.body.items
while (playlists.next) {
let regExec = /offset=(\d+)&limit=(\d+)/g.exec(playlists.next)
let offset = regExec![1]
let limit = regExec![2]
let newPlaylists = await sp.getUserPlaylists(username, { offset, limit })
playlists = newPlaylists.body
playlistList = playlistList.concat(playlists.items)
}
playlistList.forEach((playlist: any) => {
data.push(plugins.spotify._convertPlaylistStructure(playlist))
})
data = []
let playlists = await sp.getUserPlaylists(username)
let playlistList = playlists.body.items
while (playlists.next) {
const regExec = /offset=(\d+)&limit=(\d+)/g.exec(playlists.next)
const offset = regExec![1]
const limit = regExec![2]
const newPlaylists = await sp.getUserPlaylists(username, { offset, limit })
playlists = newPlaylists.body
playlistList = playlistList.concat(playlists.items)
}
playlistList.forEach((playlist: any) => {
data.push(plugins.spotify._convertPlaylistStructure(playlist))
})
} else {
data = { error: 'spotifyNotEnabled'}
data = { error: 'spotifyNotEnabled' }
}
res.send(data)
}

View File

@@ -1,3 +1,4 @@
import analyzeLink from './analyzeLink'
import getHome from './getHome'
import getCharts from './getCharts'
import mainSearch from './mainSearch'
@@ -16,6 +17,7 @@ import getQueue from './getQueue'
export default [
albumSearch,
analyzeLink,
getHome,
getCharts,
getChartTracks,

View File

@@ -8,12 +8,12 @@ import logout from './logout'
import saveSettings from './saveSettings'
export default [
loginArl,
addToQueue,
loginWithCredentials,
cancelAllDownloads,
removeFinishedDownloads,
removeFromQueue,
logout,
saveSettings
loginArl,
addToQueue,
loginWithCredentials,
cancelAllDownloads,
removeFinishedDownloads,
removeFromQueue,
logout,
saveSettings
]

View File

@@ -7,7 +7,7 @@ const handler: ApiHandler['handler'] = async (req, res) => {
const { email, password } = req.body
let accessToken = req.body.accessToken
if (!accessToken){
if (!accessToken) {
accessToken = await getAccessToken(email, password)
}
let arl

View File

@@ -4,13 +4,13 @@ import { cancelDownload } from '../../../main'
const path = '/removeFromQueue'
const handler: ApiHandler['handler'] = async (req, res) => {
const {uuid} = req.query
if (uuid){
cancelDownload(uuid)
res.send({ result: true })
}else{
res.send({ result: false })
}
const { uuid } = req.query
if (uuid) {
cancelDownload(uuid)
res.send({ result: true })
} else {
res.send({ result: false })
}
}
const apiHandler = { path, handler }

View File

@@ -1,6 +1,5 @@
import { ApiHandler } from '../../../types'
import { ApiHandler, Settings, SpotifySettings } from '../../../types'
import { saveSettings, listener } from '../../../main'
import { Settings, SpotifySettings } from '../../../types'
const path = '/saveSettings'
@@ -10,8 +9,8 @@ export interface SaveSettingsData {
}
const handler: ApiHandler['handler'] = async (req, res) => {
const { settings, spotifySettings }: SaveSettingsData = req.query
saveSettings(settings, spotifySettings)
const { settings, spotifySettings }: SaveSettingsData = req.query
saveSettings(settings, spotifySettings)
listener.send('updateSettings', { settings, spotifySettings })
res.send({ result: true })
}