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

Add type argument for RouteHandlerContext #57588

Closed
joshdover opened this issue Feb 13, 2020 · 2 comments · Fixed by #93579
Closed

Add type argument for RouteHandlerContext #57588

joshdover opened this issue Feb 13, 2020 · 2 comments · Fixed by #93579
Assignees
Labels
Feature:New Platform Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc

Comments

@joshdover
Copy link
Contributor

We currently only support "extending" the RouteHandlerContext by reopening the type:

declare module 'src/core/server' {
  interface RouteHandlerContext {
    myField: MyType;
  }
}

This has a few problems:

  • Depending on how a TS project is set up, this type extension can be present for plugins that it was not intended to be included on.
  • It's not very explicit.
  • It's not very obvious how or why it's done this way.

Instead, we could add a type argument to IRouter (and related types, like RouteHandler) for the RouteHandlerContext so plugins can explicitly specify which contexts they expect from their own context providers + any specified by their dependencies. Example:

import { MyDependencyContext } from '../../my_dependency/server';
import { RouteHandlerContext } from 'src/core/server';

interface MyRouteHandlerContext extends RouteHandlerContext {
  myDependency: MyDependencyContext;
  myContext: MyContext;
}

class Plugin {
  setup(core) {
    const router = core.http.createRouter<MyRouteHandlerContext>();
    router.get(
      { path: '/my-route', validate: false }
      async (context, req, res) => {
        context.myDependency.doThing(); // TS is happy!
        // ...
      }
    );
  }
}
@joshdover joshdover added Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Feature:New Platform labels Feb 13, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

@mshustov
Copy link
Contributor

The problem was addressed in #88718 Although, Core internal cleanup is required.
#88718 (comment)
#88718 (comment)
#88718 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:New Platform Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc
Projects
None yet
4 participants