-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: deduce base url from hosted location [LIBS-580] #832
Open
KaiVandivier
wants to merge
3
commits into
master
Choose a base branch
from
deduce-base-url
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const isUrlAbsolute = (url) => { | ||
// the URL constructor will throw for relative URLs | ||
try { | ||
new URL(url) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
/** | ||
* Deduces the base URL for the DHIS2 instance from the location at which this | ||
* app is hosted. Returns an absolute URL | ||
* @param {string} defaultBaseUrl a fallback URL that will be used if the base | ||
* URL can't be deduced from the window location | ||
* @returns {string} an absolute base URL for the DHIS2 instance | ||
*/ | ||
export const getBaseUrl = (defaultBaseUrl) => { | ||
// if defaultBaseUrl is absolute, use that | ||
if (isUrlAbsolute(defaultBaseUrl)) { | ||
return defaultBaseUrl | ||
} | ||
|
||
const { location } = window | ||
const path = location.pathname | ||
|
||
// get penultimate 3 path elements (don't need anything before), | ||
// e.g. /dev/api/apps/app-name/index.html => ['api', 'apps', 'app-name']. | ||
const splitPath = path.split('/').slice(-4, -1) | ||
// could be shorter than 3 elements for e.g. /dhis-web-maps/ | ||
const l = splitPath.length | ||
|
||
// test for core apps | ||
if (/dhis-web-.+/.test(splitPath[l - 1])) { | ||
return new URL('..', location.href).href | ||
} | ||
|
||
// test for custom apps | ||
if (splitPath[l - 3] === 'api' && splitPath[l - 2] === 'apps') { | ||
return new URL('../../..', location.href).href | ||
} | ||
|
||
// todo: handle path for global shell | ||
|
||
// otherwise, return absolute version of default | ||
return new URL(defaultBaseUrl, location.href).href | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { getBaseUrl } from './getBaseUrl.js' | ||
|
||
const testOrigin = 'https://debug.dhis2.org' | ||
// { [testPath]: expectedPath } | ||
const testPaths = { | ||
'/dev/api/apps/simple-app/index.html': '/dev/', | ||
'/analytics_dev/dhis-web-maps/plugin.html': '/analytics_dev/', | ||
'/dhis-web-line-listing/index.html': '/', | ||
'/dhis-web-user-settings/': '/', | ||
'/hmis/staging/v41/api/apps/WHO-Data-Quality-App/app.html': | ||
'/hmis/staging/v41/', | ||
} | ||
|
||
const windowLocationMock = jest | ||
.spyOn(window, 'location', 'get') | ||
.mockImplementation(() => ({ | ||
href: 'https://debug.dhis2.org/dev/', | ||
pathname: '/dev/', | ||
})) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('an absolute URL is used if provided as the default', () => { | ||
const testBaseUrl = 'http://localhost:8080/hmis/long/path/dev' | ||
|
||
const url = getBaseUrl(testBaseUrl) | ||
|
||
expect(url).toBe(testBaseUrl) | ||
}) | ||
|
||
describe('location variants', () => { | ||
const defaultRelativeBaseUrl = '..' | ||
|
||
for (const [testPath, expectedPath] of Object.entries(testPaths)) { | ||
test(testPath, () => { | ||
windowLocationMock.mockImplementation(() => ({ | ||
pathname: testPath, | ||
href: testOrigin + testPath, | ||
})) | ||
|
||
const url = getBaseUrl(defaultRelativeBaseUrl) | ||
|
||
expect(url).toBe(testOrigin + expectedPath) | ||
}) | ||
} | ||
}) | ||
|
||
test("if the function doesn't match a pattern, it falls back to the provided default", () => { | ||
const relativeDefaultBaseUrl = '../../..' | ||
const testPath = '/not/a/recognized/path/index.html' | ||
windowLocationMock.mockImplementation(() => ({ | ||
pathname: testPath, | ||
href: testOrigin + testPath, | ||
})) | ||
|
||
const url = getBaseUrl(relativeDefaultBaseUrl) | ||
|
||
expect(url).toBe(testOrigin + '/not/') | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another ambiguous path we might need to consider...
An installed app could have dhis-web-maps as a name. We should probably block installation of apps with that prefix in core...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm yeah that's an interesting case -- at
/api/apps/dhis-web-maps/
, we could either be at a weird custom app with the name'dhis-web-maps'
at a base URL of/
, OR we could be at the core Maps app in an instance weirdly hosted with the base URL/api/apps/
... I'm not sure if it's possible to figure out which case is true here hahThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could ping the server to see which base URL works 🤷 or we could decide that this will always assume that's a custom app
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could add enforcement in app hub and core app install logic to disallow custom apps with the dhis-web prefix, hopefully that would be sufficient to disambiguate these cases? And I don't think we need to cater to apps really wanting that specific URL slug...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, added that test case 🙂 👍