From 0ee47c872b781e8f80c385f668bb982fd3244e85 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Tue, 15 Aug 2023 14:39:45 +0200 Subject: [PATCH] Introduce "BaseClient" --- .eslintrc.js | 2 ++ src/lib/requests/client/BaseClient.ts | 32 +++++++++++++++++++++++++++ src/lib/requests/client/RestClient.ts | 21 ++++++------------ 3 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 src/lib/requests/client/BaseClient.ts diff --git a/.eslintrc.js b/.eslintrc.js index 8399a281..49093f35 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -27,6 +27,8 @@ module.exports = { 'prettier/prettier': 'error', + 'class-methods-use-this': 'off', + 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }], // just why diff --git a/src/lib/requests/client/BaseClient.ts b/src/lib/requests/client/BaseClient.ts new file mode 100644 index 00000000..8fee7e3f --- /dev/null +++ b/src/lib/requests/client/BaseClient.ts @@ -0,0 +1,32 @@ +/* + * 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 storage from '@/util/localStorage.tsx'; + +// eslint-disable-next-line import/prefer-default-export +export abstract class BaseClient { + protected client!: Client; + + public abstract readonly fetcher: Fetcher; + + constructor() { + this.createClient(); + } + + public getBaseUrl(): string { + const { hostname, port, protocol } = window.location; + + // if port is 3000 it's probably running from webpack development server + const inferredPort = port === '3000' ? '4567' : port; + return storage.getItem('serverBaseURL', `${protocol}//${hostname}:${inferredPort}`); + } + + protected abstract createClient(): void; + + public abstract updateConfig(config: Partial): void; +} diff --git a/src/lib/requests/client/RestClient.ts b/src/lib/requests/client/RestClient.ts index ea6b2512..a21adef1 100644 --- a/src/lib/requests/client/RestClient.ts +++ b/src/lib/requests/client/RestClient.ts @@ -7,7 +7,7 @@ */ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; -import storage from '@/util/localStorage.tsx'; +import { BaseClient } from '@/lib/requests/client/BaseClient.ts'; export enum HttpMethod { GET = 'get', @@ -28,13 +28,10 @@ export interface IRestClient { patch>(url: string, data?: any): Promise; } -export class RestClient implements IRestClient { - protected client!: AxiosInstance; - - constructor() { - this.createClient(); - } - +export class RestClient + extends BaseClient(url: string, data: any) => Promise> + implements IRestClient +{ public readonly fetcher = async ( url: string, { @@ -75,12 +72,8 @@ export class RestClient implements IRestClient { return result.data; }; - private createClient(): void { - const { hostname, port, protocol } = window.location; - - // if port is 3000 it's probably running from webpack development server - const inferredPort = port === '3000' ? '4567' : port; - const baseURL = storage.getItem('serverBaseURL', `${protocol}//${hostname}:${inferredPort}`); + protected override createClient(): void { + const baseURL = this.getBaseUrl(); this.client = axios.create({ // baseURL must not have trailing slash