-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed wrong calculated pagination size. (#628)
* fixed wrong calculated pagination size.
- Loading branch information
Showing
9 changed files
with
178 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
'@moralisweb3/evm-api': patch | ||
'@moralisweb3/evm-utils': patch | ||
--- | ||
|
||
Fixed a wrong calculated pagination size. Added the `hasNext()` method to a paginated result. Now you must call it before you call the `next()` method. | ||
|
||
```ts | ||
let response = await Moralis.EvmApi.token.getNFTOwners({ | ||
/* ... */ | ||
}); | ||
|
||
while (response.hasNext()) { | ||
response = await response.next(); | ||
// ... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './getNextParams'; | ||
export * from './checkObjEqual'; | ||
export * from './tryGetNextPageParams'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { PaginatedParams, PaginatedResult } from '../resolvers/PaginatedEndpoint'; | ||
import { tryGetNextPageParams } from './tryGetNextPageParams'; | ||
|
||
describe('tryGetNextPageParams', () => { | ||
describe('cursor mode', () => { | ||
function createData(total: number, page: number, pageSize: number): [PaginatedParams, PaginatedResult<null>] { | ||
const currentParams = { | ||
cursor: '0x1', | ||
}; | ||
const result = { | ||
total, | ||
page, | ||
page_size: pageSize, | ||
cursor: '0x2', | ||
result: null, | ||
}; | ||
return [currentParams, result]; | ||
} | ||
|
||
it('returns params when next page exists', () => { | ||
const [currentParams, result] = createData(950, 9, 100); | ||
const nextParams = tryGetNextPageParams(currentParams, result); | ||
expect(nextParams?.cursor).toEqual('0x2'); | ||
}); | ||
|
||
it('returns null when next page does not exist', () => { | ||
const [currentParams, result] = createData(950, 10, 100); | ||
const nextParams = tryGetNextPageParams(currentParams, result); | ||
expect(nextParams).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('offset mode', () => { | ||
function createData(total: number, page: number, pageSize: number): [PaginatedParams, PaginatedResult<null>] { | ||
const currentParams = { | ||
limit: 100, | ||
offset: 900, | ||
}; | ||
const result = { | ||
total, | ||
page, | ||
page_size: pageSize, | ||
cursor: '', | ||
result: null, | ||
}; | ||
return [currentParams, result]; | ||
} | ||
|
||
it('returns params when next page exists', () => { | ||
const [currentParams, result] = createData(950, 9, 100); | ||
const nextParams = tryGetNextPageParams(currentParams, result); | ||
expect(nextParams?.offset).toEqual(1000); | ||
}); | ||
|
||
it('returns null when next page does not exist', () => { | ||
const [currentParams, result] = createData(950, 10, 100); | ||
const nextParams = tryGetNextPageParams(currentParams, result); | ||
expect(nextParams).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { PaginatedParams, PaginatedResult } from '../resolvers/PaginatedEndpoint'; | ||
|
||
export const tryGetNextPageParams = <Params extends PaginatedParams>( | ||
currentParams: Params, | ||
result: PaginatedResult<unknown>, | ||
) => { | ||
const hasNextPage = result.total > result.page_size * result.page; | ||
if (!hasNextPage) { | ||
return null; | ||
} | ||
|
||
const nextParams = { ...currentParams }; | ||
if (result.cursor) { | ||
nextParams.cursor = result.cursor; | ||
} else { | ||
nextParams.offset = (result.page + 1) * (nextParams.limit || 500); | ||
} | ||
return nextParams; | ||
}; |