feat: add top-artists report

This commit is contained in:
Julian Tölle 2020-05-31 23:26:06 +02:00
parent 6a6ba493f6
commit 6fc10c40ca
18 changed files with 345 additions and 30 deletions

View file

@ -1,11 +1,13 @@
import { formatISO, parseISO } from "date-fns";
import { qs } from "../util/queryString";
import { Listen } from "./entities/listen";
import { ListenReportItem } from "./entities/listen-report-item";
import { ListenReportOptions } from "./entities/listen-report-options";
import { Pagination } from "./entities/pagination";
import { PaginationOptions } from "./entities/pagination-options";
import { TopArtistsItem } from "./entities/top-artists-item";
import { TopArtistsOptions } from "./entities/top-artists-options";
import { User } from "./entities/user";
import { qs } from "../util/queryString";
export class UnauthenticatedError extends Error {}
@ -99,10 +101,42 @@ export const getListensReport = async (
throw new UnauthenticatedError(`No token or token expired`);
}
default: {
throw new Error(`Unable to getRecentListens: ${res.status}`);
throw new Error(`Unable to getListensReport: ${res.status}`);
}
}
const rawItems: { count: number; date: string }[] = (await res.json()).items;
return rawItems.map(({ count, date }) => ({ count, date: parseISO(date) }));
};
export const getTopArtists = async (
options: TopArtistsOptions
): Promise<TopArtistsItem[]> => {
const { timePreset, customTimeStart, customTimeEnd } = options;
const res = await fetch(
`/api/v1/reports/top-artists?${qs({
timePreset,
customTimeStart: formatISO(customTimeStart),
customTimeEnd: formatISO(customTimeEnd),
})}`,
{
headers: getDefaultHeaders(),
}
);
switch (res.status) {
case 200: {
break;
}
case 401: {
throw new UnauthenticatedError(`No token or token expired`);
}
default: {
throw new Error(`Unable to getTopArtists: ${res.status}`);
}
}
const items: TopArtistsItem[] = (await res.json()).items;
return items;
};

View file

@ -0,0 +1,7 @@
import { SpotifyInfo } from "./spotify-info";
export interface Album {
id: string;
name: string;
spotify?: SpotifyInfo;
}

View file

@ -0,0 +1,7 @@
import { SpotifyInfo } from "./spotify-info";
export interface Artist {
id: string;
name: string;
spotify?: SpotifyInfo;
}

View file

@ -1,32 +1,7 @@
import { Track } from "./track";
export interface Listen {
id: string;
playedAt: string;
track: Track;
}
interface Track {
id: string;
name: string;
album: Album;
artists: Artist[];
spotify?: SpotifyInfo;
}
interface Album {
id: string;
name: string;
spotify?: SpotifyInfo;
}
interface Artist {
id: string;
name: string;
spotify?: SpotifyInfo;
}
interface SpotifyInfo {
id: string;
uri: string;
type: string;
href: string;
}

View file

@ -0,0 +1,6 @@
export interface SpotifyInfo {
id: string;
uri: string;
type: string;
href: string;
}

View file

@ -0,0 +1,9 @@
export enum TimePreset {
LAST_7_DAYS = "last_7_days",
LAST_30_DAYS = "last_30_days",
LAST_90_DAYS = "last_90_days",
LAST_180_DAYS = "last_180_days",
LAST_365_DAYS = "last_365_days",
ALL_TIME = "all_time",
CUSTOM = "custom",
}

View file

@ -0,0 +1,6 @@
import { Artist } from "./artist";
export interface TopArtistsItem {
artist: Artist;
count: number;
}

View file

@ -0,0 +1,7 @@
import { TimePreset } from "./time-preset.enum";
export interface TopArtistsOptions {
timePreset: TimePreset;
customTimeStart: Date;
customTimeEnd: Date;
}

View file

@ -0,0 +1,11 @@
import { Album } from "./album";
import { Artist } from "./artist";
import { SpotifyInfo } from "./spotify-info";
export interface Track {
id: string;
name: string;
album: Album;
artists: Artist[];
spotify?: SpotifyInfo;
}