From 554dfcced745fc96ecea851217a9d463e83e16bd Mon Sep 17 00:00:00 2001 From: RemixDev Date: Fri, 29 Jul 2022 20:05:27 +0200 Subject: [PATCH] Move /connect to /api/connect --- server/src/routes/api/get/connect.ts | 50 ++++++++++++++++++++++++++++ server/src/routes/api/get/index.ts | 2 ++ server/src/routes/index.ts | 44 ------------------------ 3 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 server/src/routes/api/get/connect.ts diff --git a/server/src/routes/api/get/connect.ts b/server/src/routes/api/get/connect.ts new file mode 100644 index 0000000..f5f9180 --- /dev/null +++ b/server/src/routes/api/get/connect.ts @@ -0,0 +1,50 @@ +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { ApiHandler } from '../../../types' +import { logger } from '../../../helpers/logger' +import { getLoginCredentials } from '../../../helpers/loginStorage' +import { sessionDZ, deemixVersion, currentVersion } from '../../../app' + +const path: ApiHandler['path'] = '/connect' +let update: any = null + +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + const dz = sessionDZ[req.session.id] + const deemix = req.app.get('deemix') + const isSingleUser = req.app.get('isSingleUser') + + if (!update) { + logger.info(`Currently running deemix-gui version ${currentVersion}`) + logger.info(`deemix-lib version ${deemixVersion}`) + update = { + currentCommit: currentVersion, + deemixVersion + } + } + + const result: any = { + update, + autologin: !dz.logged_in, + currentUser: dz.current_user, + deezerAvailable: await deemix.isDeezerAvailable(), + spotifyEnabled: deemix.plugins.spotify.enabled, + settingsData: deemix.getSettings() + } + + if (isSingleUser && result.autologin) result.singleUser = getLoginCredentials() + + if (result.settingsData.settings.autoCheckForUpdates) result.checkForUpdates = true + + const queue = deemix.getQueue() + + if (Object.keys(queue.queue).length > 0) { + result.queue = queue + } + + res.send(result) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/index.ts b/server/src/routes/api/get/index.ts index 2beb93b..8a5e49e 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -1,3 +1,4 @@ +import connect from './connect' import analyzeLink from './analyzeLink' import getHome from './getHome' import getCharts from './getCharts' @@ -19,6 +20,7 @@ import spotifyStatus from './spotifyStatus' import checkForUpdates from './checkForUpdates' export default [ + connect, albumSearch, analyzeLink, getHome, diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index 89bb249..dbbbb8d 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -1,48 +1,4 @@ import express from 'express' -// @ts-expect-error -import { Deezer } from 'deezer-js' -import { logger } from '../helpers/logger' -import { getLoginCredentials } from '../helpers/loginStorage' -import { sessionDZ, deemixVersion, currentVersion } from '../app' const router = express.Router() -let update: any = null - -router.get('/connect', async (req, res) => { - if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() - const dz = sessionDZ[req.session.id] - const deemix = req.app.get('deemix') - const isSingleUser = req.app.get('isSingleUser') - - if (!update) { - logger.info(`Currently running deemix-gui version ${currentVersion}`) - logger.info(`deemix-lib version ${deemixVersion}`) - update = { - currentCommit: currentVersion, - deemixVersion - } - } - - const result: any = { - update, - autologin: !dz.logged_in, - currentUser: dz.current_user, - deezerAvailable: await deemix.isDeezerAvailable(), - spotifyEnabled: deemix.plugins.spotify.enabled, - settingsData: deemix.getSettings() - } - - if (isSingleUser && result.autologin) result.singleUser = getLoginCredentials() - - if (result.settingsData.settings.autoCheckForUpdates) result.checkForUpdates = true - - const queue = deemix.getQueue() - - if (Object.keys(queue.queue).length > 0) { - result.queue = queue - } - - res.send(result) -}) - export default router