nfp_sites/nfp_moe/app/api/pagination.js

82 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-10-01 11:35:00 +00:00
const common = require('./common')
2019-09-14 19:03:38 +00:00
2022-04-05 14:43:23 +00:00
function hasRel(x) {
return x && x.rel;
}
function intoRels (acc, x) {
function splitRel (rel) {
acc[rel] = xtend(x, { rel: rel });
}
x.rel.split(/\s+/).forEach(splitRel);
return acc;
}
function createObjects (acc, p) {
// rel="next" => 1: rel 2: next
var m = p.match(/\s*(.+)\s*=\s*"?([^"]+)"?/)
if (m) acc[m[1]] = m[2];
return acc;
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
function extend() {
var target = {}
for (var i = 0; i < arguments.length; i++) {
var source = arguments[i]
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
target[key] = source[key]
}
}
}
return target
}
function parseLink(link) {
try {
var m = link.match(/<?([^>]*)>(.*)/)
, linkUrl = m[1]
, parts = m[2].split(';')
, qry = new URL(linkUrl).searchParams;
parts.shift();
var info = parts
.reduce(createObjects, {});
info = extend(qry, info);
info.url = linkUrl;
return info;
} catch (e) {
return null;
}
}
function parse(linkHeader) {
return linkHeader.split(/,\s*</)
.map(parseLink)
.filter(hasRel)
.reduce(intoRels, {});
}
2019-09-14 19:03:38 +00:00
exports.fetchPage = function(url) {
2019-10-01 11:35:00 +00:00
return common.sendRequest({
2019-09-14 19:03:38 +00:00
method: 'GET',
url: url,
}, true)
2019-10-01 11:35:00 +00:00
.then(function(result) {
2019-09-14 19:03:38 +00:00
return {
data: result.data,
links: parse(result.headers.link || ''),
total: Number(result.headers.pagination_total || '0'),
}
})
}