SendRegning

Pagination


Some of the rest calls that return a list, support pagination.

Pagination in the SendRegning API is achieved using query params.

  • perPage - number of elements on each page. By just sending this, you’ll automatically get the first page.
  • page - the page you request.

Pagination metadata in response header

When you start to use the pagination, you’ll receive metadata for navigation in the Link header. There are 4 things you may get in the header.

  • prev - Holds the link of it’s predecessor if it exists.
  • next - Holds the link of it’s successor if it exists.
  • first - Holds the link of the first page.
  • last - Holds the link of the last page.

Here is an example of a Link header containing all of the links above.

1
2
3
4
5
Link: </host/events/past?perPage=1&page=1>;
  rel="prev",</host/events/past?perPage=1&page=3>;
  rel="next",</host/events/past?perPage=1&page=1>;
  rel="first",</host/events/past?perPage=1&page=4>;
  rel="last"

JavaScript example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * Parse a Link header
 *
 * Link:<https://example.org/.meta>; rel=meta
 *
 * var r = parseLinkHeader(xhr.getResponseHeader('Link'));
 * r['meta']['href'] outputs https://example.org/.meta
 */
function unquote(value) {
  'use strict';

  return value.charAt(0) === '"' && value.charAt(value.length - 1) === '"' ? value.substring(1, value.length - 1) : value;
}

function parseLinkHeader(header) {
  'use strict';

  var linkexp = /<[^>]*>\s*(\s*;\s*[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g;
  var paramexp = /[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g;
  var matches = header.match(linkexp);
  var rels = {};

  for (var i = 0; i < matches.length; i++) {
    var split = matches[i].split('>');
    var href = split[0].substring(1);
    var ps = split[1];
    var link = {};
    link.href = href;
    var s = ps.match(paramexp);

    for (var j = 0; j < s.length; j++) {
      var p = s[j];
      var paramsplit = p.split('=');
      var name = paramsplit[0];
      link[name] = unquote(paramsplit[1]);
    }

    if (link.rel !== undefined) {
      rels[link.rel] = link;
    }
  }

  return rels;
}