first commit
This commit is contained in:
52
lib/blogApi.ts
Normal file
52
lib/blogApi.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user