Skip to content

Commit 11c8dd7

Browse files
authored
Typed indexer responses (#857)
1 parent 9ec8b7f commit 11c8dd7

25 files changed

+814
-256
lines changed

examples/indexer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function main() {
3939
// example: INDEXER_SEARCH_MIN_AMOUNT
4040

4141
// example: INDEXER_PAGINATE_RESULTS
42-
let nextToken = '';
42+
let nextToken: string | undefined = '';
4343

4444
// nextToken will be undefined if we reached the last page
4545
while (nextToken !== undefined) {
@@ -51,7 +51,7 @@ async function main() {
5151
.nextToken(nextToken)
5252
.do();
5353

54-
nextToken = response['next-token'];
54+
nextToken = response.nextToken;
5555
const txns = response.transactions;
5656
if (txns.length > 0)
5757
console.log(`Transaction IDs: ${response.transactions.map((t) => t.id)}`);

examples/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function indexerWaitForRound(
5454
round: number | bigint,
5555
maxAttempts: number
5656
) {
57-
let indexerRound = 0;
57+
let indexerRound = BigInt(0);
5858
let attempts = 0;
5959

6060
for (;;) {

src/client/v2/algod/compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default class Compile extends JSONRequest<
5555
query: this.query,
5656
requestHeaders: txHeaders,
5757
});
58-
return res.body;
58+
return this.prepare(res.body);
5959
}
6060

6161
// eslint-disable-next-line class-methods-use-this

src/client/v2/algod/getBlockTxids.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
3+
import { BlockTxidsResponse } from './models/types.js';
34

4-
export default class GetBlockTxids extends JSONRequest {
5+
export default class GetBlockTxids extends JSONRequest<
6+
BlockTxidsResponse,
7+
Record<string, any>
8+
> {
59
round: number;
610

711
constructor(c: HTTPClient, roundNumber: number) {
@@ -14,4 +18,11 @@ export default class GetBlockTxids extends JSONRequest {
1418
path() {
1519
return `/v2/blocks/${this.round}/txids`;
1620
}
21+
22+
// eslint-disable-next-line class-methods-use-this
23+
prepare(body: Record<string, any>): BlockTxidsResponse {
24+
return BlockTxidsResponse.fromEncodingData(
25+
BlockTxidsResponse.encodingSchema.fromPreparedJSON(body)
26+
);
27+
}
1728
}

src/client/v2/algod/suggestedParams.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export interface SuggestedParamsFromAlgod extends SuggestedParams {
1717
lastValid: bigint;
1818
genesisID: string;
1919
genesisHash: Uint8Array;
20+
21+
/**
22+
* ConsensusVersion indicates the consensus protocol version as of the last round.
23+
*/
24+
consensusVersion: string;
2025
}
2126

2227
/**
@@ -40,6 +45,7 @@ export default class SuggestedParamsRequest extends JSONRequest<
4045
genesisID: body['genesis-id'],
4146
genesisHash: base64ToBytes(body['genesis-hash']),
4247
minFee: BigInt(body['min-fee']),
48+
consensusVersion: body['consensus-version'],
4349
};
4450
}
4551
/* eslint-enable class-methods-use-this */

src/client/v2/indexer/lookupAccountAppLocalStates.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
33
import { Address } from '../../../encoding/address.js';
4+
import { ApplicationLocalStatesResponse } from './models/types.js';
45

5-
export default class LookupAccountAppLocalStates extends JSONRequest {
6+
export default class LookupAccountAppLocalStates extends JSONRequest<
7+
ApplicationLocalStatesResponse,
8+
Record<string, any>
9+
> {
610
private account: string | Address;
711

812
/**
@@ -135,4 +139,11 @@ export default class LookupAccountAppLocalStates extends JSONRequest {
135139
this.query['application-id'] = index;
136140
return this;
137141
}
142+
143+
// eslint-disable-next-line class-methods-use-this
144+
prepare(body: Record<string, any>): ApplicationLocalStatesResponse {
145+
return ApplicationLocalStatesResponse.fromEncodingData(
146+
ApplicationLocalStatesResponse.encodingSchema.fromPreparedJSON(body)
147+
);
148+
}
138149
}

src/client/v2/indexer/lookupAccountAssets.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
33
import { Address } from '../../../encoding/address.js';
4+
import { AssetHoldingsResponse } from './models/types.js';
45

5-
export default class LookupAccountAssets extends JSONRequest {
6+
export default class LookupAccountAssets extends JSONRequest<
7+
AssetHoldingsResponse,
8+
Record<string, any>
9+
> {
610
private account: string;
711

812
/**
@@ -136,4 +140,11 @@ export default class LookupAccountAssets extends JSONRequest {
136140
this.query['asset-id'] = index;
137141
return this;
138142
}
143+
144+
// eslint-disable-next-line class-methods-use-this
145+
prepare(body: Record<string, any>): AssetHoldingsResponse {
146+
return AssetHoldingsResponse.fromEncodingData(
147+
AssetHoldingsResponse.encodingSchema.fromPreparedJSON(body)
148+
);
149+
}
139150
}

src/client/v2/indexer/lookupAccountByID.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
33
import { Address } from '../../../encoding/address.js';
4+
import { AccountResponse } from './models/types.js';
45

5-
export default class LookupAccountByID extends JSONRequest {
6+
export default class LookupAccountByID extends JSONRequest<
7+
AccountResponse,
8+
Record<string, any>
9+
> {
610
private account: string;
711

812
/**
@@ -104,4 +108,11 @@ export default class LookupAccountByID extends JSONRequest {
104108
this.query.exclude = exclude;
105109
return this;
106110
}
111+
112+
// eslint-disable-next-line class-methods-use-this
113+
prepare(body: Record<string, any>): AccountResponse {
114+
return AccountResponse.fromEncodingData(
115+
AccountResponse.encodingSchema.fromPreparedJSON(body)
116+
);
117+
}
107118
}

src/client/v2/indexer/lookupAccountCreatedApplications.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
33
import { Address } from '../../../encoding/address.js';
4+
import { ApplicationsResponse } from './models/types.js';
45

5-
export default class LookupAccountCreatedApplications extends JSONRequest {
6+
export default class LookupAccountCreatedApplications extends JSONRequest<
7+
ApplicationsResponse,
8+
Record<string, any>
9+
> {
610
private account: string;
711

812
/**
@@ -136,4 +140,11 @@ export default class LookupAccountCreatedApplications extends JSONRequest {
136140
this.query['application-id'] = index;
137141
return this;
138142
}
143+
144+
// eslint-disable-next-line class-methods-use-this
145+
prepare(body: Record<string, any>): ApplicationsResponse {
146+
return ApplicationsResponse.fromEncodingData(
147+
ApplicationsResponse.encodingSchema.fromPreparedJSON(body)
148+
);
149+
}
139150
}

src/client/v2/indexer/lookupAccountCreatedAssets.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
33
import { Address } from '../../../encoding/address.js';
4+
import { AssetsResponse } from './models/types.js';
45

5-
export default class LookupAccountCreatedAssets extends JSONRequest {
6+
export default class LookupAccountCreatedAssets extends JSONRequest<
7+
AssetsResponse,
8+
Record<string, any>
9+
> {
610
private account: string;
711

812
/**
@@ -137,4 +141,11 @@ export default class LookupAccountCreatedAssets extends JSONRequest {
137141
this.query['asset-id'] = index;
138142
return this;
139143
}
144+
145+
// eslint-disable-next-line class-methods-use-this
146+
prepare(body: Record<string, any>): AssetsResponse {
147+
return AssetsResponse.fromEncodingData(
148+
AssetsResponse.encodingSchema.fromPreparedJSON(body)
149+
);
150+
}
140151
}

src/client/v2/indexer/lookupAccountTransactions.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { bytesToBase64 } from '../../../encoding/binarydata.js';
22
import { HTTPClient } from '../../client.js';
33
import JSONRequest from '../jsonrequest.js';
44
import { Address } from '../../../encoding/address.js';
5+
import { TransactionsResponse } from './models/types.js';
56

67
/**
78
* Accept base64 string or Uint8Array and output base64 string
@@ -15,7 +16,10 @@ export function base64StringFunnel(data: Uint8Array | string) {
1516
return bytesToBase64(data);
1617
}
1718

18-
export default class LookupAccountTransactions extends JSONRequest {
19+
export default class LookupAccountTransactions extends JSONRequest<
20+
TransactionsResponse,
21+
Record<string, any>
22+
> {
1923
private account: string;
2024

2125
/**
@@ -245,11 +249,12 @@ export default class LookupAccountTransactions extends JSONRequest {
245249
* .do();
246250
* ```
247251
*
248-
* @param before - rfc3339 string
252+
* @param before - rfc3339 string or Date object
249253
* @category query
250254
*/
251-
beforeTime(before: string) {
252-
this.query['before-time'] = before;
255+
beforeTime(before: string | Date) {
256+
this.query['before-time'] =
257+
before instanceof Date ? before.toISOString() : before;
253258
return this;
254259
}
255260

@@ -266,11 +271,12 @@ export default class LookupAccountTransactions extends JSONRequest {
266271
* .do();
267272
* ```
268273
*
269-
* @param after - rfc3339 string
274+
* @param after - rfc3339 string or Date object
270275
* @category query
271276
*/
272-
afterTime(after: string) {
273-
this.query['after-time'] = after;
277+
afterTime(after: string | Date) {
278+
this.query['after-time'] =
279+
after instanceof Date ? after.toISOString() : after;
274280
return this;
275281
}
276282

@@ -388,4 +394,11 @@ export default class LookupAccountTransactions extends JSONRequest {
388394
this.query['rekey-to'] = rekeyTo;
389395
return this;
390396
}
397+
398+
// eslint-disable-next-line class-methods-use-this
399+
prepare(body: Record<string, any>): TransactionsResponse {
400+
return TransactionsResponse.fromEncodingData(
401+
TransactionsResponse.encodingSchema.fromPreparedJSON(body)
402+
);
403+
}
391404
}

src/client/v2/indexer/lookupApplicationLogs.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
3+
import { ApplicationLogsResponse } from './models/types.js';
34

4-
export default class LookupApplicationLogs extends JSONRequest {
5+
export default class LookupApplicationLogs extends JSONRequest<
6+
ApplicationLogsResponse,
7+
Record<string, any>
8+
> {
59
/**
610
* Returns log messages generated by the passed in application.
711
*
@@ -154,4 +158,11 @@ export default class LookupApplicationLogs extends JSONRequest {
154158
this.query.txid = txid;
155159
return this;
156160
}
161+
162+
// eslint-disable-next-line class-methods-use-this
163+
prepare(body: Record<string, any>): ApplicationLogsResponse {
164+
return ApplicationLogsResponse.fromEncodingData(
165+
ApplicationLogsResponse.encodingSchema.fromPreparedJSON(body)
166+
);
167+
}
157168
}

src/client/v2/indexer/lookupApplications.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
3+
import { ApplicationResponse } from './models/types.js';
34

4-
export default class LookupApplications extends JSONRequest {
5+
export default class LookupApplications extends JSONRequest<
6+
ApplicationResponse,
7+
Record<string, any>
8+
> {
59
/**
610
* Returns information about the passed application.
711
*
@@ -57,4 +61,11 @@ export default class LookupApplications extends JSONRequest {
5761
this.query['include-all'] = value;
5862
return this;
5963
}
64+
65+
// eslint-disable-next-line class-methods-use-this
66+
prepare(body: Record<string, any>): ApplicationResponse {
67+
return ApplicationResponse.fromEncodingData(
68+
ApplicationResponse.encodingSchema.fromPreparedJSON(body)
69+
);
70+
}
6071
}

src/client/v2/indexer/lookupAssetBalances.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
3+
import { AssetBalancesResponse } from './models/types.js';
34

4-
export default class LookupAssetBalances extends JSONRequest {
5+
export default class LookupAssetBalances extends JSONRequest<
6+
AssetBalancesResponse,
7+
Record<string, any>
8+
> {
59
/**
610
* Returns the list of accounts which hold the given asset and their balance.
711
*
@@ -145,4 +149,11 @@ export default class LookupAssetBalances extends JSONRequest {
145149
this.query['include-all'] = value;
146150
return this;
147151
}
152+
153+
// eslint-disable-next-line class-methods-use-this
154+
prepare(body: Record<string, any>): AssetBalancesResponse {
155+
return AssetBalancesResponse.fromEncodingData(
156+
AssetBalancesResponse.encodingSchema.fromPreparedJSON(body)
157+
);
158+
}
148159
}

src/client/v2/indexer/lookupAssetByID.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import JSONRequest from '../jsonrequest.js';
22
import { HTTPClient } from '../../client.js';
3+
import { AssetResponse } from './models/types.js';
34

4-
export default class LookupAssetByID extends JSONRequest {
5+
export default class LookupAssetByID extends JSONRequest<
6+
AssetResponse,
7+
Record<string, any>
8+
> {
59
/**
610
* Returns asset information of the queried asset.
711
*
@@ -56,4 +60,11 @@ export default class LookupAssetByID extends JSONRequest {
5660
this.query['include-all'] = value;
5761
return this;
5862
}
63+
64+
// eslint-disable-next-line class-methods-use-this
65+
prepare(body: Record<string, any>): AssetResponse {
66+
return AssetResponse.fromEncodingData(
67+
AssetResponse.encodingSchema.fromPreparedJSON(body)
68+
);
69+
}
5970
}

0 commit comments

Comments
 (0)