4096b3d87b
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
112 lines
3.0 KiB
TypeScript
112 lines
3.0 KiB
TypeScript
import "server-only";
|
|
import { Query } from "appwrite";
|
|
import { publicDB, DATABASE_ID, TABLES } from "@/lib/appwrite-server";
|
|
import type {
|
|
BlogPostRow,
|
|
ContactMessageRow,
|
|
ProjectRow,
|
|
ServiceRow,
|
|
SeoPageRow,
|
|
SeoSettingsRow,
|
|
TestimonialRow,
|
|
} from "@/lib/types";
|
|
|
|
async function safeList<T>(tableId: string, queries: string[]): Promise<T[]> {
|
|
try {
|
|
const res = await publicDB.listRows({
|
|
databaseId: DATABASE_ID,
|
|
tableId,
|
|
queries,
|
|
});
|
|
return res.rows as T[];
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function listServices(opts?: { featured?: boolean }) {
|
|
const q = [Query.orderAsc("order"), Query.limit(50)];
|
|
if (opts?.featured) q.unshift(Query.equal("featured", true));
|
|
return safeList<ServiceRow>(TABLES.services, q);
|
|
}
|
|
|
|
export async function listProjects(opts?: { featured?: boolean; limit?: number }) {
|
|
const q = [Query.orderDesc("year"), Query.limit(opts?.limit ?? 50)];
|
|
if (opts?.featured) q.unshift(Query.equal("featured", true));
|
|
return safeList<ProjectRow>(TABLES.projects, q);
|
|
}
|
|
|
|
export async function listPublishedPosts(opts?: { limit?: number }) {
|
|
return safeList<BlogPostRow>(TABLES.blogPosts, [
|
|
Query.equal("status", "published"),
|
|
Query.orderDesc("published_at"),
|
|
Query.limit(opts?.limit ?? 50),
|
|
]);
|
|
}
|
|
|
|
export async function listAllPosts() {
|
|
return safeList<BlogPostRow>(TABLES.blogPosts, [
|
|
Query.orderDesc("$createdAt"),
|
|
Query.limit(200),
|
|
]);
|
|
}
|
|
|
|
export async function getPostBySlug(slug: string): Promise<BlogPostRow | null> {
|
|
const res = await safeList<BlogPostRow>(TABLES.blogPosts, [
|
|
Query.equal("slug", slug),
|
|
Query.limit(1),
|
|
]);
|
|
return res[0] ?? null;
|
|
}
|
|
|
|
export async function listTestimonials(opts?: { featured?: boolean }) {
|
|
const q = [Query.orderAsc("order"), Query.limit(50)];
|
|
if (opts?.featured) q.unshift(Query.equal("featured", true));
|
|
return safeList<TestimonialRow>(TABLES.testimonials, q);
|
|
}
|
|
|
|
export async function listMessages(status?: ContactMessageRow["status"]) {
|
|
const q = [Query.orderDesc("$createdAt"), Query.limit(200)];
|
|
if (status) q.unshift(Query.equal("status", status));
|
|
return safeList<ContactMessageRow>(TABLES.contactMessages, q);
|
|
}
|
|
|
|
export async function getSeoPage(path: string): Promise<SeoPageRow | null> {
|
|
const res = await safeList<SeoPageRow>(TABLES.seoPages, [
|
|
Query.equal("path", path),
|
|
Query.limit(1),
|
|
]);
|
|
return res[0] ?? null;
|
|
}
|
|
|
|
export async function listSeoPages() {
|
|
return safeList<SeoPageRow>(TABLES.seoPages, [
|
|
Query.orderAsc("path"),
|
|
Query.limit(200),
|
|
]);
|
|
}
|
|
|
|
export async function getSeoSettings(): Promise<SeoSettingsRow | null> {
|
|
try {
|
|
return (await publicDB.getRow({
|
|
databaseId: DATABASE_ID,
|
|
tableId: TABLES.seoSettings,
|
|
rowId: "global",
|
|
})) as unknown as SeoSettingsRow;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getRow<T>(tableId: string, rowId: string): Promise<T | null> {
|
|
try {
|
|
return (await publicDB.getRow({
|
|
databaseId: DATABASE_ID,
|
|
tableId,
|
|
rowId,
|
|
})) as unknown as T;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|