-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (28 loc) · 907 Bytes
/
index.js
File metadata and controls
32 lines (28 loc) · 907 Bytes
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
const constants = require('../constants');
/**
* レスポンスヘッダ−にpagination情報を付加する
* @param {Response} res
* @param {number} limit
* @param {number} offset
* @param {number} count
*/
const setResHeader = options => {
return (res, limit, offset, count) => {
limit = Number(limit || options.limit || constants.DEFAULT_PAGER_LIMIT);
offset = Number(offset || 0);
const totalPages = Math.ceil(count / limit);
const currentPage = Math.ceil((offset + 1) / limit);
res.setHeader('X-Pagination-Limit', limit);
res.setHeader('X-Pagination-Total-Pages', totalPages);
res.setHeader('X-Pagination-Current-Page', currentPage);
};
};
module.exports = options => {
if (options.limit) {
constants.DEFAULT_PAGER_LIMIT = options.limit;
}
return {
setResHeader: setResHeader(options),
defaultLimit: constants.DEFAULT_PAGER_LIMIT,
};
};