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

Make core plugins use kbn-types #12996

Closed
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
9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"json-loader": "0.5.3",
"json-stringify-safe": "5.0.1",
"jstimezonedetect": "1.0.5",
"kbn-types": "file:packages/@elastic/kbn-types",
"leaflet": "0.7.5",
"less": "2.7.1",
"less-loader": "2.2.3",
Expand Down
51 changes: 39 additions & 12 deletions packages/@elastic/kbn-types/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
# External types for Kibana

```
npm install
npm run build
```

This builds declaration files into `./types/packages/@elastic/kbn-types`, which
is specified as the "main" type file for this package in `./package.json`.

Now external code can use this package, e.g. something like this:
This package contains external types for Kibana, which enables plugins to use
the types, e.g.:

```ts
import { KibanaFunctionalPlugin } from 'kbn-types';
Expand All @@ -18,14 +11,32 @@ export const plugin: KibanaFunctionalPlugin<{}> = function(core) {
}
```

## Development

During development this package is built separatly from the Kibana platform.

First make sure you run `npm install`, then:

```
// to build once:
npm run build

// to watch for changes:
npm start
```

This builds declaration files into `./types/packages/@elastic/kbn-types`, which
is specified as the "main" type file for this package in `./package.json`.

If you want to play around with an example, see the `./example` folder.

## Exposing types

Every type that should be directly `import`able must be exported in
`./index.ts`.
Every type that should be `import`able must be exported in `./index.ts`.

## `@internal`
## Caveats

### `@internal`

There is one gotcha related to these declaration files, which is how TypeScript
handles imports when building them. If TypeScript needs to infer a type, but
Expand All @@ -51,3 +62,19 @@ There are two ways to fix this problem:
For grep-ability, this is the error you'll likely see if hitting this problem:

> has or is using name 'Foo' from external module "./bar" but cannot be named

## Why expose declaration files instead of TypeScript files?

This package builds TypeScript [declaration files][ts-decl] instead of just
including the TypeScript files themselves. There are a couple reasons for this:

- If we shipped `.ts` files packages that depend on `kbn-types` would have to
use the same compiler options. That means we also need to find a way to share
that in a sane manner. However, if TypeScript adds the option of using
[`extends` with a package][tsconfig-extends] it might become easier.
- Tools like ts-loader [does not work nicely][ts-loader-node-modules] with `.ts`
files in `node_modules`.

[ts-decl]: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
[tsconfig-extends]: https://github.com/Microsoft/TypeScript/issues/15984
[ts-loader-node-modules]: https://github.com/TypeStrong/ts-loader/issues/278
13 changes: 11 additions & 2 deletions packages/@elastic/kbn-types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
export {
KibanaFunctionalPlugin,
KibanaPlugin
KibanaPlugin,
KibanaPluginFeatures
} from '../../../platform/server/plugins/types';
export { Logger } from '../../../platform/logging';
export { Logger, LoggerFactory } from '../../../platform/logging';
export {
Schema,
typeOfSchema
} from '../../../platform/types';
export { ElasticsearchService } from '../../../platform/server/elasticsearch';
export { KibanaConfig } from '../../../platform/server/kibana';
export { KibanaRequest, Router } from '../../../platform/server/http';
export { KibanaError } from '../../../platform/lib/Errors';
10 changes: 10 additions & 0 deletions packages/@elastic/kbn-types/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// TODO Strictly speaking it would be great to not include this function in
// the types package. However, it is needed because of how we inject `schema`
// into configs, so we don't have to specify the full type in the config
// constructors.
// Relevant TS issues for solving this at the type level:
// https://github.com/Microsoft/TypeScript/issues/6606
// https://github.com/Microsoft/TypeScript/issues/14400
export function typeOfSchema() {
return undefined;
}
1 change: 1 addition & 0 deletions packages/@elastic/kbn-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "kbn-types",
"version": "0.0.1",
"main": "./lib",
"types": "./types/packages/@elastic/kbn-types/index.d.ts",
"scripts": {
"build": "tsc",
Expand Down
11 changes: 11 additions & 0 deletions platform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ Make sure you're running the Node version specified in `../.node-version`.

## TypeScript

Both the platform and core plugins are built using TypeScript.

First you need to build the `kbn-types` package, which is the "external types"
used by core plugins.

Go to `../packages/@elastic/kbn-types` and build the types (see the readme in
that folder).

Then, when `kbn-types` is built you can build the platform and core plugins
using:

```
npm run ts:start
```
Expand Down
2 changes: 1 addition & 1 deletion platform/plugins/pid/PidConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, typeOfSchema } from '../../types';
import { Schema, typeOfSchema } from 'kbn-types';

