-
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.
feat: renamed block_hash to hash + support disableTotal parameter (#926)
* feat: renamed block_hash to hash for response of getDateToBlock() method. * feat: add disableTotal parameter to requests of paginated methods. * updated NextPaginatedRequestResolver.
- Loading branch information
Showing
54 changed files
with
475 additions
and
47 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,5 @@ | ||
--- | ||
'@moralisweb3/common-evm-utils': patch | ||
--- | ||
|
||
Renamed the `block_hash` field to `hash` for the response of the `getDateToBlock()` method (according to the API changes). Introduced the `EvmBlockDate` class as a response of the `getDateToBlock()` method. |
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,6 @@ | ||
--- | ||
'@moralisweb3/common-evm-utils': patch | ||
'@moralisweb3/evm-api': patch | ||
--- | ||
|
||
Added the `disableTotal` parameter to requests of paginated methods. |
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,5 @@ | ||
--- | ||
'@moralisweb3/common-core': patch | ||
--- | ||
|
||
Updated the `NextPaginatedRequestResolver` class to support responses with no `total` field. |
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
81 changes: 81 additions & 0 deletions
81
packages/common/core/src/operations/request/NextPaginatedRequestResolver.test.ts
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,81 @@ | ||
import { PaginatedRequest } from '../PaginatedOperation'; | ||
import { Pagination } from '../response'; | ||
import { NextPaginatedRequestResolver } from './NextPaginatedRequestResolver'; | ||
|
||
interface TestPaginatedRequest extends PaginatedRequest { | ||
alfa: string; | ||
} | ||
|
||
describe('NextPaginatedRequestResolver', () => { | ||
const request: TestPaginatedRequest = { | ||
limit: 10, | ||
alfa: 'ALFA', | ||
}; | ||
|
||
describe('cursor mode', () => { | ||
it('returns null when cursor is not defined', () => { | ||
const pagination: Pagination = { | ||
page: 1, | ||
pageSize: 10, | ||
cursor: undefined, | ||
}; | ||
|
||
const nextRequest = NextPaginatedRequestResolver.resolve(0, request, pagination); | ||
|
||
expect(nextRequest).toBeNull(); | ||
}); | ||
|
||
it('returns next request when cursor is present', () => { | ||
const pagination: Pagination = { | ||
page: 1, | ||
pageSize: 10, | ||
cursor: 'CURSOR1', | ||
}; | ||
|
||
const nextRequest = NextPaginatedRequestResolver.resolve(0, request, pagination); | ||
|
||
expect(nextRequest).toBeDefined(); | ||
expect(nextRequest?.cursor).toBe(pagination.cursor); | ||
expect(nextRequest?.alfa).toBe(request.alfa); | ||
}); | ||
}); | ||
|
||
describe('total mode', () => { | ||
it('returns null when it is last page', () => { | ||
const pagination: Pagination = { | ||
page: 1, | ||
pageSize: 10, | ||
total: 10, | ||
}; | ||
|
||
const nextRequest = NextPaginatedRequestResolver.resolve(1, request, pagination); | ||
|
||
expect(nextRequest).toBeNull(); | ||
}); | ||
|
||
it('returns next request when it is NOT last page', () => { | ||
const pagination: Pagination = { | ||
page: 1, | ||
pageSize: 10, | ||
total: 21, | ||
}; | ||
|
||
const nextRequest = NextPaginatedRequestResolver.resolve(0, request, pagination); | ||
|
||
expect(nextRequest).toBeDefined(); | ||
expect(nextRequest?.offset).toBe(20); | ||
expect(nextRequest?.alfa).toBe(request.alfa); | ||
}); | ||
}); | ||
|
||
it('returns null when cursor and total are not defined', () => { | ||
const pagination: Pagination = { | ||
page: 1, | ||
pageSize: 10, | ||
}; | ||
|
||
const nextRequest = NextPaginatedRequestResolver.resolve(0, request, pagination); | ||
|
||
expect(nextRequest).toBeNull(); | ||
}); | ||
}); |
25 changes: 13 additions & 12 deletions
25
packages/common/core/src/operations/request/NextPaginatedRequestResolver.ts
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,24 +1,25 @@ | ||
import { PaginatedOperation, PaginatedRequest } from '../PaginatedOperation'; | ||
import { PaginatedRequest } from '../PaginatedOperation'; | ||
import { Pagination } from '../response/Pagination'; | ||
|
||
export class NextPaginatedRequestResolver { | ||
public static resolve<Request extends PaginatedRequest>( | ||
operation: PaginatedOperation<Request, unknown, unknown, unknown>, | ||
firstPageIndex: number, | ||
request: Request, | ||
pagination: Pagination, | ||
): Request | null { | ||
const currentPage = operation.firstPageIndex === 1 ? pagination.page : pagination.page + 1; | ||
const hasNextPage = pagination.total > pagination.pageSize * currentPage; | ||
if (!hasNextPage) { | ||
return null; | ||
if (pagination.cursor) { | ||
return { ...request, cursor: pagination.cursor }; | ||
} | ||
|
||
const nextParams = { ...request }; | ||
if (pagination.cursor) { | ||
nextParams.cursor = pagination.cursor; | ||
} else { | ||
nextParams.offset = (pagination.page + 1) * (nextParams.limit || 500); | ||
if (typeof pagination.total === 'number') { | ||
const currentPage = firstPageIndex === 1 ? pagination.page : pagination.page + 1; | ||
const hasNextPage = pagination.total > pagination.pageSize * currentPage; | ||
if (hasNextPage) { | ||
const offset = (pagination.page + 1) * (request.limit || 500); | ||
return { ...request, offset }; | ||
} | ||
} | ||
return nextParams; | ||
|
||
return null; | ||
} | ||
} |
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,5 +1,5 @@ | ||
export interface Pagination { | ||
total: number; | ||
total?: number; | ||
page: number; | ||
pageSize: number; | ||
cursor?: string; | ||
|
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
99 changes: 99 additions & 0 deletions
99
packages/common/evmUtils/src/dataTypes/EvmBlockDate/EvmBlockDate.ts
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,99 @@ | ||
import { EvmBlockDateData, EvmBlockDateInput } from './types'; | ||
|
||
export type EvmBlockDateish = EvmBlockDateInput | EvmBlockDate; | ||
|
||
export class EvmBlockDate { | ||
/** | ||
* Create a new instance of EvmBlockDate. | ||
* @param data - the EvmBlockDateish type. | ||
*/ | ||
public static create(data: EvmBlockDateish) { | ||
if (data instanceof EvmBlockDate) { | ||
return data; | ||
} | ||
return new EvmBlockDate(EvmBlockDate.parse(data)); | ||
} | ||
|
||
private static parse(input: EvmBlockDateInput): EvmBlockDateData { | ||
return { | ||
block: input.block, | ||
date: new Date(input.date), | ||
timestamp: input.timestamp, | ||
// TODO: the swagger currently has wrong type for `block_timestamp`, should be `string`. | ||
blockTimestamp: String(input.block_timestamp), | ||
hash: input.hash, | ||
parentHash: input.parent_hash, | ||
}; | ||
} | ||
|
||
private constructor(private readonly data: EvmBlockDateData) {} | ||
|
||
/** | ||
* @description The block number. | ||
* @example `9193266` | ||
*/ | ||
public get block(): number { | ||
return this.data.block; | ||
} | ||
|
||
/** | ||
* @description The date of the block. | ||
* @example `2020-01-01T00:00:00+00:00` | ||
*/ | ||
public get date(): Date { | ||
return this.data.date; | ||
} | ||
|
||
/** | ||
* @description The timestamp of the block | ||
* @example `1577836811` | ||
*/ | ||
public get timestamp(): number { | ||
return this.data.timestamp; | ||
} | ||
|
||
/** | ||
* @description The timestamp of the block | ||
* @example `2022-01-03T22:59:39.000Z` | ||
*/ | ||
public get blockTimestamp(): string | undefined { | ||
return this.data.blockTimestamp; | ||
} | ||
|
||
/** | ||
* @deprecated Use `blockTimestamp` instead. | ||
*/ | ||
public get block_timestamp(): string | undefined { | ||
return this.data.blockTimestamp; | ||
} | ||
|
||
/** | ||
* @description The block hash. | ||
* @example `0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171` | ||
*/ | ||
public get hash(): string | undefined { | ||
return this.data.hash; | ||
} | ||
|
||
/** | ||
* @deprecated Use `hash` instead. | ||
*/ | ||
public get block_hash(): string | undefined { | ||
return this.data.hash; | ||
} | ||
|
||
/** | ||
* @description The block hash of the parent block. | ||
* @example `0x011d1fc45839de975cc55d758943f9f1d204f80a90eb631f3bf064b80d53e045` | ||
*/ | ||
public get parentHash(): string | undefined { | ||
return this.data.parentHash; | ||
} | ||
|
||
/** | ||
* @deprecated Use `parentHash` instead. | ||
*/ | ||
public get parent_hash(): string | undefined { | ||
return this.data.parentHash; | ||
} | ||
} |
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,2 @@ | ||
export * from './EvmBlockDate'; | ||
export * from './types'; |
12 changes: 12 additions & 0 deletions
12
packages/common/evmUtils/src/dataTypes/EvmBlockDate/types.ts
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,12 @@ | ||
import { components } from '../../operations/openapi'; | ||
|
||
export type EvmBlockDateInput = components['schemas']['blockDate']; | ||
|
||
export interface EvmBlockDateData { | ||
date: Date; | ||
block: number; | ||
timestamp: number; | ||
blockTimestamp?: string; | ||
hash?: string; | ||
parentHash?: string; | ||
} |
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 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
Oops, something went wrong.