From fe0add01ee7c24c0b91781db062109424a5366c3 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 2 Feb 2022 16:09:01 +0530 Subject: [PATCH] Use union of types for RegistrationResponse --- src/matrix/registration/Registration.ts | 4 ++-- src/matrix/registration/stages/BaseRegistrationStage.ts | 6 +++--- src/matrix/registration/types/types.ts | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/matrix/registration/Registration.ts b/src/matrix/registration/Registration.ts index 15006ce2..8b4d54f9 100644 --- a/src/matrix/registration/Registration.ts +++ b/src/matrix/registration/Registration.ts @@ -17,7 +17,7 @@ limitations under the License. import type {HomeServerApi} from "../net/HomeServerApi"; import {registrationStageFromType} from "./registrationStageFromType"; import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage"; -import type {RegistrationDetails, RegistrationResponse, RegistrationFlow} from "./types/types"; +import type {RegistrationDetails, RegistrationFlow, RegistrationResponse401} from "./types/types"; type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void; @@ -42,7 +42,7 @@ export class Registration { return this.parseStagesFromResponse(response); } - parseStagesFromResponse(response: RegistrationResponse): BaseRegistrationStage { + parseStagesFromResponse(response: RegistrationResponse401): BaseRegistrationStage { const { session, params } = response; const flow = this._flowSelector(response.flows); if (!flow) { diff --git a/src/matrix/registration/stages/BaseRegistrationStage.ts b/src/matrix/registration/stages/BaseRegistrationStage.ts index 62325b4c..7b41379c 100644 --- a/src/matrix/registration/stages/BaseRegistrationStage.ts +++ b/src/matrix/registration/stages/BaseRegistrationStage.ts @@ -48,14 +48,14 @@ export abstract class BaseRegistrationStage { } parseResponse(response: RegistrationResponse) { - if (response.user_id) { + if ("user_id" in response) { // registration completed successfully return response.user_id; } - else if (response.completed?.find(c => c === this.type)) { + else if ("completed" in response && response.completed?.find(c => c === this.type)) { return this._nextStage; } - const error = response.error ?? "Could not parse response"; + const error = "error" in response? response.error: "Could not parse response"; throw new Error(error); } } diff --git a/src/matrix/registration/types/types.ts b/src/matrix/registration/types/types.ts index 97dca1be..5661e268 100644 --- a/src/matrix/registration/types/types.ts +++ b/src/matrix/registration/types/types.ts @@ -21,9 +21,9 @@ export type RegistrationDetails = { inhibitLogin: boolean; } -export type RegistrationResponse = RegistrationResponse401 & RegistrationResponseError & RegistrationResponseSuccess; +export type RegistrationResponse = RegistrationResponse401 | RegistrationResponseError | RegistrationResponseSuccess; -type RegistrationResponse401 = { +export type RegistrationResponse401 = { completed: string[]; flows: RegistrationFlow[]; params: Record;