refactor: web api client and cleanup (#128)

refactor: refactored APIClient.ts with a new fetch wrapper and changed it into an explicit-import.

chore: modified package.json not to start browser on "npm run start"

chore: cleaned up code, deleted 2mo+ useless old comments.

fix: fixed parameter collision in screens/filters/details.tsx

fix: override react-select's Select component style to make it consistent. addresses #116

Co-authored-by: anonymous <anonymous>
This commit is contained in:
stacksmash76 2022-02-10 17:47:05 +01:00 committed by GitHub
parent 6d68a5c3b7
commit b60e5f61c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 381 additions and 793 deletions

View file

@ -1,72 +1,55 @@
import {baseUrl, sseBaseUrl} from "../utils";
function baseClient(endpoint: string, method: string, { body, ...customConfig}: any = {}) {
const baseURL = baseUrl()
interface ConfigType {
body?: BodyInit | Record<string, unknown> | null;
headers?: Record<string, string>;
}
const headers = {'content-type': 'application/json'}
export async function HttpClient<T>(
endpoint: string,
method: string,
{ body, ...customConfig }: ConfigType = {}
): Promise<T> {
const config = {
method: method,
...customConfig,
body: body ? JSON.stringify(body) : null,
headers: {
...headers,
...customConfig.headers,
"Content-Type": "application/json"
},
}
// NOTE: customConfig can override the above defined settings
...customConfig
} as RequestInit;
if (body) {
config.body = JSON.stringify(body)
}
return window.fetch(`${baseURL}${endpoint}`, config)
return window.fetch(`${baseUrl()}${endpoint}`, config)
.then(async response => {
if (response.status === 401) {
// unauthorized
// window.location.assign(window.location)
if ([401, 403, 404].includes(response.status))
return Promise.reject(new Error(response.statusText));
return Promise.reject(new Error(response.statusText))
}
if (response.status === 403) {
// window.location.assign("/login")
return Promise.reject(new Error(response.statusText))
// return
}
if (response.status === 404) {
return Promise.reject(new Error(response.statusText))
}
if (response.status === 201) {
return ""
}
if (response.status === 204) {
return ""
}
if ([201, 204].includes(response.status))
return Promise.resolve(response);
if (response.ok) {
return await response.json()
return await response.json();
} else {
const errorMessage = await response.text()
return Promise.reject(new Error(errorMessage))
const errorMessage = await response.text();
return Promise.reject(new Error(errorMessage));
}
})
});
}
const appClient = {
Get: (endpoint: string) => baseClient(endpoint, "GET"),
Post: (endpoint: string, data: any) => baseClient(endpoint, "POST", { body: data }),
Put: (endpoint: string, data: any) => baseClient(endpoint, "PUT", { body: data }),
Patch: (endpoint: string, data: any) => baseClient(endpoint, "PATCH", { body: data }),
Delete: (endpoint: string) => baseClient(endpoint, "DELETE"),
Get: <T>(endpoint: string) => HttpClient<T>(endpoint, "GET"),
Post: (endpoint: string, data: any) => HttpClient<void>(endpoint, "POST", { body: data }),
Put: (endpoint: string, data: any) => HttpClient<void>(endpoint, "PUT", { body: data }),
Patch: (endpoint: string, data: any) => HttpClient<void>(endpoint, "PATCH", { body: data }),
Delete: (endpoint: string) => HttpClient<void>(endpoint, "DELETE")
}
const APIClient = {
export const APIClient = {
auth: {
login: (username: string, password: string) => appClient.Post("api/auth/login", {username: username, password: password}),
logout: () => appClient.Post(`api/auth/logout`, null),
test: () => appClient.Get(`api/auth/test`),
login: (username: string, password: string) => appClient.Post("api/auth/login", { username: username, password: password }),
logout: () => appClient.Post("api/auth/logout", null),
test: () => appClient.Get<void>("api/auth/test"),
},
actions: {
create: (action: Action) => appClient.Post("api/actions", action),
@ -75,34 +58,37 @@ const APIClient = {
toggleEnable: (id: number) => appClient.Patch(`api/actions/${id}/toggleEnabled`, null),
},
config: {
get: () => appClient.Get("api/config")
get: () => appClient.Get<Config>("api/config")
},
download_clients: {
getAll: () => appClient.Get("api/download_clients"),
create: (dc: DownloadClient) => appClient.Post(`api/download_clients`, dc),
update: (dc: DownloadClient) => appClient.Put(`api/download_clients`, dc),
getAll: () => appClient.Get<DownloadClient[]>("api/download_clients"),
create: (dc: DownloadClient) => appClient.Post("api/download_clients", dc),
update: (dc: DownloadClient) => appClient.Put("api/download_clients", dc),
delete: (id: number) => appClient.Delete(`api/download_clients/${id}`),
test: (dc: DownloadClient) => appClient.Post(`api/download_clients/test`, dc),
test: (dc: DownloadClient) => appClient.Post("api/download_clients/test", dc),
},
filters: {
getAll: () => appClient.Get("api/filters"),
getByID: (id: number) => appClient.Get(`api/filters/${id}`),
create: (filter: Filter) => appClient.Post(`api/filters`, filter),
getAll: () => appClient.Get<Filter[]>("api/filters"),
getByID: (id: number) => appClient.Get<Filter>(`api/filters/${id}`),
create: (filter: Filter) => appClient.Post("api/filters", filter),
update: (filter: Filter) => appClient.Put(`api/filters/${filter.id}`, filter),
toggleEnable: (id: number, enabled: boolean) => appClient.Put(`api/filters/${id}/enabled`, { enabled }),
delete: (id: number) => appClient.Delete(`api/filters/${id}`),
},
indexers: {
getOptions: () => appClient.Get("api/indexer/options"),
getAll: () => appClient.Get("api/indexer"),
getSchema: () => appClient.Get("api/indexer/schema"),
create: (indexer: Indexer) => appClient.Post(`api/indexer`, indexer),
update: (indexer: Indexer) => appClient.Put(`api/indexer`, indexer),
// returns indexer options for all currently present/enabled indexers
getOptions: () => appClient.Get<Indexer[]>("api/indexer/options"),
// returns indexer definitions for all currently present/enabled indexers
getAll: () => appClient.Get<IndexerDefinition[]>("api/indexer"),
// returns all possible indexer definitions
getSchema: () => appClient.Get<IndexerDefinition[]>("api/indexer/schema"),
create: (indexer: Indexer) => appClient.Post("api/indexer", indexer),
update: (indexer: Indexer) => appClient.Put("api/indexer", indexer),
delete: (id: number) => appClient.Delete(`api/indexer/${id}`),
},
irc: {
getNetworks: () => appClient.Get("api/irc"),
createNetwork: (network: Network) => appClient.Post(`api/irc`, network),
getNetworks: () => appClient.Get<IrcNetwork[]>("api/irc"),
createNetwork: (network: Network) => appClient.Post("api/irc", network),
updateNetwork: (network: Network) => appClient.Put(`api/irc/network/${network.id}`, network),
deleteNetwork: (id: number) => appClient.Delete(`api/irc/network/${id}`),
},
@ -110,9 +96,7 @@ const APIClient = {
logs: () => new EventSource(`${sseBaseUrl()}api/events?stream=logs`, { withCredentials: true })
},
release: {
find: (query?: string) => appClient.Get(`api/release${query}`),
stats: () => appClient.Get(`api/release/stats`)
find: (query?: string) => appClient.Get<ReleaseFindResponse>(`api/release${query}`),
stats: () => appClient.Get<ReleaseStats>("api/release/stats")
}
}
export default APIClient;
};