Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@
"@babel/preset-typescript": "^7.12.17",
"@tsconfig/node14": "^1.0.0",
"@types/debug": "^4.1.5",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.20",
"@types/lodash.uniqueid": "^4.0.6",
"@types/node": "^14.14.31",
"@types/node-fetch": "^2.5.8",
"@types/qs": "^6.9.5",
"@types/sinon": "^9.0.10",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1",
Expand Down
6 changes: 3 additions & 3 deletions src/dataunion/DataUnion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ export class DataUnion {
/**
* Send a joinRequest, or get into data union instantly with a data union secret
*/
async join(secret?: string): Promise<Todo> {
async join(secret?: string): Promise<JoinResponse> {
const memberAddress = this.client.getAddress() as string
const body: any = {
memberAddress
}
if (secret) { body.secret = secret }

const url = getEndpointUrl(this.client.options.restUrl, 'dataunions', this.contractAddress, 'joinRequests')
const response = await authFetch(
const response = await authFetch<JoinResponse>(
url,
this.client.session,
{
Expand Down Expand Up @@ -330,7 +330,7 @@ export class DataUnion {
*/
async createSecret(name: string = 'Untitled Data Union Secret'): Promise<string> {
const url = getEndpointUrl(this.client.options.restUrl, 'dataunions', this.contractAddress, 'secrets')
const res = await authFetch(
const res = await authFetch<{secret: string}>(
url,
this.client.session,
{
Expand Down
20 changes: 14 additions & 6 deletions src/rest/LoginEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import { getEndpointUrl } from '../utils'

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

export interface UserDetails {
name: string
username: string
imageUrlSmall?: string
imageUrlLarge?: string
lastLogin?: string
}

async function getSessionToken(url: string, props: any) {
return authFetch(
return authFetch<{ token: string }>(
url,
undefined,
{
Expand All @@ -30,7 +38,7 @@ export class LoginEndpoints {
address,
})
const url = getEndpointUrl(this.client.options.restUrl, 'login', 'challenge', address)
return authFetch(
return authFetch<{ challenge: string }>(
url,
undefined,
{
Expand All @@ -39,7 +47,7 @@ export class LoginEndpoints {
)
}

async sendChallengeResponse(challenge: string, signature: string, address: string) {
async sendChallengeResponse(challenge: { challenge: string }, signature: string, address: string) {
this.client.debug('sendChallengeResponse %o', {
challenge,
signature,
Expand Down Expand Up @@ -98,12 +106,12 @@ export class LoginEndpoints {

async getUserInfo() {
this.client.debug('getUserInfo')
return authFetch(`${this.client.options.restUrl}/users/me`, this.client.session)
return authFetch<UserDetails>(`${this.client.options.restUrl}/users/me`, this.client.session)
}

async logoutEndpoint() {
async logoutEndpoint(): Promise<void> {
this.client.debug('logoutEndpoint')
return authFetch(`${this.client.options.restUrl}/logout`, this.client.session, {
await authFetch(`${this.client.options.restUrl}/logout`, this.client.session, {
method: 'POST',
})
}
Expand Down
49 changes: 37 additions & 12 deletions src/rest/StreamEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { isKeyExchangeStream } from '../stream/KeyExchange'
import authFetch from './authFetch'
import { Todo } from '../types'
import StreamrClient from '../StreamrClient'
// TODO change this import when streamr-client-protocol exports StreamMessage type or the enums types directly
import { ContentType, EncryptionType, SignatureType, StreamMessageType } from 'streamr-client-protocol/dist/src/protocol/message_layer/StreamMessage'

const debug = debugFactory('StreamrClient')

Expand All @@ -30,6 +32,28 @@ export interface StreamListQuery {
operation?: StreamOperation
}

export interface StreamValidationInfo {
partitions: number,
requireSignedData: boolean
requireEncryptedData: boolean
}

export interface StreamMessageAsObject { // TODO this could be in streamr-protocol
streamId: string
streamPartition: number
timestamp: number
sequenceNumber: number
publisherId: string
msgChainId: string
messageType: StreamMessageType
contentType: ContentType
encryptionType: EncryptionType
groupKeyId: string|null
content: any
signatureType: SignatureType
signature: string|null
}

const agentSettings = {
keepAlive: true,
keepAliveMsecs: 5000,
Expand Down Expand Up @@ -74,7 +98,7 @@ export class StreamEndpoints {

const url = getEndpointUrl(this.client.options.restUrl, 'streams', streamId)
try {
const json = await authFetch(url, this.client.session)
const json = await authFetch<StreamProperties>(url, this.client.session)
return new Stream(this.client, json)
} catch (e) {
if (e.response && e.response.status === 404) {
Expand All @@ -89,8 +113,8 @@ export class StreamEndpoints {
query,
})
const url = getEndpointUrl(this.client.options.restUrl, 'streams') + '?' + qs.stringify(query)
const json = await authFetch(url, this.client.session)
return json ? json.map((stream: any) => new Stream(this.client, stream)) : []
const json = await authFetch<StreamProperties[]>(url, this.client.session)
return json ? json.map((stream: StreamProperties) => new Stream(this.client, stream)) : []
}

async getStreamByName(name: string) {
Expand All @@ -110,7 +134,7 @@ export class StreamEndpoints {
props,
})

const json = await authFetch(
const json = await authFetch<StreamProperties>(
getEndpointUrl(this.client.options.restUrl, 'streams'),
this.client.session,
{
Expand Down Expand Up @@ -153,7 +177,7 @@ export class StreamEndpoints {
streamId,
})
const url = getEndpointUrl(this.client.options.restUrl, 'streams', streamId, 'publishers')
const json = await authFetch(url, this.client.session)
const json = await authFetch<{ addresses: string[]}>(url, this.client.session)
return json.addresses.map((a: string) => a.toLowerCase())
}

Expand All @@ -180,7 +204,7 @@ export class StreamEndpoints {
streamId,
})
const url = getEndpointUrl(this.client.options.restUrl, 'streams', streamId, 'subscribers')
const json = await authFetch(url, this.client.session)
const json = await authFetch<{ addresses: string[] }>(url, this.client.session)
return json.addresses.map((a: string) => a.toLowerCase())
}

Expand All @@ -206,11 +230,11 @@ export class StreamEndpoints {
streamId,
})
const url = getEndpointUrl(this.client.options.restUrl, 'streams', streamId, 'validation')
const json = await authFetch(url, this.client.session)
const json = await authFetch<StreamValidationInfo>(url, this.client.session)
return json
}

async getStreamLast(streamObjectOrId: Stream|string) {
async getStreamLast(streamObjectOrId: Stream|string): Promise<StreamMessageAsObject> {
const { streamId, streamPartition = 0, count = 1 } = validateOptions(streamObjectOrId)
this.client.debug('getStreamLast %o', {
streamId,
Expand All @@ -223,14 +247,15 @@ export class StreamEndpoints {
+ `?${qs.stringify({ count })}`
)

const json = await authFetch(url, this.client.session)
const json = await authFetch<Todo>(url, this.client.session)
return json
}

async getStreamPartsByStorageNode(address: string) {
const json = await authFetch(getEndpointUrl(this.client.options.restUrl, 'storageNodes', address, 'streams'), this.client.session)
type ItemType = { id: string, partitions: number}
const json = await authFetch<ItemType[]>(getEndpointUrl(this.client.options.restUrl, 'storageNodes', address, 'streams'), this.client.session)
let result: StreamPart[] = []
json.forEach((stream: { id: string, partitions: number }) => {
json.forEach((stream: ItemType) => {
result = result.concat(StreamPart.fromStream(stream))
})
return result
Expand All @@ -248,7 +273,7 @@ export class StreamEndpoints {
})

// Send data to the stream
return authFetch(
await authFetch(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not return?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the success response POST /​streams/​id/​data is always empty. So it is better to return void than an empty object. Error response rejects the promise.

getEndpointUrl(this.client.options.restUrl, 'streams', streamId, 'data'),
this.client.session,
{
Expand Down
Loading