From a351264ede94440a06d14078a650ae405bbd2152 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sun, 8 Sep 2024 16:29:55 +0000 Subject: [PATCH] web/api: send parameters to server only if they differ from defaults --- web/src/lib/api/api.ts | 35 +++++++++++++++----------------- web/src/lib/settings/lazy-get.ts | 14 +++++++++++++ 2 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 web/src/lib/settings/lazy-get.ts diff --git a/web/src/lib/api/api.ts b/web/src/lib/api/api.ts index 5b0b674a..74993a90 100644 --- a/web/src/lib/api/api.ts +++ b/web/src/lib/api/api.ts @@ -4,41 +4,38 @@ import settings from "$lib/state/settings"; import { getSession } from "$lib/api/session"; import { currentApiURL } from "$lib/api/api-url"; import { apiOverrideWarning } from "$lib/api/safety-warning"; - import type { Optional } from "$lib/types/generic"; import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api"; +import lazySettingGetter from "$lib/settings/lazy-get"; const request = async (url: string) => { - const currentSettings = get(settings); - const saveSettings = currentSettings.save; - const privacySettings = currentSettings.privacy; + const getSetting = lazySettingGetter(get(settings)); const request = { url, - downloadMode: saveSettings.downloadMode, + downloadMode: getSetting("save", "downloadMode"), + audioBitrate: getSetting("save", "audioBitrate"), + audioFormat: getSetting("save", "audioFormat"), + tiktokFullAudio: getSetting("save", "tiktokFullAudio"), + youtubeDubBrowserLang: getSetting("save", "youtubeDubBrowserLang"), - audioBitrate: saveSettings.audioBitrate, - audioFormat: saveSettings.audioFormat, - tiktokFullAudio: saveSettings.tiktokFullAudio, - youtubeDubBrowserLang: saveSettings.youtubeDubBrowserLang, + youtubeVideoCodec: getSetting("save", "youtubeVideoCodec"), + videoQuality: getSetting("save", "videoQuality"), - youtubeVideoCodec: saveSettings.youtubeVideoCodec, - videoQuality: saveSettings.videoQuality, + filenameStyle: getSetting("save", "filenameStyle"), + disableMetadata: getSetting("save", "disableMetadata"), - filenameStyle: saveSettings.filenameStyle, - disableMetadata: saveSettings.disableMetadata, + twitterGif: getSetting("save", "twitterGif"), + tiktokH265: getSetting("save", "tiktokH265"), - twitterGif: saveSettings.twitterGif, - tiktokH265: saveSettings.tiktokH265, - - alwaysProxy: privacySettings.alwaysProxy, + alwaysProxy: getSetting("privacy", "alwaysProxy"), } await apiOverrideWarning(); - const usingCustomInstance = currentSettings.processing.enableCustomInstances - && currentSettings.processing.customInstanceURL; + const usingCustomInstance = getSetting("processing", "enableCustomInstances") + && getSetting("processing", "customInstanceURL"); const api = currentApiURL(); // FIXME: rewrite this to allow custom instances to specify their own turnstile tokens const session = usingCustomInstance ? undefined : await getSession(); diff --git a/web/src/lib/settings/lazy-get.ts b/web/src/lib/settings/lazy-get.ts new file mode 100644 index 00000000..5e11bbad --- /dev/null +++ b/web/src/lib/settings/lazy-get.ts @@ -0,0 +1,14 @@ +import type { CobaltSettings } from "$lib/types/settings"; +import defaults from "./defaults"; + +export default function lazySettingGetter(settings: CobaltSettings) { + // Returns the setting value only if it differs from the default. + return < + Context extends Exclude, + Id extends keyof CobaltSettings[Context] + >(context: Context, key: Id) => { + if (defaults[context][key] !== settings[context][key]) { + return settings[context][key]; + } + } +}