mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
web: add api response types & clean up DownloadButton
This commit is contained in:
parent
1f2c28bd02
commit
3554222f42
3 changed files with 75 additions and 18 deletions
|
@ -33,39 +33,48 @@
|
|||
}, 2500)
|
||||
}
|
||||
|
||||
// alerts are temporary, we don't have an error popup yet >_<
|
||||
const download = async (link: string) => {
|
||||
changeDownloadButton("think");
|
||||
|
||||
const response = await API.request(link);
|
||||
|
||||
if (!response) {
|
||||
changeDownloadButton("error");
|
||||
restoreDownloadButton();
|
||||
|
||||
console.log("couldn't access the api")
|
||||
return alert("couldn't access the api")
|
||||
}
|
||||
|
||||
if (["error", "rate-limit"].includes(response?.status) && response?.text) {
|
||||
if (response.status === "error" || response.status === "rate-limit") {
|
||||
changeDownloadButton("error");
|
||||
restoreDownloadButton();
|
||||
|
||||
console.log(`error from api: ${response?.text}`)
|
||||
return alert(`error from api: ${response.text}`);
|
||||
}
|
||||
|
||||
if (response?.url) {
|
||||
if (response?.status === "redirect") {
|
||||
if (response.status === "redirect") {
|
||||
changeDownloadButton("done");
|
||||
window.open(response?.url, '_blank');
|
||||
restoreDownloadButton();
|
||||
|
||||
return window.open(response.url, '_blank');
|
||||
}
|
||||
if (response?.status === "stream") {
|
||||
|
||||
if (response.status === "stream") {
|
||||
changeDownloadButton("check");
|
||||
|
||||
const probeResult = await API.probeCobaltStream(response?.url);
|
||||
const probeResult = await API.probeCobaltStream(response.url);
|
||||
|
||||
if (probeResult === 200) {
|
||||
changeDownloadButton("done");
|
||||
window.open(response?.url, '_blank');
|
||||
restoreDownloadButton();
|
||||
}
|
||||
|
||||
return window.open(response.url, '_blank');
|
||||
} else {
|
||||
changeDownloadButton("error");
|
||||
restoreDownloadButton();
|
||||
|
||||
return alert("couldn't probe the stream");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import type { CobaltAPIResponse } from "$lib/types/api";
|
||||
|
||||
const apiURL = "https://api.cobalt.tools";
|
||||
|
||||
const request = async (url: string) => {
|
||||
|
@ -5,7 +7,7 @@ const request = async (url: string) => {
|
|||
url
|
||||
}
|
||||
|
||||
const response = await fetch(`${apiURL}/api/json`, {
|
||||
const response: CobaltAPIResponse | undefined = await fetch(`${apiURL}/api/json`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(request),
|
||||
headers: {
|
||||
|
|
46
web/src/lib/types/api.ts
Normal file
46
web/src/lib/types/api.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
enum CobaltResponseType {
|
||||
Error = 'error',
|
||||
RateLimit = 'rate-limit',
|
||||
Picker = 'picker',
|
||||
Redirect = 'redirect',
|
||||
Stream = 'stream'
|
||||
}
|
||||
|
||||
type CobaltErrorResponse = {
|
||||
status: CobaltResponseType.Error | CobaltResponseType.RateLimit,
|
||||
text: string
|
||||
};
|
||||
|
||||
type CobaltPartialURLResponse = { url: string }
|
||||
|
||||
type CobaltPartialImagesPickerResponse = {
|
||||
pickerType: 'images',
|
||||
picker: CobaltPartialURLResponse[]
|
||||
}
|
||||
|
||||
type CobaltPartialVariousPickerResponse = {
|
||||
pickerType: 'various',
|
||||
picker: {
|
||||
type: 'photo' | 'video',
|
||||
url: string,
|
||||
thumb: string
|
||||
}[];
|
||||
}
|
||||
|
||||
type CobaltPickerResponse = {
|
||||
status: CobaltResponseType.Picker
|
||||
audio: string | false,
|
||||
} & (CobaltPartialImagesPickerResponse | CobaltPartialVariousPickerResponse);
|
||||
|
||||
type CobaltRedirectResponse = {
|
||||
status: CobaltResponseType.Redirect
|
||||
} & CobaltPartialURLResponse;
|
||||
|
||||
type CobaltStreamResponse = {
|
||||
status: CobaltResponseType.Stream
|
||||
} & CobaltPartialURLResponse;
|
||||
|
||||
export type CobaltAPIResponse = CobaltErrorResponse
|
||||
| CobaltPickerResponse
|
||||
| CobaltRedirectResponse
|
||||
| CobaltStreamResponse;
|
Loading…
Reference in a new issue