JAVASCRIPT
Navigate Paginated API Responses (Link Header)
Learn to effectively consume and navigate paginated API responses by parsing the 'Link' HTTP header for next, prev, first, and last URLs in JavaScript.
function parseLinkHeader(header) {
const links = {};
if (header) {
header.split(',').forEach(part => {
const section = part.split(';');
if (section.length === 2) {
const url = section[0].replace(/<(.*)>/, '$1').trim();
const name = section[1].replace(/rel="(.*)"/, '$1').trim();
links[name] = url;
}
});
}
return links;
}
async function fetchPaginatedData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const linkHeader = response.headers.get('Link');
const paginationLinks = parseLinkHeader(linkHeader);
console.log('Fetched data:', data);
console.log('Pagination links:', paginationLinks);
return { data, paginationLinks };
} catch (error) {
console.error('Error fetching paginated data:', error);
throw error;
}
}
// Example usage:
// async function browsePages() {
// let currentPageUrl = 'https://api.example.com/items?page=1&per_page=10';
// while (currentPageUrl) {
// const { data, paginationLinks } = await fetchPaginatedData(currentPageUrl);
// // Process data for the current page
// console.log('Current page data count:', data.length);
// if (paginationLinks.next) {
// currentPageUrl = paginationLinks.next;
// console.log('Moving to next page:', currentPageUrl);
// } else {
// currentPageUrl = null; // No more pages
// }
// // Optional: Add a delay to avoid rate limiting
// // await new Promise(resolve => setTimeout(resolve, 500));
// }
// console.log('Finished browsing all pages.');
// }
// browsePages();
How it works: This JavaScript snippet demonstrates how to consume an API that uses the `Link` HTTP header for pagination. The `parseLinkHeader` function extracts 'next', 'prev', 'first', and 'last' URLs from the header string, which are commonly provided by RESTful APIs. The `fetchPaginatedData` function then fetches data for a given page and returns both the data and these parsed pagination links. This allows developers to easily build navigation controls or programmatically iterate through all pages of a paginated resource.