2021-07-30 11:44:26 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { merge, pipe, omit, isNil } = require('lodash/fp');
|
|
|
|
|
|
|
|
const STRAPI_DEFAULTS = {
|
|
|
|
offset: {
|
|
|
|
start: 0,
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
page: {
|
|
|
|
page: 1,
|
|
|
|
pageSize: 10,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const paginationAttributes = ['start', 'limit', 'page', 'pageSize'];
|
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
const withMaxLimit = (limit, maxLimit = -1) => {
|
|
|
|
if (maxLimit === -1 || limit < maxLimit) {
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxLimit;
|
|
|
|
};
|
|
|
|
|
2021-07-30 11:44:26 +02:00
|
|
|
// Ensure minimum page & pageSize values (page >= 1, pageSize >= 0, start >= 0, limit >= 0)
|
|
|
|
const ensureMinValues = ({ start, limit }) => ({
|
|
|
|
start: Math.max(start, 0),
|
2021-09-07 11:23:49 +02:00
|
|
|
limit: Math.max(limit, 1),
|
2021-07-30 11:44:26 +02:00
|
|
|
});
|
|
|
|
|
2021-09-07 11:23:49 +02:00
|
|
|
const ensureMaxValues = (maxLimit = -1) => ({ start, limit }) => ({
|
2021-09-07 16:49:55 +02:00
|
|
|
start,
|
2021-09-07 11:23:49 +02:00
|
|
|
limit: withMaxLimit(limit, maxLimit),
|
|
|
|
});
|
|
|
|
|
|
|
|
const withDefaultPagination = (args, { defaults = {}, maxLimit = -1 } = {}) => {
|
2021-07-30 11:44:26 +02:00
|
|
|
const defaultValues = merge(STRAPI_DEFAULTS, defaults);
|
|
|
|
|
|
|
|
const usePagePagination = !isNil(args.page) || !isNil(args.pageSize);
|
|
|
|
const useOffsetPagination = !isNil(args.start) || !isNil(args.limit);
|
|
|
|
|
2021-09-28 18:21:21 +02:00
|
|
|
const ensureValidValues = pipe(
|
|
|
|
ensureMinValues,
|
|
|
|
ensureMaxValues(maxLimit)
|
|
|
|
);
|
2021-09-07 11:23:49 +02:00
|
|
|
|
2021-07-30 11:44:26 +02:00
|
|
|
// If there is no pagination attribute, don't modify the payload
|
|
|
|
if (!usePagePagination && !useOffsetPagination) {
|
2021-09-07 11:23:49 +02:00
|
|
|
return merge(args, ensureValidValues(defaultValues.offset));
|
2021-07-30 11:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is page & offset pagination attributes, throw an error
|
|
|
|
if (usePagePagination && useOffsetPagination) {
|
|
|
|
throw new Error('Cannot use both page & offset pagination in the same query');
|
|
|
|
}
|
|
|
|
|
|
|
|
const pagination = {};
|
|
|
|
|
|
|
|
// Start / Limit
|
|
|
|
if (useOffsetPagination) {
|
|
|
|
const { start, limit } = merge(defaultValues.offset, args);
|
|
|
|
|
|
|
|
Object.assign(pagination, { start, limit });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page / PageSize
|
|
|
|
if (usePagePagination) {
|
|
|
|
const { page, pageSize } = merge(defaultValues.page, args);
|
|
|
|
|
|
|
|
Object.assign(pagination, {
|
|
|
|
start: (page - 1) * pageSize,
|
|
|
|
limit: pageSize,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const replacePaginationAttributes = pipe(
|
|
|
|
// Remove pagination attributes
|
|
|
|
omit(paginationAttributes),
|
2021-09-07 11:23:49 +02:00
|
|
|
// Merge the object with the new pagination + ensure minimum & maximum values
|
|
|
|
merge(ensureValidValues(pagination))
|
2021-07-30 11:44:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return replacePaginationAttributes(args);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { withDefaultPagination };
|