Use union of types for RegistrationResponse

This commit is contained in:
RMidhunSuresh 2022-02-02 16:09:01 +05:30
parent a249a1b2b5
commit fe0add01ee
3 changed files with 7 additions and 7 deletions

View file

@ -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) {

View file

@ -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);
}
}

View file

@ -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<string, any>;