fix: added max limit check (#4)

This commit is contained in:
Alex Buis 2021-03-04 13:29:53 +01:00 committed by GitHub
parent 8378702b4a
commit 477180cb81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -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'],

View File

@ -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