fix: added max limit check (#4)
This commit is contained in:
parent
8378702b4a
commit
477180cb81
@ -73,6 +73,23 @@ describe('paginate', () => {
|
||||
expect(result.data).toStrictEqual(cats.slice(0, 1))
|
||||
})
|
||||
|
||||
it('should default to limit maxLimit, if more than maxLimit is given', async () => {
|
||||
const config: PaginateConfig<CatEntity> = {
|
||||
sortableColumns: ['id'],
|
||||
defaultLimit: 5,
|
||||
maxLimit: 2
|
||||
}
|
||||
const query: PaginateQuery = {
|
||||
path: '',
|
||||
page: 1,
|
||||
limit: 20,
|
||||
}
|
||||
|
||||
const result = await paginate<CatEntity>(query, repo, config)
|
||||
|
||||
expect(result.data).toStrictEqual(cats.slice(0, 2))
|
||||
})
|
||||
|
||||
it('should return correct links', async () => {
|
||||
const config: PaginateConfig<CatEntity> = {
|
||||
sortableColumns: ['id'],
|
||||
|
@ -41,7 +41,7 @@ export async function paginate<T>(
|
||||
config: PaginateConfig<T>
|
||||
): Promise<Paginated<T>> {
|
||||
let page = query.page || 1
|
||||
const limit = query.limit || config.defaultLimit || 20
|
||||
const limit = Math.min(query.limit || config.defaultLimit || 20, config.maxLimit || 100);
|
||||
const sortBy = [] as SortBy<T>
|
||||
const search = query.search
|
||||
const path = query.path
|
||||
|
Loading…
Reference in New Issue
Block a user