Files
goGin/frontend/services/heroService.ts
Beyhan Oğur 2a5b661443 first commit
2026-04-26 21:46:42 +03:00

105 lines
3.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { getSession } from "next-auth/react"
import { HeroResponse, HeroDetailResponse } from "@/types/hero"
const API_URL = (process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080") + "/api/v1"
async function getAuthHeaders() {
const session = await getSession()
return {
Authorization: `Bearer ${session?.accessToken}`,
}
}
export const heroService = {
getHeroes: async (
page: number = 1,
perPage: number = 20,
search: string = "",
soft: string = "with"
): Promise<HeroResponse> => {
const headers = await getAuthHeaders()
const params = new URLSearchParams({
page: page.toString(),
limit: perPage.toString(),
soft: soft,
})
if (search) {
params.append("search", search)
}
const res = await fetch(`${API_URL}/admin/heroes?${params}`, {
headers: headers as HeadersInit,
})
if (!res.ok) {
const errorText = await res.text()
console.error("Hero list warning:", res.status, errorText)
throw new Error(`Hero listesi alınamadı: ${res.status} ${res.statusText}`)
}
return res.json()
},
getHeroById: async (id: number): Promise<HeroDetailResponse> => {
const headers = await getAuthHeaders()
const res = await fetch(`${API_URL}/admin/heroes/${id}`, {
headers: headers as HeadersInit,
})
if (!res.ok) throw new Error("Hero detayı alınamadı")
return res.json()
},
createHero: async (formData: FormData): Promise<HeroDetailResponse> => {
// No auth header needed here because the Next.js API route handles it (it's a same-origin request)
// However, if we need to pass the session token from client to server action/route,
// standard fetch from client automatically includes cookies for NextAuth.
const res = await fetch("/api/admin/heroes", {
method: "POST",
body: formData,
})
if (!res.ok) {
const errorData = await res.json()
throw new Error(errorData.error || "Hero oluşturulamadı")
}
return res.json()
},
updateHero: async (id: number, formData: FormData): Promise<HeroDetailResponse> => {
const res = await fetch(`/api/admin/heroes/${id}`, {
method: "PUT",
body: formData,
})
if (!res.ok) {
const errorData = await res.json()
throw new Error(errorData.error || "Hero güncellenemedi")
}
return res.json()
},
deleteHero: async (id: number): Promise<void> => {
const headers = await getAuthHeaders()
const res = await fetch(`${API_URL}/admin/heroes/${id}`, {
method: "DELETE",
headers: headers as HeadersInit,
})
if (!res.ok) throw new Error("Hero silinemedi")
},
restoreHero: async (id: number): Promise<HeroDetailResponse> => {
const headers = await getAuthHeaders()
const res = await fetch(`${API_URL}/admin/heroes/${id}/restore`, {
method: "POST", // veya dökümantasyona göre PUT/PATCH
headers: headers as HeadersInit,
body: JSON.stringify({}),
})
if (!res.ok) throw new Error("Hero geri yüklenemedi")
return res.json()
},
}