diff --git a/src/lib/getDownloads.ts b/src/lib/getDownloads.ts index 492d6bd..c317481 100755 --- a/src/lib/getDownloads.ts +++ b/src/lib/getDownloads.ts @@ -6,6 +6,9 @@ import { getTAUrl } from "./constants"; const TA_BASE_URL = getTAUrl(); export const getDownloads = async (token: string, filter: boolean, pageNumber: number): Promise => { + if (!token) { + throw new Error(`Unable to fetch downloads, no token provided.`); + } const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter ? 'ignore' : 'pending'}&page=${pageNumber}`, { headers: { Accept: "application/json", @@ -30,6 +33,9 @@ export const getDownloads = async (token: string, filter: boolean, pageNumber: n }; export const sendDownloads = async (token: string, input: string): Promise => { + if (!token) { + throw new Error(`Unable to send downloads, no token provided.`); + } var data = { "data": [{ "youtube_id": input, @@ -44,7 +50,7 @@ export const sendDownloads = async (token: string, input: string): Promise => { + if (!token) { + throw new Error(`Unable to delete downloads, no token provided.`); + } const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter}`, { headers: { Accept: "application/json", @@ -64,7 +73,7 @@ export const sendDeleteAllQueuedIgnored = async (token: string, filter: string): Authorization: `Token ${token}`, mode: "no-cors", }, - method: "DELETE" + method: "DELETE", }); if (!response.ok) { throw new Error("Error removing all videos."); @@ -74,6 +83,9 @@ export const sendDeleteAllQueuedIgnored = async (token: string, filter: string): }; export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: string): Promise => { + if (!token) { + throw new Error(`Unable to delete downloads, no token provided.`); + } const response = await fetch(`${TA_BASE_URL.server}/api/download/${videoId}/`, { headers: { Accept: "application/json", @@ -81,7 +93,7 @@ export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: strin Authorization: `Token ${token}`, mode: "no-cors", }, - method: "DELETE" + method: "DELETE", }); if (!response.ok) { throw new Error("Error removing video."); @@ -91,6 +103,9 @@ export const sendDeleteVideoQueuedIgnored = async (token: string, videoId: strin }; export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string, status: string): Promise => { + if (!token) { + throw new Error(`Unable to move downloads, no token provided.`); + } var data = { "status": status }; @@ -102,7 +117,7 @@ export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string, Authorization: `Token ${token}`, mode: "no-cors", }, - method: "POST" + method: "POST", }); if (!response.ok) { throw new Error("Error moving video to" + status + "."); @@ -111,6 +126,9 @@ export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string, }; export const sendTasks = async (token: string, task: Tasks): Promise => { + if (!token) { + throw new Error(`Unable to start task, no token provided.`); + } var data = { "run": task }; @@ -122,7 +140,7 @@ export const sendTasks = async (token: string, task: Tasks): Promise => { Authorization: `Token ${token}`, mode: "no-cors", }, - method: "POST" + method: "POST", }); if (!response.ok) { throw new Error(`Error running task: ${task}.`);