Pagination
Some API endpoints will return paginated results. When results are paginated, the server will return 3 headers related to pagination:
X-Per-Page: number of results return per pageX-Page: Id of the current pageX-Total: total number of results availableLink: URLs helpers for navigation
Using the Link Header
The Link header may contain up to 4 different URLs:
<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=1>; rel="first",
<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=12>; rel="last",
<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=6>; rel="next",
<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=4>; rel="prev"rel="first": first page of the resultsrel="last": last page of the resultsrel="next": next page of resultsrel="prev": previous page of results
To iterate on results, you simply need to parse the Link header to find the rel="next" link. If the link doesn't exist, then it means that there are no more pages to display.
Example
Using JS
import parse from 'parse-link-header';
const getNextPage = (response) => {
if (response && response.headers && response.headers.link) {
const links = response.headers.link;
const parsed = parse(links);
if (parsed && parsed.next) {
return parsed.next.page;
}
}
return null;
};Last updated