Files
next-dj/lib/blogApi.ts
Beyhan Oğur e881f38e4e first commit
2026-04-26 22:12:36 +03:00

53 lines
1.4 KiB
TypeScript

import { PostType, Categorie, PaginatedResponse } from "@/Type/post";
const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || "http://127.0.0.1:8000/api/v1";
export async function getBlogPosts(page: number = 1): Promise<PaginatedResponse<PostType>> {
try {
const response = await fetch(`${API_BASE_URL}/blog/post/?page=${page}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
cache: "no-store", // Always fetch fresh data
});
if (!response.ok) {
throw new Error(`Failed to fetch blog posts: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error("Error fetching blog posts:", error);
throw error;
}
}
export async function getBlogCategories(): Promise<Categorie[]> {
try {
const response = await fetch(`${API_BASE_URL}/blog/categories/`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
cache: "no-store", // Always fetch fresh data
});
if (!response.ok) {
throw new Error(`Failed to fetch categories: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error("Error fetching categories:", error);
throw error;
}
}
export function extractPageNumber(url: string | null): number | null {
if (!url) return null;
const match = url.match(/[?&]page=(\d+)/);
return match ? parseInt(match[1], 10) : null;
}