2022-10-18 19:28:37 +00:00
|
|
|
import type { Datum, Videos } from "~/types/Videos";
|
|
|
|
import { API_URL } from "./constants.server";
|
2022-04-07 22:54:15 +00:00
|
|
|
|
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");
|
|
|
|
}
|
2022-10-18 19:28:37 +00:00
|
|
|
const response = await fetch(`${API_URL}/api/video/`, {
|
2022-04-17 00:49:20 +00:00
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `Token ${token}`,
|
|
|
|
mode: "no-cors",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2022-10-18 19:28:37 +00:00
|
|
|
throw new Error(`Failed to fetch videos: ${response.statusText}`);
|
2022-04-17 00:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
};
|
|
|
|
|
2022-10-18 19:28:37 +00:00
|
|
|
export const getVideo = async (token: string, videoId: string): Promise<Datum> => {
|
2022-04-17 00:49:20 +00:00
|
|
|
if (!token) {
|
|
|
|
throw new Error("Missing API token in request to get video");
|
|
|
|
}
|
2022-10-18 19:28:37 +00:00
|
|
|
const response = await fetch(`${API_URL}/api/video/${videoId}`, {
|
2022-04-13 20:53:39 +00:00
|
|
|
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) {
|
2022-10-18 19:28:37 +00:00
|
|
|
throw new Error(`Failed to fetch videos: ${response.statusText}`);
|
2022-04-13 18:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.json();
|
2022-04-07 22:54:15 +00:00
|
|
|
};
|