Feature/streamline backend requests (#297)
* Introduce "RequestManager"
* Use "RequestManager" - Simple replacements
- Get rid of all "util/client" imports
- replace old requests with new "RequestManager"
- remove "fetcher" from global SWR config
instead of using the SWR hooks by themselves, the "requestManager" is supposed to be used
* Use "RequestManager" - Do requests via SWR hooks
Use SWR hooks at places where it's easily usable
* Prevent trailing slashes in the "baseUrl"
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
|
||||
import storage from 'util/localStorage';
|
||||
|
||||
const { hostname, port, protocol } = window.location;
|
||||
|
||||
// if port is 3000 it's probably running from webpack devlopment server
|
||||
let inferredPort;
|
||||
if (port === '3000') {
|
||||
inferredPort = '4567';
|
||||
} else {
|
||||
inferredPort = port;
|
||||
}
|
||||
|
||||
const baseURL = storage.getItem('serverBaseURL', `${protocol}//${hostname}:${inferredPort}`);
|
||||
|
||||
const client = axios.create({
|
||||
// baseURL must not have traling slash
|
||||
baseURL,
|
||||
});
|
||||
|
||||
client.interceptors.request.use((config) => {
|
||||
if (config.data instanceof FormData) {
|
||||
Object.assign(config.headers, { 'Content-Type': 'multipart/form-data' });
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
export default client;
|
||||
|
||||
export async function fetcher<T = any>(path: string) {
|
||||
const res = await client.get(path);
|
||||
if (res.status !== 200) {
|
||||
throw new Error(res.statusText);
|
||||
}
|
||||
if (res.headers['content-type'] !== 'application/json') {
|
||||
throw new Error('Response is not json');
|
||||
}
|
||||
return res.data as T;
|
||||
}
|
||||
|
||||
export const useQuery = <Data extends any = any, Error extends any = any>(
|
||||
key: string | null,
|
||||
config?: SWRConfiguration<Data, Error>,
|
||||
): SWRResponse<Data, Error> => useSWR(key, config);
|
||||
@@ -7,7 +7,6 @@
|
||||
*/
|
||||
|
||||
import { mutate } from 'swr';
|
||||
import client from 'util/client';
|
||||
import {
|
||||
AllowedMetadataValueTypes,
|
||||
AppMetadataKeys,
|
||||
@@ -20,6 +19,7 @@ import {
|
||||
IMetadataMigration,
|
||||
MetadataKeyValuePair,
|
||||
} from 'typings';
|
||||
import requestManager from 'lib/RequestManager';
|
||||
|
||||
const APP_METADATA_KEY_PREFIX = 'webUI_';
|
||||
|
||||
@@ -314,7 +314,7 @@ export const requestUpdateMetadataValue = async (
|
||||
[metadataKey]: valueAsString,
|
||||
};
|
||||
|
||||
await client.patch(url, formData);
|
||||
await requestManager.getClient().patch(url, formData);
|
||||
await mutate(
|
||||
urlToMutate,
|
||||
{ ...metadataHolder, ...wrapMetadataWithMetaKey(wrapWithMetaKey, mutatedMetadata) },
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
*/
|
||||
|
||||
import { getMetadataFrom, requestUpdateMangaMetadata, requestUpdateServerMetadata } from 'util/metadata';
|
||||
import { useQuery } from 'util/client';
|
||||
import { IManga, Metadata, MetadataHolder, IReaderSettings, MetadataKeyValuePair } from 'typings';
|
||||
import requestManager from 'lib/RequestManager';
|
||||
|
||||
type UndefinedReaderSettings = {
|
||||
[setting in keyof IReaderSettings]: IReaderSettings[setting] | undefined;
|
||||
@@ -45,7 +45,7 @@ export const useDefaultReaderSettings = (): {
|
||||
settings: IReaderSettings;
|
||||
loading: boolean;
|
||||
} => {
|
||||
const { data: meta, isLoading } = useQuery<Metadata>('/api/v1/meta');
|
||||
const { data: meta, isLoading } = requestManager.useGetGlobalMeta();
|
||||
const settings = getReaderSettingsWithDefaultValueFallback<IReaderSettings>(meta);
|
||||
|
||||
return { metadata: meta, settings, loading: isLoading };
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { useQuery } from 'util/client';
|
||||
import { getMetadataFrom } from 'util/metadata';
|
||||
import { Metadata, ISearchSettings } from 'typings';
|
||||
import requestManager from 'lib/RequestManager';
|
||||
|
||||
export const getDefaultSettings = (): ISearchSettings => ({
|
||||
ignoreFilters: false,
|
||||
@@ -24,7 +24,7 @@ export const useSearchSettings = (): {
|
||||
settings: ISearchSettings;
|
||||
loading: boolean;
|
||||
} => {
|
||||
const { data: meta, isLoading } = useQuery<Metadata>('/api/v1/meta');
|
||||
const { data: meta, isLoading } = requestManager.useGetGlobalMeta();
|
||||
const settings = getSearchSettingsWithDefaultValueFallback(meta);
|
||||
|
||||
return { metadata: meta, settings, loading: isLoading };
|
||||
|
||||
Reference in New Issue
Block a user