From cca2c3c0d72459931b0ab9d0dfb9925a2c1d27b5 Mon Sep 17 00:00:00 2001 From: Griffithtp Date: Wed, 10 Jul 2019 23:51:25 +0100 Subject: [PATCH 1/4] fix: localhost domain download tarball button --- src/utils/url.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/url.ts b/src/utils/url.ts index 68da74f..4832633 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -6,6 +6,7 @@ export function isURL(url): boolean { return isURLValidator(url || '', { protocols: ['http', 'https', 'git+https'], require_protocol: true, + require_tld: false, }); } From 5148fdca66342a0efeb0a2298a57f7723507dafe Mon Sep 17 00:00:00 2001 From: Griffithtp Date: Sat, 13 Jul 2019 07:53:23 +0100 Subject: [PATCH 2/4] refactor: add @types/validator --- package.json | 1 + src/utils/url.ts | 2 +- yarn.lock | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) 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/utils/url.ts b/src/utils/url.ts index 4832633..119888e 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -2,7 +2,7 @@ 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, diff --git a/yarn.lock b/yarn.lock index f1b4a56..ffdd3c1 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" From 795544a14ccd58b99627946fcec9067e7cdf0823 Mon Sep 17 00:00:00 2001 From: Griffithtp Date: Sat, 13 Jul 2019 15:28:25 +0100 Subject: [PATCH 3/4] test: add unit test for url utils --- src/utils/url.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/utils/url.test.ts 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'); + }); +}); From ecc452131417c888165c7e4918d963c103cfbf16 Mon Sep 17 00:00:00 2001 From: Griffithtp Date: Sun, 14 Jul 2019 12:35:57 +0100 Subject: [PATCH 4/4] refactor: remove getBaseNamePath() and history.ts --- src/history.ts | 8 -------- src/router.tsx | 6 +++++- src/utils/url.ts | 8 -------- 3 files changed, 5 insertions(+), 17 deletions(-) delete mode 100644 src/history.ts 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.ts b/src/utils/url.ts index 119888e..d428410 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -18,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; -}