Check response code instead of existence of props

This commit is contained in:
RMidhunSuresh 2022-02-03 18:08:19 +05:30
parent c4894f2c24
commit 0ad0ecfcc2
3 changed files with 30 additions and 12 deletions

View file

@ -23,6 +23,7 @@ import type {IRequestOptions} from "../../platform/types/types.js";
export interface IHomeServerRequest { export interface IHomeServerRequest {
abort(): void; abort(): void;
response(): Promise<any>; response(): Promise<any>;
responseCode(): Promise<number>;
} }
export class HomeServerRequest implements IHomeServerRequest { export class HomeServerRequest implements IHomeServerRequest {
@ -110,6 +111,11 @@ export class HomeServerRequest implements IHomeServerRequest {
response(): Promise<any> { response(): Promise<any> {
return this._promise; return this._promise;
} }
async responseCode(): Promise<number> {
const response = await this._sourceRequest.response();
return response.status;
}
} }
import {Request as MockRequest} from "../../mocks/Request.js"; import {Request as MockRequest} from "../../mocks/Request.js";

View file

@ -57,8 +57,11 @@ export class Registration {
async submitStage(stage: BaseRegistrationStage): Promise<BaseRegistrationStage | undefined> { async submitStage(stage: BaseRegistrationStage): Promise<BaseRegistrationStage | undefined> {
const auth = stage.generateAuthenticationData(); const auth = stage.generateAuthenticationData();
const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails; const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails;
const response = await this._hsApi.register(username, password, initialDeviceDisplayName, auth, inhibitLogin).response(); const request = this._hsApi.register(username, password, initialDeviceDisplayName, auth, inhibitLogin);
return this.parseRegistrationResponse(response, stage); const response = await request.response();
const status = await request.responseCode();
const registrationResponse: RegistrationResponse = { ...response, status };
return this.parseRegistrationResponse(registrationResponse, stage);
} }
parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage { parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage {
@ -86,18 +89,24 @@ export class Registration {
return firstStage!; return firstStage!;
} }
parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) { async parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) {
if ("user_id" in response) { switch (response.status) {
// registration completed successfully case 200:
this._sessionInfo = response; this._sessionInfo = response;
return undefined; return undefined;
} case 401:
else if ("completed" in response && response.completed.find(c => c === currentStage.type)) { if (response.completed?.includes(currentStage.type)) {
return currentStage.nextStage; return currentStage.nextStage;
} }
else {
throw new Error("This stage could not be completed!");
}
case 400:
default:
const error = "error" in response? 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);
} }
}
get sessionInfo(): RegistrationResponseSuccess | undefined { get sessionInfo(): RegistrationResponseSuccess | undefined {
return this._sessionInfo; return this._sessionInfo;

View file

@ -28,17 +28,20 @@ export type RegistrationResponseMoreDataNeeded = {
flows: RegistrationFlow[]; flows: RegistrationFlow[];
params: Record<string, any>; params: Record<string, any>;
session: string; session: string;
status: 401;
} }
type RegistrationResponseError = { type RegistrationResponseError = {
errcode: string; errcode: string;
error: string; error: string;
status: 400;
} }
export type RegistrationResponseSuccess = { export type RegistrationResponseSuccess = {
user_id: string; user_id: string;
device_id: string; device_id: string;
access_token?: string; access_token?: string;
status: 200;
} }
export type RegistrationFlow = { export type RegistrationFlow = {