refactor: convert Komga config to Server Action
- Add src/app/actions/config.ts with saveKomgaConfig - Update KomgaSettings to use Server Action - Remove POST from api/komga/config route (keep GET)
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
| `DELETE /api/komga/favorites` | `removeFromFavorites()` | ✅ Done |
|
| `DELETE /api/komga/favorites` | `removeFromFavorites()` | ✅ Done |
|
||||||
| `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 |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
45
src/app/actions/config.ts
Normal file
45
src/app/actions/config.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
import { ConfigDBService } from "@/lib/services/config-db.service";
|
||||||
|
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||||
|
import { AppError } from "@/utils/errors";
|
||||||
|
import type { KomgaConfig, KomgaConfigData } from "@/types/komga";
|
||||||
|
|
||||||
|
interface SaveConfigInput {
|
||||||
|
url: string;
|
||||||
|
username: string;
|
||||||
|
password?: string;
|
||||||
|
authHeader?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sauvegarde la configuration Komga
|
||||||
|
*/
|
||||||
|
export async function saveKomgaConfig(
|
||||||
|
config: SaveConfigInput
|
||||||
|
): Promise<{ success: boolean; message: string; data?: KomgaConfig }> {
|
||||||
|
try {
|
||||||
|
const configData: KomgaConfigData = {
|
||||||
|
url: config.url,
|
||||||
|
username: config.username,
|
||||||
|
password: config.password,
|
||||||
|
authHeader: config.authHeader || "",
|
||||||
|
};
|
||||||
|
const mongoConfig = await ConfigDBService.saveConfig(configData);
|
||||||
|
|
||||||
|
// Invalider le cache
|
||||||
|
revalidatePath("/settings");
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "Configuration sauvegardée",
|
||||||
|
data: mongoConfig,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof AppError) {
|
||||||
|
return { success: false, message: error.message };
|
||||||
|
}
|
||||||
|
return { success: false, message: "Erreur lors de la sauvegarde" };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,49 +1,13 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { ConfigDBService } from "@/lib/services/config-db.service";
|
import { ConfigDBService } from "@/lib/services/config-db.service";
|
||||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||||
import type { KomgaConfig, KomgaConfigData } from "@/types/komga";
|
import type { KomgaConfig } from "@/types/komga";
|
||||||
import { getErrorMessage } from "@/utils/errors";
|
import { getErrorMessage } from "@/utils/errors";
|
||||||
import type { NextRequest } from "next/server";
|
|
||||||
import logger from "@/lib/logger";
|
import logger from "@/lib/logger";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
// GET reste utilisé pour récupérer la config
|
||||||
try {
|
|
||||||
const data: KomgaConfigData = await request.json();
|
|
||||||
const mongoConfig: KomgaConfig = await ConfigDBService.saveConfig(data);
|
|
||||||
|
|
||||||
return NextResponse.json(
|
|
||||||
{ message: "⚙️ Configuration sauvegardée avec succès", mongoConfig },
|
|
||||||
{ status: 200 }
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error({ err: error }, "Erreur lors de la sauvegarde de la configuration:");
|
|
||||||
if (error instanceof Error && error.message === "Utilisateur non authentifié") {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED,
|
|
||||||
name: "Unauthorized",
|
|
||||||
message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: ERROR_CODES.CONFIG.SAVE_ERROR,
|
|
||||||
name: "Config save error",
|
|
||||||
message: getErrorMessage(ERROR_CODES.CONFIG.SAVE_ERROR),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const mongoConfig: KomgaConfig | null = await ConfigDBService.getConfig();
|
const mongoConfig: KomgaConfig | null = await ConfigDBService.getConfig();
|
||||||
|
|||||||
@@ -7,6 +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";
|
||||||
|
|
||||||
interface KomgaSettingsProps {
|
interface KomgaSettingsProps {
|
||||||
initialConfig: KomgaConfig | null;
|
initialConfig: KomgaConfig | null;
|
||||||
@@ -90,31 +91,22 @@ export function KomgaSettings({ initialConfig }: KomgaSettingsProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/komga/config", {
|
const result = await saveKomgaConfig({
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
url: newConfig.serverUrl,
|
url: newConfig.serverUrl,
|
||||||
username: newConfig.username,
|
username: newConfig.username,
|
||||||
password: newConfig.password,
|
password: newConfig.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"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const savedConfig = await response.json();
|
|
||||||
|
|
||||||
setConfig(newConfig);
|
setConfig(newConfig);
|
||||||
setLocalInitialConfig({
|
setLocalInitialConfig({
|
||||||
url: newConfig.serverUrl,
|
url: newConfig.serverUrl,
|
||||||
username: newConfig.username,
|
username: newConfig.username,
|
||||||
userId: savedConfig.userId,
|
userId: result.data?.userId || 0,
|
||||||
authHeader: savedConfig.authHeader,
|
authHeader: result.data?.authHeader || "",
|
||||||
});
|
});
|
||||||
setIsEditingConfig(false);
|
setIsEditingConfig(false);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user