From 2106a6f903662029015b968c38cecc1e70c82f96 Mon Sep 17 00:00:00 2001 From: andrew Date: Thu, 6 Feb 2020 13:13:29 -0800 Subject: [PATCH 1/2] sessions: allow app to restart from previous session Signed-off-by: andrew --- src/ThreadsConfig.ts | 6 +++++- src/index.ts | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ThreadsConfig.ts b/src/ThreadsConfig.ts index 0143aaf1d..7570561ae 100644 --- a/src/ThreadsConfig.ts +++ b/src/ThreadsConfig.ts @@ -21,7 +21,11 @@ export class ThreadsConfig extends Config { super() this.host = `${this.apiScheme}://${this.api}:${this.threadsPort}` } - async start() { + async start(sessionId?: string) { + if (sessionId !== undefined) { + this.session = sessionId + return + } await this.refreshSession() } get sessionAPI(): string { diff --git a/src/index.ts b/src/index.ts index 6d41118c6..72333aed5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,8 +50,8 @@ export class API { ) } - async start() { - await this._threadsConfig.start() + async start(sessionId?: string) { + await this._threadsConfig.start(sessionId) return this } From 21953bf6d78568aea4650a74ec5bd85f6db607d0 Mon Sep 17 00:00:00 2001 From: andrew Date: Thu, 6 Feb 2020 13:18:09 -0800 Subject: [PATCH 2/2] adds getter for sessionid Signed-off-by: andrew --- src/ThreadsConfig.ts | 15 ++++++++------- src/index.ts | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ThreadsConfig.ts b/src/ThreadsConfig.ts index 7570561ae..ad831659b 100644 --- a/src/ThreadsConfig.ts +++ b/src/ThreadsConfig.ts @@ -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, @@ -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() @@ -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 } } diff --git a/src/index.ts b/src/index.ts index 72333aed5..cd92e96b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,6 +55,10 @@ export class API { return this } + get sessionId(): string | undefined { + return this._threadsConfig.sessionId + } + get threadsConfig(): ThreadsConfig { return this._threadsConfig }