refactor: http service (#2233)

This commit is contained in:
Mo
2023-02-28 20:43:25 -06:00
committed by GitHub
parent 6e7618b258
commit e7f1d35341
142 changed files with 1116 additions and 1307 deletions

View File

@@ -83,7 +83,7 @@ import {
ItemStream,
Platform,
} from '@standardnotes/models'
import { ClientDisplayableError } from '@standardnotes/responses'
import { ClientDisplayableError, SessionListEntry } from '@standardnotes/responses'
import { SnjsVersion } from './../Version'
import { SNLog } from '../Log'
@@ -133,7 +133,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
/**
* @deprecated will be fully replaced by @standardnotes/api::HttpService
*/
private deprecatedHttpService!: InternalServices.SNHttpService
private deprecatedHttpService!: InternalServices.DeprecatedHttpService
private declare httpService: HttpServiceInterface
private payloadManager!: InternalServices.PayloadManager
public protocolService!: EncryptionService
@@ -599,13 +599,13 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
return this.syncService.isDatabaseLoaded()
}
public getSessions(): Promise<
(Responses.HttpResponse & { data: InternalServices.RemoteSession[] }) | Responses.HttpResponse
> {
public getSessions(): Promise<Responses.HttpResponse<SessionListEntry[]>> {
return this.sessionManager.getSessionsList()
}
public async revokeSession(sessionId: UuidString): Promise<Responses.HttpResponse | undefined> {
public async revokeSession(
sessionId: UuidString,
): Promise<Responses.HttpResponse<Responses.SessionListResponse> | undefined> {
if (await this.protectionService.authorizeSessionRevoking()) {
return this.sessionManager.revokeSession(sessionId)
}
@@ -627,7 +627,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
return Common.compareVersions(userVersion, Common.ProtocolVersion.V004) >= 0
}
public async getUserSubscription(): Promise<Subscription | Responses.ClientDisplayableError> {
public async getUserSubscription(): Promise<Subscription | Responses.ClientDisplayableError | undefined> {
return this.sessionManager.getSubscription()
}
@@ -897,6 +897,9 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
service.deinit()
}
this.httpService.deinit()
;(this.httpService as unknown) = undefined
this.options.crypto.deinit()
;(this.options as unknown) = undefined
@@ -939,7 +942,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
ephemeral = false,
mergeLocal = true,
awaitSync = false,
): Promise<Responses.HttpResponse | Responses.SignInResponse> {
): Promise<Responses.HttpResponse<Responses.SignInResponse>> {
return this.userService.signIn(email, password, strict, ephemeral, mergeLocal, awaitSync)
}
@@ -1161,8 +1164,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
this.diskStorageService.provideEncryptionProvider(this.protocolService)
this.createChallengeService()
this.createLegacyHttpManager()
this.createApiService()
this.createHttpService()
this.createHttpServiceAndApiService()
this.createUserServer()
this.createUserRequestServer()
this.createUserApiService()
@@ -1419,20 +1421,6 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
this.services.push(this.userService)
}
private createApiService() {
this.apiService = new InternalServices.SNApiService(
this.deprecatedHttpService,
this.diskStorageService,
this.options.defaultHost,
this.inMemoryStore,
this.options.crypto,
this.sessionStorageMapper,
this.legacySessionStorageMapper,
this.internalEventBus,
)
this.services.push(this.apiService)
}
private createUserApiService() {
this.userApiService = new UserApiService(this.userServer, this.userRequestServer)
}
@@ -1486,7 +1474,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
}
private createLegacyHttpManager() {
this.deprecatedHttpService = new InternalServices.SNHttpService(
this.deprecatedHttpService = new InternalServices.DeprecatedHttpService(
this.environment,
this.options.appVersion,
this.internalEventBus,
@@ -1494,11 +1482,22 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
this.services.push(this.deprecatedHttpService)
}
private createHttpService() {
this.httpService = new HttpService(
this.environment,
this.options.appVersion,
SnjsVersion,
private createHttpServiceAndApiService() {
this.httpService = new HttpService(this.environment, this.options.appVersion, SnjsVersion)
this.apiService = new InternalServices.SNApiService(
this.httpService,
this.diskStorageService,
this.options.defaultHost,
this.inMemoryStore,
this.options.crypto,
this.sessionStorageMapper,
this.legacySessionStorageMapper,
this.internalEventBus,
)
this.services.push(this.apiService)
this.httpService.setCallbacks(
this.apiService.processMetaObject.bind(this.apiService),
this.apiService.setSession.bind(this.apiService),
)