diff --git a/package.json b/package.json index d05c834..c94e9bb 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@types/react": "16.8.16", "@types/react-dom": "16.8.4", "@types/react-router-dom": "4.3.2", + "@types/validator": "10.11.1", "@verdaccio/babel-preset": "0.2.1", "@verdaccio/eslint-config": "0.0.1", "@verdaccio/types": "6.1.0", diff --git a/src/history.ts b/src/history.ts deleted file mode 100644 index a8f41b8..0000000 --- a/src/history.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { createBrowserHistory } from 'history'; -import { getBaseNamePath } from './utils/url'; - -const history = createBrowserHistory({ - basename: getBaseNamePath(), -}); - -export default history; diff --git a/src/router.tsx b/src/router.tsx index dcc0e2b..394b8ee 100644 --- a/src/router.tsx +++ b/src/router.tsx @@ -2,12 +2,16 @@ import React, { Component, ReactElement } from 'react'; import { Router, Route, Switch } from 'react-router-dom'; +import { createBrowserHistory } from 'history'; import { AppContextConsumer, AppStateInterface } from './App/App'; import { asyncComponent } from './utils/asyncComponent'; -import history from './history'; import Header from './components/Header'; +const history = createBrowserHistory({ + basename: window.__VERDACCIO_BASENAME_UI_OPTIONS && window.__VERDACCIO_BASENAME_UI_OPTIONS.url_prefix, +}); + const NotFound = asyncComponent(() => import('./components/NotFound')); const VersionPackage = asyncComponent(() => import('./pages/version/Version')); const HomePage = asyncComponent(() => import('./pages/home')); diff --git a/src/utils/url.test.ts b/src/utils/url.test.ts new file mode 100644 index 0000000..33b99b1 --- /dev/null +++ b/src/utils/url.test.ts @@ -0,0 +1,27 @@ +import { isURL, isEmail, getRegistryURL } from './url'; + +describe('url', () => { + test('isURL() - should return true for localhost', () => { + expect(isURL('http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz')).toBeTruthy(); + }); + + test('isURL() - should return false when protocol is missing', () => { + expect(isURL('localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz')).toBeFalsy(); + }); + + test('isEmail() - should return true if valid', () => { + expect(isEmail('email@domain.com')).toBeTruthy(); + }); + test('isEmail() - should return false if invalid', () => { + expect(isEmail('')).toBeFalsy(); + }); + + test('getRegistryURL() - should keep slash if location is a sub directory', () => { + history.pushState({}, 'page title', '/-/web/detail'); + expect(getRegistryURL()).toBe('http://localhost/-/web/detail'); + history.pushState({}, 'page title', '/'); + }); + test('getRegistryURL() - should not add slash if location is not a sub directory', () => { + expect(getRegistryURL()).toBe('http://localhost'); + }); +}); diff --git a/src/utils/url.ts b/src/utils/url.ts index 68da74f..d428410 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -2,10 +2,11 @@ import isURLValidator from 'validator/lib/isURL'; import isEmailValidator from 'validator/lib/isEmail'; import '../../types'; -export function isURL(url): boolean { +export function isURL(url: string): boolean { return isURLValidator(url || '', { protocols: ['http', 'https', 'git+https'], require_protocol: true, + require_tld: false, }); } @@ -17,11 +18,3 @@ export function getRegistryURL(): string { // Don't add slash if it's not a sub directory return `${location.origin}${location.pathname === '/' ? '' : location.pathname}`; } - -export function getBaseNamePath(): string { - return window.__VERDACCIO_BASENAME_UI_OPTIONS && window.__VERDACCIO_BASENAME_UI_OPTIONS.url_prefix; -} - -export function getRootPath(): string { - return window.__VERDACCIO_BASENAME_UI_OPTIONS && window.__VERDACCIO_BASENAME_UI_OPTIONS.base; -} diff --git a/yarn.lock b/yarn.lock index 45962ee..3b9815c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1743,6 +1743,11 @@ resolved "https://registry.verdaccio.org/@types%2funist/-/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e" integrity sha512-FvUupuM3rlRsRtCN+fDudtmytGO6iHJuuRKS1Ss0pG5z8oX0diNEw94UEL7hgDbpN94rgaK5R7sWm6RrSkZuAQ== +"@types/validator@10.11.1": + version "10.11.1" + resolved "https://registry.npmjs.org/@types/validator/-/validator-10.11.1.tgz#76e983ce155261838463b73728856cf8bc628d53" + integrity sha512-bVhLqvb+5xUNWRFnuuecRVISTvsG6AdhrB2kb/tChgtuTTqARqlQ3rLhOPy8cINZEUB8PkR+goyWF6fWxg4iSw== + "@types/vfile-message@*": version "1.0.1" resolved "https://registry.verdaccio.org/@types%2fvfile-message/-/vfile-message-1.0.1.tgz#e1e9895cc6b36c462d4244e64e6d0b6eaf65355a"