setup vscode for consistent JS formatting
This commit is contained in:
parent
a641717245
commit
ec63e1cef7
15 changed files with 1675 additions and 1338 deletions
|
|
@ -1,90 +1,107 @@
|
|||
import { AddTorrentResponse, ListTorrentsResponse, RqbitAPI, TorrentDetails, TorrentStats, ErrorDetails } from "./rqbit-webui-src/api-types";
|
||||
import {
|
||||
AddTorrentResponse,
|
||||
ListTorrentsResponse,
|
||||
RqbitAPI,
|
||||
TorrentDetails,
|
||||
TorrentStats,
|
||||
ErrorDetails,
|
||||
} from "./rqbit-webui-src/api-types";
|
||||
|
||||
import { InvokeArgs, invoke } from "@tauri-apps/api/tauri"
|
||||
import { InvokeArgs, invoke } from "@tauri-apps/api/tauri";
|
||||
|
||||
interface InvokeErrorResponse {
|
||||
error_kind: string,
|
||||
human_readable: string,
|
||||
status: number,
|
||||
status_text: string,
|
||||
error_kind: string;
|
||||
human_readable: string;
|
||||
status: number;
|
||||
status_text: string;
|
||||
}
|
||||
|
||||
function errorToUIError(path: string): (e: InvokeErrorResponse) => Promise<never> {
|
||||
return (e: InvokeErrorResponse) => {
|
||||
console.log(e);
|
||||
let reason: ErrorDetails = {
|
||||
method: 'INVOKE',
|
||||
path: path,
|
||||
text: e.human_readable,
|
||||
status: e.status,
|
||||
statusText: e.status_text
|
||||
};
|
||||
return Promise.reject(reason);
|
||||
}
|
||||
function errorToUIError(
|
||||
path: string
|
||||
): (e: InvokeErrorResponse) => Promise<never> {
|
||||
return (e: InvokeErrorResponse) => {
|
||||
console.log(e);
|
||||
let reason: ErrorDetails = {
|
||||
method: "INVOKE",
|
||||
path: path,
|
||||
text: e.human_readable,
|
||||
status: e.status,
|
||||
statusText: e.status_text,
|
||||
};
|
||||
return Promise.reject(reason);
|
||||
};
|
||||
}
|
||||
|
||||
export async function invokeAPI<Response>(name: string, params?: InvokeArgs): Promise<Response> {
|
||||
console.log("invoking", name, params);
|
||||
const result = await invoke<Response>(name, params).catch(errorToUIError(name));
|
||||
console.log(result);
|
||||
return result;
|
||||
export async function invokeAPI<Response>(
|
||||
name: string,
|
||||
params?: InvokeArgs
|
||||
): Promise<Response> {
|
||||
console.log("invoking", name, params);
|
||||
const result = await invoke<Response>(name, params).catch(
|
||||
errorToUIError(name)
|
||||
);
|
||||
console.log(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
async function readFileAsBase64(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = function (event) {
|
||||
const base64String = (event?.target?.result as string)?.split(',')[1];
|
||||
if (base64String) {
|
||||
resolve(base64String);
|
||||
} else {
|
||||
reject(new Error('Failed to read file as base64.'));
|
||||
}
|
||||
};
|
||||
reader.onload = function (event) {
|
||||
const base64String = (event?.target?.result as string)?.split(",")[1];
|
||||
if (base64String) {
|
||||
resolve(base64String);
|
||||
} else {
|
||||
reject(new Error("Failed to read file as base64."));
|
||||
}
|
||||
};
|
||||
|
||||
reader.onerror = function (error) {
|
||||
console.log(error);
|
||||
reject(error);
|
||||
};
|
||||
reader.onerror = function (error) {
|
||||
console.log(error);
|
||||
reject(error);
|
||||
};
|
||||
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
export const API: RqbitAPI = {
|
||||
listTorrents: async function (): Promise<ListTorrentsResponse> {
|
||||
return await invokeAPI<ListTorrentsResponse>("torrents_list");
|
||||
},
|
||||
getTorrentDetails: async function (id: number): Promise<TorrentDetails> {
|
||||
return await invokeAPI<TorrentDetails>("torrent_details", { id });
|
||||
},
|
||||
getTorrentStats: async function (id: number): Promise<TorrentStats> {
|
||||
return await invokeAPI<TorrentStats>("torrent_stats", { id });
|
||||
},
|
||||
uploadTorrent: async function (data, opts): Promise<AddTorrentResponse> {
|
||||
if (data instanceof File) {
|
||||
let contents = await readFileAsBase64(data);
|
||||
return await invokeAPI<AddTorrentResponse>("torrent_create_from_base64_file", {
|
||||
contents,
|
||||
opts: opts ?? {},
|
||||
});
|
||||
listTorrents: async function (): Promise<ListTorrentsResponse> {
|
||||
return await invokeAPI<ListTorrentsResponse>("torrents_list");
|
||||
},
|
||||
getTorrentDetails: async function (id: number): Promise<TorrentDetails> {
|
||||
return await invokeAPI<TorrentDetails>("torrent_details", { id });
|
||||
},
|
||||
getTorrentStats: async function (id: number): Promise<TorrentStats> {
|
||||
return await invokeAPI<TorrentStats>("torrent_stats", { id });
|
||||
},
|
||||
uploadTorrent: async function (data, opts): Promise<AddTorrentResponse> {
|
||||
if (data instanceof File) {
|
||||
let contents = await readFileAsBase64(data);
|
||||
return await invokeAPI<AddTorrentResponse>(
|
||||
"torrent_create_from_base64_file",
|
||||
{
|
||||
contents,
|
||||
opts: opts ?? {},
|
||||
}
|
||||
return await invokeAPI<AddTorrentResponse>("torrent_create_from_url", {
|
||||
url: data,
|
||||
opts: opts ?? {},
|
||||
});
|
||||
},
|
||||
pause: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_pause", { id });
|
||||
},
|
||||
start: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_start", { id });
|
||||
},
|
||||
forget: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_forget", { id });
|
||||
},
|
||||
delete: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_delete", { id });
|
||||
);
|
||||
}
|
||||
}
|
||||
return await invokeAPI<AddTorrentResponse>("torrent_create_from_url", {
|
||||
url: data,
|
||||
opts: opts ?? {},
|
||||
});
|
||||
},
|
||||
pause: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_pause", { id });
|
||||
},
|
||||
start: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_start", { id });
|
||||
},
|
||||
forget: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_forget", { id });
|
||||
},
|
||||
delete: function (id: number): Promise<void> {
|
||||
return invokeAPI<void>("torrent_action_delete", { id });
|
||||
},
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue