Skip to content

Commit

Permalink
Expose serverBasePath on client-side (#58070) (#58185)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
joshdover and elasticmachine authored Feb 21, 2020
1 parent 8fa101a commit 6a86b57
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface IBasePath
| [get](./kibana-plugin-public.ibasepath.get.md) | <code>() =&gt; string</code> | Gets the <code>basePath</code> string. |
| [prepend](./kibana-plugin-public.ibasepath.prepend.md) | <code>(url: string) =&gt; string</code> | Prepends <code>path</code> with the basePath. |
| [remove](./kibana-plugin-public.ibasepath.remove.md) | <code>(url: string) =&gt; string</code> | Removes the prepended basePath from the <code>path</code>. |
| [serverBasePath](./kibana-plugin-public.ibasepath.serverbasepath.md) | <code>string</code> | Returns the server's root basePath as configured, without any namespace prefix.<!-- -->See for getting the basePath value for a specific request |

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [IBasePath](./kibana-plugin-public.ibasepath.md) &gt; [serverBasePath](./kibana-plugin-public.ibasepath.serverbasepath.md)

## IBasePath.serverBasePath property

Returns the server's root basePath as configured, without any namespace prefix.

See for getting the basePath value for a specific request

<b>Signature:</b>

```typescript
readonly serverBasePath: string;
```
10 changes: 10 additions & 0 deletions src/core/public/http/base_path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,14 @@ describe('BasePath', () => {
});
});
});

describe('serverBasePath', () => {
it('defaults to basePath', () => {
expect(new BasePath('/foo/bar').serverBasePath).toEqual('/foo/bar');
});

it('returns value when passed into constructor', () => {
expect(new BasePath('/foo/bar', '/foo').serverBasePath).toEqual('/foo');
});
});
});
5 changes: 4 additions & 1 deletion src/core/public/http/base_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
import { modifyUrl } from '../../utils';

