Listory/frontend/src/api/api.ts

230 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-09-05 23:35:53 +02:00
import { AxiosInstance } from "axios";
import { formatISO, parseISO } from "date-fns";
2020-05-02 21:46:41 +02:00
import { Listen } from "./entities/listen";
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";
import { TopAlbumsItem } from "./entities/top-albums-item";
import { TopAlbumsOptions } from "./entities/top-albums-options";
2020-05-31 23:26:06 +02:00
import { TopArtistsItem } from "./entities/top-artists-item";
import { TopArtistsOptions } from "./entities/top-artists-options";
2021-10-26 20:09:52 +02:00
import { TopGenresItem } from "./entities/top-genres-item";
import { TopGenresOptions } from "./entities/top-genres-options";
2021-05-22 14:57:28 +02:00
import { TopTracksItem } from "./entities/top-tracks-item";
import { TopTracksOptions } from "./entities/top-tracks-options";
export class UnauthenticatedError extends Error {}
2020-05-02 21:46:41 +02:00
export const getRecentListens = async (
2020-09-05 23:35:53 +02:00
options: PaginationOptions = { page: 1, limit: 10 },
client: AxiosInstance
2020-05-02 21:46:41 +02:00
): Promise<Pagination<Listen>> => {
const { page, limit } = options;
2020-09-05 23:35:53 +02:00
const res = await client.get<Pagination<Listen>>(`/api/v1/listens`, {
params: { page, limit },
});
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}`);
}
}
2020-09-05 23:35:53 +02:00
return res.data;
2020-05-02 21:46:41 +02:00
};
export const getListensReport = async (
2020-09-05 23:35:53 +02:00
options: ListenReportOptions,
client: AxiosInstance
): Promise<ListenReportItem[]> => {
const {
timeFrame,
time: { timePreset, customTimeStart, customTimeEnd },
} = options;
2020-09-05 23:35:53 +02:00
const res = await client.get<{ items: { count: number; date: string }[] }>(
`/api/v1/reports/listens`,
{
2020-09-05 23:35:53 +02:00
params: {
timeFrame,
timePreset,
customTimeStart: formatISO(customTimeStart),
customTimeEnd: formatISO(customTimeEnd),
},
}
);
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-09-05 23:35:53 +02:00
const {
data: { items: rawItems },
} = res;
return rawItems.map(({ count, date }) => ({ count, date: parseISO(date) }));
};
2020-05-31 23:26:06 +02:00
export const getTopArtists = async (
2020-09-05 23:35:53 +02:00
options: TopArtistsOptions,
client: AxiosInstance
2020-05-31 23:26:06 +02:00
): Promise<TopArtistsItem[]> => {
const {
time: { timePreset, customTimeStart, customTimeEnd },
} = options;
2020-05-31 23:26:06 +02:00
2020-09-05 23:35:53 +02:00
const res = await client.get<{ items: TopArtistsItem[] }>(
`/api/v1/reports/top-artists`,
2020-05-31 23:26:06 +02:00
{
2020-09-05 23:35:53 +02:00
params: {
timePreset,
customTimeStart: formatISO(customTimeStart),
customTimeEnd: formatISO(customTimeEnd),
},
2020-05-31 23:26:06 +02:00
}
);
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}`);
}
}
2020-09-05 23:35:53 +02:00
const {
data: { items },
} = res;
2020-05-31 23:26:06 +02:00
return items;
};
export const getTopAlbums = async (
options: TopAlbumsOptions,
client: AxiosInstance
): Promise<TopAlbumsItem[]> => {
const {
time: { timePreset, customTimeStart, customTimeEnd },
} = options;
const res = await client.get<{ items: TopAlbumsItem[] }>(
`/api/v1/reports/top-albums`,
{
params: {
timePreset,
customTimeStart: formatISO(customTimeStart),
customTimeEnd: formatISO(customTimeEnd),
},
}
);
switch (res.status) {
case 200: {
break;
}
case 401: {
throw new UnauthenticatedError(`No token or token expired`);
}
default: {
throw new Error(`Unable to getTopAlbums: ${res.status}`);
}
}
const {
data: { items },
} = res;
return items;
};
2021-05-22 14:57:28 +02:00
export const getTopTracks = async (
options: TopTracksOptions,
client: AxiosInstance
): Promise<TopTracksItem[]> => {
const {
time: { timePreset, customTimeStart, customTimeEnd },
} = options;
const res = await client.get<{ items: TopTracksItem[] }>(
`/api/v1/reports/top-tracks`,
{
params: {
timePreset,
customTimeStart: formatISO(customTimeStart),
customTimeEnd: formatISO(customTimeEnd),
},
}
);
switch (res.status) {
case 200: {
break;
}
case 401: {
throw new UnauthenticatedError(`No token or token expired`);
}
default: {
throw new Error(`Unable to getTopTracks: ${res.status}`);
}
}
const {
data: { items },
} = res;
return items;
};
2021-10-26 20:09:52 +02:00
export const getTopGenres = async (
options: TopGenresOptions,
client: AxiosInstance
): Promise<TopGenresItem[]> => {
const {
time: { timePreset, customTimeStart, customTimeEnd },
} = options;
const res = await client.get<{ items: TopGenresItem[] }>(
`/api/v1/reports/top-Genres`,
{
params: {
timePreset,
customTimeStart: formatISO(customTimeStart),
customTimeEnd: formatISO(customTimeEnd),
},
}
);
switch (res.status) {
case 200: {
break;
}
case 401: {
throw new UnauthenticatedError(`No token or token expired`);
}
default: {
throw new Error(`Unable to getTopGenres: ${res.status}`);
}
}
const {
data: { items },
} = res;
return items;
};