refactor: move hasNextPage from Client to utils (#1715)

* add hasNextPage

* remove unused util

* add tests

* remove hasNextPage from client
This commit is contained in:
Mayukha Vadari
2021-10-12 16:39:57 -04:00
committed by GitHub
parent 75e56ca927
commit 1a8bbfa43e
6 changed files with 39 additions and 74 deletions

View File

@@ -276,22 +276,6 @@ class Client extends EventEmitter {
return this.connection.getUrl()
}
/**
* Returns true if there are more pages of data.
*
* When there are more results than contained in the response, the response
* includes a `marker` field.
*
* See https://ripple.com/build/rippled-apis/#markers-and-pagination.
*
* @param response - Response to check for more pages on.
* @returns Whether the response has more pages of data.
* @category Network
*/
public static hasNextPage(response: MarkerResponse): boolean {
return Boolean(response.result.marker)
}
/**
* @category Network
*/

View File

@@ -15,6 +15,8 @@ import {
xAddressToClassicAddress,
} from 'ripple-address-codec'
import { Response } from '../models/methods'
import getBalanceChanges from './balanceChanges'
import { deriveKeypair, deriveXAddress } from './derive'
import { generateXAddress } from './generateAddress'
@@ -69,25 +71,6 @@ function isValidAddress(address: string): boolean {
return isValidXAddress(address) || isValidClassicAddress(address)
}
/**
* Removes undefined values from an object.
*
* @param obj - Object to remove undefined values from.
* @returns The same object, but without undefined values.
*/
function removeUndefined<T extends Record<string, unknown>>(obj: T): T {
const newObj = { ...obj }
Object.entries(obj).forEach(([key, value]) => {
if (value == null) {
/* eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- Deletes undefined values. */
delete newObj[key]
}
})
return newObj
}
/**
* Converts a string to its hex equivalent. Useful for Memos.
*
@@ -98,6 +81,22 @@ function convertStringToHex(string: string): string {
return Buffer.from(string, 'utf8').toString('hex').toUpperCase()
}
/**
* Returns true if there are more pages of data.
*
* When there are more results than contained in the response, the response
* includes a `marker` field.
*
* See https://ripple.com/build/rippled-apis/#markers-and-pagination.
*
* @param response - Response to check for more pages on.
* @returns Whether the response has more pages of data.
*/
function hasNextPage(response: Response): boolean {
// eslint-disable-next-line @typescript-eslint/dot-notation -- only checking if it exists
return Boolean(response.result['marker'])
}
const hashes = {
hashSignedTx,
hashTx,
@@ -117,7 +116,7 @@ export {
getBalanceChanges,
dropsToXrp,
xrpToDrops,
removeUndefined,
hasNextPage,
rippleTimeToISOTime,
ISOTimeToRippleTime,
rippleTimeToUnixTime,

View File

@@ -1,35 +0,0 @@
import { assert } from 'chai'
import { Client } from 'xrpl-local'
import rippled from '../fixtures/rippled'
import { setupClient, teardownClient } from '../setupClient'
describe('client.hasNextPage', function () {
beforeEach(setupClient)
afterEach(teardownClient)
it('returns true when there is another page', async function () {
this.mockRippled.addResponse('ledger_data', rippled.ledger_data.first_page)
const response = await this.client.request({ command: 'ledger_data' })
assert(Client.hasNextPage(response))
})
it('returns false when there are no more pages', async function () {
const rippledResponse = function (
request: Request,
): Record<string, unknown> {
if ('marker' in request) {
return rippled.ledger_data.last_page
}
return rippled.ledger_data.first_page
}
this.mockRippled.addResponse('ledger_data', rippledResponse)
const response = await this.client.request({ command: 'ledger_data' })
const responseNextPage = await this.client.requestNextPage(
{ command: 'ledger_data' },
response,
)
assert(!Client.hasNextPage(responseNextPage))
})
})

View File

@@ -1,6 +1,6 @@
import { assert } from 'chai'
import { Client } from 'xrpl-local'
import { hasNextPage } from 'xrpl-local'
import rippled from '../fixtures/rippled'
import { setupClient, teardownClient } from '../setupClient'
@@ -36,7 +36,7 @@ describe('client.requestNextPage', function () {
{ command: 'ledger_data' },
response,
)
assert(!Client.hasNextPage(responseNextPage))
assert(!hasNextPage(responseNextPage))
await assertRejects(
this.client.requestNextPage({ command: 'ledger_data' }, responseNextPage),
Error,

View File

@@ -2,7 +2,7 @@ import { assert } from 'chai'
import { deriveXAddress } from 'xrpl-local'
describe('client.deriveXAddress', function () {
describe('deriveXAddress', function () {
it('returns address for public key', function () {
assert.equal(
deriveXAddress({

17
test/utils/hasNextPage.ts Normal file
View File

@@ -0,0 +1,17 @@
import { assert } from 'chai'
import { hasNextPage } from 'xrpl-local'
import fixtures from '../fixtures/rippled'
describe('hasNextPage', function () {
it('returns true when response has marker', function () {
const firstPage = fixtures.ledger_data.first_page
assert.isTrue(hasNextPage(firstPage))
})
it('returns false when response does not have marker', function () {
const lastPage = fixtures.ledger_data.last_page
assert.isFalse(hasNextPage(lastPage))
})
})