Skip to content
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

Plugin types #40122

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The interface that should be returned by a `PluginInitializer`<!-- -->.
<b>Signature:</b>

```typescript
export interface Plugin<TSetup = void, TStart = void, TPluginsSetup extends {} = {}, TPluginsStart extends {} = {}>
export interface Plugin<TSetup extends object | void = object, TStart extends object | void = object, TPluginsSetup extends object = object, TPluginsStart extends object = object>
```

## Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ The `plugin` export at the root of a plugin's `public` directory should conform
<b>Signature:</b>

```typescript
export declare type PluginInitializer<TSetup, TStart, TPluginsSetup extends Record<string, unknown> = {}, TPluginsStart extends Record<string, unknown> = {}> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
export declare type PluginInitializer<TSetup extends object | void = object, TStart extends object | void = object, TPluginsSetup extends object = object, TPluginsStart extends object = object> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The interface that should be returned by a `PluginInitializer`<!-- -->.
<b>Signature:</b>

```typescript
export interface Plugin<TSetup = void, TStart = void, TPluginsSetup extends {} = {}, TPluginsStart extends {} = {}>
export interface Plugin<TSetup extends object | void = object, TStart extends object | void = object, TPluginsSetup extends object = object, TPluginsStart extends object = object>
```

## Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ The `plugin` export at the root of a plugin's `server` directory should conform
<b>Signature:</b>

```typescript
export declare type PluginInitializer<TSetup, TStart, TPluginsSetup extends Record<PluginName, unknown> = {}, TPluginsStart extends Record<PluginName, unknown> = {}> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
export declare type PluginInitializer<TSetup extends object | void = object, TStart extends object | void = object, TPluginsSetup extends object = object, TPluginsStart extends object = object> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
```
2 changes: 1 addition & 1 deletion src/core/public/plugins/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function createManifest(
} as DiscoveredPlugin;
}

let plugin: PluginWrapper<unknown, Record<string, unknown>>;
let plugin: PluginWrapper;
const initializerContext = {};
const addBasePath = (path: string) => path;

Expand Down
26 changes: 13 additions & 13 deletions src/core/public/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { DiscoveredPlugin, PluginName } from '../../server';
import { DiscoveredPlugin } from '../../server';
import { PluginInitializerContext } from './plugin_context';
import { loadPluginBundle } from './plugin_loader';
import { CoreStart, CoreSetup } from '..';
Expand All @@ -28,10 +28,10 @@ import { CoreStart, CoreSetup } from '..';
* @public
*/
export interface Plugin<
TSetup = void,
TStart = void,
TPluginsSetup extends {} = {},
TPluginsStart extends {} = {}
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
setup(core: CoreSetup, plugins: TPluginsSetup): TSetup | Promise<TSetup>;
start(core: CoreStart, plugins: TPluginsStart): TStart | Promise<TStart>;
Expand All @@ -45,10 +45,10 @@ export interface Plugin<
* @public
*/
export type PluginInitializer<
TSetup,
TStart,
TPluginsSetup extends Record<string, unknown> = {},
TPluginsStart extends Record<string, unknown> = {}
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;

/**
Expand All @@ -58,10 +58,10 @@ export type PluginInitializer<
* @internal
*/
export class PluginWrapper<
TSetup = unknown,
TStart = unknown,
TPluginsSetup extends Record<PluginName, unknown> = Record<PluginName, unknown>,
TPluginsStart extends Record<PluginName, unknown> = Record<PluginName, unknown>
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
public readonly name: DiscoveredPlugin['id'];
public readonly configPath: DiscoveredPlugin['configPath'];
Expand Down
18 changes: 9 additions & 9 deletions src/core/public/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { omit } from 'lodash';

import { DiscoveredPlugin, PluginName } from '../../server';
import { DiscoveredPlugin } from '../../server';
import { CoreContext } from '../core_system';
import { PluginWrapper } from './plugin';
import { PluginsServiceSetupDeps, PluginsServiceStartDeps } from './plugins_service';
Expand Down Expand Up @@ -59,10 +59,10 @@ export function createPluginInitializerContext(
* @internal
*/
export function createPluginSetupContext<
TSetup,
TStart,
TPluginsSetup extends Record<PluginName, unknown>,
TPluginsStart extends Record<PluginName, unknown>
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
>(
coreContext: CoreContext,
deps: PluginsServiceSetupDeps,
Expand All @@ -87,10 +87,10 @@ export function createPluginSetupContext<
* @internal
*/
export function createPluginStartContext<
TSetup,
TStart,
TPluginsSetup extends Record<PluginName, unknown>,
TPluginsStart extends Record<PluginName, unknown>
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
>(
coreContext: CoreContext,
deps: PluginsServiceStartDeps,
Expand Down
10 changes: 3 additions & 7 deletions src/core/public/plugins/plugin_loader.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@
*/

import { PluginName } from 'src/core/server';
import { LoadPluginBundle, UnknownPluginInitializer } from './plugin_loader';
import { PluginInitializer } from '..';

/**
* @param initializerProvider A function provided by the test to resolve initializers.
*/
const createLoadPluginBundleMock = (
initializerProvider: (name: PluginName) => UnknownPluginInitializer
): jest.Mock<ReturnType<LoadPluginBundle>, Parameters<LoadPluginBundle>> =>
jest.fn((addBasePath, pluginName) => {
return Promise.resolve(initializerProvider(pluginName)) as any;
});
const createLoadPluginBundleMock = (initializerProvider: (name: PluginName) => PluginInitializer) =>
jest.fn(async (addBasePath, pluginName) => initializerProvider(pluginName));

export const loadPluginBundleMock = { create: createLoadPluginBundleMock };
24 changes: 9 additions & 15 deletions src/core/public/plugins/plugin_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
import { PluginName } from '../../server';
import { PluginInitializer } from './plugin';

/**
* Unknown variant for internal use only for when plugins are not known.
* @internal
*/
export type UnknownPluginInitializer = PluginInitializer<unknown, Record<string, unknown>>;

/**
* Custom window type for loading bundles. Do not extend global Window to avoid leaking these types.
* @internal
*/
export interface CoreWindow {
__kbnNonce__: string;
__kbnBundles__: {
[pluginBundleName: string]: UnknownPluginInitializer | undefined;
[pluginBundleName: string]: object | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's a correct.__kbnBundles__ contains init functions, not objects.
https://github.com/restrry/kibana/blob/af295b43bdf27bcefd7ed293fa97e96a9656add0/src/core/public/plugins/plugin_loader.ts#L91-L99

Suggested change
[pluginBundleName: string]: object | undefined;
[pluginBundleName: string]: PluginInitializer | undefined;

Copy link
Contributor

@mshustov mshustov Jul 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That actually illustrates another problem with the current code. any not primitive type is a valid one
2019-07-04_10-14-30

};
}

Expand All @@ -60,10 +54,10 @@ export const LOAD_TIMEOUT = 120 * 1000; // 2 minutes
* @internal
*/
export const loadPluginBundle: LoadPluginBundle = <
TSetup,
TStart,
TPluginsSetup extends Record<string, unknown>,
TPluginsStart extends Record<string, unknown>
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
>(
addBasePath: (path: string) => string,
pluginName: PluginName,
Expand Down Expand Up @@ -123,10 +117,10 @@ export const loadPluginBundle: LoadPluginBundle = <
* @internal
*/
export type LoadPluginBundle = <
TSetup,
TStart,
TPluginsSetup extends Record<string, unknown>,
TPluginsStart extends Record<string, unknown>
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
>(
addBasePath: (path: string) => string,
pluginName: PluginName,
Expand Down
6 changes: 2 additions & 4 deletions src/core/public/plugins/plugins_service.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
* under the License.
*/

import { PluginName } from 'kibana/server';
import { Plugin } from './plugin';
import { loadPluginBundleMock } from './plugin_loader.mock';

export type MockedPluginInitializer = jest.Mock<Plugin<unknown, Record<string, unknown>>, any>;
export type MockedPluginInitializer = jest.Mock<Plugin, any>;

export const mockPluginInitializerProvider: jest.Mock<
MockedPluginInitializer,
[PluginName]
MockedPluginInitializer
> = jest.fn().mockRejectedValue(new Error('No provider specified'));

export const mockLoadPluginBundle = loadPluginBundleMock.create(mockPluginInitializerProvider);
Expand Down
5 changes: 1 addition & 4 deletions src/core/public/plugins/plugins_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ export interface PluginsServiceStart {
*/
export class PluginsService implements CoreService<PluginsServiceSetup, PluginsServiceStart> {
/** Plugin wrappers in topological order. */
private readonly plugins: Map<
PluginName,
PluginWrapper<unknown, Record<string, unknown>>
> = new Map();
private readonly plugins: Map<PluginName, PluginWrapper<void, object>> = new Map();
private readonly satupPlugins: PluginName[] = [];

constructor(private readonly coreContext: CoreContext) {}
Expand Down
4 changes: 2 additions & 2 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export interface OverlayStart {
}

// @public
export interface Plugin<TSetup = void, TStart = void, TPluginsSetup extends {} = {}, TPluginsStart extends {} = {}> {
export interface Plugin<TSetup extends object | void = object, TStart extends object | void = object, TPluginsSetup extends object = object, TPluginsStart extends object = object> {
// (undocumented)
setup(core: CoreSetup, plugins: TPluginsSetup): TSetup | Promise<TSetup>;
// (undocumented)
Expand All @@ -485,7 +485,7 @@ export interface Plugin<TSetup = void, TStart = void, TPluginsSetup extends {} =
}

// @public
export type PluginInitializer<TSetup, TStart, TPluginsSetup extends Record<string, unknown> = {}, TPluginsStart extends Record<string, unknown> = {}> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
export type PluginInitializer<TSetup extends object | void = object, TStart extends object | void = object, TPluginsSetup extends object = object, TPluginsStart extends object = object> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;

// @public
export interface PluginInitializerContext {
Expand Down
24 changes: 12 additions & 12 deletions src/core/server/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ export interface DiscoveredPluginInternal extends DiscoveredPlugin {
* @public
*/
export interface Plugin<
TSetup = void,
TStart = void,
TPluginsSetup extends {} = {},
TPluginsStart extends {} = {}
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
setup(core: CoreSetup, plugins: TPluginsSetup): TSetup | Promise<TSetup>;
start(core: CoreStart, plugins: TPluginsStart): TStart | Promise<TStart>;
Expand All @@ -153,10 +153,10 @@ export interface Plugin<
* @public
*/
export type PluginInitializer<
TSetup,
TStart,
TPluginsSetup extends Record<PluginName, unknown> = {},
TPluginsStart extends Record<PluginName, unknown> = {}
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;

/**
Expand All @@ -166,10 +166,10 @@ export type PluginInitializer<
* @internal
*/
export class PluginWrapper<
TSetup = unknown,
TStart = unknown,
TPluginsSetup extends Record<PluginName, unknown> = Record<PluginName, unknown>,
TPluginsStart extends Record<PluginName, unknown> = Record<PluginName, unknown>
TSetup extends object | void = object,
TStart extends object | void = object,
TPluginsSetup extends object = object,
TPluginsStart extends object = object
> {
public readonly name: PluginManifest['id'];
public readonly configPath: PluginManifest['configPath'];
Expand Down
8 changes: 4 additions & 4 deletions src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ export function createPluginInitializerContext(
* @param deps Dependencies that Plugins services gets during setup.
* @internal
*/
export function createPluginSetupContext<TPlugin, TPluginDependencies>(
export function createPluginSetupContext(
coreContext: CoreContext,
deps: PluginsServiceSetupDeps,
plugin: PluginWrapper<TPlugin, TPluginDependencies>
plugin: PluginWrapper
): CoreSetup {
return {
elasticsearch: {
Expand Down Expand Up @@ -138,10 +138,10 @@ export function createPluginSetupContext<TPlugin, TPluginDependencies>(
* @param deps Dependencies that Plugins services gets during start.
* @internal
*/
export function createPluginStartContext<TPlugin, TPluginDependencies>(
export function createPluginStartContext(
coreContext: CoreContext,
deps: PluginsServiceStartDeps,
plugin: PluginWrapper<TPlugin, TPluginDependencies>
plugin: PluginWrapper
): CoreStart {
return {};
}
Loading