fix: fixes issue where some accounts were not correctly loading subscription info (#2361)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { joinPaths, sleep } from '@standardnotes/utils'
|
import { joinPaths, sleep } from '@standardnotes/utils'
|
||||||
import { Environment } from '@standardnotes/models'
|
import { Environment } from '@standardnotes/models'
|
||||||
import { Session, SessionToken } from '@standardnotes/domain-core'
|
import { LegacySession, Session, SessionToken } from '@standardnotes/domain-core'
|
||||||
import {
|
import {
|
||||||
HttpStatusCode,
|
HttpStatusCode,
|
||||||
HttpRequestParams,
|
HttpRequestParams,
|
||||||
@@ -18,7 +18,7 @@ import { FetchRequestHandler } from './FetchRequestHandler'
|
|||||||
import { RequestHandlerInterface } from './RequestHandlerInterface'
|
import { RequestHandlerInterface } from './RequestHandlerInterface'
|
||||||
|
|
||||||
export class HttpService implements HttpServiceInterface {
|
export class HttpService implements HttpServiceInterface {
|
||||||
private session: Session | null
|
private session?: Session | LegacySession
|
||||||
private __latencySimulatorMs?: number
|
private __latencySimulatorMs?: number
|
||||||
private declare host: string
|
private declare host: string
|
||||||
|
|
||||||
@@ -29,7 +29,6 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
private requestHandler: RequestHandlerInterface
|
private requestHandler: RequestHandlerInterface
|
||||||
|
|
||||||
constructor(private environment: Environment, private appVersion: string, private snjsVersion: string) {
|
constructor(private environment: Environment, private appVersion: string, private snjsVersion: string) {
|
||||||
this.session = null
|
|
||||||
this.requestHandler = new FetchRequestHandler(this.snjsVersion, this.appVersion, this.environment)
|
this.requestHandler = new FetchRequestHandler(this.snjsVersion, this.appVersion, this.environment)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,12 +41,12 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public deinit(): void {
|
public deinit(): void {
|
||||||
this.session = null
|
;(this.session as unknown) = undefined
|
||||||
;(this.updateMetaCallback as unknown) = undefined
|
;(this.updateMetaCallback as unknown) = undefined
|
||||||
;(this.refreshSessionCallback as unknown) = undefined
|
;(this.refreshSessionCallback as unknown) = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
setSession(session: Session): void {
|
setSession(session: Session | LegacySession): void {
|
||||||
this.session = session
|
this.session = session
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,6 +58,18 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
return this.host
|
return this.host
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getSessionAccessToken(): string | undefined {
|
||||||
|
if (!this.session) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.session instanceof Session) {
|
||||||
|
return this.session.accessToken.value
|
||||||
|
} else {
|
||||||
|
return this.session.accessToken
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async get<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>> {
|
async get<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>> {
|
||||||
if (!this.host) {
|
if (!this.host) {
|
||||||
throw new Error('Attempting to make network request before host is set')
|
throw new Error('Attempting to make network request before host is set')
|
||||||
@@ -68,7 +79,7 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
url: joinPaths(this.host, path),
|
url: joinPaths(this.host, path),
|
||||||
params,
|
params,
|
||||||
verb: HttpVerb.Get,
|
verb: HttpVerb.Get,
|
||||||
authentication: authentication ?? this.session?.accessToken.value,
|
authentication: authentication ?? this.getSessionAccessToken(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +101,7 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
url: joinPaths(this.host, path),
|
url: joinPaths(this.host, path),
|
||||||
params,
|
params,
|
||||||
verb: HttpVerb.Post,
|
verb: HttpVerb.Post,
|
||||||
authentication: authentication ?? this.session?.accessToken.value,
|
authentication: authentication ?? this.getSessionAccessToken(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +110,7 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
url: joinPaths(this.host, path),
|
url: joinPaths(this.host, path),
|
||||||
params,
|
params,
|
||||||
verb: HttpVerb.Put,
|
verb: HttpVerb.Put,
|
||||||
authentication: authentication ?? this.session?.accessToken.value,
|
authentication: authentication ?? this.getSessionAccessToken(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +119,7 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
url: joinPaths(this.host, path),
|
url: joinPaths(this.host, path),
|
||||||
params,
|
params,
|
||||||
verb: HttpVerb.Patch,
|
verb: HttpVerb.Patch,
|
||||||
authentication: authentication ?? this.session?.accessToken.value,
|
authentication: authentication ?? this.getSessionAccessToken(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +128,7 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
url: joinPaths(this.host, path),
|
url: joinPaths(this.host, path),
|
||||||
params,
|
params,
|
||||||
verb: HttpVerb.Delete,
|
verb: HttpVerb.Delete,
|
||||||
authentication: authentication ?? this.session?.accessToken.value,
|
authentication: authentication ?? this.getSessionAccessToken(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +141,7 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
if (this.inProgressRefreshSessionPromise && !isRefreshRequest) {
|
if (this.inProgressRefreshSessionPromise && !isRefreshRequest) {
|
||||||
await this.inProgressRefreshSessionPromise
|
await this.inProgressRefreshSessionPromise
|
||||||
|
|
||||||
httpRequest.authentication = this.session?.accessToken.value
|
httpRequest.authentication = this.getSessionAccessToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await this.requestHandler.handleRequest<T>(httpRequest)
|
const response = await this.requestHandler.handleRequest<T>(httpRequest)
|
||||||
@@ -152,7 +163,7 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
httpRequest.authentication = this.session?.accessToken.value
|
httpRequest.authentication = this.getSessionAccessToken()
|
||||||
|
|
||||||
return this.runHttp(httpRequest)
|
return this.runHttp(httpRequest)
|
||||||
}
|
}
|
||||||
@@ -161,7 +172,11 @@ export class HttpService implements HttpServiceInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async refreshSession(): Promise<boolean> {
|
private async refreshSession(): Promise<boolean> {
|
||||||
if (this.session === null) {
|
if (!this.session) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.session instanceof LegacySession) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { Session } from '@standardnotes/domain-core'
|
import { LegacySession, Session } from '@standardnotes/domain-core'
|
||||||
import { HttpRequest, HttpRequestParams, HttpResponse, HttpResponseMeta } from '@standardnotes/responses'
|
import { HttpRequest, HttpRequestParams, HttpResponse, HttpResponseMeta } from '@standardnotes/responses'
|
||||||
|
|
||||||
export interface HttpServiceInterface {
|
export interface HttpServiceInterface {
|
||||||
setHost(host: string): void
|
setHost(host: string): void
|
||||||
getHost(): string
|
getHost(): string
|
||||||
setSession(session: Session): void
|
|
||||||
get<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
get<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
||||||
getExternal<T>(url: string, params?: HttpRequestParams): Promise<HttpResponse<T>>
|
getExternal<T>(url: string, params?: HttpRequestParams): Promise<HttpResponse<T>>
|
||||||
post<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
post<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
||||||
@@ -12,9 +12,12 @@ export interface HttpServiceInterface {
|
|||||||
patch<T>(path: string, params: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
patch<T>(path: string, params: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
||||||
delete<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
delete<T>(path: string, params?: HttpRequestParams, authentication?: string): Promise<HttpResponse<T>>
|
||||||
runHttp<T>(httpRequest: HttpRequest): Promise<HttpResponse<T>>
|
runHttp<T>(httpRequest: HttpRequest): Promise<HttpResponse<T>>
|
||||||
|
|
||||||
|
setSession(session: Session | LegacySession): void
|
||||||
setCallbacks(
|
setCallbacks(
|
||||||
updateMetaCallback: (meta: HttpResponseMeta) => void,
|
updateMetaCallback: (meta: HttpResponseMeta) => void,
|
||||||
refreshSessionCallback: (session: Session) => void,
|
refreshSessionCallback: (session: Session) => void,
|
||||||
): void
|
): void
|
||||||
|
|
||||||
deinit(): void
|
deinit(): void
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,9 +169,7 @@ export class SNSessionManager
|
|||||||
private setSession(session: Session | LegacySession, persist = true): void {
|
private setSession(session: Session | LegacySession, persist = true): void {
|
||||||
this.session = session
|
this.session = session
|
||||||
|
|
||||||
if (session instanceof Session) {
|
this.httpService.setSession(session)
|
||||||
this.httpService.setSession(session)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.apiService.setSession(session, persist)
|
this.apiService.setSession(session, persist)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user