2021-12-30 22:40:57 +00:00
|
|
|
import RelationLink from './relation-link.js';
|
|
|
|
|
|
|
|
type ApiReleaseData = {
|
|
|
|
'artist-credit': Array<{
|
|
|
|
name: string;
|
|
|
|
joinphrase: string;
|
|
|
|
}>;
|
|
|
|
'cover-art-archive': {
|
|
|
|
front: boolean;
|
|
|
|
};
|
|
|
|
id: string;
|
|
|
|
relations: Array<{
|
|
|
|
url: {
|
|
|
|
resource: string;
|
|
|
|
};
|
|
|
|
}>;
|
|
|
|
title: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
interface IRelease {
|
|
|
|
artist: string;
|
|
|
|
image: string | undefined;
|
|
|
|
links: RelationLink[];
|
|
|
|
title: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default interface Release extends IRelease {}
|
|
|
|
|
|
|
|
export default class Release {
|
|
|
|
public static async fromMbid(mbid: string): Promise<Release> {
|
|
|
|
const apiResponse = await window.fetch(this.apiUrl(mbid), {
|
|
|
|
headers: {
|
|
|
|
accept: 'application/json',
|
2022-01-06 20:12:20 +00:00
|
|
|
'user-agent': hrefPlusUserAgent,
|
2021-12-30 22:40:57 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!apiResponse.ok) {
|
|
|
|
throw new Error(`No release found with MBID ${mbid}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = (await apiResponse.json()) as ApiReleaseData;
|
|
|
|
|
|
|
|
const artist = data['artist-credit']
|
|
|
|
.map(({name, joinphrase}) => `${name}${joinphrase}`)
|
|
|
|
.join('');
|
|
|
|
|
|
|
|
const image = data['cover-art-archive'].front
|
|
|
|
? `https://coverartarchive.org/release/${mbid}/front-500`
|
|
|
|
: undefined;
|
|
|
|
|
2022-01-07 21:32:34 +00:00
|
|
|
const relations = new Set(data.relations.map(({url}) => url.resource));
|
|
|
|
const links = Array.from(relations)
|
|
|
|
.map((url) => new RelationLink(url))
|
2021-12-30 22:40:57 +00:00
|
|
|
.sort((a, b) => a.text.localeCompare(b.text));
|
|
|
|
|
|
|
|
return new Release({
|
|
|
|
artist,
|
|
|
|
image,
|
|
|
|
links,
|
|
|
|
title: data.title,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static apiUrl(mbid: string): string {
|
|
|
|
const root = 'https://musicbrainz.org/ws/2';
|
|
|
|
return `${root}/release/${mbid}?inc=artists+url-rels`;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(release: IRelease) {
|
|
|
|
Object.assign(this, release, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
public display(): string {
|
|
|
|
return `${this.artist} - ${this.title}`;
|
|
|
|
}
|
|
|
|
}
|