web/api: remove stuff that was moved to api-client
This commit is contained in:
parent
6d4477a962
commit
5d7cd861f3
3 changed files with 0 additions and 173 deletions
|
@ -1,85 +1,3 @@
|
||||||
import { get } from "svelte/store";
|
|
||||||
|
|
||||||
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 getSetting = lazySettingGetter(get(settings));
|
|
||||||
|
|
||||||
const request = {
|
|
||||||
url,
|
|
||||||
|
|
||||||
downloadMode: getSetting("save", "downloadMode"),
|
|
||||||
audioBitrate: getSetting("save", "audioBitrate"),
|
|
||||||
audioFormat: getSetting("save", "audioFormat"),
|
|
||||||
tiktokFullAudio: getSetting("save", "tiktokFullAudio"),
|
|
||||||
youtubeDubBrowserLang: getSetting("save", "youtubeDubBrowserLang"),
|
|
||||||
|
|
||||||
youtubeVideoCodec: getSetting("save", "youtubeVideoCodec"),
|
|
||||||
videoQuality: getSetting("save", "videoQuality"),
|
|
||||||
|
|
||||||
filenameStyle: getSetting("save", "filenameStyle"),
|
|
||||||
disableMetadata: getSetting("save", "disableMetadata"),
|
|
||||||
|
|
||||||
twitterGif: getSetting("save", "twitterGif"),
|
|
||||||
tiktokH265: getSetting("save", "tiktokH265"),
|
|
||||||
|
|
||||||
alwaysProxy: getSetting("privacy", "alwaysProxy"),
|
|
||||||
}
|
|
||||||
|
|
||||||
await apiOverrideWarning();
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
let extraHeaders = {}
|
|
||||||
|
|
||||||
if (session) {
|
|
||||||
if ("error" in session) {
|
|
||||||
if (session.error.code !== "error.api.auth.not_configured") {
|
|
||||||
return session;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
extraHeaders = {
|
|
||||||
"Authorization": `Bearer ${session.token}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const response: Optional<CobaltAPIResponse> = await fetch(api, {
|
|
||||||
method: "POST",
|
|
||||||
redirect: "manual",
|
|
||||||
signal: AbortSignal.timeout(10000),
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
headers: {
|
|
||||||
"Accept": "application/json",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
...extraHeaders,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then(r => r.json())
|
|
||||||
.catch((e) => {
|
|
||||||
if (e?.message?.includes("timed out")) {
|
|
||||||
return {
|
|
||||||
status: "error",
|
|
||||||
error: {
|
|
||||||
code: "error.api.timed_out"
|
|
||||||
}
|
|
||||||
} as CobaltErrorResponse;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
const probeCobaltTunnel = async (url: string) => {
|
const probeCobaltTunnel = async (url: string) => {
|
||||||
const request = await fetch(`${url}&p=1`).catch(() => {});
|
const request = await fetch(`${url}&p=1`).catch(() => {});
|
||||||
if (request?.status === 200) {
|
if (request?.status === 200) {
|
||||||
|
@ -89,6 +7,5 @@ const probeCobaltTunnel = async (url: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
request,
|
|
||||||
probeCobaltTunnel,
|
probeCobaltTunnel,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,66 +0,0 @@
|
||||||
import turnstile from "$lib/api/turnstile";
|
|
||||||
import { writable, get } from "svelte/store";
|
|
||||||
import { currentApiURL } from "$lib/api/api-url";
|
|
||||||
|
|
||||||
import type { CobaltSession, CobaltErrorResponse, CobaltSessionResponse } from "$lib/types/api";
|
|
||||||
|
|
||||||
const cachedSession = writable<CobaltSession | undefined>();
|
|
||||||
|
|
||||||
export const requestSession = async() => {
|
|
||||||
const apiEndpoint = `${currentApiURL()}/session`;
|
|
||||||
|
|
||||||
let requestHeaders = {};
|
|
||||||
|
|
||||||
const turnstileResponse = turnstile.getResponse();
|
|
||||||
if (turnstileResponse) {
|
|
||||||
requestHeaders = {
|
|
||||||
"cf-turnstile-response": turnstileResponse
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const response: CobaltSessionResponse = await fetch(apiEndpoint, {
|
|
||||||
method: "POST",
|
|
||||||
redirect: "manual",
|
|
||||||
signal: AbortSignal.timeout(10000),
|
|
||||||
headers: requestHeaders,
|
|
||||||
})
|
|
||||||
.then(r => r.json())
|
|
||||||
.catch((e) => {
|
|
||||||
if (e?.message?.includes("timed out")) {
|
|
||||||
return {
|
|
||||||
status: "error",
|
|
||||||
error: {
|
|
||||||
code: "error.api.timed_out"
|
|
||||||
}
|
|
||||||
} as CobaltErrorResponse
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
turnstile.update();
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getSession = async () => {
|
|
||||||
const currentTime = () => Math.floor(new Date().getTime() / 1000);
|
|
||||||
const cache = get(cachedSession);
|
|
||||||
|
|
||||||
if (cache?.token && cache?.exp - 2 > currentTime()) {
|
|
||||||
return cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
const newSession = await requestSession();
|
|
||||||
|
|
||||||
if (!newSession) return {
|
|
||||||
status: "error",
|
|
||||||
error: {
|
|
||||||
code: "error.api.unreachable"
|
|
||||||
}
|
|
||||||
} as CobaltErrorResponse
|
|
||||||
|
|
||||||
if (!("status" in newSession)) {
|
|
||||||
newSession.exp = currentTime() + newSession.exp;
|
|
||||||
cachedSession.set(newSession);
|
|
||||||
}
|
|
||||||
return newSession;
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
const getResponse = () => {
|
|
||||||
const turnstileElement = document.getElementById("turnstile-widget");
|
|
||||||
|
|
||||||
if (turnstileElement) {
|
|
||||||
return window?.turnstile?.getResponse(turnstileElement);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const update = () => {
|
|
||||||
const turnstileElement = document.getElementById("turnstile-widget");
|
|
||||||
|
|
||||||
if (turnstileElement) {
|
|
||||||
return window?.turnstile?.reset(turnstileElement);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
|
||||||
getResponse,
|
|
||||||
update,
|
|
||||||
}
|
|
Loading…
Reference in a new issue