Skip to content

Commit d5cde14

Browse files
authored
fix: added safe parse of int params and treating NaN as undefined to boil down to use default params from config for page and limit (#1100)
1 parent 84ad82b commit d5cde14

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/decorator.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,35 @@ describe('Decorator', () => {
202202
cursor: 'abc123',
203203
})
204204
})
205+
206+
it('should use default params if not valid values provided', () => {
207+
const context = fastifyContextFactory({
208+
page: 'NOTANUMBER',
209+
limit: 'NOTANUMBER',
210+
sortBy: ['NOTEXISTEN:BLABLA'],
211+
search: 'white',
212+
'filter.notUsed': '$fake:$eqaa:Kitty',
213+
'filter.notUsedSecond': 'something',
214+
select: ['notExisted'],
215+
cursor: 'abc123',
216+
})
217+
218+
const result: PaginateQuery = decoratorfactory(null, context)
219+
220+
expect(result).toStrictEqual({
221+
page: undefined,
222+
limit: undefined,
223+
sortBy: [['NOTEXISTEN', 'BLABLA']],
224+
search: 'white',
225+
searchBy: undefined,
226+
withDeleted: undefined,
227+
path: 'http://localhost/items',
228+
filter: {
229+
notUsed: '$fake:$eqaa:Kitty',
230+
notUsedSecond: 'something',
231+
},
232+
select: ['notExisted'],
233+
cursor: 'abc123',
234+
})
235+
})
205236
})

src/decorator.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createParamDecorator, ExecutionContext } from '@nestjs/common'
22
import type { Request as ExpressRequest } from 'express'
33
import type { FastifyRequest } from 'fastify'
44
import { Dictionary, isString, mapKeys, pickBy } from 'lodash'
5+
import { isNil } from './helper'
56

67
function isRecord(data: unknown): data is Record<string, unknown> {
78
return data !== null && typeof data === 'object' && !Array.isArray(data)
@@ -51,6 +52,19 @@ function parseParam<T>(queryParam: unknown, parserLogic: (param: string, res: an
5152
return res.length ? res : undefined
5253
}
5354

55+
function parseIntParam(v: unknown): number | undefined {
56+
if (isNil(v)) {
57+
return undefined
58+
}
59+
60+
const result = Number.parseInt(v.toString(), 10)
61+
62+
if (Number.isNaN(result)) {
63+
return undefined
64+
}
65+
return result
66+
}
67+
5468
export const Paginate = createParamDecorator((_data: unknown, ctx: ExecutionContext): PaginateQuery => {
5569
let path: string
5670
let query: Record<string, unknown>
@@ -96,8 +110,8 @@ export const Paginate = createParamDecorator((_data: unknown, ctx: ExecutionCont
96110
)
97111

98112
return {
99-
page: query.page ? parseInt(query.page.toString(), 10) : undefined,
100-
limit: query.limit ? parseInt(query.limit.toString(), 10) : undefined,
113+
page: parseIntParam(query.page),
114+
limit: parseIntParam(query.limit),
101115
sortBy,
102116
search: query.search ? query.search.toString() : undefined,
103117
searchBy,

0 commit comments

Comments
 (0)