mirror of
https://github.com/wukko/cobalt.git
synced 2025-04-29 22:14:26 +02:00
web/api: move api request creation to saving-handler & limit the type
prerequisites for reusing the requests 👀
This commit is contained in:
parent
9225b31986
commit
f9c0decd4c
@ -1,7 +1,6 @@
|
|||||||
import { get } from "svelte/store";
|
import { get } from "svelte/store";
|
||||||
|
|
||||||
import settings from "$lib/state/settings";
|
import settings from "$lib/state/settings";
|
||||||
import lazySettingGetter from "$lib/settings/lazy-get";
|
|
||||||
|
|
||||||
import { getSession } from "$lib/api/session";
|
import { getSession } from "$lib/api/session";
|
||||||
import { currentApiURL } from "$lib/api/api-url";
|
import { currentApiURL } from "$lib/api/api-url";
|
||||||
@ -10,7 +9,7 @@ import cachedInfo from "$lib/state/server-info";
|
|||||||
import { getServerInfo } from "$lib/api/server-info";
|
import { getServerInfo } from "$lib/api/server-info";
|
||||||
|
|
||||||
import type { Optional } from "$lib/types/generic";
|
import type { Optional } from "$lib/types/generic";
|
||||||
import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api";
|
import type { CobaltAPIResponse, CobaltErrorResponse, CobaltSaveRequestBody } from "$lib/types/api";
|
||||||
|
|
||||||
const getAuthorization = async () => {
|
const getAuthorization = async () => {
|
||||||
const processing = get(settings).processing;
|
const processing = get(settings).processing;
|
||||||
@ -43,32 +42,7 @@ const getAuthorization = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = async (url: string) => {
|
const request = async (request: CobaltSaveRequestBody) => {
|
||||||
const getSetting = lazySettingGetter(get(settings));
|
|
||||||
|
|
||||||
const request = {
|
|
||||||
url,
|
|
||||||
|
|
||||||
alwaysProxy: getSetting("save", "alwaysProxy"),
|
|
||||||
localProcessing: getSetting("save", "localProcessing"),
|
|
||||||
downloadMode: getSetting("save", "downloadMode"),
|
|
||||||
|
|
||||||
filenameStyle: getSetting("save", "filenameStyle"),
|
|
||||||
disableMetadata: getSetting("save", "disableMetadata"),
|
|
||||||
|
|
||||||
audioBitrate: getSetting("save", "audioBitrate"),
|
|
||||||
audioFormat: getSetting("save", "audioFormat"),
|
|
||||||
tiktokFullAudio: getSetting("save", "tiktokFullAudio"),
|
|
||||||
youtubeDubLang: getSetting("save", "youtubeDubLang"),
|
|
||||||
|
|
||||||
youtubeVideoCodec: getSetting("save", "youtubeVideoCodec"),
|
|
||||||
videoQuality: getSetting("save", "videoQuality"),
|
|
||||||
youtubeHLS: getSetting("save", "youtubeHLS"),
|
|
||||||
|
|
||||||
convertGif: getSetting("save", "convertGif"),
|
|
||||||
allowH265: getSetting("save", "allowH265"),
|
|
||||||
}
|
|
||||||
|
|
||||||
await getServerInfo();
|
await getServerInfo();
|
||||||
|
|
||||||
const getCachedInfo = get(cachedInfo);
|
const getCachedInfo = get(cachedInfo);
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import API from "$lib/api/api";
|
import API from "$lib/api/api";
|
||||||
|
import settings from "$lib/state/settings";
|
||||||
|
import lazySettingGetter from "$lib/settings/lazy-get";
|
||||||
|
|
||||||
import { get } from "svelte/store";
|
import { get } from "svelte/store";
|
||||||
import { t } from "$lib/i18n/translations";
|
import { t } from "$lib/i18n/translations";
|
||||||
@ -26,7 +28,32 @@ export const savingHandler = async (link: string) => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const response = await API.request(link);
|
const getSetting = lazySettingGetter(get(settings));
|
||||||
|
|
||||||
|
const request = {
|
||||||
|
url: link,
|
||||||
|
|
||||||
|
alwaysProxy: getSetting("save", "alwaysProxy"),
|
||||||
|
localProcessing: getSetting("save", "localProcessing"),
|
||||||
|
downloadMode: getSetting("save", "downloadMode"),
|
||||||
|
|
||||||
|
filenameStyle: getSetting("save", "filenameStyle"),
|
||||||
|
disableMetadata: getSetting("save", "disableMetadata"),
|
||||||
|
|
||||||
|
audioBitrate: getSetting("save", "audioBitrate"),
|
||||||
|
audioFormat: getSetting("save", "audioFormat"),
|
||||||
|
tiktokFullAudio: getSetting("save", "tiktokFullAudio"),
|
||||||
|
youtubeDubLang: getSetting("save", "youtubeDubLang"),
|
||||||
|
|
||||||
|
youtubeVideoCodec: getSetting("save", "youtubeVideoCodec"),
|
||||||
|
videoQuality: getSetting("save", "videoQuality"),
|
||||||
|
youtubeHLS: getSetting("save", "youtubeHLS"),
|
||||||
|
|
||||||
|
convertGif: getSetting("save", "convertGif"),
|
||||||
|
allowH265: getSetting("save", "allowH265"),
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await API.request(request);
|
||||||
|
|
||||||
if (!response) {
|
if (!response) {
|
||||||
downloadButtonState.set("error");
|
downloadButtonState.set("error");
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
import defaults from "$lib/settings/defaults";
|
||||||
import type { CobaltSettings } from "$lib/types/settings";
|
import type { CobaltSettings } from "$lib/types/settings";
|
||||||
import defaults from "./defaults";
|
|
||||||
|
|
||||||
export default function lazySettingGetter(settings: CobaltSettings) {
|
export default function lazySettingGetter(settings: CobaltSettings) {
|
||||||
// Returns the setting value only if it differs from the default.
|
// Returns the setting value only if it differs from the default.
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import type { CobaltSettings } from "$lib/types/settings";
|
||||||
|
|
||||||
enum CobaltResponseType {
|
enum CobaltResponseType {
|
||||||
Error = 'error',
|
Error = 'error',
|
||||||
Picker = 'picker',
|
Picker = 'picker',
|
||||||
@ -91,6 +93,12 @@ export type CobaltServerInfo = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: strict partial
|
||||||
|
// this allows for extra properties, which is not ideal,
|
||||||
|
// but i couldn't figure out how to make a strict partial :(
|
||||||
|
export type CobaltSaveRequestBody =
|
||||||
|
{ url: string } & Partial<Omit<CobaltSettings['save'], 'savingMethod'>>;
|
||||||
|
|
||||||
export type CobaltSessionResponse = CobaltSession | CobaltErrorResponse;
|
export type CobaltSessionResponse = CobaltSession | CobaltErrorResponse;
|
||||||
export type CobaltServerInfoResponse = CobaltServerInfo | CobaltErrorResponse;
|
export type CobaltServerInfoResponse = CobaltServerInfo | CobaltErrorResponse;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user