Fetch Paginated Posts and Users
🧩 Syntax:
const API_BASE_URL = 'http://localhost:3004';
export const fetchPost = async (postId: string) => {
const response = await fetch(`${API_BASE_URL}/posts/${postId}`);
if (!response.ok) throw new Error('Failed to fetch post');
return response.json();
};
// Fetch multiple posts with pagination
export const fetchPosts = async (page: number, limit: number = 10) => {
const response = await fetch(`${API_BASE_URL}/posts?_page=${page}&_limit=${limit}&_sort=created_at&_order=desc`);
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
};
// Fetch a user by id
export const fetchUser = async (userId: string) => {
const response = await fetch(`${API_BASE_URL}/users/${userId}`);
if (!response.ok) throw new Error('Failed to fetch user');
return response.json();
};