Skip to content

Commit

Permalink
Add pattern for plugin-scoped services
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Jul 15, 2019
1 parent ec8ec82 commit bedca06
Show file tree
Hide file tree
Showing 62 changed files with 509 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@
<b>Signature:</b>

```typescript
elasticsearch: {
adminClient$: Observable<ClusterClient>;
dataClient$: Observable<ClusterClient>;
createClient: (type: string, clientConfig?: Partial<ElasticsearchClientConfig>) => ClusterClient;
};
elasticsearch: ElasticsearchServiceSetup;
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,5 @@
<b>Signature:</b>

```typescript
http: {
registerOnPreAuth: HttpServiceSetup['registerOnPreAuth'];
registerAuth: HttpServiceSetup['registerAuth'];
registerOnPostAuth: HttpServiceSetup['registerOnPostAuth'];
basePath: HttpServiceSetup['basePath'];
createNewServer: HttpServiceSetup['createNewServer'];
isTlsEnabled: HttpServiceSetup['isTlsEnabled'];
};
http: HttpServiceSetup;
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export interface CoreSetup

| Property | Type | Description |
| --- | --- | --- |
| [elasticsearch](./kibana-plugin-server.coresetup.elasticsearch.md) | <code>{</code><br/><code> adminClient$: Observable&lt;ClusterClient&gt;;</code><br/><code> dataClient$: Observable&lt;ClusterClient&gt;;</code><br/><code> createClient: (type: string, clientConfig?: Partial&lt;ElasticsearchClientConfig&gt;) =&gt; ClusterClient;</code><br/><code> }</code> | |
| [http](./kibana-plugin-server.coresetup.http.md) | <code>{</code><br/><code> registerOnPreAuth: HttpServiceSetup['registerOnPreAuth'];</code><br/><code> registerAuth: HttpServiceSetup['registerAuth'];</code><br/><code> registerOnPostAuth: HttpServiceSetup['registerOnPostAuth'];</code><br/><code> basePath: HttpServiceSetup['basePath'];</code><br/><code> createNewServer: HttpServiceSetup['createNewServer'];</code><br/><code> isTlsEnabled: HttpServiceSetup['isTlsEnabled'];</code><br/><code> }</code> | |
| [elasticsearch](./kibana-plugin-server.coresetup.elasticsearch.md) | <code>ElasticsearchServiceSetup</code> | |
| [http](./kibana-plugin-server.coresetup.http.md) | <code>HttpServiceSetup</code> | |

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ export interface ElasticsearchServiceSetup
| [adminClient$](./kibana-plugin-server.elasticsearchservicesetup.adminclient$.md) | <code>Observable&lt;ClusterClient&gt;</code> | |
| [createClient](./kibana-plugin-server.elasticsearchservicesetup.createclient.md) | <code>(type: string, clientConfig?: Partial&lt;ElasticsearchClientConfig&gt;) =&gt; ClusterClient</code> | Create application specific Elasticsearch cluster API client with customized config. |
| [dataClient$](./kibana-plugin-server.elasticsearchservicesetup.dataclient$.md) | <code>Observable&lt;ClusterClient&gt;</code> | |
| [legacy](./kibana-plugin-server.elasticsearchservicesetup.legacy.md) | <code>{</code><br/><code> readonly config$: Observable&lt;ElasticsearchConfig&gt;;</code><br/><code> }</code> | |

This file was deleted.

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-server](./kibana-plugin-server.md) &gt; [LegacyCoreSetup](./kibana-plugin-server.legacycoresetup.md) &gt; [elasticsearch](./kibana-plugin-server.legacycoresetup.elasticsearch.md)

## LegacyCoreSetup.elasticsearch property

<b>Signature:</b>

