From 9fc36abf069bf33d9114bac61da92b2a767951df Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Tue, 11 May 2021 20:21:37 +0200 Subject: [PATCH] fix: type checking errors; feat: added .yarnrc file; chore: linting --- server/.yarnrc | 1 + server/Makefile | 6 +-- server/src/main.ts | 6 +-- server/src/middlewares.ts | 24 +++++---- server/src/routes/api/get/albumSearch.ts | 4 +- server/src/routes/api/get/getChartTracks.ts | 4 +- server/src/routes/api/get/getCharts.ts | 4 +- server/src/routes/api/get/getHome.ts | 4 +- server/src/routes/api/get/getSettings.ts | 2 +- server/src/routes/api/get/getTracklist.ts | 10 ++-- server/src/routes/api/get/getUserAlbums.ts | 30 +++++------ server/src/routes/api/get/getUserArtists.ts | 30 +++++------ server/src/routes/api/get/getUserFavorites.ts | 50 +++++++++---------- server/src/routes/api/get/getUserPlaylists.ts | 30 +++++------ server/src/routes/api/get/getUserTracks.ts | 30 +++++------ server/src/routes/api/get/index.ts | 26 +++++----- server/src/routes/api/get/mainSearch.ts | 6 +-- server/src/routes/api/get/search.ts | 8 +-- server/src/routes/api/post/login-arl.ts | 6 +-- server/src/routes/index.ts | 8 +-- webui | 2 +- 21 files changed, 146 insertions(+), 145 deletions(-) create mode 100644 server/.yarnrc diff --git a/server/.yarnrc b/server/.yarnrc new file mode 100644 index 0000000..a5395f5 --- /dev/null +++ b/server/.yarnrc @@ -0,0 +1 @@ +save-prefix: "" diff --git a/server/Makefile b/server/Makefile index d9fa2ac..0e0347f 100644 --- a/server/Makefile +++ b/server/Makefile @@ -1,10 +1,10 @@ NODE_BIN ?= .\node_modules\.bin lint: - @$(NODE_BIN)\eslint ./src/** --fix + $(NODE_BIN)\eslint ./src/** --fix test: - @$(NODE_BIN)\jest + $(NODE_BIN)\jest build: lint test - @$(NODE_BIN)\tsc + $(NODE_BIN)\tsc diff --git a/server/src/main.ts b/server/src/main.ts index 6cc942f..766ec8b 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,8 +1,6 @@ // @ts-expect-error -import { Deezer } from 'deezer-js' -// @ts-expect-error import deemix from 'deemix' -export let settings: any = deemix.settings.load() +export const settings: any = deemix.settings.load() export const defaultSettings: any = deemix.settings.DEFAULTS -export let sessionDZ: any = {} +export const sessionDZ: any = {} diff --git a/server/src/middlewares.ts b/server/src/middlewares.ts index 111a5a0..c5ef7f0 100644 --- a/server/src/middlewares.ts +++ b/server/src/middlewares.ts @@ -4,14 +4,14 @@ import express from 'express' import cookieParser from 'cookie-parser' import session from 'express-session' -declare module 'express-session' { - export interface SessionData { - dz: any; - } -} - import { WEBUI_DIR } from './helpers/paths' +declare module 'express-session' { + export interface SessionData { + dz: any + } +} + export function registerMiddlewares(app: Application) { if (process.env.NODE_ENV !== 'test') { app.use(logger('dev')) @@ -20,10 +20,12 @@ export function registerMiddlewares(app: Application) { app.use(express.json()) app.use(express.urlencoded({ extended: false })) app.use(cookieParser()) - app.use(session({ - secret: "U2hoLCBpdHMgYSBzZWNyZXQh", - resave: true, - saveUninitialized: true - })) + app.use( + session({ + secret: 'U2hoLCBpdHMgYSBzZWNyZXQh', + resave: true, + saveUninitialized: true + }) + ) app.use(express.static(WEBUI_DIR)) } diff --git a/server/src/routes/api/get/albumSearch.ts b/server/src/routes/api/get/albumSearch.ts index 007cd75..ba10f33 100644 --- a/server/src/routes/api/get/albumSearch.ts +++ b/server/src/routes/api/get/albumSearch.ts @@ -1,7 +1,7 @@ import { RequestHandler } from 'express' -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' export interface RawAlbumQuery { @@ -26,7 +26,7 @@ const path: ApiHandler['path'] = '/album-search/' const handler: RequestHandler<{}, {}, {}, RawAlbumQuery> = async (req, res, next) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] if (!req.query) { res.status(400).send() diff --git a/server/src/routes/api/get/getChartTracks.ts b/server/src/routes/api/get/getChartTracks.ts index cd2c3c3..f2a0483 100644 --- a/server/src/routes/api/get/getChartTracks.ts +++ b/server/src/routes/api/get/getChartTracks.ts @@ -1,7 +1,7 @@ import { RequestHandler } from 'express' -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' import { isObjectEmpy } from '../../../helpers/primitive-checks' @@ -18,7 +18,7 @@ const path: ApiHandler['path'] = '/getChartTracks' const handler: RequestHandler<{}, {}, {}, RawChartTracksQuery> = async (req, res, next) => { try { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] if (isObjectEmpy(req.query) || !req.query.id) { throw new BadRequestError() diff --git a/server/src/routes/api/get/getCharts.ts b/server/src/routes/api/get/getCharts.ts index 9079437..acbac1f 100644 --- a/server/src/routes/api/get/getCharts.ts +++ b/server/src/routes/api/get/getCharts.ts @@ -1,6 +1,6 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getCharts' @@ -10,7 +10,7 @@ let chartsCache: any const handler: ApiHandler['handler'] = async (req, res) => { if (!chartsCache) { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] const chartsData = await dz.api.get_countries_charts() const countries: any[] = [] diff --git a/server/src/routes/api/get/getHome.ts b/server/src/routes/api/get/getHome.ts index d275d79..964e48f 100644 --- a/server/src/routes/api/get/getHome.ts +++ b/server/src/routes/api/get/getHome.ts @@ -1,6 +1,6 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getHome' @@ -9,7 +9,7 @@ let homeCache: any const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] if (!homeCache) { homeCache = await dz.api.get_chart(0, { limit: 30 }) diff --git a/server/src/routes/api/get/getSettings.ts b/server/src/routes/api/get/getSettings.ts index 6ee4b1d..8adb241 100644 --- a/server/src/routes/api/get/getSettings.ts +++ b/server/src/routes/api/get/getSettings.ts @@ -4,7 +4,7 @@ import { settings, defaultSettings } from '../../../main' const path: ApiHandler['path'] = '/getSettings' const handler: ApiHandler['handler'] = async (_, res) => { - res.send({settings, defaultSettings}) + res.send({ settings, defaultSettings }) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getTracklist.ts b/server/src/routes/api/get/getTracklist.ts index e5e3100..b39843e 100644 --- a/server/src/routes/api/get/getTracklist.ts +++ b/server/src/routes/api/get/getTracklist.ts @@ -1,20 +1,20 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getTracklist' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] - let list_id = String(req.query.id) - let list_type = String(req.query.type) + const list_id = String(req.query.id) + const list_type = String(req.query.type) switch (list_type) { case 'artist': { const artistAPI = await dz.api.get_artist(list_id) - artistAPI.releases = await dz.gw.get_artist_discography_tabs(list_id, {limit: 100}) + artistAPI.releases = await dz.gw.get_artist_discography_tabs(list_id, { limit: 100 }) res.send(artistAPI) break } diff --git a/server/src/routes/api/get/getUserAlbums.ts b/server/src/routes/api/get/getUserAlbums.ts index 9bc43ed..68d7b19 100644 --- a/server/src/routes/api/get/getUserAlbums.ts +++ b/server/src/routes/api/get/getUserAlbums.ts @@ -1,27 +1,27 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getUserAlbums' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] - let data + const dz = sessionDZ[req.session.id] + let data - if (dz.logged_in){ - let userID = dz.current_user.id - try { - data = await dz.api.get_user_albums(userID, {limit: -1}) - data = data.data - } catch { - data = await dz.gw.get_user_albums(userID, {limit: -1}) - } - } else { - data = {error: "notLoggedIn"} - } - res.send(data) + if (dz.logged_in) { + const userID = dz.current_user.id + try { + data = await dz.api.get_user_albums(userID, { limit: -1 }) + data = data.data + } catch { + data = await dz.gw.get_user_albums(userID, { limit: -1 }) + } + } else { + data = { error: 'notLoggedIn' } + } + res.send(data) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getUserArtists.ts b/server/src/routes/api/get/getUserArtists.ts index 6b36105..206e716 100644 --- a/server/src/routes/api/get/getUserArtists.ts +++ b/server/src/routes/api/get/getUserArtists.ts @@ -1,27 +1,27 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getUserArtists' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] - let data + const dz = sessionDZ[req.session.id] + let data - if (dz.logged_in){ - let userID = dz.current_user.id - try { - data = await dz.api.get_user_artists(userID, {limit: -1}) - data = data.data - } catch { - data = await dz.gw.get_user_artists(userID, {limit: -1}) - } - } else { - data = {error: "notLoggedIn"} - } - res.send(data) + if (dz.logged_in) { + const userID = dz.current_user.id + try { + data = await dz.api.get_user_artists(userID, { limit: -1 }) + data = data.data + } catch { + data = await dz.gw.get_user_artists(userID, { limit: -1 }) + } + } else { + data = { error: 'notLoggedIn' } + } + res.send(data) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getUserFavorites.ts b/server/src/routes/api/get/getUserFavorites.ts index 97c77a9..b118d0f 100644 --- a/server/src/routes/api/get/getUserFavorites.ts +++ b/server/src/routes/api/get/getUserFavorites.ts @@ -1,39 +1,39 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getUserFavorites' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] - let result: any = {} + let result: any = {} - if (dz.logged_in){ - let userID = dz.current_user.id + if (dz.logged_in) { + const userID = dz.current_user.id - try { - let data - data = await dz.api.get_user_playlists(userID, {limit: -1}) - result['playlists'] = data.data - data = await dz.api.get_user_albums(userID, {limit: -1}) - result['albums'] = data.data - data = await dz.api.get_user_artists(userID, {limit: -1}) - result['artists'] = data.data - data = await dz.api.get_user_tracks(userID, {limit: -1}) - result['tracks'] = data.data - } catch { - result['playlists'] = await dz.gw.get_user_playlists(userID, {limit: -1}) - result['albums'] = await dz.gw.get_user_albums(userID, {limit: -1}) - result['artists'] = await dz.gw.get_user_artists(userID, {limit: -1}) - result['tracks'] = await dz.gw.get_user_tracks(userID, {limit: -1}) - } - } else { - result = {error: "notLoggedIn"} - } - res.send(result) + try { + let data + data = await dz.api.get_user_playlists(userID, { limit: -1 }) + result.playlists = data.data + data = await dz.api.get_user_albums(userID, { limit: -1 }) + result.albums = data.data + data = await dz.api.get_user_artists(userID, { limit: -1 }) + result.artists = data.data + data = await dz.api.get_user_tracks(userID, { limit: -1 }) + result.tracks = data.data + } catch { + result.playlists = await dz.gw.get_user_playlists(userID, { limit: -1 }) + result.albums = await dz.gw.get_user_albums(userID, { limit: -1 }) + result.artists = await dz.gw.get_user_artists(userID, { limit: -1 }) + result.tracks = await dz.gw.get_user_tracks(userID, { limit: -1 }) + } + } else { + result = { error: 'notLoggedIn' } + } + res.send(result) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getUserPlaylists.ts b/server/src/routes/api/get/getUserPlaylists.ts index f3c0360..d1248e4 100644 --- a/server/src/routes/api/get/getUserPlaylists.ts +++ b/server/src/routes/api/get/getUserPlaylists.ts @@ -1,27 +1,27 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getUserPlaylists' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] - let data + const dz = sessionDZ[req.session.id] + let data - if (dz.logged_in){ - let userID = dz.current_user.id - try { - data = await dz.api.get_user_playlists(userID, {limit: -1}) - data = data.data - } catch { - data = await dz.gw.get_user_playlists(userID, {limit: -1}) - } - } else { - data = {error: "notLoggedIn"} - } - res.send(data) + if (dz.logged_in) { + const userID = dz.current_user.id + try { + data = await dz.api.get_user_playlists(userID, { limit: -1 }) + data = data.data + } catch { + data = await dz.gw.get_user_playlists(userID, { limit: -1 }) + } + } else { + data = { error: 'notLoggedIn' } + } + res.send(data) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getUserTracks.ts b/server/src/routes/api/get/getUserTracks.ts index 4eea7f1..da60127 100644 --- a/server/src/routes/api/get/getUserTracks.ts +++ b/server/src/routes/api/get/getUserTracks.ts @@ -1,27 +1,27 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getUserTracks' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] - let data + const dz = sessionDZ[req.session.id] + let data - if (dz.logged_in){ - let userID = dz.current_user.id - try { - data = await dz.api.get_user_tracks(userID, {limit: -1}) - data = data.data - } catch { - data = await dz.gw.get_user_tracks(userID, {limit: -1}) - } - } else { - data = {error: "notLoggedIn"} - } - res.send(data) + if (dz.logged_in) { + const userID = dz.current_user.id + try { + data = await dz.api.get_user_tracks(userID, { limit: -1 }) + data = data.data + } catch { + data = await dz.gw.get_user_tracks(userID, { limit: -1 }) + } + } else { + data = { error: 'notLoggedIn' } + } + res.send(data) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/index.ts b/server/src/routes/api/get/index.ts index b11c692..2c0ca91 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -13,17 +13,17 @@ import getUserPlaylists from './getUserPlaylists' import getUserFavorites from './getUserFavorites' export default [ - albumSearch, - getHome, - getCharts, - getChartTracks, - mainSearch, - search, - getTracklist, - getSettings, - getUserTracks, - getUserAlbums, - getUserArtists, - getUserPlaylists, - getUserFavorites + albumSearch, + getHome, + getCharts, + getChartTracks, + mainSearch, + search, + getTracklist, + getSettings, + getUserTracks, + getUserAlbums, + getUserArtists, + getUserPlaylists, + getUserFavorites ] diff --git a/server/src/routes/api/get/mainSearch.ts b/server/src/routes/api/get/mainSearch.ts index bb8283f..e7c6ed6 100644 --- a/server/src/routes/api/get/mainSearch.ts +++ b/server/src/routes/api/get/mainSearch.ts @@ -1,16 +1,16 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/mainSearch' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] const term = String(req.query.term) - let results = await dz.gw.search(term) + const results = await dz.gw.search(term) const order: string[] = [] results.ORDER.forEach((element: string) => { if (['TOP_RESULT', 'TRACK', 'ALBUM', 'ARTIST', 'PLAYLIST'].includes(element)) order.push(element) diff --git a/server/src/routes/api/get/search.ts b/server/src/routes/api/get/search.ts index 5dea51d..e660ce7 100644 --- a/server/src/routes/api/get/search.ts +++ b/server/src/routes/api/get/search.ts @@ -1,20 +1,20 @@ -import { ApiHandler } from '../../../types' // @ts-expect-error import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/search' const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] const term = String(req.query.term) const type = String(req.query.type) const start = parseInt(String(req.query.start)) const nb = parseInt(String(req.query.nb)) - let data; + let data switch (type) { case 'track': @@ -39,7 +39,7 @@ const handler: ApiHandler['handler'] = async (req, res) => { data = await dz.api.search(term, { limit: nb, index: start }) break } - + data.type = type res.send(data) } diff --git a/server/src/routes/api/post/login-arl.ts b/server/src/routes/api/post/login-arl.ts index 8525a7a..c58e7ab 100644 --- a/server/src/routes/api/post/login-arl.ts +++ b/server/src/routes/api/post/login-arl.ts @@ -21,7 +21,7 @@ const path: ApiHandler['path'] = '/login-arl/' const handler: RequestHandler<{}, {}, {}, RawLoginArlQuery> = async (req, res, next) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] if (!req.query) { res.status(400).send() @@ -44,7 +44,7 @@ const handler: RequestHandler<{}, {}, {}, RawLoginArlQuery> = async (req, res, n let response if (process.env.NODE_ENV !== 'test') { - if (!dz.logged_in){ + if (!dz.logged_in) { response = await dz.login_via_arl(...loginParams) response = response ? 1 : 0 } else { @@ -55,7 +55,7 @@ const handler: RequestHandler<{}, {}, {}, RawLoginArlQuery> = async (req, res, n response = await testDz.login_via_arl(...loginParams) } console.log(response) - let returnValue = {status: response, arl: req.query.arl, user: dz.current_user} + const returnValue = { status: response, arl: req.query.arl, user: dz.current_user } res.status(200).send(returnValue) next() diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index b60e3e6..d474a22 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -16,14 +16,14 @@ router.get('/', (_, res) => { router.get('/connect', (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - let dz = sessionDZ[req.session.id] + const dz = sessionDZ[req.session.id] res.send({ update: { - currentCommit: "testing", - latestCommit: "testing", + currentCommit: 'testing', + latestCommit: 'testing', updateAvailable: false, - deemixVersion: "3.0_beta" + deemixVersion: '3.0_beta' }, autologin: !dz.logged_in, currentUser: dz.current_user, diff --git a/webui b/webui index 6baf7d9..9710a5f 160000 --- a/webui +++ b/webui @@ -1 +1 @@ -Subproject commit 6baf7d989bafdf1100422d03308ddedf49af1c2c +Subproject commit 9710a5f19f6614fefc760e1771cb67b410ee55de