102 lines
2.5 KiB
TypeScript
102 lines
2.5 KiB
TypeScript
import { toast } from 'vue-sonner';
|
|
import type { DirectoryEntry, FileType } from '~/types';
|
|
import type { Warren } from '~/types/warrens';
|
|
|
|
export async function getWarrens(): Promise<Record<string, Warren>> {
|
|
const { data: arr, error } = await useFetch<Warren[]>(
|
|
getApiUrl('warrens'),
|
|
{
|
|
method: 'GET',
|
|
}
|
|
);
|
|
|
|
if (arr.value == null) {
|
|
throw error.value?.name;
|
|
}
|
|
|
|
const warrens: Record<string, Warren> = {};
|
|
|
|
for (const warren of arr.value) {
|
|
warrens[warren.id] = warren;
|
|
}
|
|
|
|
return warrens;
|
|
}
|
|
|
|
export async function getWarrenDirectory(
|
|
path: string
|
|
): Promise<DirectoryEntry[]> {
|
|
const { data: entries, error } = await useFetch<DirectoryEntry[]>(
|
|
getApiUrl(`warrens/${path}`),
|
|
{
|
|
method: 'GET',
|
|
}
|
|
);
|
|
|
|
if (entries.value == null) {
|
|
throw error.value?.name;
|
|
}
|
|
|
|
return entries.value;
|
|
}
|
|
|
|
export async function createDirectory(
|
|
path: string,
|
|
directoryName: string
|
|
): Promise<{ success: boolean }> {
|
|
const { status } = await useFetch(
|
|
getApiUrl(`warrens/${path}/${directoryName}`),
|
|
{
|
|
method: 'POST',
|
|
}
|
|
);
|
|
|
|
if (status.value !== 'success') {
|
|
toast.error('Directory', {
|
|
id: 'CREATE_DIRECTORY_TOAST',
|
|
description: `Failed to create directory`,
|
|
});
|
|
return { success: false };
|
|
}
|
|
|
|
await refreshNuxtData('current-directory');
|
|
|
|
toast.success('Directory', {
|
|
id: 'CREATE_DIRECTORY_TOAST',
|
|
description: `Successfully created directory: ${directoryName}`,
|
|
});
|
|
|
|
return { success: true };
|
|
}
|
|
|
|
export async function deleteWarrenEntry(
|
|
path: string,
|
|
directoryName: string,
|
|
fileType: FileType
|
|
): Promise<{ success: boolean }> {
|
|
const { status } = await useFetch(
|
|
getApiUrl(`warrens/${path}/${directoryName}?fileType=${fileType}`),
|
|
{
|
|
method: 'DELETE',
|
|
}
|
|
);
|
|
|
|
const toastTitle = fileType.slice(0, 1).toUpperCase() + fileType.slice(1);
|
|
|
|
if (status.value !== 'success') {
|
|
toast.error(toastTitle, {
|
|
id: 'DELETE_DIRECTORY_TOAST',
|
|
description: `Failed to delete ${fileType}`,
|
|
});
|
|
return { success: false };
|
|
}
|
|
|
|
await refreshNuxtData('current-directory');
|
|
|
|
toast.success(toastTitle, {
|
|
id: 'DELETE_DIRECTORY_TOAST',
|
|
description: `Successfully deleted ${fileType}: ${directoryName}`,
|
|
});
|
|
return { success: true };
|
|
}
|