Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/paginate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,37 @@ describe('paginate', () => {
expect(page.meta.totalItems).toBe(42)
})

it('should fix currentPage when page is out of bounds', async () => {
const config: PaginateConfig<ToyShopEntity> = {
sortableColumns: ['id'],
maxLimit: 10,
}
const query: PaginateQuery = {
path: '',
page: 200,
}

const result = await paginate<ToyShopEntity>(query, toyShopRepo, config)

expect(result.meta.currentPage).toBe(1)
expect(result.meta.totalPages).toEqual(result.meta.currentPage)
})

it('should fix links when page is out of bounds', async () => {
const config: PaginateConfig<ToyShopEntity> = {
sortableColumns: ['id'],
maxLimit: 10,
}
const query: PaginateQuery = {
path: '/toys',
page: 200,
}

const result = await paginate<ToyShopEntity>(query, toyShopRepo, config)

expect(result.links.current).toBe('/toys?page=1&limit=10&sortBy=id:ASC')
})

describe('should return result based on date column filter', () => {
it('with $not and $null operators', async () => {
const config: PaginateConfig<CatEntity> = {
Expand Down Expand Up @@ -3606,6 +3637,8 @@ describe('paginate', () => {
})

expect(result.data).toStrictEqual(expectedResult)
expect(result.links.last).toBeUndefined()
expect(result.links.next).toBeUndefined()
expect(result.links.current).toBe('?page=1&limit=20&sortBy=home.countCat:ASC')
})
})
Expand Down
17 changes: 9 additions & 8 deletions src/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ export async function paginate<T extends ObjectLiteral>(
query.limit === PaginationLimit.COUNTER_ONLY
? PaginationLimit.COUNTER_ONLY
: isPaginated === true
? maxLimit === PaginationLimit.NO_PAGINATION
? query.limit ?? defaultLimit
: query.limit === PaginationLimit.NO_PAGINATION
? defaultLimit
: Math.min(query.limit ?? defaultLimit, maxLimit)
: defaultLimit
? maxLimit === PaginationLimit.NO_PAGINATION
? (query.limit ?? defaultLimit)
: query.limit === PaginationLimit.NO_PAGINATION
? defaultLimit
: Math.min(query.limit ?? defaultLimit, maxLimit)
: defaultLimit

const generateNullCursor = (): string => {
return 'A' + '0'.repeat(15) // null values ​​should be looked up last, so use the smallest prefix
Expand Down Expand Up @@ -979,13 +979,14 @@ export async function paginate<T extends ObjectLiteral>(
const itemsPerPage = limit === PaginationLimit.COUNTER_ONLY ? totalItems : isPaginated ? limit : items.length
const totalItemsForMeta = limit === PaginationLimit.COUNTER_ONLY || isPaginated ? totalItems : items.length
const totalPages = isPaginated ? Math.ceil(totalItems / limit) : 1
const currentPage = Math.max(1, Math.min(page, totalPages))

const results: Paginated<T> = {
data: items,
meta: {
itemsPerPage: config.paginationType === PaginationType.CURSOR ? items.length : itemsPerPage,
totalItems: config.paginationType === PaginationType.CURSOR ? undefined : totalItemsForMeta,
currentPage: config.paginationType === PaginationType.CURSOR ? undefined : page,
currentPage: config.paginationType === PaginationType.CURSOR ? undefined : currentPage,
totalPages: config.paginationType === PaginationType.CURSOR ? undefined : totalPages,
sortBy,
search: query.search,
Expand All @@ -1010,7 +1011,7 @@ export async function paginate<T extends ObjectLiteral>(
: {
first: page == 1 ? undefined : buildLink(1),
previous: page - 1 < 1 ? undefined : buildLink(page - 1),
current: buildLink(page),
current: buildLink(currentPage),
next: page + 1 > totalPages ? undefined : buildLink(page + 1),
last: page == totalPages || !totalItems ? undefined : buildLink(totalPages),
}
Expand Down
Loading