Skip to content

Commit 4b1ff6d

Browse files
authored
types: simplify pagination (#110)
1 parent a2d6166 commit 4b1ff6d

11 files changed

Lines changed: 62 additions & 49 deletions

File tree

src/interfaces.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface IResponse<T> {
55
}
66

77
export type DataResponse<T> = { data: T; errors: any };
8-
export interface PaginableResponse {
8+
export interface ResponseWithPagination {
99
meta: {
1010
count: number;
1111
pageCount: number;
@@ -22,12 +22,24 @@ export interface PaginableResponse {
2222
* An API response
2323
*
2424
* @template T `body` content, will add `meta` pagination data if is an array
25+
*/
26+
export interface ApiResponse<T> extends IResponse<DataResponse<T>> {}
27+
28+
/**
29+
* An API extended response
30+
*
31+
* @template T `body` content, will add `meta` pagination data if is an array
32+
* @template U Custom `meta` data to add (optional)
33+
*/
34+
export interface ApiExtendedResponse<T, U extends Record<string, any>> extends IResponse<DataResponse<T> & U> {}
35+
36+
/**
37+
* An API paginable response
38+
*
39+
* @template T `body` content, will add `meta` pagination data if is an array
2540
* @template U Custom `meta` data to add (optional)
26-
* @template V Should the request be auto-paginated ? (optional)
2741
*/
28-
export type ApiResponse<T, U extends Record<string, any> = {}, V extends boolean = true> = IResponse<
29-
DataResponse<T> & (V extends true ? (T extends any[] ? PaginableResponse : {}) : {}) & U
30-
>;
42+
export interface ApiResponseWithPagination<T> extends ApiExtendedResponse<T, ResponseWithPagination> {}
3143

3244
// Export type ApiResponse<T, U extends Record<string, any> = {}> = IResponse<DataResponse<T> & (T extends any[] ? PaginableResponse : {})>;
3345

src/resources/blocks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiQuery, ApiResponse } from "../interfaces";
1+
import { ApiQuery, ApiResponse, ApiResponseWithPagination } from "../interfaces";
22
import { Resource } from "./resource";
33
import {
44
AllBlockApiQuery,
@@ -21,7 +21,7 @@ export class Blocks extends Resource {
2121
*
2222
* This dataset contains millions of blocks; thus for analytical purposes, we recommend you use the Elasticsearch plugin or query the database directly.
2323
*/
24-
public async all(query?: AllBlockApiQuery): Promise<ApiResponse<Block[]>> {
24+
public async all(query?: AllBlockApiQuery): Promise<ApiResponseWithPagination<Block[]>> {
2525
return this.sendGet("blocks", query);
2626
}
2727

@@ -61,7 +61,7 @@ export class Blocks extends Resource {
6161
public async transactions(
6262
idOrHeight: string,
6363
query?: TransactionsInBlockApiQuery,
64-
): Promise<ApiResponse<Transaction[]>> {
64+
): Promise<ApiResponseWithPagination<Transaction[]>> {
6565
return this.sendGet(`blocks/${idOrHeight}/transactions`, query);
6666
}
6767

@@ -72,7 +72,7 @@ export class Blocks extends Resource {
7272
*
7373
* Filtering for blocks at the Node side is a lot more efficient than requesting a large payload and filtering it at the client side.
7474
*/
75-
public async search(payload?: SearchBlockApiBody, query?: ApiQuery): Promise<ApiResponse<Block[]>> {
75+
public async search(payload?: SearchBlockApiBody, query?: ApiQuery): Promise<ApiResponseWithPagination<Block[]>> {
7676
return this.sendPost("blocks/search", payload, query);
7777
}
7878
}

src/resources/bridgechains.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResponse } from "../interfaces";
1+
import { ApiResponseWithPagination } from "../interfaces";
22
import { Resource } from "./resource";
33
import {
44
AllBridgechainsApiQuery,
@@ -13,7 +13,7 @@ export class Bridgechains extends Resource {
1313
*
1414
* Returns a list of all registered bridgechains on the network.
1515
*/
16-
public async all(query?: AllBridgechainsApiQuery): Promise<ApiResponse<Bridgechain[]>> {
16+
public async all(query?: AllBridgechainsApiQuery): Promise<ApiResponseWithPagination<Bridgechain[]>> {
1717
return this.sendGet("bridgechains", query);
1818
}
1919

@@ -23,7 +23,7 @@ export class Bridgechains extends Resource {
2323
public async search(
2424
payload?: SearchBridgechainsApiBody,
2525
query?: SearchBridgechainsApiQuery,
26-
): Promise<ApiResponse<Bridgechain[]>> {
26+
): Promise<ApiResponseWithPagination<Bridgechain[]>> {
2727
return this.sendPost("bridgechains/search", payload, query);
2828
}
2929
}

src/resources/businesses.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResponse } from "../interfaces";
1+
import { ApiResponse, ApiResponseWithPagination } from "../interfaces";
22
import { Resource } from "./resource";
33
import {
44
AllBusinessesApiQuery,
@@ -13,7 +13,7 @@ export class Businesses extends Resource {
1313
/**
1414
* List all businesses
1515
*/
16-
public async all(query?: AllBusinessesApiQuery): Promise<ApiResponse<Business[]>> {
16+
public async all(query?: AllBusinessesApiQuery): Promise<ApiResponseWithPagination<Business[]>> {
1717
return this.sendGet("businesses", query);
1818
}
1919

@@ -34,7 +34,7 @@ export class Businesses extends Resource {
3434
public async bridgechains(
3535
walletAddress: string,
3636
query?: BusinessBridgechainsApiQuery,
37-
): Promise<ApiResponse<Bridgechain[]>> {
37+
): Promise<ApiResponseWithPagination<Bridgechain[]>> {
3838
return this.sendGet(`businesses/${walletAddress}/bridgechains`, query);
3939
}
4040

@@ -44,10 +44,7 @@ export class Businesses extends Resource {
4444
* @param walletAddress The identifier of the wallet to be retrieved.
4545
* @param genesisHash The genesis hash of the bridgechain to be retrieved.
4646
*/
47-
public async bridgechain(
48-
walletAddress: string,
49-
genesisHash: string,
50-
): Promise<ApiResponse<Bridgechain>> {
47+
public async bridgechain(walletAddress: string, genesisHash: string): Promise<ApiResponse<Bridgechain>> {
5148
return this.sendGet(`businesses/${walletAddress}/bridgechains/${genesisHash}`);
5249
}
5350

@@ -57,7 +54,7 @@ export class Businesses extends Resource {
5754
public async search(
5855
payload?: SearchBusinesssApiBody,
5956
query?: SearchBusinesssApiQuery,
60-
): Promise<ApiResponse<Business[]>> {
57+
): Promise<ApiResponseWithPagination<Business[]>> {
6158
return this.sendPost("businesses/search", payload, query);
6259
}
6360
}

src/resources/delegates.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiQuery, ApiResponse } from "../interfaces";
1+
import { ApiQuery, ApiResponse, ApiResponseWithPagination } from "../interfaces";
22
import {
33
AllDelegatesApiQuery,
44
DelegateBlocksApiQuery,
@@ -26,7 +26,7 @@ export class Delegates extends Resource {
2626
*
2727
* If a Delegate Node is offline, it is still returned through this API; as the `delegate` resource is not concerned with the actual nodes, only with the on-chain registrations and wallets.
2828
*/
29-
public async all(query?: AllDelegatesApiQuery): Promise<ApiResponse<Delegate[]>> {
29+
public async all(query?: AllDelegatesApiQuery): Promise<ApiResponseWithPagination<Delegate[]>> {
3030
return this.sendGet("delegates", query);
3131
}
3232

@@ -55,7 +55,7 @@ export class Delegates extends Resource {
5555
public async blocks(
5656
usernameOrAddressOrPublicKey: string,
5757
query?: DelegateBlocksApiQuery,
58-
): Promise<ApiResponse<Block[]>> {
58+
): Promise<ApiResponseWithPagination<Block[]>> {
5959
return this.sendGet(`delegates/${usernameOrAddressOrPublicKey}/blocks`, query);
6060
}
6161

@@ -69,7 +69,7 @@ export class Delegates extends Resource {
6969
public async voters(
7070
usernameOrAddressOrPublicKey: string,
7171
query?: DelegateVotersApiQuery,
72-
): Promise<ApiResponse<Voter[]>> {
72+
): Promise<ApiResponseWithPagination<Voter[]>> {
7373
return this.sendGet(`delegates/${usernameOrAddressOrPublicKey}/voters`, query);
7474
}
7575

@@ -78,7 +78,7 @@ export class Delegates extends Resource {
7878
*
7979
* For fine-grained searches, use the search endpoint.
8080
*/
81-
public async search(payload?: SearchDelegatesApiBody, query?: ApiQuery): Promise<ApiResponse<Delegate[]>> {
81+
public async search(payload?: SearchDelegatesApiBody, query?: ApiQuery): Promise<ApiResponseWithPagination<Delegate[]>> {
8282
return this.sendPost("delegates/search", payload, query);
8383
}
8484
}

src/resources/locks.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResponse } from "../interfaces";
1+
import { ApiResponse, ApiResponseWithPagination } from "../interfaces";
22
import {
33
AllLocksApiQuery,
44
SearchLocksApiBody,
@@ -17,7 +17,7 @@ export class Locks extends Resource {
1717
/**
1818
* List all locks
1919
*/
20-
public async all(query?: AllLocksApiQuery): Promise<ApiResponse<Lock[]>> {
20+
public async all(query?: AllLocksApiQuery): Promise<ApiResponseWithPagination<Lock[]>> {
2121
return this.sendGet("locks", query);
2222
}
2323

@@ -32,7 +32,10 @@ export class Locks extends Resource {
3232
/**
3333
* Search locks
3434
*/
35-
public async search(payload?: SearchLocksApiBody, query?: SearchLocksApiQuery): Promise<ApiResponse<Lock[]>> {
35+
public async search(
36+
payload?: SearchLocksApiBody,
37+
query?: SearchLocksApiQuery,
38+
): Promise<ApiResponseWithPagination<Lock[]>> {
3639
return this.sendPost("locks/search", payload, query);
3740
}
3841

@@ -42,7 +45,7 @@ export class Locks extends Resource {
4245
public async unlocked(
4346
payload?: SearchLocksUnlockedApiBody,
4447
query?: SearchLocksUnlockedApiQuery,
45-
): Promise<ApiResponse<Transaction[]>> {
48+
): Promise<ApiResponseWithPagination<Transaction[]>> {
4649
return this.sendPost("locks/unlocked", payload, query);
4750
}
4851
}

src/resources/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResponse } from "../interfaces";
1+
import { ApiResponse, ApiExtendedResponse } from "../interfaces";
22
import { Resource } from "./resource";
33
import {
44
NodeConfiguration,
@@ -40,7 +40,7 @@ export class Node extends Resource {
4040
*
4141
* @param days The number of days which will be regarded.
4242
*/
43-
public async fees(days: number): Promise<ApiResponse<NodeFeeStatisticsBody, NodeFeeStatisticsMeta, false>> {
43+
public async fees(days: number): Promise<ApiExtendedResponse<NodeFeeStatisticsBody, NodeFeeStatisticsMeta>> {
4444
return this.sendGet("node/fees", { days });
4545
}
4646

src/resources/peers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResponse } from "../interfaces";
1+
import { ApiResponse, ApiResponseWithPagination } from "../interfaces";
22
import { AllPeersApiQuery, Peer } from "../resourcesTypes/peers";
33
import { Resource } from "./resource";
44

@@ -13,7 +13,7 @@ export class Peers extends Resource {
1313
*
1414
* Returns all peers known by the Node. These are not necessarily all peers; only public Nodes appear here.
1515
*/
16-
public async all(query?: AllPeersApiQuery): Promise<ApiResponse<Peer[]>> {
16+
public async all(query?: AllPeersApiQuery): Promise<ApiResponseWithPagination<Peer[]>> {
1717
return this.sendGet("peers", query);
1818
}
1919

src/resources/rounds.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ export class Rounds extends Resource {
77
*
88
* @param roundNumber The number of wanted round.
99
*/
10-
public async delegates(
11-
roundNumber: number,
12-
): Promise<ApiResponse<{ publicKey: string; votes: string }, null, false>> {
10+
public async delegates(roundNumber: number): Promise<ApiResponse<{ publicKey: string; votes: string }[]>> {
1311
return this.sendGet(`rounds/${roundNumber}/delegates`);
1412
}
1513
}

src/resources/transactions.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiQuery, ApiResponse } from "../interfaces";
1+
import { ApiQuery, ApiResponse, ApiResponseWithPagination } from "../interfaces";
22
import {
33
AllTransactionsApiQuery,
44
SearchTransactionsApiBody,
@@ -18,7 +18,7 @@ export class Transactions extends Resource {
1818
*
1919
* The paginated API is used to query for multiple transactions. You can apply _filters_ through the query parameter to search for specific transactions.
2020
*/
21-
public async all(query?: AllTransactionsApiQuery): Promise<ApiResponse<Transaction[]>> {
21+
public async all(query?: AllTransactionsApiQuery): Promise<ApiResponseWithPagination<Transaction[]>> {
2222
return this.sendGet("transactions", query);
2323
}
2424

@@ -57,7 +57,7 @@ export class Transactions extends Resource {
5757
*
5858
* If you have set the transaction with a fee of near zero, it might not be picked up by a Delegate and will time out.
5959
*/
60-
public async allUnconfirmed(query?: ApiQuery): Promise<ApiResponse<Transaction[]>> {
60+
public async allUnconfirmed(query?: ApiQuery): Promise<ApiResponseWithPagination<Transaction[]>> {
6161
return this.sendGet("transactions/unconfirmed", query);
6262
}
6363

@@ -81,7 +81,10 @@ export class Transactions extends Resource {
8181
*
8282
* It is best to filter as many transactions node side, instead of dissecting the response client side.
8383
*/
84-
public async search(payload: SearchTransactionsApiBody, query?: ApiQuery): Promise<ApiResponse<Transaction[]>> {
84+
public async search(
85+
payload: SearchTransactionsApiBody,
86+
query?: ApiQuery,
87+
): Promise<ApiResponseWithPagination<Transaction[]>> {
8588
return this.sendPost("transactions/search", payload, query);
8689
}
8790

0 commit comments

Comments
 (0)