diff --git a/fabric-contract-api/test/typescript/tsconfig.json b/fabric-contract-api/test/typescript/tsconfig.json index 7f284e23..02a31c3a 100644 --- a/fabric-contract-api/test/typescript/tsconfig.json +++ b/fabric-contract-api/test/typescript/tsconfig.json @@ -8,7 +8,10 @@ "compilerOptions": { "module": "commonjs", "target": "es2017", - "sourceMap": true + "sourceMap": true, + "lib": [ + "esnext" + ] }, "exclude": [ "node_modules" diff --git a/fabric-shim/test/typescript/chaincode.ts b/fabric-shim/test/typescript/chaincode.ts index db3db79f..ae8095f0 100644 --- a/fabric-shim/test/typescript/chaincode.ts +++ b/fabric-shim/test/typescript/chaincode.ts @@ -77,6 +77,7 @@ class TestTS implements ChaincodeInterface { this.testCompositeKey(stub); await this.testState(stub); await this.testOtherIteratorCalls(stub); + await this.testAsyncIterators(stub); await this.testPrivateData(stub); await this.testOtherStubCalls(stub); this.testClientIdentity(stub); @@ -147,7 +148,7 @@ class TestTS implements ChaincodeInterface { this.testStateQueryIterator(queryResult); } - async testIterator(iterator: Iterators.CommonIterator) { + async testIterator(iterator: Iterators.CommonIterator) { const historyNext: Promise = iterator.next(); const nextVal: any = await historyNext; const historyClose: Promise = iterator.close(); @@ -170,7 +171,7 @@ class TestTS implements ChaincodeInterface { } async testStateQueryIterator(stateQuery: Iterators.StateQueryIterator) { - const stateNext: Iterators.NextResult = await stateQuery.next(); + const stateNext: Iterators.NextResult = await stateQuery.next(); await stateQuery.close(); const done: boolean = stateNext.done; const keyVal: Iterators.KV = stateNext.value; @@ -197,6 +198,21 @@ class TestTS implements ChaincodeInterface { } + async testAsyncIterators(stub: ChaincodeStub): Promise { + const iterator = stub.getStateByRange('key2', 'key6'); + for await (const res of iterator) { + const value = res.value; + } + const iteratorPage = stub.getStateByRangeWithPagination('key2', 'key6', 3); + for await (const res of iteratorPage) { + const value = res.value; + } + const iteratorHistory = stub.getHistoryForKey('key1'); + for await (const res of iteratorHistory) { + const tx_id = res.tx_id; + } + } + async testOtherStubCalls(stub: ChaincodeStub): Promise { const binding: string = stub.getBinding(); const channelID: string = stub.getChannelID(); diff --git a/fabric-shim/test/typescript/tsconfig.json b/fabric-shim/test/typescript/tsconfig.json index 7f284e23..02a31c3a 100644 --- a/fabric-shim/test/typescript/tsconfig.json +++ b/fabric-shim/test/typescript/tsconfig.json @@ -8,7 +8,10 @@ "compilerOptions": { "module": "commonjs", "target": "es2017", - "sourceMap": true + "sourceMap": true, + "lib": [ + "esnext" + ] }, "exclude": [ "node_modules" diff --git a/fabric-shim/types/index.d.ts b/fabric-shim/types/index.d.ts index 185950ea..946b7e3f 100644 --- a/fabric-shim/types/index.d.ts +++ b/fabric-shim/types/index.d.ts @@ -71,6 +71,10 @@ declare module 'fabric-shim' { ERROR: number; } + interface AsyncIterable { + [Symbol.asyncIterator]: () => AsyncIterator; + } + export class ChaincodeStub { getArgs(): string[]; getStringArgs(): string[]; @@ -90,14 +94,14 @@ declare module 'fabric-shim' { deleteState(key: string): Promise; setStateValidationParameter(key: string, ep: Buffer): Promise; getStateValidationParameter(key: string): Promise; - getStateByRange(startKey: string, endKey: string): Promise; - getStateByRangeWithPagination(startKey: string, endKey: string, pageSize: number, bookmark?: string): Promise>; - getStateByPartialCompositeKey(objectType: string, attributes: string[]): Promise; - getStateByPartialCompositeKeyWithPagination(objectType: string, attributes: string[], pageSize: number, bookmark?: string): Promise>; + getStateByRange(startKey: string, endKey: string): Promise & AsyncIterable; + getStateByRangeWithPagination(startKey: string, endKey: string, pageSize: number, bookmark?: string): Promise> & AsyncIterable; + getStateByPartialCompositeKey(objectType: string, attributes: string[]): Promise & AsyncIterable; + getStateByPartialCompositeKeyWithPagination(objectType: string, attributes: string[], pageSize: number, bookmark?: string): Promise> & AsyncIterable; - getQueryResult(query: string): Promise; - getQueryResultWithPagination(query: string, pageSize: number, bookmark?: string): Promise>; - getHistoryForKey(key: string): Promise; + getQueryResult(query: string): Promise & AsyncIterable; + getQueryResultWithPagination(query: string, pageSize: number, bookmark?: string): Promise> & AsyncIterable; + getHistoryForKey(key: string): Promise & AsyncIterable; invokeChaincode(chaincodeName: string, args: string[], channel: string): Promise; setEvent(name: string, payload: Buffer): void; @@ -111,9 +115,9 @@ declare module 'fabric-shim' { deletePrivateData(collection: string, key: string): Promise; setPrivateDataValidationParameter(collection: string, key: string, ep: Buffer): Promise; getPrivateDataValidationParameter(collection: string, key: string): Promise; - getPrivateDataByRange(collection: string, startKey: string, endKey: string): Promise; - getPrivateDataByPartialCompositeKey(collection: string, objectType: string, attributes: string[]): Promise; - getPrivateDataQueryResult(collection: string, query: string): Promise; + getPrivateDataByRange(collection: string, startKey: string, endKey: string): Promise & AsyncIterable; + getPrivateDataByPartialCompositeKey(collection: string, objectType: string, attributes: string[]): Promise & AsyncIterable; + getPrivateDataQueryResult(collection: string, query: string): Promise & AsyncIterable; static RESPONSE_CODE: ResponseCode; } @@ -130,32 +134,29 @@ declare module 'fabric-shim' { export namespace Iterators { - interface CommonIterator extends EventEmitter { + interface CommonIterator { close(): Promise; - next(): Promise; - } - - interface HistoryQueryIterator extends CommonIterator { - next(): Promise; + next(): Promise>; } - interface StateQueryIterator extends CommonIterator { - next(): Promise; - } - - interface NextResult { - value: KV; + interface NextResult { + value: T; done: boolean; } + type HistoryQueryIterator = CommonIterator; + type StateQueryIterator = CommonIterator; + interface NextKeyModificationResult { value: KeyModification; done: boolean; } interface KV { + namespace: string; key: string; value: ProtobufBytes; + getNamespace(): string; getKey(): string; getValue(): ProtobufBytes; } diff --git a/fabric-shim/types/tsconfig.json b/fabric-shim/types/tsconfig.json index 8eabad9c..ed09ed38 100644 --- a/fabric-shim/types/tsconfig.json +++ b/fabric-shim/types/tsconfig.json @@ -22,7 +22,10 @@ "*": [ "node_modules/*" ] - } + }, + "lib": [ + "esnext" + ] }, "files": [ "index.d.ts" diff --git a/test/fv/annotations/tsconfig.json b/test/fv/annotations/tsconfig.json index fe939a27..89a816e6 100644 --- a/test/fv/annotations/tsconfig.json +++ b/test/fv/annotations/tsconfig.json @@ -6,6 +6,9 @@ "module": "commonjs", "declaration": true, "experimentalDecorators": true, - "emitDecoratorMetadata": true + "emitDecoratorMetadata": true, + "lib": [ + "esnext" + ] } } \ No newline at end of file