Skip to content

Commit

Permalink
adds getter for sessionid
Browse files Browse the repository at this point in the history
Signed-off-by: andrew <[email protected]>
  • Loading branch information
andrewxhill committed Feb 6, 2020
1 parent 2106a6f commit 21953bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/ThreadsConfig.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { grpc } from '@improbable-eng/grpc-web'
import { Config } from '@textile/threads-client'
import axios, { AxiosRequestConfig } from 'axios'

type Session = {
id: string
session_id: string
}
export class ThreadsConfig extends Config {
public host: string
public session?: string
public sessionId?: string
constructor(
public token: string,
public deviceId: string,
Expand All @@ -23,7 +24,7 @@ export class ThreadsConfig extends Config {
}
async start(sessionId?: string) {
if (sessionId !== undefined) {
this.session = sessionId
this.sessionId = sessionId
return
}
await this.refreshSession()
Expand Down Expand Up @@ -51,24 +52,24 @@ export class ThreadsConfig extends Config {
if (resp.status !== 200) {
new Error(resp.statusText)
}
this.session = resp.data.session_id
this.sessionId = resp.data.session_id
}
_wrapMetadata(values?: { [key: string]: any }): { [key: string]: any } | undefined {
if (!this.session) {
if (!this.sessionId) {
return values
}
const response = values ?? {}
if ('Authorization' in response || 'authorization' in response) {
return response
}
response['Authorization'] = `Bearer ${this.session}`
response['Authorization'] = `Bearer ${this.sessionId}`
return response
}
_wrapBrowserHeaders(values: grpc.Metadata): grpc.Metadata {
if (!this.session) {
if (!this.sessionId) {
return values
}
values.set('Authorization', `Bearer ${this.session}`)
values.set('Authorization', `Bearer ${this.sessionId}`)
return values
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class API {
return this
}

get sessionId(): string | undefined {
return this._threadsConfig.sessionId
}

get threadsConfig(): ThreadsConfig {
return this._threadsConfig
}
Expand Down

0 comments on commit 21953bf

Please sign in to comment.