mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-17 23:45:29 -05:00
fix: updated combine url fix method (#1647)
* test: added more combineBaseUrl tests * fix: optimized and updated combineBaseUrl method logic
This commit is contained in:
parent
32aabca641
commit
4f43347b50
2 changed files with 14 additions and 4 deletions
|
@ -141,12 +141,19 @@ export function validateMetadata(object: Package, name: string): Package {
|
|||
* @return {String} base registry url
|
||||
*/
|
||||
export function combineBaseUrl(protocol: string, host: string | void, prefix?: string | void): string {
|
||||
let result = `${protocol}://${host}`;
|
||||
const result = `${protocol}://${host}`;
|
||||
|
||||
if (prefix) {
|
||||
prefix = prefix.replace(/\/$/, '');
|
||||
const prefixOnlySlash = prefix === '/';
|
||||
if (prefix && !prefixOnlySlash) {
|
||||
if (prefix.endsWith('/')) {
|
||||
prefix = prefix.slice(0, -1);
|
||||
}
|
||||
|
||||
result = prefix.indexOf('/') === 0 ? `${result}${prefix}` : prefix;
|
||||
if (prefix.startsWith('/')) {
|
||||
return `${result}${prefix}`;
|
||||
}
|
||||
|
||||
return prefix;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -212,7 +212,10 @@ describe('Utilities', () => {
|
|||
});
|
||||
|
||||
test('should create a base url for registry', () => {
|
||||
expect(combineBaseUrl("http", 'domain', '')).toEqual('http://domain');
|
||||
expect(combineBaseUrl("http", 'domain', '/')).toEqual('http://domain');
|
||||
expect(combineBaseUrl("http", 'domain', '/prefix/')).toEqual('http://domain/prefix');
|
||||
expect(combineBaseUrl("http", 'domain', '/prefix/deep')).toEqual('http://domain/prefix/deep');
|
||||
expect(combineBaseUrl("http", 'domain', 'only-prefix')).toEqual('only-prefix');
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue