fix: Node 26 uyumsuzluğunu çöz — node-appwrite -> appwrite SDK
Sorun:
- node-appwrite paketi 'node-fetch-native-with-agent' polyfill'i kullanıyor
- Node.js 26'nın undici implementation'ı ile uyumsuz
- 'fetch failed / InvalidArgumentError: invalid onError method' hatası
- Login dahil tüm Appwrite çağrıları başarısız
Çözüm:
- Tüm node-appwrite kullanımını browser SDK 'appwrite'a geçir
- Browser SDK native fetch kullanıyor, Node 26 uyumlu
- API key tabanlı admin client yerine session cookie tabanlı user client
- Public reads (read('any')): publicDB (auth'suz client)
- Admin CRUD: userDB(sessionSecret) (cookie'deki session)
- Storage upload doğrudan File objesi alıyor (InputFile.fromBuffer gerekmez)
Etkilenen dosyalar:
- lib/appwrite-server.ts: publicClient + sessionClient
- lib/auth.ts: requireSessionSecret eklendi
- lib/admin-actions.ts: tüm action'lar sessionClient kullanıyor
- app/actions.ts: publicDB
- lib/data.ts: publicDB
- app/admin/login/actions.ts: appwrite SDK
- app/admin/(protected)/page.tsx, medya/page.tsx: userDB/userStorage
End-to-end test edildi:
✓ Login (401 doğru hata)
✓ Public read (services)
✓ Anonim create (contact form)
✓ npm run build 23 route
This commit is contained in:
+48
-46
@@ -1,20 +1,23 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { ID } from "node-appwrite";
|
||||
import { InputFile } from "node-appwrite/file";
|
||||
import { ID } from "appwrite";
|
||||
import {
|
||||
adminDB,
|
||||
adminStorage,
|
||||
DATABASE_ID,
|
||||
MEDIA_BUCKET_ID,
|
||||
TABLES,
|
||||
userDB,
|
||||
userStorage,
|
||||
} from "@/lib/appwrite-server";
|
||||
import { getCurrentUser } from "@/lib/auth";
|
||||
import { requireSessionSecret } from "@/lib/auth";
|
||||
|
||||
async function gate() {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) throw new Error("Yetkisiz");
|
||||
async function db() {
|
||||
const secret = await requireSessionSecret();
|
||||
return userDB(secret);
|
||||
}
|
||||
async function storage() {
|
||||
const secret = await requireSessionSecret();
|
||||
return userStorage(secret);
|
||||
}
|
||||
|
||||
function slugify(s: string) {
|
||||
@@ -55,30 +58,29 @@ function strArr(v: FormDataEntryValue | null) {
|
||||
// ─── Media upload ────────────────────────────────────────────────
|
||||
|
||||
export async function uploadMedia(formData: FormData): Promise<void> {
|
||||
await gate();
|
||||
const file = formData.get("file");
|
||||
if (!(file instanceof File) || file.size === 0) {
|
||||
throw new Error("Dosya seçilmedi");
|
||||
}
|
||||
const bytes = Buffer.from(await file.arrayBuffer());
|
||||
await adminStorage.createFile({
|
||||
const s = await storage();
|
||||
await s.createFile({
|
||||
bucketId: MEDIA_BUCKET_ID,
|
||||
fileId: ID.unique(),
|
||||
file: InputFile.fromBuffer(bytes, file.name),
|
||||
file,
|
||||
});
|
||||
revalidatePath("/admin/medya");
|
||||
}
|
||||
|
||||
export async function deleteMediaFile(fileId: string) {
|
||||
await gate();
|
||||
await adminStorage.deleteFile({ bucketId: MEDIA_BUCKET_ID, fileId });
|
||||
const s = await storage();
|
||||
await s.deleteFile({ bucketId: MEDIA_BUCKET_ID, fileId });
|
||||
revalidatePath("/admin/medya");
|
||||
}
|
||||
|
||||
// ─── Blog ────────────────────────────────────────────────────────
|
||||
|
||||
export async function saveBlogPost(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = str(formData.get("id"));
|
||||
const title = str(formData.get("title"));
|
||||
if (!title) throw new Error("Başlık zorunlu");
|
||||
@@ -105,14 +107,14 @@ export async function saveBlogPost(formData: FormData) {
|
||||
};
|
||||
|
||||
if (id) {
|
||||
await adminDB.updateRow({
|
||||
await d.updateRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.blogPosts,
|
||||
rowId: id,
|
||||
data,
|
||||
});
|
||||
} else {
|
||||
await adminDB.createRow({
|
||||
await d.createRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.blogPosts,
|
||||
rowId: ID.unique(),
|
||||
@@ -125,9 +127,9 @@ export async function saveBlogPost(formData: FormData) {
|
||||
}
|
||||
|
||||
export async function deleteBlogPost(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = String(formData.get("id"));
|
||||
await adminDB.deleteRow({
|
||||
await d.deleteRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.blogPosts,
|
||||
rowId: id,
|
||||
@@ -139,7 +141,7 @@ export async function deleteBlogPost(formData: FormData) {
|
||||
// ─── Services ────────────────────────────────────────────────────
|
||||
|
||||
export async function saveService(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = str(formData.get("id"));
|
||||
const title = str(formData.get("title"));
|
||||
if (!title) throw new Error("Başlık zorunlu");
|
||||
@@ -156,14 +158,14 @@ export async function saveService(formData: FormData) {
|
||||
featured: bool(formData.get("featured")),
|
||||
};
|
||||
if (id) {
|
||||
await adminDB.updateRow({
|
||||
await d.updateRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.services,
|
||||
rowId: id,
|
||||
data,
|
||||
});
|
||||
} else {
|
||||
await adminDB.createRow({
|
||||
await d.createRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.services,
|
||||
rowId: slug,
|
||||
@@ -176,9 +178,9 @@ export async function saveService(formData: FormData) {
|
||||
}
|
||||
|
||||
export async function deleteService(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = String(formData.get("id"));
|
||||
await adminDB.deleteRow({
|
||||
await d.deleteRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.services,
|
||||
rowId: id,
|
||||
@@ -190,7 +192,7 @@ export async function deleteService(formData: FormData) {
|
||||
// ─── Projects ────────────────────────────────────────────────────
|
||||
|
||||
export async function saveProject(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = str(formData.get("id"));
|
||||
const title = str(formData.get("title"));
|
||||
if (!title) throw new Error("Başlık zorunlu");
|
||||
@@ -211,14 +213,14 @@ export async function saveProject(formData: FormData) {
|
||||
};
|
||||
|
||||
if (id) {
|
||||
await adminDB.updateRow({
|
||||
await d.updateRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.projects,
|
||||
rowId: id,
|
||||
data,
|
||||
});
|
||||
} else {
|
||||
await adminDB.createRow({
|
||||
await d.createRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.projects,
|
||||
rowId: ID.unique(),
|
||||
@@ -231,9 +233,9 @@ export async function saveProject(formData: FormData) {
|
||||
}
|
||||
|
||||
export async function deleteProject(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = String(formData.get("id"));
|
||||
await adminDB.deleteRow({
|
||||
await d.deleteRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.projects,
|
||||
rowId: id,
|
||||
@@ -245,7 +247,7 @@ export async function deleteProject(formData: FormData) {
|
||||
// ─── Testimonials ────────────────────────────────────────────────
|
||||
|
||||
export async function saveTestimonial(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = str(formData.get("id"));
|
||||
const name = str(formData.get("name"));
|
||||
if (!name) throw new Error("Ad zorunlu");
|
||||
@@ -264,14 +266,14 @@ export async function saveTestimonial(formData: FormData) {
|
||||
};
|
||||
|
||||
if (id) {
|
||||
await adminDB.updateRow({
|
||||
await d.updateRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.testimonials,
|
||||
rowId: id,
|
||||
data,
|
||||
});
|
||||
} else {
|
||||
await adminDB.createRow({
|
||||
await d.createRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.testimonials,
|
||||
rowId: ID.unique(),
|
||||
@@ -283,9 +285,9 @@ export async function saveTestimonial(formData: FormData) {
|
||||
}
|
||||
|
||||
export async function deleteTestimonial(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = String(formData.get("id"));
|
||||
await adminDB.deleteRow({
|
||||
await d.deleteRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.testimonials,
|
||||
rowId: id,
|
||||
@@ -296,7 +298,7 @@ export async function deleteTestimonial(formData: FormData) {
|
||||
// ─── SEO Settings (singleton) ────────────────────────────────────
|
||||
|
||||
export async function saveSeoSettings(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const data = {
|
||||
site_name: str(formData.get("site_name")),
|
||||
site_description: str(formData.get("site_description")),
|
||||
@@ -309,14 +311,14 @@ export async function saveSeoSettings(formData: FormData) {
|
||||
gtm_id: str(formData.get("gtm_id")),
|
||||
};
|
||||
try {
|
||||
await adminDB.updateRow({
|
||||
await d.updateRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.seoSettings,
|
||||
rowId: "global",
|
||||
data,
|
||||
});
|
||||
} catch {
|
||||
await adminDB.createRow({
|
||||
await d.createRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.seoSettings,
|
||||
rowId: "global",
|
||||
@@ -330,7 +332,7 @@ export async function saveSeoSettings(formData: FormData) {
|
||||
// ─── SEO Page (per path) ─────────────────────────────────────────
|
||||
|
||||
export async function saveSeoPage(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = str(formData.get("id"));
|
||||
const path = str(formData.get("path"));
|
||||
if (!path) throw new Error("Path zorunlu");
|
||||
@@ -345,14 +347,14 @@ export async function saveSeoPage(formData: FormData) {
|
||||
};
|
||||
|
||||
if (id) {
|
||||
await adminDB.updateRow({
|
||||
await d.updateRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.seoPages,
|
||||
rowId: id,
|
||||
data,
|
||||
});
|
||||
} else {
|
||||
await adminDB.createRow({
|
||||
await d.createRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.seoPages,
|
||||
rowId: ID.unique(),
|
||||
@@ -364,9 +366,9 @@ export async function saveSeoPage(formData: FormData) {
|
||||
}
|
||||
|
||||
export async function deleteSeoPage(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = String(formData.get("id"));
|
||||
await adminDB.deleteRow({
|
||||
await d.deleteRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.seoPages,
|
||||
rowId: id,
|
||||
@@ -377,14 +379,14 @@ export async function deleteSeoPage(formData: FormData) {
|
||||
// ─── Contact messages ────────────────────────────────────────────
|
||||
|
||||
export async function updateMessageStatus(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = String(formData.get("id"));
|
||||
const status = String(formData.get("status")) as
|
||||
| "new"
|
||||
| "read"
|
||||
| "replied"
|
||||
| "archived";
|
||||
await adminDB.updateRow({
|
||||
await d.updateRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.contactMessages,
|
||||
rowId: id,
|
||||
@@ -394,9 +396,9 @@ export async function updateMessageStatus(formData: FormData) {
|
||||
}
|
||||
|
||||
export async function deleteMessage(formData: FormData) {
|
||||
await gate();
|
||||
const d = await db();
|
||||
const id = String(formData.get("id"));
|
||||
await adminDB.deleteRow({
|
||||
await d.deleteRow({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.contactMessages,
|
||||
rowId: id,
|
||||
|
||||
Reference in New Issue
Block a user