import axios, { AxiosError } from 'axios'; import { License } from '@/types'; import { toast } from 'vue-sonner'; const API_URL = '/api/licenses'; export default { /** * Fetches all licenses * @returns Promise */ async getLicenses(): Promise { try { const response = await axios.get(API_URL); return response.data; } catch (error) { toast.error('Fehler beim Abrufen der Lizenzen', { description: (error as AxiosError).message }); console.error(error); return null; } }, /** * Fetches a single license by ID * @param id - The ID of the license to fetch * @returns Promise */ async getLicense(id: number): Promise { try { const response = await axios.get(`${API_URL}/${id}`); return response.data; } catch (error) { toast.error('Fehler beim Abrufen der Lizenz', { description: (error as AxiosError).message }); console.error(error); return null; } }, /** * Creates a new license * @param license - The license data to create * @returns Promise */ async createLicense(license: License): Promise { try { const response = await axios.post(API_URL, license); return response.data; } catch (error) { toast.error('Fehler beim Erstellen der Lizenz', { description: (error as AxiosError).message }); console.error(error); return null; } }, /** * Updates an existing license * @param license - The license data to update * @returns Promise */ async updateLicense(license: License): Promise { try { const response = await axios.put(`${API_URL}/${license.id}`, license); return response.data; } catch (error) { toast.error('Fehler beim Aktualisieren der Lizenz', { description: (error as AxiosError).message }); console.error(error); return null; } }, /** * Deletes a license by ID * @param id - The ID of the license to delete * @returns Promise */ async deleteLicense(id: number): Promise { try { await axios.delete(`${API_URL}/${id}`); return true; } catch (error) { toast.error('Fehler beim Löschen der Lizenz', { description: (error as AxiosError).message }); console.error(error); return false; } }, /** * Downloads a license as a JSON file * @param id - The ID of the license to download * @returns Promise */ async downloadLicense(id: number): Promise { try { const response = await axios.get(`/licenses/download/${id}`, { responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', `license_${id}.json`); document.body.appendChild(link); link.click(); link.remove(); } catch (error) { toast.error('Fehler beim Herunterladen der Lizenz', { description: (error as AxiosError).message }); console.error(error); } }, /** * Validates a license * @param licenseId - The ID of the license to validate * @returns Promise<{ is_valid: boolean, is_signature_valid: boolean, is_expired: boolean } | null> */ async validateLicense(licenseId: number): Promise<{ is_valid: boolean, is_signature_valid: boolean, is_expired: boolean } | null> { try { const response = await axios.post('/api/licenses/validate', { license_id: licenseId }); return response.data; } catch (error) { toast.error('Fehler beim Validieren der Lizenz', { description: (error as AxiosError).message }); console.error(error); return null; } } };