Set the fetch limit to 100 by default and add a default param

This commit is contained in:
Alexandre Bodin 2019-04-16 17:23:58 +02:00
parent b0f2b9b857
commit e2869f0b7e

View File

@ -5,23 +5,20 @@
const _ = require('lodash');
const defaults = {
start: 0,
limit: 10,
};
/**
* Global convertor
* @param {Object} params
*/
const convertRestQueryParams = (params = {}) => {
const convertRestQueryParams = (params = {}, defaults = {}) => {
if (typeof params !== 'object' || params === null) {
throw new Error(
`convertRestQueryParams expected an object got ${object === null ? 'null' : typeof object}`
`convertRestQueryParams expected an object got ${
params === null ? 'null' : typeof params
}`
);
}
let finalParams = { ...defaults };
let finalParams = Object.assign({ start: 0, limit: 100 }, defaults);
if (Object.keys(params).length === 0) {
return finalParams;
@ -55,7 +52,9 @@ const convertRestQueryParams = (params = {}) => {
*/
const convertSortQueryParams = sortQuery => {
if (typeof sortQuery !== 'string') {
throw new Error(`convertSortQueryParams expected a string, got ${typeof sortQuery}`);
throw new Error(
`convertSortQueryParams expected a string, got ${typeof sortQuery}`
);
}
const sortKeys = [];
@ -88,7 +87,9 @@ const convertStartQueryParams = startQuery => {
const startAsANumber = _.toNumber(startQuery);
if (!_.isInteger(startAsANumber) || startAsANumber < 0) {
throw new Error(`convertStartQueryParams expected a positive integer got ${startAsANumber}`);
throw new Error(
`convertStartQueryParams expected a positive integer got ${startAsANumber}`
);
}
return {
@ -103,8 +104,13 @@ const convertStartQueryParams = startQuery => {
const convertLimitQueryParams = limitQuery => {
const limitAsANumber = _.toNumber(limitQuery);
if (!_.isInteger(limitAsANumber) || (limitAsANumber !== -1 && limitAsANumber < 0)) {
throw new Error(`convertLimitQueryParams expected a positive integer got ${limitAsANumber}`);
if (
!_.isInteger(limitAsANumber) ||
(limitAsANumber !== -1 && limitAsANumber < 0)
) {
throw new Error(
`convertLimitQueryParams expected a positive integer got ${limitAsANumber}`
);
}
return {