From da81eb0d2c1262d087d861b47efe1e42ecadab16 Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Mon, 6 Jan 2025 10:46:09 +0000 Subject: [PATCH 1/3] add JSDoc to config utility fn --- config/util.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config/util.js b/config/util.js index bfd482ce..a1463f66 100644 --- a/config/util.js +++ b/config/util.js @@ -115,6 +115,13 @@ export function combineConfigs (environment) { return _.merge({}, defaultConfig, customConfig) } +/** + * Validates the config object against the ConfigSchema. + * + * @param {*} config + * @returns {v.InferOutput} + * @throws {v.ValibotError} + */ export const validateConfig = (config) => { try { return v.parse(ConfigSchema, config) From 2632d0815136dbc66848dd9391c4cb27fd666b51 Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Mon, 6 Jan 2025 10:57:45 +0000 Subject: [PATCH 2/3] pagination JSDoc and tweaks --- src/middleware/common.middleware.js | 27 +++++++++++++++++---------- src/utils/pagination.js | 7 +++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/middleware/common.middleware.js b/src/middleware/common.middleware.js index c06eb857..145172c8 100644 --- a/src/middleware/common.middleware.js +++ b/src/middleware/common.middleware.js @@ -1,3 +1,4 @@ +// @ts-check import logger from '../utils/logger.js' import { types } from '../utils/logging.js' import performanceDbApi from '../services/performanceDbApi.js' @@ -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 */ @@ -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) } @@ -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) { @@ -161,22 +166,24 @@ 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') { + paginationObj.items.push({ type: 'number', number: item, href: `${baseSubpath}/${item}`, - current: pageNumber === parseInt(item) - } + current: pageNumber === item + }) } - }) + logger.warn('unexpected pagination item', { item, dataRange, types: types.App, endpoint: req.originalUrl }) + } req.pagination = paginationObj diff --git a/src/utils/pagination.js b/src/utils/pagination.js index b2171440..f1f0b1a1 100644 --- a/src/utils/pagination.js +++ b/src/utils/pagination.js @@ -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) From f3efb6bd632caa2afdf4216a9af5ac47c0e691b2 Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Thu, 9 Jan 2025 10:42:50 +0000 Subject: [PATCH 3/3] fix: fold the warning into 'else' --- src/middleware/common.middleware.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/middleware/common.middleware.js b/src/middleware/common.middleware.js index 145172c8..f0597203 100644 --- a/src/middleware/common.middleware.js +++ b/src/middleware/common.middleware.js @@ -181,8 +181,9 @@ export const createPaginationTemplateParams = (req, res, next) => { href: `${baseSubpath}/${item}`, current: pageNumber === item }) + } else { + logger.warn('unexpected pagination item', { item, dataRange, types: types.App, endpoint: req.originalUrl }) } - logger.warn('unexpected pagination item', { item, dataRange, types: types.App, endpoint: req.originalUrl }) } req.pagination = paginationObj