feat(server): implemented getChartTracks api; feat(server): added some error helpers

This commit is contained in:
Roberto Tonino 2021-04-24 21:53:24 +02:00
parent 4919c8d698
commit 2433209676
4 changed files with 53 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1 @@
export const isObjectEmpy = (obj: any) => Object.keys(obj).length === 0

View File

@ -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

View File

@ -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]