export class BasePath {
constructor(private readonly basePath: string = '') {}
constructor(
private readonly basePath: string = '',
public readonly serverBasePath: string = basePath
) {}

public get = () => {
return this.basePath;
Expand Down
5 changes: 4 additions & 1 deletion src/core/public/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export class HttpService implements CoreService<HttpSetup, HttpStart> {

public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup {
const kibanaVersion = injectedMetadata.getKibanaVersion();
const basePath = new BasePath(injectedMetadata.getBasePath());
const basePath = new BasePath(
injectedMetadata.getBasePath(),
injectedMetadata.getServerBasePath()
);
const fetchService = new Fetch({ basePath, kibanaVersion });
const loadingCount = this.loadingCount.setup({ fatalErrors });

Expand Down
7 changes: 7 additions & 0 deletions src/core/public/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ export interface IBasePath {
* Removes the prepended basePath from the `path`.
*/
remove: (url: string) => string;

/**
* Returns the server's root basePath as configured, without any namespace prefix.
*
* See {@link BasePath.get} for getting the basePath value for a specific request
*/
readonly serverBasePath: string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { InjectedMetadataService, InjectedMetadataSetup } from './injected_metad
const createSetupContractMock = () => {
const setupContract: jest.Mocked<InjectedMetadataSetup> = {
getBasePath: jest.fn(),
getServerBasePath: jest.fn(),
getKibanaVersion: jest.fn(),
getKibanaBranch: jest.fn(),
getCspConfig: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface InjectedMetadataParams {
buildNumber: number;
branch: string;
basePath: string;
serverBasePath: string;
category?: AppCategory;
csp: {
warnLegacyBrowsers: boolean;
Expand Down Expand Up @@ -115,6 +116,10 @@ export class InjectedMetadataService {
return this.state.basePath;
},

getServerBasePath: () => {
return this.state.serverBasePath;
},

getKibanaVersion: () => {
return this.state.version;
},
Expand Down Expand Up @@ -161,6 +166,7 @@ export class InjectedMetadataService {
*/
export interface InjectedMetadataSetup {
getBasePath: () => string;
getServerBasePath: () => string;
getKibanaBuildNumber: () => number;
getKibanaBranch: () => string;
getKibanaVersion: () => string;
Expand Down
2 changes: 2 additions & 0 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@ export interface IBasePath {
get: () => string;
prepend: (url: string) => string;
remove: (url: string) => string;
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "kibana" does not have an export "BasePath"
readonly serverBasePath: string;
}

// @public
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/core/server/rendering/rendering_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class RenderingService implements CoreService<RenderingServiceSetup> {
) => {
const { env } = this.coreContext;
const basePath = http.basePath.get(request);
const serverBasePath = http.basePath.serverBasePath;
const settings = {
defaults: uiSettings.getRegistered(),
user: includeUserSettings ? await uiSettings.getUserProvided() : {},
Expand All @@ -79,6 +80,7 @@ export class RenderingService implements CoreService<RenderingServiceSetup> {
buildNumber: env.packageInfo.buildNum,
branch: env.packageInfo.branch,
basePath,
serverBasePath,
env,
legacyMode: appId !== 'core',
i18n: {
Expand Down
1 change: 1 addition & 0 deletions src/core/server/rendering/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface RenderingMetadata {
buildNumber: number;
branch: string;
basePath: string;
serverBasePath: string;
env: Env;
legacyMode: boolean;
i18n: {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('apiKeysManagementApp', () => {
expect(setBreadcrumbs).toHaveBeenCalledWith([{ href: '#/some-base-path', text: 'API Keys' }]);
expect(container).toMatchInlineSnapshot(`
<div>
Page: {"notifications":{"toasts":{}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"apiKeysAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}}}
Page: {"notifications":{"toasts":{}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"apiKeysAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}}}
</div>
`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('roleMappingsManagementApp', () => {
expect(setBreadcrumbs).toHaveBeenCalledWith([{ href: `#${basePath}`, text: 'Role Mappings' }]);
expect(container).toMatchInlineSnapshot(`
<div>
Role Mappings Page: {"notifications":{"toasts":{}},"roleMappingsAPI":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"}}
Role Mappings Page: {"notifications":{"toasts":{}},"roleMappingsAPI":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"}}
</div>
`);

Expand All @@ -75,7 +75,7 @@ describe('roleMappingsManagementApp', () => {
]);
expect(container).toMatchInlineSnapshot(`
<div>
Role Mapping Edit Page: {"roleMappingsAPI":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"rolesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"notifications":{"toasts":{}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"}}
Role Mapping Edit Page: {"roleMappingsAPI":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"rolesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"notifications":{"toasts":{}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"}}
</div>
`);

Expand All @@ -98,7 +98,7 @@ describe('roleMappingsManagementApp', () => {
]);
expect(container).toMatchInlineSnapshot(`
<div>
Role Mapping Edit Page: {"name":"someRoleMappingName","roleMappingsAPI":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"rolesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"notifications":{"toasts":{}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"}}
Role Mapping Edit Page: {"name":"someRoleMappingName","roleMappingsAPI":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"rolesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"notifications":{"toasts":{}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"}}
</div>
`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('rolesManagementApp', () => {
expect(setBreadcrumbs).toHaveBeenCalledWith([{ href: `#${basePath}`, text: 'Roles' }]);
expect(container).toMatchInlineSnapshot(`
<div>
Roles Page: {"notifications":{"toasts":{}},"rolesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}}}
Roles Page: {"notifications":{"toasts":{}},"rolesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}}}
</div>
`);

Expand All @@ -86,7 +86,7 @@ describe('rolesManagementApp', () => {
]);
expect(container).toMatchInlineSnapshot(`
<div>
Role Edit Page: {"action":"edit","rolesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"userAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"indicesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"privilegesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"fatalErrors":{},"license":{"features$":{"_isScalar":false}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"uiCapabilities":{"catalogue":{},"management":{},"navLinks":{}}}
Role Edit Page: {"action":"edit","rolesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"userAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"indicesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"privilegesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"fatalErrors":{},"license":{"features$":{"_isScalar":false}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"uiCapabilities":{"catalogue":{},"management":{},"navLinks":{}}}
</div>
`);

Expand All @@ -109,7 +109,7 @@ describe('rolesManagementApp', () => {
]);
expect(container).toMatchInlineSnapshot(`
<div>
Role Edit Page: {"action":"edit","roleName":"someRoleName","rolesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"userAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"indicesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"privilegesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"fatalErrors":{},"license":{"features$":{"_isScalar":false}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"uiCapabilities":{"catalogue":{},"management":{},"navLinks":{}}}
Role Edit Page: {"action":"edit","roleName":"someRoleName","rolesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"userAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"indicesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"privilegesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"fatalErrors":{},"license":{"features$":{"_isScalar":false}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"uiCapabilities":{"catalogue":{},"management":{},"navLinks":{}}}
</div>
`);

Expand All @@ -132,7 +132,7 @@ describe('rolesManagementApp', () => {
]);
expect(container).toMatchInlineSnapshot(`
<div>
Role Edit Page: {"action":"clone","roleName":"someRoleName","rolesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"userAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"indicesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"privilegesAPIClient":{"http":{"basePath":{"basePath":""},"anonymousPaths":{}}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"fatalErrors":{},"license":{"features$":{"_isScalar":false}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"uiCapabilities":{"catalogue":{},"management":{},"navLinks":{}}}
Role Edit Page: {"action":"clone","roleName":"someRoleName","rolesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"userAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"indicesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"privilegesAPIClient":{"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}}},"http":{"basePath":{"basePath":"","serverBasePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"fatalErrors":{},"license":{"features$":{"_isScalar":false}},"docLinks":{"esDocBasePath":"https://www.elastic.co/guide/en/elasticsearch/reference/mocked-test-branch/"},"uiCapabilities":{"catalogue":{},"management":{},"navLinks":{}}}
</div>
`);

Expand Down
Loading

0 comments on commit 6a86b57

Please sign in to comment.