🪄 Adds api service with updates

This commit is contained in:
Rune Harlyk
2024-05-08 13:26:40 +02:00
committed by Rune Harlyk
parent 4c66c428e6
commit b7ae17f3bf
19 changed files with 391 additions and 573 deletions
@@ -3,11 +3,12 @@
import Spinner from "$lib/components/Spinner.svelte";
import FolderIcon from '~icons/mdi/folder-outline';
import Folder from "./Folder.svelte";
import { api } from "$lib/api";
const getFiles = async () => {
const response = await fetch('/api/files/list');
if (response.ok) {
return response.json();
const result = await api.get('/api/files/list')
if (result.isOk()) {
return result.inner;
}
};
</script>
@@ -26,25 +26,25 @@
import SDK from '~icons/tabler/sdk';
import type { SystemInformation, Analytics } from '$lib/types/models';
import { socket } from '$lib/stores/socket';
import { api } from '$lib/api';
import { convertSeconds } from '$lib/utilities';
let systemInformation: SystemInformation;
async function getSystemStatus() {
try {
const response = await fetch('/api/systemStatus', {
method: 'GET',
headers: {
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
'Content-Type': 'application/json'
}
});
systemInformation = await response.json();
} catch (error) {
console.log('Error:', error);
}
const result = await api.get<SystemInformation>('/api/systemStatus');
if (result.isErr()){
console.error('Error:', result.inner);
return
}
systemInformation = result.inner
return systemInformation;
}
const postFactoryReset = async () => await api.post('/api/factoryReset')
const postSleep = async () => await api.post('api/sleep')
onMount(() => socket.on('analytics', handleSystemData));
onDestroy(() => socket.off('analytics', handleSystemData));
@@ -52,14 +52,7 @@
const handleSystemData = (data: Analytics) =>
(systemInformation = { ...systemInformation, ...data });
async function postRestart() {
const response = await fetch('/api/restart', {
method: 'POST',
headers: {
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic'
}
});
}
const postRestart = async () => await api.post('/api/restart');
function confirmRestart() {
openModal(ConfirmDialog, {
@@ -76,15 +69,6 @@
});
}
async function postFactoryReset() {
const response = await fetch('/api/factoryReset', {
method: 'POST',
headers: {
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic'
}
});
}
function confirmReset() {
openModal(ConfirmDialog, {
title: 'Confirm Factory Reset',
@@ -100,15 +84,6 @@
});
}
async function postSleep() {
const response = await fetch('/api/sleep', {
method: 'POST',
headers: {
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic'
}
});
}
function confirmSleep() {
openModal(ConfirmDialog, {
title: 'Confirm Going to Sleep',
@@ -123,33 +98,6 @@
}
});
}
function convertSeconds(seconds: number) {
// Calculate the number of seconds, minutes, hours, and days
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
let days = Math.floor(hours / 24);
// Calculate the remaining hours, minutes, and seconds
hours = hours % 24;
minutes = minutes % 60;
seconds = seconds % 60;
// Create the formatted string
let result = '';
if (days > 0) {
result += days + ' day' + (days > 1 ? 's' : '') + ' ';
}
if (hours > 0) {
result += hours + ' hour' + (hours > 1 ? 's' : '') + ' ';
}
if (minutes > 0) {
result += minutes + ' minute' + (minutes > 1 ? 's' : '') + ' ';
}
result += seconds + ' second' + (seconds > 1 ? 's' : '');
return result;
}
</script>
<SettingsCard collapsible={false}>
@@ -1,5 +1,4 @@
<script lang="ts">
import { user } from '$lib/stores/user';
import { page } from '$app/stores';
import { openModal, closeModal, closeAllModals } from 'svelte-modals';
import { slide } from 'svelte/transition';
@@ -14,43 +13,29 @@
import Error from '~icons/tabler/circle-x';
import { compareVersions } from 'compare-versions';
import GithubUpdateDialog from '$lib/components/GithubUpdateDialog.svelte';
import { assets } from '$app/paths';
import InfoDialog from '$lib/components/InfoDialog.svelte';
import Check from '~icons/tabler/check';
import { api } from '$lib/api';
async function getGithubAPI() {
try {
const githubResponse = await fetch(
'https://api.github.com/repos/' + $page.data.github + '/releases',
{
method: 'GET',
headers: {
accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
}
}
);
const results = await githubResponse.json();
return results;
} catch (error) {
console.error('Error:', error);
}
return;
const headers = {
accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
}
const result = await api.get(`https://api.github.com/repos/${$page.data.github}/releases`, {headers})
if (result.isErr()) {
console.error('Error:', result.inner);
return
}
return result.inner as any;
}
async function postGithubDownload(url: string) {
try {
const apiResponse = await fetch('/api/downloadUpdate', {
method: 'POST',
headers: {
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
'Content-Type': 'application/json'
},
body: JSON.stringify({ download_url: url })
});
} catch (error) {
console.error('Error:', error);
}
const result = await api.post('/api/downloadUpdate', { download_url: url })
if (result.isErr()) {
console.error('Error:', result.inner);
return
}
}
function confirmGithubUpdate(assets: any) {
@@ -1,30 +1,20 @@
<script lang="ts">
import { openModal, closeModal } from 'svelte-modals';
import { user } from '$lib/stores/user';
import { page } from '$app/stores';
import ConfirmDialog from '$lib/components/ConfirmDialog.svelte';
import SettingsCard from '$lib/components/SettingsCard.svelte';
import OTA from '~icons/tabler/file-upload';
import Warning from '~icons/tabler/alert-triangle';
import Cancel from '~icons/tabler/x';
import { api } from '$lib/api';
let files: FileList;
async function uploadBIN() {
try {
const formData = new FormData();
formData.append('file', files[0]);
const response = await fetch('/api/uploadFirmware', {
method: 'POST',
headers: {
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic'
},
body: formData
});
const result = await response.json();
} catch (error) {
console.error('Error:', error);
}
const formData = new FormData();
formData.append('file', files[0]);
const result = await api.post('/api/uploadFirmware', formData)
if (result.isErr())
console.error('Error:', result.inner);
}
function confirmBinUpload() {