feat(server): implemented getChartTracks api; feat(server): added some error helpers
This commit is contained in:
parent
4919c8d698
commit
2433209676
11
server/src/helpers/errors.ts
Normal file
11
server/src/helpers/errors.ts
Normal 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
|
1
server/src/helpers/primitive-checks.ts
Normal file
1
server/src/helpers/primitive-checks.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const isObjectEmpy = (obj: any) => Object.keys(obj).length === 0
|
@ -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
|
@ -4,5 +4,6 @@ import mainSearch from './mainSearch'
|
|||||||
import search from './search'
|
import search from './search'
|
||||||
import getTracklist from './getTracklist'
|
import getTracklist from './getTracklist'
|
||||||
import albumSearch from './albumSearch'
|
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]
|
||||||
|
Loading…
Reference in New Issue
Block a user