diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index 32ba539..c7331a1 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -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 = { + sortableColumns: ['id'], + defaultLimit: 5, + maxLimit: 2 + } + const query: PaginateQuery = { + path: '', + page: 1, + limit: 20, + } + + const result = await paginate(query, repo, config) + + expect(result.data).toStrictEqual(cats.slice(0, 2)) + }) + it('should return correct links', async () => { const config: PaginateConfig = { sortableColumns: ['id'], diff --git a/src/paginate.ts b/src/paginate.ts index 1cdd5cc..94a8788 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -41,7 +41,7 @@ export async function paginate( config: PaginateConfig ): Promise> { 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 const search = query.search const path = query.path