Skip to content

Commit

Permalink
fix: type assertion for settings object keys
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijithvijayan committed Jul 20, 2020
1 parent 529f22c commit 684d054
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
2 changes: 2 additions & 0 deletions source/Options/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ const Form: React.FC = () => {
formStateValues.host.trim()) ||
Kutt.hostUrl,
};

// API call
const response:
| SuccessfulApiKeyCheckProperties
| ApiErroredProperties = await messageUtil.send(
Expand Down
20 changes: 11 additions & 9 deletions source/Options/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,28 @@ const Options: React.FC = () => {
useEffect(() => {
async function getSavedSettings(): Promise<void> {
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,
Expand Down
1 change: 1 addition & 0 deletions source/Popup/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const Header: React.FC = () => {
apikey: extensionSettingsState.apikey,
hostUrl: extensionSettingsState.host.hostUrl,
};

// request API
const response:
| SuccessfulApiKeyCheckProperties
Expand Down
31 changes: 17 additions & 14 deletions source/Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 &&

This comment has been minimized.

Copy link
@abhijithvijayan

abhijithvijayan Jul 20, 2020

Author Member

settings.host could be undefined, causing trim() to fail
This is fixed with ?.trim()

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
};
}
}
Expand All @@ -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;

Expand All @@ -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,
},
Expand All @@ -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,
},
Expand Down

0 comments on commit 684d054

Please sign in to comment.