Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit e047453

Browse files
authored
Types for authFetch (#207)
Type annotations for authFetch endpoints (and utils.test.ts).
1 parent 17c996c commit e047453

File tree

9 files changed

+221
-47
lines changed

9 files changed

+221
-47
lines changed

package-lock.json

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@
5858
"@babel/preset-typescript": "^7.12.17",
5959
"@tsconfig/node14": "^1.0.0",
6060
"@types/debug": "^4.1.5",
61+
"@types/express": "^4.17.11",
6162
"@types/jest": "^26.0.20",
6263
"@types/lodash.uniqueid": "^4.0.6",
6364
"@types/node": "^14.14.31",
65+
"@types/node-fetch": "^2.5.8",
6466
"@types/qs": "^6.9.5",
67+
"@types/sinon": "^9.0.10",
6568
"@types/uuid": "^8.3.0",
6669
"@typescript-eslint/eslint-plugin": "^4.15.1",
6770
"@typescript-eslint/parser": "^4.15.1",

src/dataunion/DataUnion.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ export class DataUnion {
9292
/**
9393
* Send a joinRequest, or get into data union instantly with a data union secret
9494
*/
95-
async join(secret?: string): Promise<Todo> {
95+
async join(secret?: string): Promise<JoinResponse> {
9696
const memberAddress = this.client.getAddress() as string
9797
const body: any = {
9898
memberAddress
9999
}
100100
if (secret) { body.secret = secret }
101101

102102
const url = getEndpointUrl(this.client.options.restUrl, 'dataunions', this.contractAddress, 'joinRequests')
103-
const response = await authFetch(
103+
const response = await authFetch<JoinResponse>(
104104
url,
105105
this.client.session,
106106
{
@@ -330,7 +330,7 @@ export class DataUnion {
330330
*/
331331
async createSecret(name: string = 'Untitled Data Union Secret'): Promise<string> {
332332
const url = getEndpointUrl(this.client.options.restUrl, 'dataunions', this.contractAddress, 'secrets')
333-
const res = await authFetch(
333+
const res = await authFetch<{secret: string}>(
334334
url,
335335
this.client.session,
336336
{

src/rest/LoginEndpoints.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ import { getEndpointUrl } from '../utils'
33

44
import authFetch, { AuthFetchError } from './authFetch'
55

6+
export interface UserDetails {
7+
name: string
8+
username: string
9+
imageUrlSmall?: string
10+
imageUrlLarge?: string
11+
lastLogin?: string
12+
}
13+
614
async function getSessionToken(url: string, props: any) {
7-
return authFetch(
15+
return authFetch<{ token: string }>(
816
url,
917
undefined,
1018
{
@@ -30,7 +38,7 @@ export class LoginEndpoints {
3038
address,
3139
})
3240
const url = getEndpointUrl(this.client.options.restUrl, 'login', 'challenge', address)
33-
return authFetch(
41+
return authFetch<{ challenge: string }>(
3442
url,
3543
undefined,
3644
{
@@ -39,7 +47,7 @@ export class LoginEndpoints {
3947
)
4048
}
4149

42-
async sendChallengeResponse(challenge: string, signature: string, address: string) {
50+
async sendChallengeResponse(challenge: { challenge: string }, signature: string, address: string) {
4351
this.client.debug('sendChallengeResponse %o', {
4452
challenge,
4553
signature,
@@ -98,12 +106,12 @@ export class LoginEndpoints {
98106

99107
async getUserInfo() {
100108
this.client.debug('getUserInfo')
101-
return authFetch(`${this.client.options.restUrl}/users/me`, this.client.session)
109+
return authFetch<UserDetails>(`${this.client.options.restUrl}/users/me`, this.client.session)
102110
}
103111

104-
async logoutEndpoint() {
112+
async logoutEndpoint(): Promise<void> {
105113
this.client.debug('logoutEndpoint')
106-
return authFetch(`${this.client.options.restUrl}/logout`, this.client.session, {
114+
await authFetch(`${this.client.options.restUrl}/logout`, this.client.session, {
107115
method: 'POST',
108116
})
109117
}

src/rest/StreamEndpoints.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { isKeyExchangeStream } from '../stream/KeyExchange'
1313
import authFetch from './authFetch'
1414
import { Todo } from '../types'
1515
import 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

1719
const 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+
3357
const 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

Comments
 (0)