From 684d0543fd75da3a0357f999075067c8c8783ae9 Mon Sep 17 00:00:00 2001 From: abhijithvijayan <34790378+abhijithvijayan@users.noreply.github.com> Date: Mon, 20 Jul 2020 22:25:10 +0530 Subject: [PATCH] fix: type assertion for settings object keys --- source/Options/Form.tsx | 2 ++ source/Options/Options.tsx | 20 +++++++++++--------- source/Popup/Header.tsx | 1 + source/Popup/Popup.tsx | 31 +++++++++++++++++-------------- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/source/Options/Form.tsx b/source/Options/Form.tsx index 800ca85..31b5493 100644 --- a/source/Options/Form.tsx +++ b/source/Options/Form.tsx @@ -153,6 +153,8 @@ const Form: React.FC = () => { formStateValues.host.trim()) || Kutt.hostUrl, }; + + // API call const response: | SuccessfulApiKeyCheckProperties | ApiErroredProperties = await messageUtil.send( diff --git a/source/Options/Options.tsx b/source/Options/Options.tsx index 41816ca..0c56a8e 100644 --- a/source/Options/Options.tsx +++ b/source/Options/Options.tsx @@ -26,26 +26,28 @@ const Options: React.FC = () => { useEffect(() => { async function getSavedSettings(): Promise { const {settings = {}} = await getExtensionSettings(); - const advancedSettings: boolean = (settings?.advanced && true) || false; + const advancedSettings: boolean = + (settings?.advanced as boolean) || false; + const defaultHost: HostProperties = (advancedSettings && - settings?.host && - isValidUrl(`${settings.host}`) && { - hostDomain: settings.host + (settings?.host as string) && + isValidUrl(settings.host as string) && { + hostDomain: (settings.host as string) .replace('http://', '') .replace('https://', '') .replace('www.', '') .split(/[/?#]/)[0], // extract domain - hostUrl: settings.host.endsWith('/') - ? settings.host.slice(0, -1) - : settings.host, // slice `/` at the end + hostUrl: (settings.host as string).endsWith('/') + ? (settings.host as string).slice(0, -1) + : (settings.host as string), // slice `/` at the end }) || Kutt; // inject existing keys (if field doesn't exist, use default) const defaultExtensionConfig = { - apikey: settings?.apikey?.trim() || '', - history: (settings?.history && true) || false, + apikey: (settings?.apikey as string)?.trim() || '', + history: (settings?.history as boolean) || false, advanced: defaultHost.hostUrl.trim() !== Kutt.hostUrl && advancedSettings, // disable `advanced` if customhost is not set host: defaultHost, diff --git a/source/Popup/Header.tsx b/source/Popup/Header.tsx index 8f97f12..0ed9bd7 100644 --- a/source/Popup/Header.tsx +++ b/source/Popup/Header.tsx @@ -43,6 +43,7 @@ const Header: React.FC = () => { apikey: extensionSettingsState.apikey, hostUrl: extensionSettingsState.host.hostUrl, }; + // request API const response: | SuccessfulApiKeyCheckProperties diff --git a/source/Popup/Popup.tsx b/source/Popup/Popup.tsx index 142e070..84a2a09 100644 --- a/source/Popup/Popup.tsx +++ b/source/Popup/Popup.tsx @@ -60,19 +60,22 @@ const Popup: React.FC = () => { const migrationSettings: any = {}; let performMigration = false; - if (key.trim().length > 0) { + if ((key as string).trim().length > 0) { // map it to `settings.apikey` migrationSettings.apikey = key; performMigration = true; } - if (host.trim().length > 0 && userOptions.devMode) { + if ( + (host as string).trim().length > 0 && + (userOptions.devMode as boolean) + ) { // map `host` to `settings.host` migrationSettings.host = host; // set `advanced` to true migrationSettings.advanced = true; performMigration = true; } - if (userOptions.keepHistory) { + if (userOptions.keepHistory as boolean) { // set `settings.history` to true migrationSettings.history = true; performMigration = true; @@ -95,7 +98,7 @@ const Popup: React.FC = () => { // No API Key set if ( !Object.prototype.hasOwnProperty.call(settings, 'apikey') || - settings.apikey === '' + (settings.apikey as string) === '' ) { requestStatusDispatch({ type: RequestStatusActionTypes.SET_REQUEST_STATUS, @@ -122,23 +125,23 @@ const Popup: React.FC = () => { // If `advanced` field is true if ( Object.prototype.hasOwnProperty.call(settings, 'advanced') && - settings.advanced + (settings.advanced as boolean) ) { // If `host` field is set if ( Object.prototype.hasOwnProperty.call(settings, 'host') && - settings.host.trim().length > 0 && - isValidUrl(settings.host) + (settings.host as string)?.trim().length > 0 && + isValidUrl(settings.host as string) ) { defaultHost = { - hostDomain: settings.host + hostDomain: (settings.host as string) .replace('http://', '') .replace('https://', '') .replace('www.', '') .split(/[/?#]/)[0], // extract domain - hostUrl: settings.host.endsWith('/') - ? settings.host.slice(0, -1) - : settings.host, // slice `/` at the end + hostUrl: (settings.host as string).endsWith('/') + ? (settings.host as string).slice(0, -1) + : (settings.host as string), // slice `/` at the end }; } } @@ -162,7 +165,7 @@ const Popup: React.FC = () => { // `user` & `apikey` fields exist on storage if ( Object.prototype.hasOwnProperty.call(settings, 'user') && - settings.user + (settings.user as UserSettingsResponseProperties) ) { const {user}: {user: UserSettingsResponseProperties} = settings; @@ -184,7 +187,7 @@ const Popup: React.FC = () => { extensionSettingsDispatch({ type: ExtensionSettingsActionTypes.HYDRATE_EXTENSION_SETTINGS, payload: { - apikey: settings.apikey.trim(), + apikey: (settings.apikey as string)?.trim(), domainOptions: optionsList, host: defaultHost, }, @@ -194,7 +197,7 @@ const Popup: React.FC = () => { extensionSettingsDispatch({ type: ExtensionSettingsActionTypes.HYDRATE_EXTENSION_SETTINGS, payload: { - apikey: settings.apikey.trim(), + apikey: (settings.apikey as string)?.trim(), domainOptions: defaultOptions, host: defaultHost, },