Improving typescript types
This commit is contained in:
parent
e9d4dc4e3a
commit
99db087cf1
7 changed files with 190 additions and 33 deletions
|
|
@ -104,16 +104,34 @@ export interface ErrorDetails {
|
|||
text: string,
|
||||
};
|
||||
|
||||
export type Duration = number;
|
||||
|
||||
export interface PeerConnectionOptions {
|
||||
connect_timeout?: Duration | null;
|
||||
read_write_timeout?: Duration | null;
|
||||
keep_alive_interval?: Duration | null;
|
||||
}
|
||||
|
||||
export interface AddTorrentOptions {
|
||||
paused?: boolean;
|
||||
only_files_regex?: string | null;
|
||||
only_files?: number[] | null;
|
||||
overwrite?: boolean;
|
||||
list_only?: boolean;
|
||||
output_folder?: string | null;
|
||||
sub_folder?: string | null;
|
||||
peer_opts?: PeerConnectionOptions | null;
|
||||
force_tracker_interval?: Duration | null;
|
||||
initial_peers?: string[] | null; // Assuming SocketAddr is equivalent to a string in TypeScript
|
||||
preferred_id?: number | null;
|
||||
}
|
||||
|
||||
|
||||
export interface RqbitAPI {
|
||||
listTorrents: () => Promise<ListTorrentsResponse>,
|
||||
getTorrentDetails: (index: number) => Promise<TorrentDetails>,
|
||||
getTorrentStats: (index: number) => Promise<TorrentStats>;
|
||||
uploadTorrent: (data: string | File, opts?: {
|
||||
listOnly?: boolean,
|
||||
selectedFiles?: Array<number>,
|
||||
unpopularTorrent?: boolean,
|
||||
initialPeers?: Array<string> | null,
|
||||
}) => Promise<AddTorrentResponse>;
|
||||
uploadTorrent: (data: string | File, opts?: AddTorrentOptions) => Promise<AddTorrentResponse>;
|
||||
|
||||
pause: (index: number) => Promise<void>;
|
||||
start: (index: number) => Promise<void>;
|
||||
|
|
|
|||
|
|
@ -55,25 +55,22 @@ export const API: RqbitAPI = {
|
|||
return makeRequest('GET', `/torrents/${index}/stats/v1`);
|
||||
},
|
||||
|
||||
uploadTorrent: (data: string | File, opts?: {
|
||||
listOnly?: boolean,
|
||||
selectedFiles?: Array<number>,
|
||||
unpopularTorrent?: boolean,
|
||||
initialPeers?: Array<string> | null,
|
||||
}): Promise<AddTorrentResponse> => {
|
||||
opts = opts || {};
|
||||
uploadTorrent: (data, opts): Promise<AddTorrentResponse> => {
|
||||
let url = '/torrents?&overwrite=true';
|
||||
if (opts.listOnly) {
|
||||
if (opts?.list_only) {
|
||||
url += '&list_only=true';
|
||||
}
|
||||
if (opts.selectedFiles != null) {
|
||||
url += `&only_files=${opts.selectedFiles.join(',')}`;
|
||||
if (opts?.only_files != null) {
|
||||
url += `&only_files=${opts.only_files.join(',')}`;
|
||||
}
|
||||
if (opts.unpopularTorrent) {
|
||||
url += '&peer_connect_timeout=20&peer_read_write_timeout=60';
|
||||
if (opts?.peer_opts?.connect_timeout) {
|
||||
url += `&peer_connect_timeout=${opts.peer_opts.connect_timeout}`;
|
||||
}
|
||||
if (opts.initialPeers) {
|
||||
url += `&initial_peers=${opts.initialPeers.join(',')}`;
|
||||
if (opts?.peer_opts?.read_write_timeout) {
|
||||
url += `&peer_read_write_timeout=${opts.peer_opts.read_write_timeout}`;
|
||||
}
|
||||
if (opts?.initial_peers) {
|
||||
url += `&initial_peers=${opts.initial_peers.join(',')}`;
|
||||
}
|
||||
if (typeof data === 'string') {
|
||||
url += '&is_url=true';
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { MouseEventHandler, RefObject, createContext, useContext, useEffect, useRef, useState } from 'react';
|
||||
import { ProgressBar, Button, Container, Row, Col, Alert, Modal, Form, Spinner } from 'react-bootstrap';
|
||||
import { AddTorrentResponse, TorrentDetails, TorrentId, TorrentStats, ErrorDetails as ApiErrorDetails, STATE_INITIALIZING, STATE_LIVE, STATE_PAUSED, STATE_ERROR, RqbitAPI, ErrorDetails, ListTorrentsResponse } from './api-types';
|
||||
import { AddTorrentResponse, TorrentDetails, TorrentId, TorrentStats, ErrorDetails as ApiErrorDetails, STATE_INITIALIZING, STATE_LIVE, STATE_PAUSED, STATE_ERROR, RqbitAPI, ErrorDetails, ListTorrentsResponse, AddTorrentOptions } from './api-types';
|
||||
|
||||
interface Error {
|
||||
text: string,
|
||||
|
|
@ -13,28 +13,28 @@ interface ContextType {
|
|||
}
|
||||
|
||||
export const APIContext = createContext<RqbitAPI>({
|
||||
listTorrents: function (): Promise<ListTorrentsResponse> {
|
||||
listTorrents: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
},
|
||||
getTorrentDetails: function (index: number): Promise<TorrentDetails> {
|
||||
getTorrentDetails: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
},
|
||||
getTorrentStats: function (index: number): Promise<TorrentStats> {
|
||||
getTorrentStats: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
},
|
||||
uploadTorrent: function (data: string | File, opts?: { listOnly?: boolean | undefined; selectedFiles?: number[] | undefined; unpopularTorrent?: boolean | undefined; initialPeers?: string[] | null | undefined; } | undefined): Promise<AddTorrentResponse> {
|
||||
uploadTorrent: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
},
|
||||
pause: function (index: number): Promise<void> {
|
||||
pause: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
},
|
||||
start: function (index: number): Promise<void> {
|
||||
start: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
},
|
||||
forget: function (index: number): Promise<void> {
|
||||
forget: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
},
|
||||
delete: function (index: number): Promise<void> {
|
||||
delete: () => {
|
||||
throw new Error('Function not implemented.');
|
||||
}
|
||||
});
|
||||
|
|
@ -439,7 +439,7 @@ const UploadButton: React.FC<{
|
|||
let t = setTimeout(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await API.uploadTorrent(data, { listOnly: true });
|
||||
const response = await API.uploadTorrent(data, { list_only: true });
|
||||
setListTorrentResponse(response);
|
||||
} catch (e) {
|
||||
setListTorrentError({ text: 'Error listing torrent files', details: e as ErrorDetails });
|
||||
|
|
@ -624,7 +624,18 @@ const FileSelectionModal = (props: {
|
|||
}
|
||||
setUploading(true);
|
||||
let initialPeers = listTorrentResponse.seen_peers ? listTorrentResponse.seen_peers.slice(0, 32) : null;
|
||||
API.uploadTorrent(data, { selectedFiles, unpopularTorrent, initialPeers }).then(() => {
|
||||
let opts: AddTorrentOptions = {
|
||||
overwrite: true,
|
||||
only_files: selectedFiles,
|
||||
initial_peers: initialPeers,
|
||||
};
|
||||
if (unpopularTorrent) {
|
||||
opts.peer_opts = {
|
||||
connect_timeout: 20,
|
||||
read_write_timeout: 60,
|
||||
};
|
||||
}
|
||||
API.uploadTorrent(data, opts).then(() => {
|
||||
onHide();
|
||||
ctx.refreshTorrents();
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue