From 2783c7754e508bae96da89aca6f2e719c05a2366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Schm=C3=B6cker?= Date: Fri, 6 Dec 2024 17:26:29 +0100 Subject: [PATCH] chore: renamed handleFetch to fetchWithNotify --- .../src/lib/components/DeployActions.svelte | 6 +++--- frontend/src/lib/components/DeployModal.svelte | 4 ++-- .../lib/{fetchHandler.ts => fetchWithNotify.ts} | 2 +- frontend/src/lib/hostkey.ts | 9 +++++++-- frontend/src/lib/state.ts | 6 +++--- frontend/src/lib/taskstatus.ts | 17 +++++++++++------ frontend/src/routes/(authenticated)/+layout.ts | 6 +++--- .../device-details/EditHostkeyModal.svelte | 6 +++--- .../device-details/SectionActions.svelte | 8 ++++---- .../routes/(authenticated)/devices/+page.svelte | 11 ++++++++--- .../src/routes/(authenticated)/history/+page.ts | 4 ++-- .../history/RollbackModal.svelte | 4 ++-- 12 files changed, 49 insertions(+), 34 deletions(-) rename frontend/src/lib/{fetchHandler.ts => fetchWithNotify.ts} (94%) diff --git a/frontend/src/lib/components/DeployActions.svelte b/frontend/src/lib/components/DeployActions.svelte index bb1eb811..78b7d86d 100644 --- a/frontend/src/lib/components/DeployActions.svelte +++ b/frontend/src/lib/components/DeployActions.svelte @@ -6,14 +6,14 @@ import Boxes from 'lucide-svelte/icons/boxes'; import DeployModal from '$lib/components/DeployModal.svelte'; import { invalidate } from '$app/navigation'; - import { handleFetch } from '$lib/fetchHandler'; + import { fetchWithNotify } from '$lib/fetchWithNotify'; const build = async () => { - await handleFetch(`/api/action/build`, { method: 'POST' }); + await fetchWithNotify(`/api/action/build`, { method: 'POST' }); }; const update = async () => { - await handleFetch(`/api/action/update`, { method: 'POST' }); + await fetchWithNotify(`/api/action/update`, { method: 'POST' }); invalidate((url) => url.pathname === '/api/available_modules'); }; diff --git a/frontend/src/lib/components/DeployModal.svelte b/frontend/src/lib/components/DeployModal.svelte index d38b5979..f69ceb15 100644 --- a/frontend/src/lib/components/DeployModal.svelte +++ b/frontend/src/lib/components/DeployModal.svelte @@ -2,14 +2,14 @@ import { t } from 'svelte-i18n'; import { Button, Modal, Label, Input } from 'flowbite-svelte'; import { invalidate } from '$app/navigation'; - import { handleFetch } from '$lib/fetchHandler'; + import { fetchWithNotify } from '$lib/fetchWithNotify'; export let open = false; $: summary = new Date().toLocaleString() + ': '; const deploy = async () => { - await handleFetch(`/api/action/deploy?summary=${summary}`, { + await fetchWithNotify(`/api/action/deploy?summary=${summary}`, { method: 'POST' }); await invalidate((url) => url.pathname === '/api/history'); diff --git a/frontend/src/lib/fetchHandler.ts b/frontend/src/lib/fetchWithNotify.ts similarity index 94% rename from frontend/src/lib/fetchHandler.ts rename to frontend/src/lib/fetchWithNotify.ts index 4484da83..877ed985 100644 --- a/frontend/src/lib/fetchHandler.ts +++ b/frontend/src/lib/fetchWithNotify.ts @@ -4,7 +4,7 @@ export type CustomResponseMessage = { [statusCode: number]: string | null; }; -export const handleFetch = async ( +export const fetchWithNotify = async ( url: string, init?: RequestInit, customResponse?: CustomResponseMessage, diff --git a/frontend/src/lib/hostkey.ts b/frontend/src/lib/hostkey.ts index f1f8428f..aaeca76b 100644 --- a/frontend/src/lib/hostkey.ts +++ b/frontend/src/lib/hostkey.ts @@ -1,4 +1,4 @@ -import { handleFetch } from './fetchHandler'; +import { fetchWithNotify } from './fetchWithNotify'; export type Hostkey = { identifier: string; @@ -8,7 +8,12 @@ export type Hostkey = { export const getHostkey = async (fetch: typeof window.fetch, identifier: string) => { let hostkey: Hostkey | null = null; - const response = await handleFetch(`/api/hostkey/${identifier}`, undefined, { 404: null }, fetch); + const response = await fetchWithNotify( + `/api/hostkey/${identifier}`, + undefined, + { 404: null }, + fetch + ); if (response.ok) { hostkey = await response.json(); } diff --git a/frontend/src/lib/state.ts b/frontend/src/lib/state.ts index 135b33f1..67b603d2 100644 --- a/frontend/src/lib/state.ts +++ b/frontend/src/lib/state.ts @@ -1,7 +1,7 @@ import { invalidate } from '$app/navigation'; import { derived, writable } from 'svelte/store'; import { queryParam } from 'sveltekit-search-params'; -import { handleFetch } from './fetchHandler'; +import { fetchWithNotify } from './fetchWithNotify'; export type ModuleSettings = { type: string; @@ -97,7 +97,7 @@ state.subscribe((value) => { export const saveState = async () => { state.set(currentState); - const response = await handleFetch(`/api/state`, { + const response = await fetchWithNotify(`/api/state`, { method: 'PATCH', headers: { 'content-type': 'application/json' @@ -109,7 +109,7 @@ export const saveState = async () => { }; export const build = async () => { - await handleFetch(`/api/action/build`, { method: 'POST' }); + await fetchWithNotify(`/api/action/build`, { method: 'POST' }); }; export const getTagByIdentifier = (state: State, identifier: string) => { diff --git a/frontend/src/lib/taskstatus.ts b/frontend/src/lib/taskstatus.ts index a9dc2d7d..d1ca26ed 100644 --- a/frontend/src/lib/taskstatus.ts +++ b/frontend/src/lib/taskstatus.ts @@ -1,7 +1,7 @@ import { browser } from '$app/environment'; import { derived, writable, type Readable } from 'svelte/store'; import { invalidate } from '$app/navigation'; -import { handleFetch } from './fetchHandler'; +import { fetchWithNotify } from './fetchWithNotify'; export type TaskState = 'pending' | 'running' | 'completed' | 'failed'; export type TaskVanilla = { @@ -65,22 +65,27 @@ taskStatus.subscribe((value) => { }); export const getAllTasks = async (fetch: typeof window.fetch = window.fetch) => { - const response = await handleFetch(`/api/tasks`, undefined, {}, fetch); + const response = await fetchWithNotify(`/api/tasks`, undefined, {}, fetch); return (await response.json()) as TaskList; }; export const getTask = async (taskId: string, fetch: typeof window.fetch = window.fetch) => { - const response = await handleFetch(`/api/tasks/${taskId}`, undefined, {}, fetch); + const response = await fetchWithNotify(`/api/tasks/${taskId}`, undefined, {}, fetch); return (await response.json()) as Task; }; export const cancelTask = async (taskId: string, fetch: typeof window.fetch = window.fetch) => { - const response = await handleFetch(`/api/tasks/${taskId}/cancel`, { method: 'POST' }, {}, fetch); + const response = await fetchWithNotify( + `/api/tasks/${taskId}/cancel`, + { method: 'POST' }, + {}, + fetch + ); return response; }; export const retryTask = async (taskId: string, fetch: typeof window.fetch = window.fetch) => { - const response = await handleFetch( + const response = await fetchWithNotify( `/api/tasks/${taskId}/retry`, { method: 'POST' @@ -92,7 +97,7 @@ export const retryTask = async (taskId: string, fetch: typeof window.fetch = win }; export const runImmediately = async (taskId: string, fetch: typeof window.fetch = window.fetch) => { - const response = await handleFetch( + const response = await fetchWithNotify( `/api/tasks/${taskId}/run_immediately`, { method: 'POST' diff --git a/frontend/src/routes/(authenticated)/+layout.ts b/frontend/src/routes/(authenticated)/+layout.ts index 60ca8af9..6c1ee012 100644 --- a/frontend/src/routes/(authenticated)/+layout.ts +++ b/frontend/src/routes/(authenticated)/+layout.ts @@ -5,7 +5,7 @@ import type { LayoutLoad } from './$types'; import type { State, Module } from '$lib/state'; import { error, redirect } from '@sveltejs/kit'; import { getAllTasks } from '$lib/taskstatus'; -import { handleFetch } from '$lib/fetchHandler'; +import { fetchWithNotify } from '$lib/fetchWithNotify'; export const load = (async ({ fetch, url, data }) => { let lang = 'en'; @@ -33,7 +33,7 @@ export const load = (async ({ fetch, url, data }) => { } await waitLocale(lang); - const stateResponse = await handleFetch( + const stateResponse = await fetchWithNotify( `/api/state`, { method: 'GET', @@ -53,7 +53,7 @@ export const load = (async ({ fetch, url, data }) => { error(500, 'Could not fetch state'); } - const availableModulesResponse = await handleFetch( + const availableModulesResponse = await fetchWithNotify( `/api/available_modules`, { method: 'GET', diff --git a/frontend/src/routes/(authenticated)/device-details/EditHostkeyModal.svelte b/frontend/src/routes/(authenticated)/device-details/EditHostkeyModal.svelte index eeebac5f..aa5b7433 100644 --- a/frontend/src/routes/(authenticated)/device-details/EditHostkeyModal.svelte +++ b/frontend/src/routes/(authenticated)/device-details/EditHostkeyModal.svelte @@ -3,7 +3,7 @@ import { Button, Helper, Input, Label, Modal, Spinner, Tooltip } from 'flowbite-svelte'; import type { Hostkey } from '$lib/hostkey'; import type { Device } from '$lib/state'; - import { handleFetch } from '$lib/fetchHandler'; + import { fetchWithNotify } from '$lib/fetchWithNotify'; export let open = false; @@ -30,7 +30,7 @@ const submitData = async () => { if (!device) return; - const response = await handleFetch(`/api/hostkey/${device.identifier}`, { + const response = await fetchWithNotify(`/api/hostkey/${device.identifier}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' @@ -50,7 +50,7 @@ scanningPublicKeyError = ''; isScanningPublicKey = true; - const response = await handleFetch(`/api/scan-public-key?host=${deviceHost}`, { + const response = await fetchWithNotify(`/api/scan-public-key?host=${deviceHost}`, { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/frontend/src/routes/(authenticated)/device-details/SectionActions.svelte b/frontend/src/routes/(authenticated)/device-details/SectionActions.svelte index 478cb93d..45da57f1 100644 --- a/frontend/src/routes/(authenticated)/device-details/SectionActions.svelte +++ b/frontend/src/routes/(authenticated)/device-details/SectionActions.svelte @@ -5,24 +5,24 @@ import { Button, Input, Tooltip } from 'flowbite-svelte'; import Download from 'lucide-svelte/icons/download'; import RotateCcw from 'lucide-svelte/icons/rotate-ccw'; - import { handleFetch } from '$lib/fetchHandler'; + import { fetchWithNotify } from '$lib/fetchWithNotify'; export let device: Device; const restartDevice = async (device: Device) => { - await handleFetch(`/api/action/restart-device?identifier=${device.identifier}`, { + await fetchWithNotify(`/api/action/restart-device?identifier=${device.identifier}`, { method: 'POST' }); }; const buildAndDownloadImage = async (device: Device) => { - await handleFetch(`/api/action/build-download-image?identifier=${device.identifier}`, { + await fetchWithNotify(`/api/action/build-download-image?identifier=${device.identifier}`, { method: 'POST' }); }; const buildAndDownloadImageForClone = async (device: Device) => { - const response = await handleFetch( + const response = await fetchWithNotify( `/api/action/build-download-image-for-clone?identifier=${device.identifier}`, { method: 'POST' diff --git a/frontend/src/routes/(authenticated)/devices/+page.svelte b/frontend/src/routes/(authenticated)/devices/+page.svelte index 77010e8b..ddfdac68 100644 --- a/frontend/src/routes/(authenticated)/devices/+page.svelte +++ b/frontend/src/routes/(authenticated)/devices/+page.svelte @@ -27,6 +27,7 @@ import type { KeyboardEventHandler, MouseEventHandler, TouchEventHandler } from 'svelte/elements'; import { flip } from 'svelte/animate'; import { nameToIdentifier, nameValidation } from '$lib/nameValidation'; + import { fetchWithNotify } from '$lib/fetchWithNotify'; const flipDurationMs = 200; let dragDisabled = true; @@ -83,18 +84,22 @@ const success = await saveState(); if (success) { - const oldHostkeyRequest = await fetch(`/api/hostkey/${oldIdentifier}`); + const oldHostkeyRequest = await fetchWithNotify( + `/api/hostkey/${oldIdentifier}`, + undefined, + { 404: null } + ); if (oldHostkeyRequest.ok) { const oldHostkey = await oldHostkeyRequest.json(); - await fetch(`/api/hostkey/${identifier}`, { + await fetchWithNotify(`/api/hostkey/${identifier}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(oldHostkey) }); - await fetch(`/api/hostkey/${oldIdentifier}`, { method: 'DELETE' }); + await fetchWithNotify(`/api/hostkey/${oldIdentifier}`, { method: 'DELETE' }); } return true; } diff --git a/frontend/src/routes/(authenticated)/history/+page.ts b/frontend/src/routes/(authenticated)/history/+page.ts index 88c96052..ba0bc084 100644 --- a/frontend/src/routes/(authenticated)/history/+page.ts +++ b/frontend/src/routes/(authenticated)/history/+page.ts @@ -1,9 +1,9 @@ -import { handleFetch } from '$lib/fetchHandler'; +import { fetchWithNotify } from '$lib/fetchWithNotify'; import type { Commit } from '$lib/history'; import type { PageLoad } from './$types'; export const load = (async ({ fetch }) => { - const history_response = handleFetch( + const history_response = fetchWithNotify( `/api/history`, { method: 'GET', diff --git a/frontend/src/routes/(authenticated)/history/RollbackModal.svelte b/frontend/src/routes/(authenticated)/history/RollbackModal.svelte index 71d17451..4cd01556 100644 --- a/frontend/src/routes/(authenticated)/history/RollbackModal.svelte +++ b/frontend/src/routes/(authenticated)/history/RollbackModal.svelte @@ -3,14 +3,14 @@ import { invalidate } from '$app/navigation'; import { Button, Modal } from 'flowbite-svelte'; import type { Commit } from '$lib/history'; - import { handleFetch } from '$lib/fetchHandler'; + import { fetchWithNotify } from '$lib/fetchWithNotify'; export let commit: Commit | undefined; const revertCommit = async (commitSHA: string | undefined) => { if (commitSHA === undefined) return; - await handleFetch(`/api/history/revert-commit?commit_sha=${commitSHA}`, { method: 'POST' }); + await fetchWithNotify(`/api/history/revert-commit?commit_sha=${commitSHA}`, { method: 'POST' }); await invalidate((url) => url.pathname === '/api/history' || url.pathname === '/api/state'); commit = undefined;