Skip to content

Commit 6ef4b7e

Browse files
committed
add absolute option to getUrlForApp (elastic#57193)
1 parent d02fb7a commit 6ef4b7e

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

docs/development/core/public/kibana-plugin-public.applicationstart.geturlforapp.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
## ApplicationStart.getUrlForApp() method
66

7-
Returns a relative URL to a given app, including the global base path.
7+
Returns an URL to a given app, including the global base path. By default, the URL is relative (/basePath/app/my-app). Use the `absolute` option to generate an absolute url (http://host:port/basePath/app/my-app)
8+
9+
Note that when generating absolute urls, the protocol, host and port are determined from the browser location.
810

911
<b>Signature:</b>
1012

1113
```typescript
1214
getUrlForApp(appId: string, options?: {
1315
path?: string;
16+
absolute?: boolean;
1417
}): string;
1518
```
1619

@@ -19,7 +22,7 @@ getUrlForApp(appId: string, options?: {
1922
| Parameter | Type | Description |
2023
| --- | --- | --- |
2124
| appId | <code>string</code> | |
22-
| options | <code>{</code><br/><code> path?: string;</code><br/><code> }</code> | |
25+
| options | <code>{</code><br/><code> path?: string;</code><br/><code> absolute?: boolean;</code><br/><code> }</code> | |
2326

2427
<b>Returns:</b>
2528

docs/development/core/public/kibana-plugin-public.applicationstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface ApplicationStart
2222

2323
| Method | Description |
2424
| --- | --- |
25-
| [getUrlForApp(appId, options)](./kibana-plugin-public.applicationstart.geturlforapp.md) | Returns a relative URL to a given app, including the global base path. |
25+
| [getUrlForApp(appId, options)](./kibana-plugin-public.applicationstart.geturlforapp.md) | Returns an URL to a given app, including the global base path. By default, the URL is relative (/basePath/app/my-app). Use the <code>absolute</code> option to generate an absolute url (http://host:port/basePath/app/my-app)<!-- -->Note that when generating absolute urls, the protocol, host and port are determined from the browser location. |
2626
| [navigateToApp(appId, options)](./kibana-plugin-public.applicationstart.navigatetoapp.md) | Navigate to a given app |
2727
| [registerMountContext(contextName, provider)](./kibana-plugin-public.applicationstart.registermountcontext.md) | Register a context provider for application mounting. Will only be available to applications that depend on the plugin that registered this context. Deprecated, use [CoreSetup.getStartServices()](./kibana-plugin-public.coresetup.getstartservices.md)<!-- -->. |
2828

src/core/public/application/application_service.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,23 @@ describe('#start()', () => {
580580

581581
it('creates URLs with path parameter', async () => {
582582
service.setup(setupDeps);
583-
584583
const { getUrlForApp } = await service.start(startDeps);
585584

586585
expect(getUrlForApp('app1', { path: 'deep/link' })).toBe('/base-path/app/app1/deep/link');
587586
expect(getUrlForApp('app1', { path: '/deep//link/' })).toBe('/base-path/app/app1/deep/link');
588587
expect(getUrlForApp('app1', { path: '//deep/link//' })).toBe('/base-path/app/app1/deep/link');
589588
expect(getUrlForApp('app1', { path: 'deep/link///' })).toBe('/base-path/app/app1/deep/link');
590589
});
590+
591+
it('creates absolute URLs when `absolute` parameter is true', async () => {
592+
service.setup(setupDeps);
593+
const { getUrlForApp } = await service.start(startDeps);
594+
595+
expect(getUrlForApp('app1', { absolute: true })).toBe('http://localhost/base-path/app/app1');
596+
expect(getUrlForApp('app2', { path: 'deep/link', absolute: true })).toBe(
597+
'http://localhost/base-path/app/app2/deep/link'
598+
);
599+
});
591600
});
592601

593602
describe('navigateToApp', () => {

src/core/public/application/application_service.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,13 @@ export class ApplicationService {
272272
takeUntil(this.stop$)
273273
),
274274
registerMountContext: this.mountContext.registerContext,
275-
getUrlForApp: (appId, { path }: { path?: string } = {}) =>
276-
http.basePath.prepend(getAppUrl(availableMounters, appId, path)),
275+
getUrlForApp: (
276+
appId,
277+
{ path, absolute = false }: { path?: string; absolute?: boolean } = {}
278+
) => {
279+
const relUrl = http.basePath.prepend(getAppUrl(availableMounters, appId, path));
280+
return absolute ? relativeToAbsolute(relUrl) : relUrl;
281+
},
277282
navigateToApp: async (appId, { path, state }: { path?: string; state?: any } = {}) => {
278283
if (await this.shouldNavigate(overlays)) {
279284
this.appLeaveHandlers.delete(this.currentAppId$.value!);
@@ -364,3 +369,10 @@ const updateStatus = <T extends AppBase>(app: T, statusUpdaters: AppUpdaterWrapp
364369
...changes,
365370
};
366371
};
372+
373+
function relativeToAbsolute(url: string) {
374+
// convert all link urls to absolute urls
375+
const a = document.createElement('a');
376+
a.setAttribute('href', url);
377+
return a.href;
378+
}

src/core/public/application/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,17 @@ export interface ApplicationStart {
593593
navigateToApp(appId: string, options?: { path?: string; state?: any }): Promise<void>;
594594

595595
/**
596-
* Returns a relative URL to a given app, including the global base path.
596+
* Returns an URL to a given app, including the global base path.
597+
* By default, the URL is relative (/basePath/app/my-app).
598+
* Use the `absolute` option to generate an absolute url (http://host:port/basePath/app/my-app)
599+
*
600+
* Note that when generating absolute urls, the protocol, host and port are determined from the browser location.
601+
*
597602
* @param appId
598603
* @param options.path - optional path inside application to deep link to
604+
* @param options.absolute - if true, will returns an absolute url instead of a relative one
599605
*/
600-
getUrlForApp(appId: string, options?: { path?: string }): string;
606+
getUrlForApp(appId: string, options?: { path?: string; absolute?: boolean }): string;
601607

602608
/**
603609
* Register a context provider for application mounting. Will only be available to applications that depend on the

src/core/public/public.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export interface ApplicationStart {
101101
currentAppId$: Observable<string | undefined>;
102102
getUrlForApp(appId: string, options?: {
103103
path?: string;
104+
absolute?: boolean;
104105
}): string;
105106
navigateToApp(appId: string, options?: {
106107
path?: string;

0 commit comments

Comments
 (0)