Skip to content

Commit

Permalink
fix(types): correct type of getTxField PE-2284
Browse files Browse the repository at this point in the history
  • Loading branch information
djwhitt committed Oct 17, 2022
1 parent b3bef1f commit 2f15049
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/arweave/composite-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,10 @@ export class ArweaveCompositeClient
}
}

async getTxField<T>(
async getTxField<K extends keyof PartialJsonTransaction>(
txId: string,
field: keyof PartialJsonTransaction,
): Promise<T> {
field: K,
): Promise<PartialJsonTransaction[K]> {
try {
this.failureSimulator.maybeFail();

Expand Down
2 changes: 1 addition & 1 deletion src/arweave/tx-chunks-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class TxChunksDataSource implements TxDataSource {

try {
const [txDataRoot, txOffset] = await Promise.all([
this.chainSource.getTxField<string>(txId, 'data_root'),
this.chainSource.getTxField(txId, 'data_root'),
this.chainSource.getTxOffset(txId),
]);
const size = +txOffset.size;
Expand Down
5 changes: 4 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ export interface ChainSource {
getBlockByHeight(height: number): Promise<PartialJsonBlock>;
getTx(txId: string): Promise<PartialJsonTransaction>;
getTxOffset(txId: string): Promise<JsonTransactionOffset>;
getTxField<T>(txId: string, field: keyof PartialJsonTransaction): Promise<T>;
getTxField<K extends keyof PartialJsonTransaction>(
txId: string,
field: K,
): Promise<PartialJsonTransaction[K]>;
getBlockAndTxsByHeight(height: number): Promise<{
block: PartialJsonBlock;
txs: PartialJsonTransaction[];
Expand Down
8 changes: 4 additions & 4 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ export class ArweaveChainSourceStub implements ChainSource {
}
}

async getTxField<T>(
async getTxField<K extends keyof PartialJsonTransaction>(
txId: string,
field: keyof PartialJsonTransaction,
): Promise<T> {
field: K,
): Promise<PartialJsonTransaction[K]> {
const tx = await this.getTx(txId);
return tx[field] as T;
return tx[field];
}

async getBlockAndTxsByHeight(height: number) {
Expand Down

0 comments on commit 2f15049

Please sign in to comment.