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:
@@ -13,6 +13,7 @@
|
|||||||
| `PUT /api/preferences` | `updatePreferences()` | ✅ Done |
|
| `PUT /api/preferences` | `updatePreferences()` | ✅ Done |
|
||||||
| `POST /api/komga/libraries/[libraryId]/scan` | `scanLibrary()` | ✅ Done |
|
| `POST /api/komga/libraries/[libraryId]/scan` | `scanLibrary()` | ✅ Done |
|
||||||
| `POST /api/komga/config` | `saveKomgaConfig()` | ✅ Done |
|
| `POST /api/komga/config` | `saveKomgaConfig()` | ✅ Done |
|
||||||
|
| `POST /api/komga/test` | `testKomgaConnection()` | ✅ Done |
|
||||||
| `PUT /api/user/password` | `changePassword()` | ✅ Done |
|
| `PUT /api/user/password` | `changePassword()` | ✅ Done |
|
||||||
| `POST /api/auth/register` | `registerUser()` | ✅ Done |
|
| `POST /api/auth/register` | `registerUser()` | ✅ Done |
|
||||||
| `PATCH /api/admin/users/[userId]` | `updateUserRoles()` | ✅ Done |
|
| `PATCH /api/admin/users/[userId]` | `updateUserRoles()` | ✅ Done |
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
|
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
import { ConfigDBService } from "@/lib/services/config-db.service";
|
import { ConfigDBService } from "@/lib/services/config-db.service";
|
||||||
|
import { TestService } from "@/lib/services/test.service";
|
||||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||||
import { AppError } from "@/utils/errors";
|
import { AppError } from "@/utils/errors";
|
||||||
import type { KomgaConfig, KomgaConfigData } from "@/types/komga";
|
import type { KomgaConfig, KomgaConfigData, KomgaLibrary } from "@/types/komga";
|
||||||
|
|
||||||
interface SaveConfigInput {
|
interface SaveConfigInput {
|
||||||
url: string;
|
url: string;
|
||||||
@@ -13,6 +14,34 @@ interface SaveConfigInput {
|
|||||||
authHeader?: string;
|
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
|
* Sauvegarde la configuration Komga
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { TestService } from "@/lib/services/test.service";
|
|
||||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
|
||||||
import { getErrorMessage } from "@/utils/errors";
|
|
||||||
import type { KomgaLibrary } from "@/types/komga";
|
|
||||||
import type { NextRequest } from "next/server";
|
|
||||||
import logger from "@/lib/logger";
|
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
|
||||||
try {
|
|
||||||
const { serverUrl, username, password } = await request.json();
|
|
||||||
const authHeader = Buffer.from(`${username}:${password}`).toString("base64");
|
|
||||||
|
|
||||||
const { libraries }: { libraries: KomgaLibrary[] } = await TestService.testConnection({
|
|
||||||
serverUrl,
|
|
||||||
authHeader,
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
message: `✅ Connexion réussie ! ${libraries.length} bibliothèque${
|
|
||||||
libraries.length > 1 ? "s" : ""
|
|
||||||
} trouvée${libraries.length > 1 ? "s" : ""}`,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error({ err: error }, "Erreur lors du test de connexion:");
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: ERROR_CODES.KOMGA.CONNECTION_ERROR,
|
|
||||||
name: "Connection error",
|
|
||||||
message: getErrorMessage(ERROR_CODES.KOMGA.CONNECTION_ERROR),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,7 @@ import { Network, Loader2 } from "lucide-react";
|
|||||||
import type { KomgaConfig } from "@/types/komga";
|
import type { KomgaConfig } from "@/types/komga";
|
||||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card";
|
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card";
|
||||||
import logger from "@/lib/logger";
|
import logger from "@/lib/logger";
|
||||||
import { saveKomgaConfig } from "@/app/actions/config";
|
import { saveKomgaConfig, testKomgaConnection } from "@/app/actions/config";
|
||||||
|
|
||||||
interface KomgaSettingsProps {
|
interface KomgaSettingsProps {
|
||||||
initialConfig: KomgaConfig | null;
|
initialConfig: KomgaConfig | null;
|
||||||
@@ -41,21 +41,10 @@ export function KomgaSettings({ initialConfig }: KomgaSettingsProps) {
|
|||||||
const password = formData.get("password") as string;
|
const password = formData.get("password") as string;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/komga/test", {
|
const result = await testKomgaConnection(serverUrl.trim(), username, password || config.password);
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
serverUrl: serverUrl.trim(),
|
|
||||||
username,
|
|
||||||
password: password || config.password,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!result.success) {
|
||||||
const data = await response.json();
|
throw new Error(result.message);
|
||||||
throw new Error(data.error || t("settings.komga.error.message"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
|
|||||||
Reference in New Issue
Block a user