mirror of
https://github.com/BradNut/graphbrainz
synced 2025-09-08 17:40:32 +00:00
83 lines
2 KiB
JavaScript
83 lines
2 KiB
JavaScript
import qs from 'qs'
|
|
import Client, { ClientError } from './client'
|
|
|
|
export class MusicBrainzError extends ClientError {}
|
|
|
|
export default class MusicBrainz extends Client {
|
|
constructor ({
|
|
baseURL = process.env.MUSICBRAINZ_BASE_URL || 'http://musicbrainz.org/ws/2/',
|
|
errorClass = MusicBrainzError,
|
|
limit = 5,
|
|
period = 5500,
|
|
...options
|
|
} = {}) {
|
|
super({ baseURL, errorClass, limit, period, ...options })
|
|
}
|
|
|
|
parseErrorMessage (response, body) {
|
|
if (body && body.error) {
|
|
return body.error
|
|
}
|
|
return super.parseErrorMessage(response, body)
|
|
}
|
|
|
|
stringifyParams (params) {
|
|
if (Array.isArray(params.inc)) {
|
|
params = {
|
|
...params,
|
|
inc: params.inc.join('+')
|
|
}
|
|
}
|
|
if (Array.isArray(params.type)) {
|
|
params = {
|
|
...params,
|
|
type: params.type.join('|')
|
|
}
|
|
}
|
|
if (Array.isArray(params.status)) {
|
|
params = {
|
|
...params,
|
|
status: params.status.join('|')
|
|
}
|
|
}
|
|
return qs.stringify(params, {
|
|
skipNulls: true,
|
|
filter: (key, value) => value === '' ? undefined : value
|
|
})
|
|
}
|
|
|
|
getURL (path, params) {
|
|
const query = params ? this.stringifyParams(params) : ''
|
|
return query ? `${path}?${query}` : path
|
|
}
|
|
|
|
getLookupURL (entity, id, params) {
|
|
if (id == null) {
|
|
return this.getBrowseURL(entity, params)
|
|
}
|
|
return this.getURL(`${entity}/${id}`, params)
|
|
}
|
|
|
|
lookup (entity, id, params = {}) {
|
|
const url = this.getLookupURL(entity, id, params)
|
|
return this.get(url, { json: true, qs: { fmt: 'json' } })
|
|
}
|
|
|
|
getBrowseURL (entity, params) {
|
|
return this.getURL(entity, params)
|
|
}
|
|
|
|
browse (entity, params = {}) {
|
|
const url = this.getBrowseURL(entity, params)
|
|
return this.get(url, { json: true, qs: { fmt: 'json' } })
|
|
}
|
|
|
|
getSearchURL (entity, query, params) {
|
|
return this.getURL(entity, { ...params, query })
|
|
}
|
|
|
|
search (entity, query, params = {}) {
|
|
const url = this.getSearchURL(entity, query, params)
|
|
return this.get(url, { json: true, qs: { fmt: 'json' } })
|
|
}
|
|
}
|