Skip to content

Commit

Permalink
chore: renamed handleFetch to fetchWithNotify
Browse files Browse the repository at this point in the history
  • Loading branch information
MSchmoecker committed Dec 6, 2024
1 parent af3782b commit 2783c77
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 34 deletions.
6 changes: 3 additions & 3 deletions frontend/src/lib/components/DeployActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/components/DeployModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/lib/hostkey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleFetch } from './fetchHandler';
import { fetchWithNotify } from './fetchWithNotify';

export type Hostkey = {
identifier: string;
Expand All @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/lib/state.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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'
Expand All @@ -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) => {
Expand Down
17 changes: 11 additions & 6 deletions frontend/src/lib/taskstatus.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/routes/(authenticated)/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/routes/(authenticated)/devices/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/routes/(authenticated)/history/+page.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2783c77

Please sign in to comment.