Zipline-Android-App/functions/zipline/folders.ts

268 lines
4.9 KiB
TypeScript

import type { APIFoldersNoIncl, APIFolders, APIFolder } from "@/types/zipline";
import axios, { type AxiosError } from "axios";
import * as db from "@/functions/database";
// GET /api/user/folders
export async function getFolders<T extends boolean | undefined = undefined>(
noIncl?: T,
): Promise<T extends true ? APIFoldersNoIncl | string : APIFolders | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
const params = new URLSearchParams();
if (noIncl) params.append("noincl", "true");
try {
const res = await axios.get(`${url}/api/user/folders?${params}`, {
headers: {
Authorization: token,
},
});
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
// GET /api/user/folders/[id]
export async function getFolder(id: string): Promise<APIFolder | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.get(`${url}/api/user/folders/${id}`, {
headers: {
Authorization: token,
},
});
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
// POST /api/user/folders
export async function createFolder(
name: string,
isPublic = false,
): Promise<APIFolder | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.post(
`${url}/api/user/folders`,
{
name,
isPublic,
},
{
headers: {
Authorization: token,
},
},
);
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
interface FolderEditOptions {
allowUploads?: boolean;
public?: boolean;
name?: string;
}
// PATCH /api/user/folders/[id]
export async function editFolder(
id: string,
options: FolderEditOptions = {}
): Promise<APIFolder | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.patch(
`${url}/api/user/folders/${id}`,
options,
{
headers: {
Authorization: token,
},
},
);
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
// DELETE /api/user/folders/[id]
export async function deleteFolder(id: string): Promise<APIFolder | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.delete(`${url}/api/user/folders/${id}`, {
headers: {
Authorization: token,
},
data: {
delete: "folder",
},
});
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
// DELETE /api/user/folders/[folderId]
export async function removeFileFromFolder(
folderId: string,
fileId: string,
): Promise<APIFolder | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.delete(`${url}/api/user/folders/${folderId}`, {
headers: {
Authorization: token,
},
data: {
delete: "file",
id: fileId,
},
});
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}
// POST /api/user/folders/[folderId]
export async function addFileToFolder(
folderId: string,
fileId: string,
): Promise<APIFolder | string> {
const token = db.get("token");
const url = db.get("url");
if (!url || !token) return "Invalid token or URL";
try {
const res = await axios.post(
`${url}/api/user/folders/${folderId}`,
{
id: fileId,
},
{
headers: {
Authorization: token,
},
},
);
return res.data;
} catch (e) {
const error = e as AxiosError;
const data = error.response?.data as
| {
error: string;
statusCode: number;
}
| undefined;
if (data) return data.error;
return "Something went wrong...";
}
}