-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Update http route registration in migration guide and examples #48518
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a8ba3b
Update http route registration in migration guide and examples
rudolf 47a2c52
Migration examples: added more types, table of contents
rudolf 4f98d66
Use __legacy namespace within CoreSetup
rudolf 8477048
Inject legacy dependencies as third argument into plugins
rudolf 8b05c2a
Legacy third argument for browserside and other updates
rudolf c47f0f1
Clarify comments
rudolf 935555b
Edits
rudolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,20 @@ | |
This document is a list of examples of how to migrate plugin code from legacy | ||
APIs to their New Platform equivalents. | ||
|
||
- [Migration Examples](#migration-examples) | ||
- [Configuration](#configuration) | ||
- [Declaring config schema](#declaring-config-schema) | ||
- [Using New Platform config from a Legacy plugin](#using-new-platform-config-from-a-legacy-plugin) | ||
- [Create a New Platform plugin](#create-a-new-platform-plugin) | ||
- [HTTP Routes](#http-routes) | ||
- [Route Registration](#route-registration) | ||
- [1. Legacy route registration](#1-legacy-route-registration) | ||
- [2. New Platform shim using legacy router](#2-new-platform-shim-using-legacy-router) | ||
- [3. New Platform shim using New Platform router](#3-new-platform-shim-using-new-platform-router) | ||
- [4. New Platform plugin](#4-new-platform-plugin) | ||
- [Accessing Services](#accessing-services) | ||
- [Chrome](#chrome) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the TOC |
||
|
||
## Configuration | ||
|
||
### Declaring config schema | ||
|
@@ -150,15 +164,18 @@ This interface has a different API with slightly different behaviors. | |
not have native support for converting Boom exceptions into HTTP responses. | ||
|
||
### Route Registration | ||
Because of the incompatibility between the legacy and New Platform HTTP Route | ||
API's it might be helpful to break up your migration work into several stages. | ||
|
||
#### 1. Legacy route registration | ||
```ts | ||
// Legacy route registration | ||
// legacy/plugins/myplugin/index.ts | ||
import Joi from 'joi'; | ||
|
||
new kibana.Plugin({ | ||
init(server) { | ||
server.route({ | ||
path: '/api/my-plugin/my-route', | ||
path: '/api/demoplugin/search', | ||
method: 'POST', | ||
options: { | ||
validate: { | ||
|
@@ -173,17 +190,133 @@ new kibana.Plugin({ | |
}); | ||
} | ||
}); | ||
``` | ||
|
||
#### 2. New Platform shim using legacy router | ||
Create a New Platform shim and inject the legacy `server.route` into your | ||
plugin's setup function. This shim isn't exactly the same as the New | ||
Platform's API's but it allows us to leave all of our route registration | ||
untouched. | ||
|
||
// New Platform equivalent | ||
```ts | ||
// legacy/plugins/demoplugin/index.ts | ||
import { Plugin, DemoPluginCoreSetup } from './server/plugin'; | ||
export default (kibana) => { | ||
return new kibana.Plugin({ | ||
id: 'demo_plugin', | ||
|
||
init(server) { | ||
// core shim | ||
const coreSetup: DemoPluginCoreSetup = { | ||
http: { | ||
route: server.route | ||
}, | ||
}; | ||
|
||
new Plugin().setup(coreSetup); | ||
} | ||
} | ||
} | ||
``` | ||
```ts | ||
// legacy/plugins/demoplugin/server/plugin.ts | ||
import { Legacy } from 'kibana'; | ||
|
||
export interface DemoPluginCoreSetup { | ||
http: { | ||
route: Legacy.Server['route']; | ||
}; | ||
}; | ||
|
||
export class Plugin { | ||
public setup(core: DemoPluginCoreSetup, plugins: DemoPluginsSetup) { | ||
// HTTP functionality from legacy platform, accessed in a way that's | ||
// compatible with NP conventions even if not 100% the same | ||
core.http.route({ | ||
path: '/api/demoplugin/search', | ||
method: 'POST', | ||
options: { | ||
validate: { | ||
payload: Joi.object({ | ||
field1: Joi.string().required(), | ||
}), | ||
} | ||
}, | ||
async handler(req) { | ||
return { message: `Received field1: ${req.payload.field1}` }; | ||
}, | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
#### 3. New Platform shim using New Platform router | ||
We now switch the shim to use the real New Platform HTTP API's in `coreSetup` | ||
instead of relying on the legacy `server.route`. Since our plugin is now using | ||
the New Platform API's we are guaranteed that our HTTP route handling is 100% | ||
compatible with the New Platform. As a result, we will also have to adapt our | ||
route registration accordingly. | ||
```ts | ||
// legacy/plugins/demoplugin/index.ts | ||
import { Plugin } from './server/plugin'; | ||
export default (kibana) => { | ||
return new kibana.Plugin({ | ||
id: 'demo_plugin', | ||
|
||
init(server) { | ||
// core shim | ||
const coreSetup = { | ||
http: server.newPlatform.setup.core.http, | ||
}; | ||
|
||
new Plugin().setup(coreSetup); | ||
} | ||
} | ||
} | ||
``` | ||
```ts | ||
// legacy/plugins/demoplugin/server/plugin.ts | ||
import { schema } from '@kbn/config-schema'; | ||
import { CoreSetup } from 'src/core/server'; | ||
|
||
class Plugin { | ||
public setup(core: CoreSetup) { | ||
const router = core.http.createRouter(); | ||
router.post( | ||
{ | ||
path: '/api/demoplugin/search', | ||
validate: { | ||
body: schema.object({ | ||
field1: schema.string(), | ||
}), | ||
} | ||
}, | ||
(context, req, res) => { | ||
return res.ok({ | ||
body: { | ||
message: `Received field1: ${req.body.field1}` | ||
} | ||
}); | ||
} | ||
) | ||
} | ||
} | ||
``` | ||
|
||
#### 4. New Platform plugin | ||
As the final step we delete the shim and move all our code into a New Platform | ||
plugin. Since we were already consuming the New Platform API's no code changes | ||
are necessary inside `plugin.ts`. | ||
```ts | ||
// plugins/demoplugin/server/plugin.ts | ||
import { schema } from '@kbn/config-schema'; | ||
|
||
class Plugin { | ||
public setup(core) { | ||
const router = core.http.createRouter(); | ||
router.post( | ||
{ | ||
path: '/api/my-plugin/my-route', | ||
path: '/api/demoplugin/search', | ||
validate: { | ||
body: schema.object({ | ||
field1: schema.string(), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any intention to provide
core.http.route
as a method in the NP, that works like this? If not, then I think showing this as a "shim" is confusing because I interpret "shim" as "massage the old way so that it looks and works like the new way".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words if in NP
core.http
exists, butcore.http.route
isn't going to exist, then this shim breaks your future code in a hard to notice way.If we need an example of how to keep using legacy router in the shim (do we need this?) I would say maybe shimming it as
core.legacyHttp.route
is a clearer way of indicating what's going on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
core.http.route
will never exist.I think the intention is to use types to minimize the legacy API surface that's depended on and make these dependencies explicit. So instead of just say "I depend on
Legacy.Server
" you narrow it down to just the hapi router or just a dependency on another plugin exposed via theserver
object.I agree that this introduces confusion in this case and like your idea of making it more explicit. The shimming instructions were written before most of the API's were available in CoreSetup. Now that the real coreSetup has many useful API's another approach might be to inject three variables into setup:
This gives you the added safety that you're relying on Core's
CoreSetup
so you don't have to wonder if your shimmedMyPluginCoreSetup
is accurately shimming core.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that idea a lot to be honest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is to use the
__legacy
namespace that some plugins have been doing:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like what you have here and would be happy to see it merge.
Is that correct? I opened #44174 about
core.http.route
and @restrry replied "should be done as a part of #44620" so I thought it was coming eventuallyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have broad consensus that registering routes with a configuration object could be a nicer API but I don't think we have concrete designs for how it will be exposed. I imagined that we might still have a
core.http.createRouter(path: string)
method like today, but that the Router instance returned from this would allow you to dorouter.add({...})
orrouter.register({...})
However, this conversation kinda changed my mind on the our approach to shimming plugins. We used to recommend constructing a
CoreSetup
that looks as close as possible to what CoreSetup will look like in the New Platform. This has caused a lot of confusion and speculation which I realise now doesn't add much value.As @jasonrhodes pointed out:
Instead of making it look the same, I think the goal should rather be to have legacy be as different and noticeable as possible, and make the exact legacy API surface explicit by picking only the legacy methods that you depend on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exposing a function on a router instance, vs
core.http
is also what I was expecting. Thanks for the explanation & confirmation. Sorry for the noise.