Skip to content

Establish pattern for typing legacy plugins - #26045

Merged
joshdover merged 5 commits into
elastic:masterfrom
joshdover:es-plugin-types
Dec 17, 2018
Merged

joshdover merged 5 commits into
elastic:masterfrom
joshdover:es-plugin-types

Conversation

@joshdover

@joshdover joshdover commented Nov 21, 2018 •

Copy link
Copy Markdown
Contributor

Summary

This adds a new common pattern for adding type definitions to legacy plugins and the KbnServer itself.

Now plugins that wish to use TypeScript can import our customized Server type from src/server/kbn_server to get a type that has definitions supplied for each of the objects on server.plugins (eventually).

Plugins (core_plugins, x-pack, and external) will be able to use our exported types by importing the Legacy namespace:

import { Legacy } from 'kibana'

function registerRoute(server: Legacy.Server) {
  // has full type defintions for this plugin
  const { callWithRequest } = server.plugins.elasticsearch.getCluster('admin');  
}

// Could reference plugin types directly
type ESPlugin = Legacy.Plugins.elasticsearch.ElasticsearchPlugin;

@joshdover joshdover added Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// v7.0.0 v6.6.0 labels Nov 21, 2018
@elasticmachine

Copy link
Copy Markdown
Contributor

Pinging @elastic/kibana-platform

@joshdover joshdover changed the title [WIP] Establish pattern for typing legacy plugins Establish pattern for typing legacy plugins Nov 21, 2018
Comment thread src/core_plugins/elasticsearch/index.d.ts Outdated
@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@joshdover joshdover added the WIP Work in progress label Nov 30, 2018
@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@stacey-gammon

Copy link
Copy Markdown

Any idea on the ETA of this PR? I'd like to use KibanaConfig :)

Comment thread index.d.ts Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a top level typings folder too, I think types is new in this PR? I think this will end up being confusing. Maybe they can be merged?

https://github.com/elastic/kibana/tree/master/typings

@stacey-gammon stacey-gammon Dec 13, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

hm, no there isn't, so actually types folder must be a build artifact, hence the gitignore? Still a bit confusing then I think... but I also am not fully aware of everything that is going on with the types setup, and that typings folder is pretty new so maybe there was just a disconnect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The types directory is built from yarn build:types which gets run on yarn kbn bootstrap. In master it goes to the target/types directory but in this PR I changed it to types.

The reason I changed it to not be target is that the build wipes out that directory before building and I was previously trying to import from it which was failing Typescript build for x-pack. Let me confirm, but I think after some of the other changes I made, I can put this back the way it was before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed, I'll put this back to pulling from the target directory to reduce confusion. Thanks for catching this!

@joshdover
joshdover requested a review from a team December 13, 2018 16:52
@elasticmachine

This comment has been minimized.

@elasticmachine

Copy link
Copy Markdown
Contributor

💔 Build Failed

@sorenlouv sorenlouv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes to APM look good 👍

@epixa

epixa commented Dec 14, 2018

Copy link
Copy Markdown
Contributor

Why import type information differently depending on whether the plugin is internal or not?

Comment thread index.d.ts Outdated
Comment thread index.d.ts Outdated
Comment thread src/core/server/legacy_compat/legacy_service.test.ts Outdated
Comment thread index.d.ts
/**
* All exports from TS source files (where the implementation is actualy done in TS).
*/
export * from './target/types/type_exports';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: btw, I just realized that we may need to add one more Project to src/dev/build/tasks/transpile_typescript_task.js with tsconfig.types.json and run it as the first one, so that plugins that depend on generated types can be successfully built/transpiled during build? We have just one now, src/plugins/test, but it's excluded from build. It's semi out-of-scope for this PR so feel free to ignore that :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've updated the build to build this project, but in the repo root rather than the build root. The reason for this is we build the typescript files from the repo root in x-pack so the types will need to be present there. I've also added an alias so you can now do this in any x-pack code:

import { Legacy } from 'kibana'

@@ -0,0 +1,391 @@
/*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: is this file crafted by you or we have copied it from somewhere? Just checking whether I should review it or not :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's written by me. Due to the way we use strings in callWithRequest to lookup methods on the elasticsearch.js Client I had to write all of those method calls with the appropriate params and return types from '@types/elasticsearch'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Got it, thanks!

Comment thread src/server/kbn_server.d.ts Outdated
import { ElasticsearchPlugin } from '../legacy/core_plugins/elasticsearch';

export interface KibanaConfig {
get<T = any>(key: string): T;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: should it rather be T = unknown?

Comment thread src/server/kbn_server.d.ts Outdated
Comment thread src/server/kbn_server.d.ts Outdated

import { GraphQLSchema } from 'graphql';
import { Request, ResponseToolkit, Server } from 'hapi';
import { Request, ResponseToolkit, Server } from 'src/server/kbn_server';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: why can't we use relative paths here and point to the root index.d.ts with Legacy namespace?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See comment above, you can now do import { Legacy } from 'kibana'

@joshdover

Copy link
Copy Markdown
Contributor Author

@epixa I've changed the build process so now they can all import the same way, directly from 'kibana'.

@elasticmachine

Copy link
Copy Markdown
Contributor

💔 Build Failed

@joshdover

Copy link
Copy Markdown
Contributor Author

retest

@elasticmachine

Copy link
Copy Markdown
Contributor

💚 Build Succeeded

@azasypkin azasypkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The overall shape of this PR and the part related to the new platform look good to me, thanks! The only part I haven't looked closely at is the the Elasticsearch client types, but we can improve them along the way if/when needed.

Comment thread kibana.d.ts
@@ -0,0 +1,48 @@
/*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: just curious, why not index.d.ts as it was before? I believe there is even a convention to rely on index.d.ts from the project root if types isn't specified in package.json (not that we shouldn't set it explicitly, but still).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TypeScript compiler doesn't support using the compilerOptions.paths feature to reference the index.d.ts file, but it works when using a different name. Using this option was necessary to support import ... from 'kibana' for x-pack and core packages.

Comment thread src/core/server/legacy_compat/legacy_service.test.ts Outdated
@elasticmachine

Copy link
Copy Markdown
Contributor

💚 Build Succeeded

@joshdover
joshdover merged commit 08fd427 into elastic:master Dec 17, 2018
@joshdover
joshdover deleted the es-plugin-types branch December 17, 2018 18:52
joshdover added a commit to joshdover/kibana that referenced this pull request Dec 17, 2018
* Add legacy types and export them for plugins
* Add support for core_plugins to import from 'kibana'
joshdover added a commit that referenced this pull request Dec 17, 2018
* Add legacy types and export them for plugins
* Add support for core_plugins to import from 'kibana'
@joshdover

Copy link
Copy Markdown
Contributor Author

6.x: 16a900a

patrykkopycinski pushed a commit to patrykkopycinski/kibana that referenced this pull request May 6, 2026
* Add legacy types and export them for plugins
* Add support for core_plugins to import from 'kibana'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// v6.6.0 v7.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants