-
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
Conversation
Pinging @elastic/kibana-platform (Team:Platform) |
src/core/MIGRATION.md
Outdated
elasticsearch: ElasticsearchPlugin // note: Elasticsearch is in Core in NP, rather than a plugin | ||
http: { | ||
route: Legacy.Server['route'] // note: NP uses `http.createRouter()` |
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, but core.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.
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.
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 the server
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:
import { CoreSetup } from 'src/core/server';
export interface LegacySetup {
server: {
route: Legacy.Server['route']
}
}
export class Plugin {
public setup(core: CoreSetup, plugins: {}, legacy: LegacySetup) {
legacy.route({...})
}
This gives you the added safety that you're relying on Core's CoreSetup
so you don't have to wonder if your shimmed MyPluginCoreSetup
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:
import { CoreSetup } from 'src/core/server';
export interface DemoPluginCoreSetup extends CoreSetup {
__legacy: {
server: {
route: Legacy.Server['route'];
};
};
}
export class Plugin {
public setup(core: CoreSetup, plugins: {}) {
core.__legacy.server.route({...})
}
}
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.
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.
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 do router.add({...})
or router.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:
then this shim breaks your future code in a hard to notice way.
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.
- [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 comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the TOC
> property instead of introducing a third parameter to your plugins lifecycle | ||
> function. The important thing is that you reduce the legacy API surface that | ||
> you depend on to a minimum by only picking and injecting the methods you | ||
> require and that you clearly differentiate legacy dependencies in a namespace. |
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.
👍
Summary
Update http route registration in migration guide and migration examples. Make it clearer that a shim exposing
server.route
isn't NP compatible, but can be a helpful way to break down the migration into steps.[skip-ci]
Checklist
Use
strikethroughsto remove checklist items you don't feel are applicable to this PR.For maintainers