Skip to content
Discussion options

You must be logged in to vote

ky returns standard Response objects, so you can access headers directly:

const response = await ky.get('https://api.example.com/items');

// read the X-Pagination header
const paginationHeader = response.headers.get('X-Pagination');
const pagination = JSON.parse(paginationHeader);

console.log(pagination.totalRecords);
console.log(pagination.pageNumber);
console.log(pagination.totalPages);

// get the body
const data = await response.json();

or combine it in one call:

async function fetchWithPagination(url) {
	const response = await ky.get(url);
	
	return {
		data: await response.json(),
		pagination: JSON.parse(response.headers.get('X-Pagination'))
	};
}

// usage
const {data, pagination} 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants