tubearchivist-frontend/src/lib/getVideos.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Video } from "../types/video";
import { Videos } from "../types/videos";
import { getTAUrl } from "./constants";
const TA_BASE_URL = getTAUrl();
2022-04-13 18:08:32 +00:00
export const getVideos = async (token: string): Promise<Videos> => {
if (!token) {
throw new Error("Missing API token in request to get videos");
}
const response = await fetch(`${TA_BASE_URL.server}/api/video/`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
});
if (!response.ok) {
throw new Error("Failed to fetch videos");
}
return response.json();
};
export const getVideo = async (
token: string,
videoId: string
): Promise<Video> => {
if (!token) {
throw new Error("Missing API token in request to get video");
}
const response = await fetch(`${TA_BASE_URL.server}/api/video/${videoId}`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Token ${token}`,
mode: "no-cors",
},
});
2022-04-13 18:08:32 +00:00
if (!response.ok) {
throw new Error("Failed to fetch videos");
}
return response.json();
};