Skip to content

Commit

Permalink
feat(api): add paginated gateway support
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jul 11, 2024
1 parent 4d1609d commit b23efa8
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 10 deletions.
35 changes: 30 additions & 5 deletions src/common/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,36 @@ export class IOReadable implements AoIORead {
});
}

async getGateways(): Promise<
Record<string, AoGateway> | Record<string, never>
> {
return this.process.read<Record<string, AoGateway>>({
tags: [{ name: 'Action', value: 'Gateways' }],
async getGateways(
pageParams?: PaginationParams,
): Promise<PaginationResult<AoGateway & { gatewayAddress: WalletAddress }>> {
const params = {
page: 1,
pageSize: 100,
sortBy: 'gatewayAddress',
sortOrder: 'asc',
...pageParams,
};

const alTags = [
{ name: 'Action', value: 'Paginated-Gateways' },
{ name: 'Page', value: params.page.toString() },
{ name: 'Page-Size', value: params.pageSize.toString() },
{ name: 'Sort-By', value: params.sortBy },
{ name: 'Sort-Order', value: params.sortOrder },
];

const prunedTags: { name: string; value: string }[] = alTags.filter(
(tag: {
name: string;
value: string | undefined;
}): tag is { name: string; value: string } => tag.value !== undefined,
);

return this.process.read<
PaginationResult<AoGateway & { gatewayAddress: WalletAddress }>
>({
tags: prunedTags,
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ export interface AoIORead {
}: {
address: WalletAddress;
}): Promise<AoGateway | undefined>;
getGateways(): Promise<
Record<WalletAddress, AoGateway> | Record<string, never>
>;
getGateways(
params?: PaginationParams,
): Promise<PaginationResult<AoGateway & { gatewayAddress: WalletAddress }>>;
getBalance(params: { address: WalletAddress }): Promise<number>;
getBalances(): Promise<Record<WalletAddress, number> | Record<string, never>>;
getArNSRecord({
Expand Down
48 changes: 47 additions & 1 deletion tests/e2e/cjs/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,55 @@ describe('IO', async () => {
assert.ok(reservedNames);
});

it('should be able to get gateways', async () => {
it('should be able to get first page of gateways', async () => {
const gateways = await io.getGateways();
assert.ok(gateways);
assert(gateways.page === 1);
assert(gateways.pageSize === 100);
assert(gateways.sortOrder === 'asc');
assert(gateways.sortBy === 'gatewayAddress');
assert(typeof gateways.hasNextPage === 'boolean');
assert(typeof gateways.totalItems === 'number');
assert(typeof gateways.totalPages === 'number');
assert(typeof gateways.sortBy === 'string');
assert(typeof gateways.sortOrder === 'string');
assert(Array.isArray(gateways.items));
// check the records
gateways.items.forEach((gateway) => {
assert(typeof gateway.gatewayAddress === 'string');
assert(typeof gateway.observerAddress === 'string');
assert(typeof gateway.startTimestamp === 'number');
assert(typeof gateway.operatorStake === 'number');
assert(typeof gateway.totalDelegatedStake === 'number');
});
});

it('should be able to get a specific page of gateways', async () => {
const gateways = await io.getGateways({
page: 2,
pageSize: 1,
sortBy: 'operatorStake',
sortOrder: 'desc',
});
assert.ok(gateways);
assert(gateways.page === 2);
assert(gateways.pageSize === 1);
assert(gateways.sortOrder === 'desc');
assert(gateways.sortBy === 'operatorStake');
assert(typeof gateways.hasNextPage === 'boolean');
assert(typeof gateways.totalItems === 'number');
assert(typeof gateways.totalPages === 'number');
assert(typeof gateways.sortBy === 'string');
assert(typeof gateways.sortOrder === 'string');
assert(Array.isArray(gateways.items));
// check the records
gateways.items.forEach((gateway) => {
assert(typeof gateway.gatewayAddress === 'string');
assert(typeof gateway.observerAddress === 'string');
assert(typeof gateway.startTimestamp === 'number');
assert(typeof gateway.operatorStake === 'number');
assert(typeof gateway.totalDelegatedStake === 'number');
});
});

it('should be able to get a single gateway', async () => {
Expand Down
48 changes: 47 additions & 1 deletion tests/e2e/esm/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,55 @@ describe('IO', async () => {
assert.ok(reservedNames);
});

it('should be able to get gateways', async () => {
it('should be able to get first page of gateways', async () => {
const gateways = await io.getGateways();
assert.ok(gateways);
assert(gateways.page === 1);
assert(gateways.pageSize === 100);
assert(gateways.sortOrder === 'asc');
assert(gateways.sortBy === 'gatewayAddress');
assert(typeof gateways.hasNextPage === 'boolean');
assert(typeof gateways.totalItems === 'number');
assert(typeof gateways.totalPages === 'number');
assert(typeof gateways.sortBy === 'string');
assert(typeof gateways.sortOrder === 'string');
assert(Array.isArray(gateways.items));
// check the records
gateways.items.forEach((gateway) => {
assert(typeof gateway.gatewayAddress === 'string');
assert(typeof gateway.observerAddress === 'string');
assert(typeof gateway.startTimestamp === 'number');
assert(typeof gateway.operatorStake === 'number');
assert(typeof gateway.totalDelegatedStake === 'number');
});
});

it('should be able to get a specific page of gateways', async () => {
const gateways = await io.getGateways({
page: 2,
pageSize: 1,
sortBy: 'operatorStake',
sortOrder: 'desc',
});
assert.ok(gateways);
assert(gateways.page === 2);
assert(gateways.pageSize === 1);
assert(gateways.sortOrder === 'desc');
assert(gateways.sortBy === 'operatorStake');
assert(typeof gateways.hasNextPage === 'boolean');
assert(typeof gateways.totalItems === 'number');
assert(typeof gateways.totalPages === 'number');
assert(typeof gateways.sortBy === 'string');
assert(typeof gateways.sortOrder === 'string');
assert(Array.isArray(gateways.items));
// check the gateways
gateways.items.forEach((gateway) => {
assert(typeof gateway.gatewayAddress === 'string');
assert(typeof gateway.observerAddress === 'string');
assert(typeof gateway.startTimestamp === 'number');
assert(typeof gateway.operatorStake === 'number');
assert(typeof gateway.totalDelegatedStake === 'number');
});
});

it('should be able to get a single gateway', async () => {
Expand Down

0 comments on commit b23efa8

Please sign in to comment.