const createPidSchema = (schema: Schema) =>
schema.object({
Expand Down
3 changes: 1 addition & 2 deletions platform/plugins/pid/PidFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { writeFileSync, unlinkSync } from 'fs';

import { PidConfig } from './PidConfig';
import { LoggerFactory, Logger } from '../../logging';
import { KibanaError } from '../../lib/Errors';
import { LoggerFactory, Logger, KibanaError } from 'kbn-types';

const FILE_ALREADY_EXISTS = 'EEXIST';

Expand Down
26 changes: 26 additions & 0 deletions platform/plugins/pid/PidPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { KibanaPlugin, Logger, KibanaPluginFeatures } from 'kbn-types';

import { PidConfig } from './PidConfig';
import { PidService } from './PidService';

export class PidPlugin implements KibanaPlugin<void> {
log: Logger;
pidService: PidService;

constructor(kibana: KibanaPluginFeatures) {
this.log = kibana.logger.get();

const config$ = kibana.config.createIfExists(PidConfig);
this.pidService = new PidService(config$, kibana.logger);
}

start() {
this.log.info('starting PidService');
this.pidService.start();
}

stop() {
this.log.info('stopping PidService');
this.pidService.stop();
}
}
2 changes: 1 addition & 1 deletion platform/plugins/pid/PidService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Observable, Subscription } from 'rxjs';
import { noop } from 'lodash';
import { LoggerFactory } from 'kbn-types';

import { PidConfig } from './PidConfig';
import { PidFile } from './PidFile';
import { LoggerFactory } from '../../logging';

export class PidService {
private readonly pid$: Observable<undefined>;
Expand Down
28 changes: 2 additions & 26 deletions platform/plugins/pid/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
import { KibanaPlugin, KibanaPluginFeatures } from '../../server/plugins/types';
import { Logger } from '../../logging';

import { PidConfig } from './PidConfig';
import { PidService } from './PidService';
import { PidPlugin } from './PidPlugin';

export const configPath = 'pid';

export const dependencies = [];

export const plugin = class implements KibanaPlugin<void> {
log: Logger;
pidService: PidService;

constructor(kibana: KibanaPluginFeatures) {
this.log = kibana.logger.get();

const config$ = kibana.config.createIfExists(PidConfig);
this.pidService = new PidService(config$, kibana.logger);
}

start() {
this.log.info('starting PidService');
this.pidService.start();
}

stop() {
this.log.info('stopping PidService');
this.pidService.stop();
}
};
export const plugin = PidPlugin;
2 changes: 1 addition & 1 deletion platform/plugins/reporting/ReportingConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, typeOfSchema } from '../../types';
import { Schema, typeOfSchema } from 'kbn-types';

const createReportingSchema = (schema: Schema) =>
schema.object({
Expand Down
2 changes: 1 addition & 1 deletion platform/plugins/reporting/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KibanaFunctionalPlugin } from '../../server/plugins/types';
import { KibanaFunctionalPlugin } from 'kbn-types';
import { XPackPluginType } from '../xpack';
import { ReportingConfig } from './ReportingConfig';

Expand Down
5 changes: 1 addition & 4 deletions platform/plugins/savedObjects/SavedObjectsService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Observable } from 'rxjs';

// TODO Change imports to some types file
import { ElasticsearchService } from '../../server/elasticsearch/ElasticsearchService';
import { KibanaConfig } from '../../server/kibana';
import { KibanaRequest } from '../../server/http';
import { ElasticsearchService, KibanaConfig, KibanaRequest } from 'kbn-types';

export class SavedObjectsService {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion platform/plugins/savedObjects/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KibanaFunctionalPlugin } from '../../server/plugins/types';
import { KibanaFunctionalPlugin } from 'kbn-types';
import { SavedObjectsService } from './SavedObjectsService';
import { registerEndpoints } from './registerEndpoints';

Expand Down
4 changes: 1 addition & 3 deletions platform/plugins/savedObjects/registerEndpoints.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Router } from '../../server/http';
import { LoggerFactory } from '../../logging';
import { Schema } from '../../types';
import { Router, LoggerFactory, Schema } from 'kbn-types';
import { SavedObjectsService } from './SavedObjectsService';

export function registerEndpoints(
Expand Down
2 changes: 1 addition & 1 deletion platform/plugins/timelion/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KibanaFunctionalPlugin } from '../../server/plugins/types';
import { KibanaFunctionalPlugin } from 'kbn-types';
import { XPackPluginType } from '../xpack';

export const configPath = undefined;
Expand Down
2 changes: 1 addition & 1 deletion platform/plugins/timelionPluginA/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KibanaFunctionalPlugin } from '../../server/plugins/types';
import { KibanaFunctionalPlugin } from 'kbn-types';
import { TimelionPluginType } from '../timelion';
import { TimelionPluginBType } from '../timelionPluginB';

Expand Down
2 changes: 1 addition & 1 deletion platform/plugins/timelionPluginB/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KibanaFunctionalPlugin } from '../../server/plugins/types';
import { KibanaFunctionalPlugin } from 'kbn-types';
import { TimelionPluginType } from '../timelion';

interface TimelionPluginBExports {
Expand Down
3 changes: 1 addition & 2 deletions platform/plugins/xpack/XPackConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Duration } from 'moment';

import { Schema, typeOfSchema } from '../../types';
import { Schema, typeOfSchema } from 'kbn-types';

const createXPackConfig = (schema: Schema) =>
schema.object({
Expand Down
2 changes: 1 addition & 1 deletion platform/plugins/xpack/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable } from 'rxjs';

import { KibanaFunctionalPlugin } from '../../server/plugins/types';
import { KibanaFunctionalPlugin } from 'kbn-types';
import { XPackConfig } from './XPackConfig';

export const configPath = ['xpack', 'xpack_main'];
Expand Down