@@ -13,6 +13,8 @@ import { isKeyExchangeStream } from '../stream/KeyExchange'
1313import authFetch from './authFetch'
1414import { Todo } from '../types'
1515import StreamrClient from '../StreamrClient'
16+ // TODO change this import when streamr-client-protocol exports StreamMessage type or the enums types directly
17+ import { ContentType , EncryptionType , SignatureType , StreamMessageType } from 'streamr-client-protocol/dist/src/protocol/message_layer/StreamMessage'
1618
1719const debug = debugFactory ( 'StreamrClient' )
1820
@@ -30,6 +32,28 @@ export interface StreamListQuery {
3032 operation ?: StreamOperation
3133}
3234
35+ export interface StreamValidationInfo {
36+ partitions : number ,
37+ requireSignedData : boolean
38+ requireEncryptedData : boolean
39+ }
40+
41+ export interface StreamMessageAsObject { // TODO this could be in streamr-protocol
42+ streamId : string
43+ streamPartition : number
44+ timestamp : number
45+ sequenceNumber : number
46+ publisherId : string
47+ msgChainId : string
48+ messageType : StreamMessageType
49+ contentType : ContentType
50+ encryptionType : EncryptionType
51+ groupKeyId : string | null
52+ content : any
53+ signatureType : SignatureType
54+ signature : string | null
55+ }
56+
3357const agentSettings = {
3458 keepAlive : true ,
3559 keepAliveMsecs : 5000 ,
@@ -74,7 +98,7 @@ export class StreamEndpoints {
7498
7599 const url = getEndpointUrl ( this . client . options . restUrl , 'streams' , streamId )
76100 try {
77- const json = await authFetch ( url , this . client . session )
101+ const json = await authFetch < StreamProperties > ( url , this . client . session )
78102 return new Stream ( this . client , json )
79103 } catch ( e ) {
80104 if ( e . response && e . response . status === 404 ) {
@@ -89,8 +113,8 @@ export class StreamEndpoints {
89113 query,
90114 } )
91115 const url = getEndpointUrl ( this . client . options . restUrl , 'streams' ) + '?' + qs . stringify ( query )
92- const json = await authFetch ( url , this . client . session )
93- return json ? json . map ( ( stream : any ) => new Stream ( this . client , stream ) ) : [ ]
116+ const json = await authFetch < StreamProperties [ ] > ( url , this . client . session )
117+ return json ? json . map ( ( stream : StreamProperties ) => new Stream ( this . client , stream ) ) : [ ]
94118 }
95119
96120 async getStreamByName ( name : string ) {
@@ -110,7 +134,7 @@ export class StreamEndpoints {
110134 props,
111135 } )
112136
113- const json = await authFetch (
137+ const json = await authFetch < StreamProperties > (
114138 getEndpointUrl ( this . client . options . restUrl , 'streams' ) ,
115139 this . client . session ,
116140 {
@@ -153,7 +177,7 @@ export class StreamEndpoints {
153177 streamId,
154178 } )
155179 const url = getEndpointUrl ( this . client . options . restUrl , 'streams' , streamId , 'publishers' )
156- const json = await authFetch ( url , this . client . session )
180+ const json = await authFetch < { addresses : string [ ] } > ( url , this . client . session )
157181 return json . addresses . map ( ( a : string ) => a . toLowerCase ( ) )
158182 }
159183
@@ -180,7 +204,7 @@ export class StreamEndpoints {
180204 streamId,
181205 } )
182206 const url = getEndpointUrl ( this . client . options . restUrl , 'streams' , streamId , 'subscribers' )
183- const json = await authFetch ( url , this . client . session )
207+ const json = await authFetch < { addresses : string [ ] } > ( url , this . client . session )
184208 return json . addresses . map ( ( a : string ) => a . toLowerCase ( ) )
185209 }
186210
@@ -206,11 +230,11 @@ export class StreamEndpoints {
206230 streamId,
207231 } )
208232 const url = getEndpointUrl ( this . client . options . restUrl , 'streams' , streamId , 'validation' )
209- const json = await authFetch ( url , this . client . session )
233+ const json = await authFetch < StreamValidationInfo > ( url , this . client . session )
210234 return json
211235 }
212236
213- async getStreamLast ( streamObjectOrId : Stream | string ) {
237+ async getStreamLast ( streamObjectOrId : Stream | string ) : Promise < StreamMessageAsObject > {
214238 const { streamId, streamPartition = 0 , count = 1 } = validateOptions ( streamObjectOrId )
215239 this . client . debug ( 'getStreamLast %o' , {
216240 streamId,
@@ -223,14 +247,15 @@ export class StreamEndpoints {
223247 + `?${ qs . stringify ( { count } ) } `
224248 )
225249
226- const json = await authFetch ( url , this . client . session )
250+ const json = await authFetch < Todo > ( url , this . client . session )
227251 return json
228252 }
229253
230254 async getStreamPartsByStorageNode ( address : string ) {
231- const json = await authFetch ( getEndpointUrl ( this . client . options . restUrl , 'storageNodes' , address , 'streams' ) , this . client . session )
255+ type ItemType = { id : string , partitions : number }
256+ const json = await authFetch < ItemType [ ] > ( getEndpointUrl ( this . client . options . restUrl , 'storageNodes' , address , 'streams' ) , this . client . session )
232257 let result : StreamPart [ ] = [ ]
233- json . forEach ( ( stream : { id : string , partitions : number } ) => {
258+ json . forEach ( ( stream : ItemType ) => {
234259 result = result . concat ( StreamPart . fromStream ( stream ) )
235260 } )
236261 return result
@@ -248,7 +273,7 @@ export class StreamEndpoints {
248273 } )
249274
250275 // Send data to the stream
251- return authFetch (
276+ await authFetch (
252277 getEndpointUrl ( this . client . options . restUrl , 'streams' , streamId , 'data' ) ,
253278 this . client . session ,
254279 {
0 commit comments