Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSDoc to pagination code #762

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ export function combineConfigs (environment) {
return _.merge({}, defaultConfig, customConfig)
}

/**
* Validates the config object against the ConfigSchema.
*
* @param {*} config
* @returns {v.InferOutput<typeof ConfigSchema>}
* @throws {v.ValibotError}
*/
export const validateConfig = (config) => {
try {
return v.parse(ConfigSchema, config)
Expand Down
28 changes: 18 additions & 10 deletions src/middleware/common.middleware.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import logger from '../utils/logger.js'
import { types } from '../utils/logging.js'
import performanceDbApi from '../services/performanceDbApi.js'
Expand All @@ -11,7 +12,7 @@ import datasette from '../services/datasette.js'
* the function that threw the error.
*
* @param {Error} err
* @param {{handlerName: string}} req
* @param {import('express').Request & {handlerName: string}} req
* @param {*} res
* @param {*} next
*/
Expand Down Expand Up @@ -126,6 +127,7 @@ export const show404IfPageNumberNotInRange = (req, res, next) => {

if (pageNumber > dataRange.maxPageNumber || pageNumber < 1) {
const error = new Error('page number not in range')
// @ts-ignore
error.status = 404
return next(error)
}
Expand All @@ -145,10 +147,13 @@ export const createPaginationTemplateParams = (req, res, next) => {
return next(error)
}

/**
* @type {{ previous: { href: string } | undefined, next: { href: string } | undefined, items: { type: 'ellipsis' | 'number', number?: number, href: string, ellipsis?: boolean, current?: boolean }[] }}
*/
const paginationObj = {
previous: undefined,
next: undefined,
items: undefined
items: []
}

if (pageNumber > 1) {
Expand All @@ -161,22 +166,25 @@ export const createPaginationTemplateParams = (req, res, next) => {
href: `${baseSubpath}/${pageNumber + 1}`
}
}
paginationObj.items = pagination(dataRange.maxPageNumber, Math.min(pageNumber, dataRange.maxPageNumber)).map(item => {

for (const item of pagination(dataRange.maxPageNumber, Math.min(pageNumber, dataRange.maxPageNumber))) {
if (item === '...') {
return {
paginationObj.items.push({
type: 'ellipsis',
ellipsis: true,
href: '#'
}
} else {
return {
})
} else if (typeof item === 'number') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if it's not a number? The original code suggests that the item was a string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The not a number case is the ellipsis string ('...'), handled in the first branch. But now that I look at it, the warning should be in an 'else'. Updated that.

paginationObj.items.push({
type: 'number',
number: item,
href: `${baseSubpath}/${item}`,
current: pageNumber === parseInt(item)
}
current: pageNumber === item
})
} else {
logger.warn('unexpected pagination item', { item, dataRange, types: types.App, endpoint: req.originalUrl })
}
})
}

req.pagination = paginationObj

Expand Down
7 changes: 7 additions & 0 deletions src/utils/pagination.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const { min, max } = Math
const range = (lo, hi) => Array.from({ length: hi - lo }, (_, i) => i + lo)

/**
*
* @param {number} count
* @param {number} current
* @param {string} ellipsis
* @returns {(number | string)[]}
*/
export const pagination = (count, current, ellipsis = '...') => {
if (count <= 5) {
return range(1, count + 1)
Expand Down
Loading