Add some types for interpter plugin registration (#31600) (#32562)

* Some basic types for top level legacy plugin registration

* put typescript interface closer to implementation

* Use default export, you have to

* fix build and address code review comments
This commit is contained in:
Stacey Gammon 2019-03-06 12:40:22 -05:00 committed by GitHub
parent 71b1b60f70
commit fc2176ecd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 73 additions and 13 deletions

5
kibana.d.ts vendored
View file

@ -25,6 +25,7 @@ export * from './target/types/type_exports';
* All exports from TS ambient definitions (where types are added for JS source in a .d.ts file).
*/
import * as LegacyElasticsearch from './src/legacy/core_plugins/elasticsearch';
import * as LegacyKibanaPluginSpec from './src/legacy/plugin_discovery/plugin_spec/plugin_spec_options';
import * as LegacyKibanaServer from './src/legacy/server/kbn_server';
/**
@ -40,6 +41,10 @@ export namespace Legacy {
export type SavedObjectsService = LegacyKibanaServer.SavedObjectsService;
export type Server = LegacyKibanaServer.Server;
export type InitPluginFunction = LegacyKibanaPluginSpec.InitPluginFunction;
export type UiExports = LegacyKibanaPluginSpec.UiExports;
export type PluginSpecOptions = LegacyKibanaPluginSpec.PluginSpecOptions;
export namespace Plugins {
export namespace elasticsearch {
export type Plugin = LegacyElasticsearch.ElasticsearchPlugin;

View file

@ -18,10 +18,12 @@
*/
import { resolve } from 'path';
import init from './init';
import { Legacy } from '../../../../kibana';
import { init } from './init';
export default function (kibana) {
return new kibana.Plugin({
// tslint:disable-next-line
export default function InterpreterPlugin(kibana: any) {
const config: Legacy.PluginSpecOptions = {
id: 'interpreter',
require: ['kibana', 'elasticsearch'],
publicDir: resolve(__dirname, 'public'),
@ -29,6 +31,7 @@ export default function (kibana) {
injectDefaultVars: server => ({ serverBasePath: server.config().get('server.basePath') }),
},
init,
});
}
};
return new kibana.Plugin(config);
}

View file

@ -17,17 +17,26 @@
* under the License.
*/
import { routes } from './server/routes';
// @ts-ignore
import { registryFactory } from '@kbn/interpreter/common';
// @ts-ignore
import { registries } from '@kbn/interpreter/server';
export default async function (server /*options*/) {
// @ts-ignore
import { routes } from './server/routes';
import { Legacy } from '../../../../kibana';
export async function init(server: Legacy.Server /*options*/) {
server.injectUiAppVars('canvas', () => {
const config = server.config();
const basePath = config.get('server.basePath');
const reportingBrowserType = (() => {
const configKey = 'xpack.reporting.capture.browser.type';
if (!config.has(configKey)) return null;
if (!config.has(configKey)) {
return null;
}
return config.get(configKey);
})();

View file

@ -0,0 +1,32 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Server } from '../../server/kbn_server';
export type InitPluginFunction = (server: Server) => void;
export interface UiExports {
injectDefaultVars: (server: Server) => { [key: string]: any };
}
export interface PluginSpecOptions {
id: string;
require: string[];
publicDir: string;
uiExports?: UiExports;
init: InitPluginFunction;
}

View file

@ -27,6 +27,7 @@ import { SavedObjectsClient, SavedObjectsService } from './saved_objects';
export interface KibanaConfig {
get<T>(key: string): T;
has(key: string): boolean;
}
// Extend the defaults with the plugins and server methods we need.
@ -43,6 +44,7 @@ declare module 'hapi' {
config: () => KibanaConfig;
indexPatternsServiceFactory: IndexPatternsServiceFactory;
savedObjects: SavedObjectsService;
injectUiAppVars: (pluginName: string, getAppVars: () => { [key: string]: any }) => void;
}
interface Request {

View file

@ -39,6 +39,9 @@ export function createMockServer(config: { [key: string]: any } = defaultConfig)
get(key: string) {
return config[key];
},
has(key: string) {
return config.hasOwnProperty(key);
},
};
};

View file

@ -30,7 +30,8 @@ describe('timeseriesFetcher', () => {
end: 1528977600000,
client: clientSpy,
config: {
get: () => 'myIndex' as any
get: () => 'myIndex' as any,
has: () => true
}
}
});

View file

@ -24,7 +24,8 @@ describe('transactionGroupsFetcher', () => {
case 'xpack.apm.ui.transactionGroupBucketSize':
return 100;
}
})
}),
has: () => true
}
};
const bodyQuery = { my: 'bodyQuery' };

View file

@ -26,7 +26,8 @@ describe('getAnomalySeries', () => {
end: 500000,
client: clientSpy,
config: {
get: () => 'myIndex' as any
get: () => 'myIndex' as any,
has: () => true
}
}
})) as AnomalyTimeSeriesResponse;

View file

@ -22,7 +22,8 @@ describe('timeseriesFetcher', () => {
end: 1528977600000,
client: clientSpy,
config: {
get: () => 'myIndex' as any
get: () => 'myIndex' as any,
has: () => true
}
}
});

View file

@ -10,6 +10,7 @@ interface TestSetup extends Setup {
client: jest.Mock;
config: {
get: jest.Mock;
has: () => boolean;
};
}
@ -22,7 +23,8 @@ export function getSetupMock(overrides: Partial<TestSetup> = {}) {
term: { field: 'test.esfilter.query' }
},
config: {
get: jest.fn()
get: jest.fn(),
has: () => true
},
...overrides
};