refactor: convert Komga test connection to Server Action

- Add testKomgaConnection to config.ts
- Update KomgaSettings to use Server Action
- Remove api/komga/test route
This commit is contained in:
2026-02-28 11:09:48 +01:00
parent b40f59bec6
commit eec51b7ef8
4 changed files with 35 additions and 53 deletions

View File

@@ -2,9 +2,10 @@
import { revalidatePath } from "next/cache";
import { ConfigDBService } from "@/lib/services/config-db.service";
import { TestService } from "@/lib/services/test.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { AppError } from "@/utils/errors";
import type { KomgaConfig, KomgaConfigData } from "@/types/komga";
import type { KomgaConfig, KomgaConfigData, KomgaLibrary } from "@/types/komga";
interface SaveConfigInput {
url: string;
@@ -13,6 +14,34 @@ interface SaveConfigInput {
authHeader?: string;
}
/**
* Teste la connexion à Komga
*/
export async function testKomgaConnection(
serverUrl: string,
username: string,
password: string
): Promise<{ success: boolean; message: string }> {
try {
const authHeader = Buffer.from(`${username}:${password}`).toString("base64");
const { libraries }: { libraries: KomgaLibrary[] } = await TestService.testConnection({
serverUrl,
authHeader,
});
return {
success: true,
message: `Connexion réussie ! ${libraries.length} bibliothèque${libraries.length > 1 ? "s" : ""} trouvée${libraries.length > 1 ? "s" : ""}`,
};
} catch (error) {
if (error instanceof AppError) {
return { success: false, message: error.message };
}
return { success: false, message: "Erreur lors de la connexion" };
}
}
/**
* Sauvegarde la configuration Komga
*/