web: add api response types & clean up DownloadButton

This commit is contained in:
wukko
2024-06-16 18:53:45 +06:00
parent 1f2c28bd02
commit 3554222f42
3 changed files with 75 additions and 18 deletions

View File

@@ -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
View 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;