app2-api/middlewares/paginate.js
2019-08-05 19:44:16 +02:00

126 lines
3.9 KiB
JavaScript

'use strict';
// Node.js pagination middleware and view helpers.
// * Author: [@niftylettuce](https://twitter.com/#!/niftylettuce)
// * Source: <https://github.com/niftylettuce/middleware paginate>
const qs = require('qs');
const url = require('url');
const _ = require('lodash');
//const util = require('util');
const href = (req) => {
return function (prev, params) {
const query = _.clone(req.query);
if (typeof prev === 'object') {
params = prev;
prev = false;
} else {
prev = (typeof prev === 'boolean') ? prev : false;
query.page = parseInt(query.page, 10);
query.page = prev ? query.page -= 1 : query.page += 1;
query.page = (query.page < 1) ? 1 : query.page;
}
// allow overriding querystring params
// (useful for sorting and filtering)
// another alias for `_.assign` is `_.extend`
if (_.isObject(params))
query = _.assign(query, params);
return url.parse(req.originalUrl).pathname + '?' + qs.stringify(query);
};
};
const hasNextPages = (req) => {
return function (pageCount) {
if (typeof pageCount !== 'number' || pageCount < 0)
throw new Error('middleware paginate: `pageCount` is not a number >= 0');
return req.query.page < pageCount;
};
};
const getArrayPages = (req) => {
return function (limit, pageCount, currentPage) {
const maxPage = pageCount;
// limit default is 3
limit = limit || 3;
if (typeof limit !== 'number' || limit < 0)
throw new Error('middleware paginate: `limit` is not a number >= 0');
if (typeof pageCount !== 'number' || pageCount < 0)
throw new Error('middleware paginate: `pageCount` is not a number >= 0');
currentPage = parseInt(currentPage, 10);
if (Number.isNaN(currentPage) || currentPage < 0)
throw new Error('middleware paginate: `currentPage` is not a number >= 0');
if (limit > 0) {
let end = Math.min(Math.max(currentPage + Math.floor(limit / 2), limit), pageCount);
let start = Math.max(1, (currentPage < (limit - 1)) ? 1 : (end - limit) + 1);
let pages = [];
for (let i = start; i <= end; i++) {
pages.push({
number: i,
url: href(req)()
.replace('page=' + (currentPage + 1), 'page=' + i)
});
}
return pages;
}
}
}
const middleware = (limit = 10, maxLimit = 50) => {
const _limit = (typeof limit === 'number') ? parseInt(limit, 10) : 10;
const _maxLimit = (typeof maxLimit === 'number') ? parseInt(maxLimit, 10) : 50;
return function _middleware(req, res, next) {
req.query.page = (typeof req.query.page === 'string') ? parseInt(req.query.page, 10) || 1 : 1;
req.query.limit = (typeof req.query.limit === 'string') ? parseInt(req.query.limit, 10) || 0 : _limit;
if (req.query.limit > _maxLimit)
req.query.limit = _maxLimit;
if (req.query.page < 1)
req.query.page = 1;
if (req.query.limit < 0)
req.query.limit = 0;
//req.skip = req.offset = (req.query.page * req.query.limit) - req.query.limit;
res.locals.paginate = res.locals.paginate ? res.locals.paginate : {};
res.locals.paginate.page = req.query.page;
res.locals.paginate.limit = req.query.limit;
res.locals.paginate.href = href(req);
res.locals.paginate.hasPreviousPages = req.query.page > 1;
res.locals.paginate.hasNextPages = hasNextPages(req);
res.locals.paginate.getArrayPages = getArrayPages(req);
// Todo lo relativo a paginación va ahora a res.locals.paginate
delete req.query.page;
delete req.query.limit;
next();
};
};
module.exports = {
href,
hasNextPages,
getArrayPages,
middleware
}