From a02ab943749c1f474cb0d7e0abe5a93c0f4b0d31 Mon Sep 17 00:00:00 2001 From: Ayush Sehrawat <69469790+AyushSehrawat@users.noreply.github.com> Date: Sun, 14 Jul 2024 14:51:06 +0530 Subject: [PATCH] refactor: move code with private env to different file (#521) * fix: add BACKEND_URL arg to avoid build error * fix: add BACKEND_URL arg to avoid build error * refactor: move code with private env to different file --- frontend/src/lib/forms/helpers.server.ts | 39 ++++++++++++++++++ frontend/src/lib/forms/helpers.ts | 40 ------------------- .../routes/settings/content/+page.server.ts | 4 +- .../routes/settings/general/+page.server.ts | 4 +- .../settings/mediaserver/+page.server.ts | 4 +- .../routes/settings/scrapers/+page.server.ts | 4 +- 6 files changed, 43 insertions(+), 52 deletions(-) create mode 100644 frontend/src/lib/forms/helpers.server.ts diff --git a/frontend/src/lib/forms/helpers.server.ts b/frontend/src/lib/forms/helpers.server.ts new file mode 100644 index 00000000..6993daf2 --- /dev/null +++ b/frontend/src/lib/forms/helpers.server.ts @@ -0,0 +1,39 @@ +import { BACKEND_URL } from '$env/static/private'; + +// TODO: Add toCheck +export async function setSettings(fetch: any, toSet: any) { + const settings = await fetch(`${BACKEND_URL}/settings/set`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(toSet) + }); + const settingsData = await settings.json(); + + return { + data: settingsData + }; +} + +export async function saveSettings(fetch: any) { + const data = await fetch(`${BACKEND_URL}/settings/save`, { + method: 'POST' + }); + const response = await data.json(); + + return { + data: response + }; +} + +export async function loadSettings(fetch: any) { + const data = await fetch(`${BACKEND_URL}/settings/load`, { + method: 'GET' + }); + const response = await data.json(); + + return { + data: response + }; +} diff --git a/frontend/src/lib/forms/helpers.ts b/frontend/src/lib/forms/helpers.ts index d4a4d95c..93493f4c 100644 --- a/frontend/src/lib/forms/helpers.ts +++ b/frontend/src/lib/forms/helpers.ts @@ -1,46 +1,6 @@ import { type SuperValidated, type Infer } from 'sveltekit-superforms'; -import { BACKEND_URL } from '$env/static/private'; - import { z } from 'zod'; -// TODO: Add toCheck -export async function setSettings(fetch: any, toSet: any) { - const settings = await fetch(`${BACKEND_URL}/settings/set`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(toSet) - }); - const settingsData = await settings.json(); - - return { - data: settingsData - }; -} - -export async function saveSettings(fetch: any) { - const data = await fetch(`${BACKEND_URL}/settings/save`, { - method: 'POST' - }); - const response = await data.json(); - - return { - data: response - }; -} - -export async function loadSettings(fetch: any) { - const data = await fetch(`${BACKEND_URL}/settings/load`, { - method: 'GET' - }); - const response = await data.json(); - - return { - data: response - }; -} - // General Settings ----------------------------------------------------------------------------------- export const generalSettingsToGet: string[] = ['debug', 'log', 'symlink', 'downloaders']; diff --git a/frontend/src/routes/settings/content/+page.server.ts b/frontend/src/routes/settings/content/+page.server.ts index 191fdb49..417c20ab 100644 --- a/frontend/src/routes/settings/content/+page.server.ts +++ b/frontend/src/routes/settings/content/+page.server.ts @@ -3,14 +3,12 @@ import { superValidate, message } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { fail, error, redirect } from '@sveltejs/kit'; import { - setSettings, - saveSettings, - loadSettings, contentSettingsSchema, contentSettingsToGet, contentSettingsToPass, contentSettingsToSet } from '$lib/forms/helpers'; +import { setSettings, saveSettings, loadSettings } from '$lib/forms/helpers.server'; import { BACKEND_URL } from '$env/static/private'; export const load: PageServerLoad = async ({ fetch }) => { diff --git a/frontend/src/routes/settings/general/+page.server.ts b/frontend/src/routes/settings/general/+page.server.ts index e38d469c..2606f0a6 100644 --- a/frontend/src/routes/settings/general/+page.server.ts +++ b/frontend/src/routes/settings/general/+page.server.ts @@ -3,14 +3,12 @@ import { superValidate, message } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { fail, error, redirect } from '@sveltejs/kit'; import { - setSettings, - saveSettings, - loadSettings, generalSettingsSchema, generalSettingsToGet, generalSettingsToPass, generalSettingsToSet } from '$lib/forms/helpers'; +import { setSettings, saveSettings, loadSettings } from '$lib/forms/helpers.server'; import { BACKEND_URL } from '$env/static/private'; export const load: PageServerLoad = async ({ fetch }) => { diff --git a/frontend/src/routes/settings/mediaserver/+page.server.ts b/frontend/src/routes/settings/mediaserver/+page.server.ts index ed168612..d66fed9a 100644 --- a/frontend/src/routes/settings/mediaserver/+page.server.ts +++ b/frontend/src/routes/settings/mediaserver/+page.server.ts @@ -3,14 +3,12 @@ import { superValidate, message } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { fail, error, redirect } from '@sveltejs/kit'; import { - setSettings, - saveSettings, - loadSettings, mediaServerSettingsSchema, mediaServerSettingsToGet, mediaServerSettingsToPass, mediaServerSettingsToSet } from '$lib/forms/helpers'; +import { setSettings, saveSettings, loadSettings } from '$lib/forms/helpers.server'; import { BACKEND_URL } from '$env/static/private'; export const load: PageServerLoad = async ({ fetch }) => { diff --git a/frontend/src/routes/settings/scrapers/+page.server.ts b/frontend/src/routes/settings/scrapers/+page.server.ts index 6a835160..68b873ed 100644 --- a/frontend/src/routes/settings/scrapers/+page.server.ts +++ b/frontend/src/routes/settings/scrapers/+page.server.ts @@ -3,14 +3,12 @@ import { superValidate, message } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { fail, error, redirect } from '@sveltejs/kit'; import { - setSettings, - saveSettings, - loadSettings, scrapersSettingsSchema, scrapersSettingsToGet, scrapersSettingsToPass, scrapersSettingsToSet } from '$lib/forms/helpers'; +import { setSettings, saveSettings, loadSettings } from '$lib/forms/helpers.server'; import { BACKEND_URL } from '$env/static/private'; export const load: PageServerLoad = async ({ fetch }) => {