aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar Vijay A <vraravam@users.noreply.github.com>2023-05-25 13:04:39 +0530
committerLibravatar Vijay A <vraravam@users.noreply.github.com>2023-05-25 13:04:39 +0530
commit44c5193ab8b25422ae5cb679ce788a8c636108ac (patch)
treedc29c85d019bb5f807d7f2041eaf3ebba3215431 /src
parentBump eslint-plugin-unicorn from 42.0.0 to 46.0.0 (diff)
downloadferdium-app-44c5193ab8b25422ae5cb679ce788a8c636108ac.tar.gz
ferdium-app-44c5193ab8b25422ae5cb679ce788a8c636108ac.tar.zst
ferdium-app-44c5193ab8b25422ae5cb679ce788a8c636108ac.zip
Minor refactoring to remove code duplication
Diffstat (limited to 'src')
-rw-r--r--src/components/settings/services/EditServiceForm.tsx8
-rw-r--r--src/helpers/url-helpers.ts9
-rw-r--r--src/models/Service.ts9
3 files changed, 13 insertions, 13 deletions
diff --git a/src/components/settings/services/EditServiceForm.tsx b/src/components/settings/services/EditServiceForm.tsx
index 2b87dff1e..c04a53b50 100644
--- a/src/components/settings/services/EditServiceForm.tsx
+++ b/src/components/settings/services/EditServiceForm.tsx
@@ -2,7 +2,6 @@ import { Component, FormEvent, ReactElement } from 'react';
2import { observer } from 'mobx-react'; 2import { observer } from 'mobx-react';
3import { Link } from 'react-router-dom'; 3import { Link } from 'react-router-dom';
4import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl'; 4import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';
5import normalizeUrl from 'normalize-url';
6import { mdiInformation } from '@mdi/js'; 5import { mdiInformation } from '@mdi/js';
7import { noop } from 'lodash'; 6import { noop } from 'lodash';
8import Form from '../../../lib/Form'; 7import Form from '../../../lib/Form';
@@ -20,6 +19,7 @@ import Icon from '../../ui/icon';
20import { H3 } from '../../ui/headline'; 19import { H3 } from '../../ui/headline';
21import { IRecipe } from '../../../models/Recipe'; 20import { IRecipe } from '../../../models/Recipe';
22import Service from '../../../models/Service'; 21import Service from '../../../models/Service';
22import { normalizedUrl } from '../../../helpers/url-helpers';
23 23
24const messages = defineMessages({ 24const messages = defineMessages({
25 saveService: { 25 saveService: {
@@ -194,11 +194,7 @@ class EditServiceForm extends Component<IProps, IState> {
194 if (recipe.validateUrl && values.customUrl) { 194 if (recipe.validateUrl && values.customUrl) {
195 this.setState({ isValidatingCustomUrl: true }); 195 this.setState({ isValidatingCustomUrl: true });
196 try { 196 try {
197 values.customUrl = normalizeUrl(values.customUrl, { 197 values.customUrl = normalizedUrl(values.customUrl);
198 stripAuthentication: false,
199 stripWWW: false,
200 removeTrailingSlash: false,
201 });
202 isValid = await recipe.validateUrl(values.customUrl); 198 isValid = await recipe.validateUrl(values.customUrl);
203 } catch (error) { 199 } catch (error) {
204 console.warn('ValidateURL', error); 200 console.warn('ValidateURL', error);
diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts
index 9c5cf7752..795d3f2cb 100644
--- a/src/helpers/url-helpers.ts
+++ b/src/helpers/url-helpers.ts
@@ -2,6 +2,7 @@
2import { URL } from 'url'; 2import { URL } from 'url';
3import { ensureDirSync, existsSync } from 'fs-extra'; 3import { ensureDirSync, existsSync } from 'fs-extra';
4import { shell } from 'electron'; 4import { shell } from 'electron';
5import normalizeUrl from 'normalize-url';
5import { ALLOWED_PROTOCOLS } from '../config'; 6import { ALLOWED_PROTOCOLS } from '../config';
6 7
7const debug = require('../preload-safe-debug')('Ferdium:Helpers:url'); 8const debug = require('../preload-safe-debug')('Ferdium:Helpers:url');
@@ -49,3 +50,11 @@ export function openExternalUrl(
49 shell.openExternal(fixedUrl.toString()); 50 shell.openExternal(fixedUrl.toString());
50 } 51 }
51} 52}
53
54export function normalizedUrl(url: string) {
55 return normalizeUrl(url, {
56 stripAuthentication: false,
57 stripWWW: false,
58 removeTrailingSlash: false,
59 });
60}
diff --git a/src/models/Service.ts b/src/models/Service.ts
index 0cbfa60e2..af0085185 100644
--- a/src/models/Service.ts
+++ b/src/models/Service.ts
@@ -1,12 +1,11 @@
1import { autorun, action, computed, makeObservable, observable } from 'mobx'; 1import { autorun, action, computed, makeObservable, observable } from 'mobx';
2import { ipcRenderer } from 'electron'; 2import { ipcRenderer } from 'electron';
3import { webContents } from '@electron/remote'; 3import { webContents } from '@electron/remote';
4import normalizeUrl from 'normalize-url';
5import { join } from 'path'; 4import { join } from 'path';
6import ElectronWebView from 'react-electron-web-view'; 5import ElectronWebView from 'react-electron-web-view';
7 6
8import { todosStore } from '../features/todos'; 7import { todosStore } from '../features/todos';
9import { isValidExternalURL } from '../helpers/url-helpers'; 8import { isValidExternalURL, normalizedUrl } from '../helpers/url-helpers';
10import UserAgent from './UserAgent'; 9import UserAgent from './UserAgent';
11import { DEFAULT_SERVICE_ORDER, DEFAULT_SERVICE_SETTINGS } from '../config'; 10import { DEFAULT_SERVICE_ORDER, DEFAULT_SERVICE_SETTINGS } from '../config';
12import { ifUndefined } from '../jsUtils'; 11import { ifUndefined } from '../jsUtils';
@@ -327,11 +326,7 @@ export default class Service {
327 if (this.recipe.hasCustomUrl && this.customUrl) { 326 if (this.recipe.hasCustomUrl && this.customUrl) {
328 let url: string = ''; 327 let url: string = '';
329 try { 328 try {
330 url = normalizeUrl(this.customUrl, { 329 url = normalizedUrl(this.customUrl);
331 stripAuthentication: false,
332 stripWWW: false,
333 removeTrailingSlash: false,
334 });
335 } catch { 330 } catch {
336 console.error( 331 console.error(
337 `Service (${this.recipe.name}): '${this.customUrl}' is not a valid Url.`, 332 `Service (${this.recipe.name}): '${this.customUrl}' is not a valid Url.`,