```typescript
elasticsearch: ElasticsearchServiceSetup & {
readonly legacy: {
readonly config$: Observable<ElasticsearchConfig>;
};
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [LegacyCoreSetup](./kibana-plugin-server.legacycoresetup.md)

## LegacyCoreSetup interface


<b>Signature:</b>

```typescript
export interface LegacyCoreSetup extends CoreSetup
```
## Properties
| Property | Type | Description |
| --- | --- | --- |
| [elasticsearch](./kibana-plugin-server.legacycoresetup.elasticsearch.md) | <code>ElasticsearchServiceSetup &amp; {</code><br/><code> readonly legacy: {</code><br/><code> readonly config$: Observable&lt;ElasticsearchConfig&gt;;</code><br/><code> };</code><br/><code> }</code> | |
| [plugins](./kibana-plugin-server.legacycoresetup.plugins.md) | <code>PluginsServiceSetup</code> | |
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [LegacyCoreSetup](./kibana-plugin-server.legacycoresetup.md) &gt; [plugins](./kibana-plugin-server.legacycoresetup.plugins.md)

## LegacyCoreSetup.plugins property

<b>Signature:</b>

```typescript
plugins: PluginsServiceSetup;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [LegacyCoreStart](./kibana-plugin-server.legacycorestart.md)

## LegacyCoreStart interface


<b>Signature:</b>

```typescript
export interface LegacyCoreStart extends CoreStart
```
## Properties
| Property | Type | Description |
| --- | --- | --- |
| [plugins](./kibana-plugin-server.legacycorestart.plugins.md) | <code>PluginsServiceStart</code> | |
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [InternalCoreStart](./kibana-plugin-server.internalcorestart.md) &gt; [plugins](./kibana-plugin-server.internalcorestart.plugins.md)
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [LegacyCoreStart](./kibana-plugin-server.legacycorestart.md) &gt; [plugins](./kibana-plugin-server.legacycorestart.plugins.md)

## InternalCoreStart.plugins property
## LegacyCoreStart.plugins property

<b>Signature:</b>

Expand Down
3 changes: 2 additions & 1 deletion docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [FakeRequest](./kibana-plugin-server.fakerequest.md) | Fake request object created manually by Kibana plugins. |
| [HttpServiceSetup](./kibana-plugin-server.httpservicesetup.md) | |
| [HttpServiceStart](./kibana-plugin-server.httpservicestart.md) | |
| [InternalCoreStart](./kibana-plugin-server.internalcorestart.md) | |
| [KibanaRequestRoute](./kibana-plugin-server.kibanarequestroute.md) | Request specific route information exposed to a handler. |
| [LegacyCoreSetup](./kibana-plugin-server.legacycoresetup.md) | |
| [LegacyCoreStart](./kibana-plugin-server.legacycorestart.md) | |
| [Logger](./kibana-plugin-server.logger.md) | Logger exposes all the necessary methods to log any type of information and this is the interface used by the logging consumers including plugins. |
| [LoggerFactory](./kibana-plugin-server.loggerfactory.md) | The single purpose of <code>LoggerFactory</code> interface is to define a way to retrieve a context-based logger instance. |
| [LogMeta](./kibana-plugin-server.logmeta.md) | Contextual metadata |
Expand Down
24 changes: 19 additions & 5 deletions src/core/public/application/application_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
*/

import { capabilitiesServiceMock } from './capabilities/capabilities_service.mock';
import { ApplicationService, ApplicationSetup, ApplicationStart } from './application_service';
import {
ApplicationService,
ApplicationSetup,
ApplicationStart,
InternalApplicationStart,
} from './application_service';

type ApplicationServiceContract = PublicMethodsOf<ApplicationService>;

Expand All @@ -28,18 +33,27 @@ const createSetupContractMock = (): jest.Mocked<ApplicationSetup> => ({
});

const createStartContractMock = (): jest.Mocked<ApplicationStart> => ({
mount: jest.fn(),
...capabilitiesServiceMock.createStartContract(),
capabilities: capabilitiesServiceMock.createStartContract().capabilities,
});

const createInternalStartContractMock = (): DeeplyMockedKeys<InternalApplicationStart> => {
const internalStartContract = {
availableApps: [],
forPlugin: jest.fn(),
};
internalStartContract.forPlugin.mockImplementation(createStartContractMock);
return internalStartContract;
};

const createMock = (): jest.Mocked<ApplicationServiceContract> => ({
setup: jest.fn().mockReturnValue(createSetupContractMock()),
start: jest.fn().mockReturnValue(createStartContractMock()),
setup: jest.fn().mockResolvedValue(createSetupContractMock()),
start: jest.fn().mockReturnValue(createInternalStartContractMock()),
stop: jest.fn(),
});

