Skip to content

Commit

Permalink
Add some types for interpter plugin registration (elastic#31600)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stacey-gammon committed Mar 5, 2019
1 parent 5a23351 commit bce3614
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 13 deletions.
5 changes: 5 additions & 0 deletions kibana.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
*/

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'),
uiExports: {
injectDefaultVars: server => ({ serverBasePath: server.config().get('server.basePath') }),
},
init,
});
}
};

return new kibana.Plugin(config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})();

Expand Down
32 changes: 32 additions & 0 deletions src/legacy/plugin_discovery/plugin_spec/plugin_spec_options.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions src/legacy/server/kbn_server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -43,6 +44,7 @@ declare module 'hapi' {
config: () => KibanaConfig;
indexPatternsServiceFactory: IndexPatternsServiceFactory;
savedObjects: SavedObjectsService;
injectUiAppVars: (pluginName: string, getAppVars: () => { [key: string]: any }) => void;
}

interface Request {
Expand Down
3 changes: 3 additions & 0 deletions src/legacy/server/saved_objects/routes/_mock_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export function createMockServer(config: { [key: string]: any } = defaultConfig)
get(key: string) {
return config[key];
},
has(key: string) {
return config.hasOwnProperty(key);
},
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe('timeseriesFetcher', () => {
end: 1528977600000,
client: clientSpy,
config: {
get: () => 'myIndex' as any
get: () => 'myIndex' as any,
has: () => true
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe('transactionGroupsFetcher', () => {
case 'xpack.apm.ui.transactionGroupBucketSize':
return 100;
}
})
}),
has: () => true
}
};
const bodyQuery = { my: 'bodyQuery' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('getAnomalySeries', () => {
end: 500000,
client: clientSpy,
config: {
get: () => 'myIndex' as any
get: () => 'myIndex' as any,
has: () => true
}
}
})) as AnomalyTimeSeriesResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('timeseriesFetcher', () => {
end: 1528977600000,
client: clientSpy,
config: {
get: () => 'myIndex' as any
get: () => 'myIndex' as any,
has: () => true
}
}
});
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/apm/server/testHelpers/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface TestSetup extends Setup {
client: jest.Mock;
config: {
get: jest.Mock;
has: () => boolean;
};
}

Expand All @@ -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
};
Expand Down

0 comments on commit bce3614

Please sign in to comment.