2020-05-09 19:22:43 +02:00
|
|
|
import { formatISO, parseISO } from "date-fns";
|
2020-05-31 23:26:06 +02:00
|
|
|
import { qs } from "../util/queryString";
|
2020-05-02 21:46:41 +02:00
|
|
|
import { Listen } from "./entities/listen";
|
2020-05-09 19:22:43 +02:00
|
|
|
import { ListenReportItem } from "./entities/listen-report-item";
|
|
|
|
|
import { ListenReportOptions } from "./entities/listen-report-options";
|
2020-05-02 21:46:41 +02:00
|
|
|
import { Pagination } from "./entities/pagination";
|
|
|
|
|
import { PaginationOptions } from "./entities/pagination-options";
|
2020-05-31 23:26:06 +02:00
|
|
|
import { TopArtistsItem } from "./entities/top-artists-item";
|
|
|
|
|
import { TopArtistsOptions } from "./entities/top-artists-options";
|
2020-05-02 21:46:41 +02:00
|
|
|
import { User } from "./entities/user";
|
2020-05-02 03:03:19 +02:00
|
|
|
|
|
|
|
|
export class UnauthenticatedError extends Error {}
|
|
|
|
|
|
|
|
|
|
const getToken = (): string => {
|
|
|
|
|
const cookieValue = document.cookie.replace(
|
|
|
|
|
/(?:(?:^|.*;\s*)listory_access_token\s*=\s*([^;]*).*$)|^.*$/,
|
|
|
|
|
"$1"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return cookieValue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getDefaultHeaders = (): Headers => {
|
|
|
|
|
const headers = new Headers();
|
|
|
|
|
|
|
|
|
|
headers.append("Content-Type", "application/json");
|
|
|
|
|
headers.append("Authorization", `Bearer ${getToken()}`);
|
|
|
|
|
|
|
|
|
|
return headers;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getUsersMe = async (): Promise<User> => {
|
2020-05-02 21:46:05 +02:00
|
|
|
const res = await fetch(`/api/v1/users/me`, { headers: getDefaultHeaders() });
|
2020-05-02 03:03:19 +02:00
|
|
|
|
|
|
|
|
switch (res.status) {
|
|
|
|
|
case 200: {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 401: {
|
|
|
|
|
throw new UnauthenticatedError(`No token or token expired`);
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
throw new Error(`Unable to getUsersMe: ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const user: User = await res.json();
|
|
|
|
|
return user;
|
|
|
|
|
};
|
2020-05-02 21:46:41 +02:00
|
|
|
|
|
|
|
|
export const getRecentListens = async (
|
|
|
|
|
options: PaginationOptions = { page: 1, limit: 10 }
|
|
|
|
|
): Promise<Pagination<Listen>> => {
|
|
|
|
|
const { page, limit } = options;
|
|
|
|
|
|
2020-06-01 21:17:40 +02:00
|
|
|
const res = await fetch(
|
|
|
|
|
`/api/v1/listens?${qs({ page: page.toString(), limit: limit.toString() })}`,
|
|
|
|
|
{
|
|
|
|
|
headers: getDefaultHeaders(),
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-05-02 21:46:41 +02:00
|
|
|
|
|
|
|
|
switch (res.status) {
|
|
|
|
|
case 200: {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 401: {
|
|
|
|
|
throw new UnauthenticatedError(`No token or token expired`);
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
throw new Error(`Unable to getRecentListens: ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const listens: Pagination<Listen> = await res.json();
|
|
|
|
|
return listens;
|
|
|
|
|
};
|
2020-05-09 19:22:43 +02:00
|
|
|
|
|
|
|
|
export const getListensReport = async (
|
|
|
|
|
options: ListenReportOptions
|
|
|
|
|
): Promise<ListenReportItem[]> => {
|
|
|
|
|
const { timeFrame, timeStart, timeEnd } = options;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(
|
2020-06-01 21:17:40 +02:00
|
|
|
`/api/v1/reports/listens?${qs({
|
|
|
|
|
timeFrame,
|
|
|
|
|
timeStart: formatISO(timeStart),
|
|
|
|
|
timeEnd: formatISO(timeEnd),
|
|
|
|
|
})}`,
|
2020-05-09 19:22:43 +02:00
|
|
|
{
|
|
|
|
|
headers: getDefaultHeaders(),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
switch (res.status) {
|
|
|
|
|
case 200: {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 401: {
|
|
|
|
|
throw new UnauthenticatedError(`No token or token expired`);
|
|
|
|
|
}
|
|
|
|
|
default: {
|
2020-05-31 23:26:06 +02:00
|
|
|
throw new Error(`Unable to getListensReport: ${res.status}`);
|
2020-05-09 19:22:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rawItems: { count: number; date: string }[] = (await res.json()).items;
|
|
|
|
|
return rawItems.map(({ count, date }) => ({ count, date: parseISO(date) }));
|
|
|
|
|
};
|
2020-05-31 23:26:06 +02:00
|
|
|
|
|
|
|
|
export const getTopArtists = async (
|
|
|
|
|
options: TopArtistsOptions
|
|
|
|
|
): Promise<TopArtistsItem[]> => {
|
2020-07-12 17:16:33 +02:00
|
|
|
const {
|
|
|
|
|
time: { timePreset, customTimeStart, customTimeEnd },
|
|
|
|
|
} = options;
|
2020-05-31 23:26:06 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|