tubearchivist-frontend/src/lib/getPlaylists.ts

56 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-04-13 18:08:07 +00:00
import { Playlist } from "../types/playlist";
import { Playlists } from "../types/playlists";
2022-04-23 18:10:12 +00:00
import { getTAUrl } from "./constants";
const TA_BASE_URL = getTAUrl();
2022-04-13 18:08:07 +00:00
export const getPlaylists = async (token: string): Promise<Playlists> => {
if (!token) {
throw new Error(`No token provided when fetching a playlists`);
}
2022-04-23 18:10:12 +00:00
const response = await fetch(`${TA_BASE_URL.server}/api/playlist/`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
});
2022-04-13 18:08:07 +00:00
if (!response.ok) {
throw new Error(
`Error getting playlists information: ${response.statusText}`
);
}
return response.json();
};
export const getPlaylist = async (
token: string,
playlistId: string
): Promise<Playlist> => {
if (!token) {
throw new Error(
`No token provided when fetching a playlist: ${playlistId}`
);
}
2022-04-23 18:10:12 +00:00
const response = await fetch(
`${TA_BASE_URL.server}/api/playlist/${playlistId}`,
{
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
}
);
if (!response.ok) {
throw new Error(
`Error getting playlists information: ${response.statusText}`
);
2022-04-13 18:08:07 +00:00
}
return response.json();
};