mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 11:35:04 +01:00
Use union of types for RegistrationResponse
This commit is contained in:
parent
a249a1b2b5
commit
fe0add01ee
@ -17,7 +17,7 @@ limitations under the License.
|
|||||||
import type {HomeServerApi} from "../net/HomeServerApi";
|
import type {HomeServerApi} from "../net/HomeServerApi";
|
||||||
import {registrationStageFromType} from "./registrationStageFromType";
|
import {registrationStageFromType} from "./registrationStageFromType";
|
||||||
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
|
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;
|
type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void;
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ export class Registration {
|
|||||||
return this.parseStagesFromResponse(response);
|
return this.parseStagesFromResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
parseStagesFromResponse(response: RegistrationResponse): BaseRegistrationStage {
|
parseStagesFromResponse(response: RegistrationResponse401): BaseRegistrationStage {
|
||||||
const { session, params } = response;
|
const { session, params } = response;
|
||||||
const flow = this._flowSelector(response.flows);
|
const flow = this._flowSelector(response.flows);
|
||||||
if (!flow) {
|
if (!flow) {
|
||||||
|
@ -48,14 +48,14 @@ export abstract class BaseRegistrationStage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parseResponse(response: RegistrationResponse) {
|
parseResponse(response: RegistrationResponse) {
|
||||||
if (response.user_id) {
|
if ("user_id" in response) {
|
||||||
// registration completed successfully
|
// registration completed successfully
|
||||||
return response.user_id;
|
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;
|
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);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,9 @@ export type RegistrationDetails = {
|
|||||||
inhibitLogin: boolean;
|
inhibitLogin: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RegistrationResponse = RegistrationResponse401 & RegistrationResponseError & RegistrationResponseSuccess;
|
export type RegistrationResponse = RegistrationResponse401 | RegistrationResponseError | RegistrationResponseSuccess;
|
||||||
|
|
||||||
type RegistrationResponse401 = {
|
export type RegistrationResponse401 = {
|
||||||
completed: string[];
|
completed: string[];
|
||||||
flows: RegistrationFlow[];
|
flows: RegistrationFlow[];
|
||||||
params: Record<string, any>;
|
params: Record<string, any>;
|
||||||
|
Loading…
Reference in New Issue
Block a user