export const applicationServiceMock = {
create: createMock,
createSetupContract: createSetupContractMock,
createInternalStartContract: createInternalStartContractMock,
createStartContract: createStartContractMock,
};
17 changes: 10 additions & 7 deletions src/core/public/application/application_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { Observable, BehaviorSubject } from 'rxjs';
import { CapabilitiesStart, CapabilitiesService, Capabilities } from './capabilities';
import { InjectedMetadataStart } from '../injected_metadata';
import { CoreService } from '../../types';

interface BaseApp {
id: string;
Expand Down Expand Up @@ -99,11 +100,14 @@ export interface ApplicationSetup {
}

export interface ApplicationStart {
mount: (mountHandler: Function) => void;
availableApps: CapabilitiesStart['availableApps'];
capabilities: CapabilitiesStart['capabilities'];
}

export interface InternalApplicationStart {
availableApps: CapabilitiesStart['availableApps'];
forPlugin(): ApplicationStart;
}

interface StartDeps {
injectedMetadata: InjectedMetadataStart;
}
Expand All @@ -112,12 +116,12 @@ interface StartDeps {
* Service that is responsible for registering new applications.
* @internal
*/
export class ApplicationService {
export class ApplicationService implements CoreService<ApplicationSetup, ApplicationStart> {
private readonly apps$ = new BehaviorSubject<App[]>([]);
private readonly legacyApps$ = new BehaviorSubject<LegacyApp[]>([]);
private readonly capabilities = new CapabilitiesService();

public setup(): ApplicationSetup {
public setup() {
return {
registerApp: (app: App) => {
this.apps$.next([...this.apps$.value, app]);
Expand All @@ -128,7 +132,7 @@ export class ApplicationService {
};
}

public async start({ injectedMetadata }: StartDeps): Promise<ApplicationStart> {
public async start({ injectedMetadata }: StartDeps) {
this.apps$.complete();
this.legacyApps$.complete();

Expand All @@ -139,9 +143,8 @@ export class ApplicationService {
});

return {
mount() {},
capabilities,
availableApps,
forPlugin: () => ({ capabilities }),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const createStartContractMock = (

type CapabilitiesServiceContract = PublicMethodsOf<CapabilitiesService>;
const createMock = (): jest.Mocked<CapabilitiesServiceContract> => ({
start: jest.fn().mockImplementation(({ apps }) => createStartContractMock(apps)),
start: jest.fn().mockImplementation(async ({ apps }) => createStartContractMock(apps)),
});

export const capabilitiesServiceMock = {
Expand Down
7 changes: 6 additions & 1 deletion src/core/public/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
* under the License.
*/

export { ApplicationService, ApplicationSetup, ApplicationStart } from './application_service';
export {
ApplicationService,
ApplicationSetup,
ApplicationStart,
InternalApplicationStart,
} from './application_service';
export { Capabilities } from './capabilities';
18 changes: 15 additions & 3 deletions src/core/public/chrome/chrome_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
ChromeBreadcrumb,
ChromeService,
InternalChromeStart,
ChromeStart,
} from './chrome_service';

const createStartContractMock = () => {
const startContract: jest.Mocked<InternalChromeStart> = {
getComponent: jest.fn(),
const startContract: DeeplyMockedKeys<ChromeStart> = {
navLinks: {
getNavLinks$: jest.fn(),
has: jest.fn(),
Expand Down Expand Up @@ -76,17 +76,29 @@ const createStartContractMock = () => {
return startContract;
};

const createInternalStartContractMock = (): DeeplyMockedKeys<InternalChromeStart> => {
const startContract = createStartContractMock();
const internalStartContract: DeeplyMockedKeys<InternalChromeStart> = {
getComponent: jest.fn(),
forPlugin: jest.fn(),
};
internalStartContract.forPlugin.mockReturnValue(startContract);
return internalStartContract;
};

type ChromeServiceContract = PublicMethodsOf<ChromeService>;
const createMock = () => {
const mocked: jest.Mocked<ChromeServiceContract> = {
setup: jest.fn(),
start: jest.fn(),
stop: jest.fn(),
};
mocked.start.mockResolvedValue(createStartContractMock());
mocked.start.mockResolvedValue(createInternalStartContractMock());
return mocked;
};

export const chromeServiceMock = {
create: createMock,
createInternalStartContract: createInternalStartContractMock,
createStartContract: createStartContractMock,
};
Loading

0 comments on commit bedca06

Please sign in to comment.