From 92627555c6b0d56c96a95e31780e3015b3e637e9 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Sat, 4 Sep 2021 22:10:28 +0530 Subject: refactoring: Use 'ifUndefined*' consistently. Added some error checking --- src/models/News.ts | 4 ++++ src/models/RecipePreview.ts | 16 ++++++++++++++-- src/models/Service.js | 13 ++++++------- src/models/User.ts | 4 ++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/models/News.ts b/src/models/News.ts index d1ae6fcef..a6ff86dda 100644 --- a/src/models/News.ts +++ b/src/models/News.ts @@ -19,6 +19,10 @@ export default class News { sticky: boolean = false; constructor(data: INews) { + if (!data) { + throw Error('News config not valid'); + } + if (!data.id) { throw Error('News requires Id'); } diff --git a/src/models/RecipePreview.ts b/src/models/RecipePreview.ts index 351ecf765..4d2cc8450 100644 --- a/src/models/RecipePreview.ts +++ b/src/models/RecipePreview.ts @@ -1,5 +1,13 @@ // @flow +interface IRecipePreview { + id: string; + name: string; + icon: string; + featured: boolean; + aliases: string[]; +} + export default class RecipePreview { id: string = ''; @@ -11,9 +19,13 @@ export default class RecipePreview { aliases: string[] = []; - constructor(data: { id: any; }) { + constructor(data: IRecipePreview) { + if (!data) { + throw Error('RecipePreview config not valid'); + } + if (!data.id) { - throw Error('RecipePreview requires Id'); + throw Error(`RecipePreview '${data.name}' requires Id`); } Object.assign(this, data); diff --git a/src/models/Service.js b/src/models/Service.js index dbbd622aa..ee6fe8b6e 100644 --- a/src/models/Service.js +++ b/src/models/Service.js @@ -106,13 +106,12 @@ export default class Service { this.userAgentModel = new UserAgent(recipe.overrideUserAgent); - // TODO: Should these also follow the 'ifUndefined*' style? - this.id = data.id || this.id; - this.name = data.name || this.name; - this.team = data.team || this.team; - this.customUrl = data.customUrl || this.customUrl; - // this.customIconUrl = data.customIconUrl || this.customIconUrl; - this.iconUrl = data.iconUrl || this.iconUrl; + this.id = ifUndefinedString(data.id, this.id); + this.name = ifUndefinedString(data.name, this.name); + this.team = ifUndefinedString(data.team, this.team); + this.customUrl = ifUndefinedString(data.customUrl, this.customUrl); + // this.customIconUrl = ifUndefinedString(data.customIconUrl, this.customIconUrl); + this.iconUrl = ifUndefinedString(data.iconUrl, this.iconUrl); this.order = ifUndefinedNumber(data.order, this.order); this.isEnabled = ifUndefinedBoolean(data.isEnabled, this.isEnabled); diff --git a/src/models/User.ts b/src/models/User.ts index 771d11955..54a6838df 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -42,6 +42,10 @@ export default class User { @observable team = {}; constructor(data: IUser) { + if (!data) { + throw Error('User config not valid'); + } + if (!data.id) { throw Error('User requires Id'); } -- cgit v1.2.3-70-g09d2