Created
December 13, 2019 02:52
-
-
Save package71/8aa319d8e522968fda191082599898a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @name API.plugin.pageAPI */ | |
module.exports = { | |
/** | |
* @param page - number | |
* @param limit - number | |
* @param config - object | |
* @param config.maxPage - number | |
* @param config.maxLimit - number | |
* @param config.defaultLimit - number | |
* @param config.defaultPage - number | |
* | |
* */ | |
get: function (page, limit, config) { | |
let maxPage = 1e6; | |
let maxLimit = 100; | |
let defaultLimit = 20; | |
let defaultPage = 1; | |
if (config) { | |
if (config.maxPage && !isNaN(config.maxPage) && +config.maxPage > 0) | |
maxPage = Math.abs(+config.maxPage); | |
if (config.maxLimit && !isNaN(config.maxLimit) && +config.maxLimit > 0) | |
maxLimit = Math.abs(+config.maxLimit); | |
if (config.defaultLimit && !isNaN(config.defaultLimit) && +config.defaultLimit > 0) | |
defaultLimit = Math.abs(+config.defaultLimit); | |
if (config.defaultPage && !isNaN(config.defaultPage) && +config.defaultPage > 0) | |
defaultPage = Math.abs(+config.defaultPage); | |
} | |
if (!page) page = defaultPage; | |
if (!limit) limit = defaultLimit; | |
if (page > maxPage) page = defaultPage; | |
if (limit > maxLimit) limit = maxLimit; | |
const offset = limit * (page - 1); | |
return {offset, limit, page}; | |
}, | |
docsParams: (API) => { | |
return { | |
page: { | |
type: API.types.INTEGER(10), | |
title: 'Page number', | |
error_code: 5111529144583693 | |
}, | |
limit: { | |
type: API.types.INTEGER(10), | |
title: 'Limit rows on the page ', | |
error_code: 9851529144592304 | |
}, | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment