1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +01:00

formatApiPath / formatAssetPath - Implement smart functionality to detect double subpath (#2777)

Today we have two functions that wrap our paths with the subpath. Since
all of our customers are hosted on a subpath, such as /eubb1001, we need
to account for this path when building API paths and asset paths.

These functions could be smarter, we could, for example detect if we
have already added the base path and ignore it if it already exists.

**This PR implements this and adds 2 capabilities:**

1. If there is list of paths that need to be joined and one is subset of
another, it is removed.
2. All duplicate paths in the list are removed
This commit is contained in:
sjaanus 2023-01-02 12:06:33 +02:00 committed by GitHub
parent 340bcf1d1b
commit d5e47ac352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -28,6 +28,7 @@ test('formatAssetPath', () => {
expect(formatAssetPath('/a', '/x')).toEqual('/x/a');
expect(formatAssetPath('/a/', '/x/')).toEqual('/x/a');
expect(formatAssetPath('a/b/', 'x/y/')).toEqual('/x/y/a/b');
expect(formatAssetPath('x/y/', 'x/y/')).toEqual('/x/y');
expect(formatAssetPath('//a//b//', '//x//y//')).toEqual('/x/y/a/b');
});
@ -42,6 +43,7 @@ test('formatApiPath', () => {
expect(formatApiPath('/', '/')).toEqual('');
expect(formatApiPath('a', 'x')).toEqual('/x/a');
expect(formatApiPath('/a', '/x')).toEqual('/x/a');
expect(formatApiPath('/a', '/x/a')).toEqual('/x/a');
expect(formatApiPath('/a/', '/x/')).toEqual('/x/a');
expect(formatApiPath('a/b/', 'x/y/')).toEqual('/x/y/a/b');
expect(formatApiPath('//a//b//', '//x//y//')).toEqual('/x/y/a/b');

View File

@ -23,7 +23,14 @@ export const parseBasePath = (value = basePathMetaTagContent()): string => {
// Join paths with a leading separator and without a trailing separator.
const joinPaths = (...paths: string[]): string => {
return ['', ...paths]
const filteredPaths = paths.filter(path => {
return !paths.some(
currentPath => currentPath !== path && currentPath.includes(path)
);
});
const uniquePaths = [...new Set(filteredPaths)];
return ['', ...uniquePaths]
.join('/')
.replace(/\/+$/g, '') // Remove trailing separators.
.replace(/\/+/g, '/'); // Collapse repeated separators.