All files / lib/util apiPaginate.ts

26.32% Statements 5/19
0% Branches 0/8
0% Functions 0/3
26.32% Lines 5/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 33 34 35 36 37 38 39 4016x 16x   16x   16x   16x                                                                
const LIMIT_DEFAULT = 50
const LIMIT_MAX = 1000
 
const OFFSET_DEFAULT = 0
 
const DEFAULT_MAX_RESULT_WINDOW = 10000
 
const parseIntValue = (value, defaultValue, maxLimit?) => {
  if (!value) {
    return defaultValue
  }
 
  const v = parseInt(value)
  if (!maxLimit) {
    return v
  }
 
  return v <= maxLimit ? v : maxLimit
}
 
export default {
  parseOptionsForElasticSearch(params) {
    const limit = parseIntValue(params.limit, LIMIT_DEFAULT, LIMIT_MAX)
    const offset = parseIntValue(params.offset, OFFSET_DEFAULT)
 
    // See https://github.com/crowi/crowi/pull/293
    if (limit + offset > DEFAULT_MAX_RESULT_WINDOW) {
      throw new Error(`(limit + offset) must be less than or equal to ${DEFAULT_MAX_RESULT_WINDOW}`)
    }
 
    return { limit, offset }
  },
  parseOptions(params) {
    const limit = parseIntValue(params.limit, LIMIT_DEFAULT, LIMIT_MAX)
    const offset = parseIntValue(params.offset, OFFSET_DEFAULT)
 
    return { limit, offset }
  },
}