Skip to content

Commit

Permalink
fix: pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jan 22, 2024
1 parent 9a25584 commit 3710dce
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 182 deletions.
12 changes: 11 additions & 1 deletion src/app.ts
Expand Up @@ -48,7 +48,17 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {

app.get('/:name', (req, res, next) => {
const { name = '' } = req.params
res.locals['data'] = service.find(name, req.query)
const query = Object.fromEntries(Object.entries(req.query)
.map(([key, value]) => {
if (['_start', '_end', '_limit', '_page', '_per_page'].includes(key) && typeof value === 'string') {
return [key, parseInt(value)]
} else {
return [key, value]
}
})
.filter(([_, value]) => !Number.isNaN(value))
)
res.locals['data'] = service.find(name, query)
next()
})

Expand Down

0 comments on commit 3710dce

Please sign in to comment.