From 243320967674f25048f24b990e07adb0ac1a0e1c Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Sat, 24 Apr 2021 21:53:24 +0200 Subject: [PATCH] feat(server): implemented getChartTracks api; feat(server): added some error helpers --- server/src/helpers/errors.ts | 11 ++++++ server/src/helpers/primitive-checks.ts | 1 + server/src/routes/api/get/getChartTracks.ts | 39 +++++++++++++++++++++ server/src/routes/api/get/index.ts | 3 +- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 server/src/helpers/errors.ts create mode 100644 server/src/helpers/primitive-checks.ts diff --git a/server/src/helpers/errors.ts b/server/src/helpers/errors.ts new file mode 100644 index 0000000..758de31 --- /dev/null +++ b/server/src/helpers/errors.ts @@ -0,0 +1,11 @@ +export const logError = (console: any) => (errorText: string) => console.error(`[deemix-server]: ${errorText}`) +export const consoleError = logError(console) + +export class BadRequestError extends Error { + constructor() { + super() + this.message = 'Bad request!' + } +} + +export const isBadRequestError = (error: any) => error instanceof BadRequestError diff --git a/server/src/helpers/primitive-checks.ts b/server/src/helpers/primitive-checks.ts new file mode 100644 index 0000000..9d0b73a --- /dev/null +++ b/server/src/helpers/primitive-checks.ts @@ -0,0 +1 @@ +export const isObjectEmpy = (obj: any) => Object.keys(obj).length === 0 diff --git a/server/src/routes/api/get/getChartTracks.ts b/server/src/routes/api/get/getChartTracks.ts index e69de29..6c90761 100644 --- a/server/src/routes/api/get/getChartTracks.ts +++ b/server/src/routes/api/get/getChartTracks.ts @@ -0,0 +1,39 @@ +import { RequestHandler } from 'express' +import { ApiHandler } from '../../../types' +import { dz } from '../../../main' +import { isObjectEmpy } from '../../../helpers/primitive-checks' +import { BadRequestError, consoleError, isBadRequestError } from '../../../helpers/errors' + +export interface RawChartTracksQuery { + id: string + index?: number + limit?: number +} + +const path: ApiHandler['path'] = '/get-chart-tracks' + +const handler: RequestHandler<{}, {}, {}, RawChartTracksQuery> = async (req, res, next) => { + try { + if (isObjectEmpy(req.query) || !req.query.id) { + throw new BadRequestError() + } + + const playlistId = req.query.id + const index = req.query.index + const limit = req.query.limit + + const response = await dz.api.get_playlist_tracks(playlistId, { index, limit }) + res.status(200).send(response) + next() + } catch (error) { + if (isBadRequestError(error)) { + consoleError(error.message) + res.status(400).send() + return next() + } + } +} + +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 c1446fc..b6aaf89 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -4,5 +4,6 @@ import mainSearch from './mainSearch' import search from './search' import getTracklist from './getTracklist' import albumSearch from './albumSearch' +import getChartTracks from './getChartTracks' -export default [albumSearch, getHome, getCharts, mainSearch, search, getTracklist] +export default [albumSearch, getHome, getCharts, getChartTracks, mainSearch, search, getTracklist]