import { toast } from 'vue-sonner'; import type { DirectoryEntry, FileType } from '~/types'; import type { Warren } from '~/types/warrens'; export async function getWarrens(): Promise> { const { data: arr, error } = await useFetch( getApiUrl('warrens'), { method: 'GET', } ); if (arr.value == null) { throw error.value?.name; } const warrens: Record = {}; for (const warren of arr.value) { warrens[warren.id] = warren; } return warrens; } export async function getWarrenDirectory( path: string ): Promise { const { data: entries, error } = await useFetch( 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 }; }