2023-08-15 14:39:45 +02:00
|
|
|
/*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-10-05 23:22:42 +02:00
|
|
|
import { AppStorage } from '@/lib/storage/AppStorage.ts';
|
2025-07-27 00:03:51 +02:00
|
|
|
import { UserRefreshMutation } from '@/lib/graphql/generated/graphql.ts';
|
|
|
|
|
import { AuthManager } from '@/features/authentication/AuthManager.ts';
|
|
|
|
|
import { AbortableApolloMutationResponse } from '@/lib/requests/RequestManager.ts';
|
2025-09-24 23:13:29 +02:00
|
|
|
import { SubpathUtil } from '@/lib/utils/SubpathUtil.ts';
|
2025-10-15 02:50:43 +02:00
|
|
|
import { ControlledPromise } from '@/lib/ControlledPromise.ts';
|
|
|
|
|
|
|
|
|
|
interface QueuedRequest {
|
|
|
|
|
execute: () => void;
|
|
|
|
|
resolve: (value: any) => void;
|
|
|
|
|
reject: (error: any) => void;
|
|
|
|
|
}
|
2023-08-15 14:39:45 +02:00
|
|
|
|
|
|
|
|
export abstract class BaseClient<Client, ClientConfig, Fetcher> {
|
2024-08-25 22:34:01 +02:00
|
|
|
protected abstract client: Client;
|
2023-08-15 14:39:45 +02:00
|
|
|
|
|
|
|
|
public abstract readonly fetcher: Fetcher;
|
|
|
|
|
|
2025-07-27 00:03:51 +02:00
|
|
|
private static activeTokenRefreshPromise: Promise<UserRefreshMutation | null | undefined> | null = null;
|
|
|
|
|
|
2025-10-15 02:50:43 +02:00
|
|
|
private static onTokenRefreshComplete: (() => void) | null = null;
|
|
|
|
|
|
|
|
|
|
protected requestQueue: QueuedRequest[] = [];
|
|
|
|
|
|
|
|
|
|
public static setTokenRefreshCompleteCallback(callback: (() => void) | null): void {
|
|
|
|
|
BaseClient.onTokenRefreshComplete = callback;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-27 00:03:51 +02:00
|
|
|
protected static async refreshAccessToken(
|
|
|
|
|
refreshFn: (refreshToken: string) => AbortableApolloMutationResponse<UserRefreshMutation>,
|
|
|
|
|
): Promise<UserRefreshMutation | null | undefined> {
|
|
|
|
|
const refreshToken = AuthManager.getRefreshToken();
|
|
|
|
|
|
|
|
|
|
if (!AuthManager.isAuthRequired()) {
|
|
|
|
|
AuthManager.setAuthRequired(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!refreshToken) {
|
|
|
|
|
throw new Error('No refresh token found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.activeTokenRefreshPromise) {
|
|
|
|
|
return this.activeTokenRefreshPromise;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 02:50:43 +02:00
|
|
|
AuthManager.setIsRefreshingToken(true);
|
|
|
|
|
|
2025-07-27 00:03:51 +02:00
|
|
|
const refreshRequest = refreshFn(refreshToken).response;
|
|
|
|
|
this.activeTokenRefreshPromise = refreshRequest.then((result) => result.data);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const result = await refreshRequest;
|
|
|
|
|
const { data } = result;
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
|
throw new Error('No refreshed access token returned');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AuthManager.setAccessToken(data.refreshToken.accessToken);
|
2025-10-15 02:50:43 +02:00
|
|
|
AuthManager.setAuthInitialized(true);
|
|
|
|
|
|
|
|
|
|
BaseClient.onTokenRefreshComplete?.();
|
2025-07-27 00:03:51 +02:00
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
AuthManager.removeTokens();
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
this.activeTokenRefreshPromise = null;
|
2025-10-15 02:50:43 +02:00
|
|
|
AuthManager.setIsRefreshingToken(false);
|
2025-07-27 00:03:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected constructor(
|
|
|
|
|
protected handleRefreshToken: (refreshToken: string) => AbortableApolloMutationResponse<UserRefreshMutation>,
|
|
|
|
|
) {}
|
|
|
|
|
|
2023-08-15 14:39:45 +02:00
|
|
|
public getBaseUrl(): string {
|
2025-08-13 11:03:04 +02:00
|
|
|
const { hostname, port, protocol } = window.location;
|
|
|
|
|
|
|
|
|
|
const defaultUrl = import.meta.env.DEV
|
|
|
|
|
? import.meta.env.VITE_SERVER_URL_DEFAULT
|
|
|
|
|
: `${protocol}//${hostname}:${port}`;
|
|
|
|
|
|
2025-09-24 00:23:19 +03:00
|
|
|
const serverBaseURL = AppStorage.local.getItemParsed('serverBaseURL', defaultUrl);
|
|
|
|
|
|
|
|
|
|
// Apply subpath configuration to the base URL
|
2025-09-24 23:13:29 +02:00
|
|
|
return SubpathUtil.getApiBaseUrl(serverBaseURL);
|
2023-08-15 14:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-15 02:50:43 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
protected shouldQueueRequest(operationName?: string): boolean {
|
|
|
|
|
return AuthManager.shouldQueueRequests();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected enqueueRequest<T>(executor: () => Promise<T>, operationName?: string): Promise<T> {
|
|
|
|
|
if (!this.shouldQueueRequest(operationName)) {
|
|
|
|
|
return executor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { promise: requestPromise, reject, resolve } = new ControlledPromise<T>();
|
|
|
|
|
this.requestQueue.push({
|
|
|
|
|
execute: () => {
|
|
|
|
|
executor().then(resolve).catch(reject);
|
|
|
|
|
},
|
|
|
|
|
resolve,
|
|
|
|
|
reject,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return requestPromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public processQueue(): void {
|
|
|
|
|
const queue = [...this.requestQueue];
|
|
|
|
|
this.requestQueue = [];
|
|
|
|
|
|
|
|
|
|
queue.forEach((request) => {
|
|
|
|
|
request.execute();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected clearQueue(error?: Error): void {
|
|
|
|
|
const queue = [...this.requestQueue];
|
|
|
|
|
this.requestQueue = [];
|
|
|
|
|
|
|
|
|
|
queue.forEach((request) => {
|
|
|
|
|
request.reject(error ?? new Error('Request queue cleared'));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 14:39:45 +02:00
|
|
|
public abstract updateConfig(config: Partial<ClientConfig>): void;
|
|
|
|
|
}
|