From 7f7951f78ff9b49db41bdd5ceb641ee738b85c80 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Fri, 14 Feb 2020 12:44:33 +0100 Subject: [PATCH 1/4] start to parametrize --- src/core/server/http/router/error_wrapper.ts | 10 +-- src/core/server/http/router/router.ts | 69 ++++++++++++-------- src/core/server/http/types.ts | 9 ++- src/core/server/legacy/legacy_service.ts | 5 +- src/core/server/plugins/plugin_context.ts | 5 +- 5 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/core/server/http/router/error_wrapper.ts b/src/core/server/http/router/error_wrapper.ts index 8f895753c38c3..c58ca7c3f229b 100644 --- a/src/core/server/http/router/error_wrapper.ts +++ b/src/core/server/http/router/error_wrapper.ts @@ -24,12 +24,12 @@ import { RequestHandler } from './router'; import { RequestHandlerContext } from '../../../server'; import { RouteMethod } from './route'; -export const wrapErrors = ( - handler: RequestHandler -): RequestHandler => { +export const wrapErrors = ( + handler: RequestHandler +): RequestHandler => { return async ( - context: RequestHandlerContext, - request: KibanaRequest, + context: C, + request: KibanaRequest, response: KibanaResponseFactory ) => { try { diff --git a/src/core/server/http/router/router.ts b/src/core/server/http/router/router.ts index bb56ee3727d1a..3a5f6ec61f02e 100644 --- a/src/core/server/http/router/router.ts +++ b/src/core/server/http/router/router.ts @@ -42,9 +42,13 @@ interface RouterRoute { * * @public */ -export type RouteRegistrar = ( +export type RouteRegistrar = < + P, + Q, + B +>( route: RouteConfig, - handler: RequestHandler + handler: RequestHandler ) => void; /** @@ -53,7 +57,7 @@ export type RouteRegistrar = ( * * @public */ -export interface IRouter { +export interface IRouter { /** * Resulted path */ @@ -64,35 +68,35 @@ export interface IRouter { * @param route {@link RouteConfig} - a route configuration. * @param handler {@link RequestHandler} - a function to call to respond to an incoming request */ - get: RouteRegistrar<'get'>; + get: RouteRegistrar<'get', Context>; /** * Register a route handler for `POST` request. * @param route {@link RouteConfig} - a route configuration. * @param handler {@link RequestHandler} - a function to call to respond to an incoming request */ - post: RouteRegistrar<'post'>; + post: RouteRegistrar<'post', Context>; /** * Register a route handler for `PUT` request. * @param route {@link RouteConfig} - a route configuration. * @param handler {@link RequestHandler} - a function to call to respond to an incoming request */ - put: RouteRegistrar<'put'>; + put: RouteRegistrar<'put', Context>; /** * Register a route handler for `PATCH` request. * @param route {@link RouteConfig} - a route configuration. * @param handler {@link RequestHandler} - a function to call to respond to an incoming request */ - patch: RouteRegistrar<'patch'>; + patch: RouteRegistrar<'patch', Context>; /** * Register a route handler for `DELETE` request. * @param route {@link RouteConfig} - a route configuration. * @param handler {@link RequestHandler} - a function to call to respond to an incoming request */ - delete: RouteRegistrar<'delete'>; + delete: RouteRegistrar<'delete', Context>; /** * Wrap a router handler to catch and converts legacy boom errors to proper custom errors. @@ -108,9 +112,15 @@ export interface IRouter { getRoutes: () => RouterRoute[]; } -export type ContextEnhancer = ( - handler: RequestHandler -) => RequestHandlerEnhanced; +export type ContextEnhancer< + P, + Q, + B, + Method extends RouteMethod, + Context extends RequestHandlerContext +> = ( + handler: RequestHandler +) => RequestHandlerEnhanced; function getRouteFullPath(routerPath: string, routePath: string) { // If router's path ends with slash and route's path starts with slash, @@ -193,22 +203,22 @@ function validOptions( /** * @internal */ -export class Router implements IRouter { +export class Router implements IRouter { public routes: Array> = []; - public get: IRouter['get']; - public post: IRouter['post']; - public delete: IRouter['delete']; - public put: IRouter['put']; - public patch: IRouter['patch']; + public get: IRouter['get']; + public post: IRouter['post']; + public delete: IRouter['delete']; + public put: IRouter['put']; + public patch: IRouter['patch']; constructor( public readonly routerPath: string, private readonly log: Logger, - private readonly enhanceWithContext: ContextEnhancer + private readonly enhanceWithContext: ContextEnhancer ) { const buildMethod = (method: Method) => ( route: RouteConfig, - handler: RequestHandler + handler: RequestHandler ) => { const routeSchemas = routeSchemasFromRouteConfig(route, method); @@ -237,7 +247,9 @@ export class Router implements IRouter { return [...this.routes]; } - public handleLegacyErrors(handler: RequestHandler): RequestHandler { + public handleLegacyErrors( + handler: RequestHandler + ): RequestHandler { return wrapErrors(handler); } @@ -249,7 +261,7 @@ export class Router implements IRouter { }: { request: Request; responseToolkit: ResponseToolkit; - handler: RequestHandlerEnhanced; + handler: RequestHandlerEnhanced; routeSchemas?: RouteValidator; }) { let kibanaRequest: KibanaRequest; @@ -274,9 +286,13 @@ type WithoutHeadArgument = T extends (first: any, ...rest: infer Params) => i ? (...rest: Params) => Return : never; -type RequestHandlerEnhanced = WithoutHeadArgument< - RequestHandler ->; +type RequestHandlerEnhanced< + P, + Q, + B, + Method extends RouteMethod, + Context extends RequestHandlerContext +> = WithoutHeadArgument>; /** * A function executed when route path matched requested resource path. @@ -316,9 +332,10 @@ export type RequestHandler< P = unknown, Q = unknown, B = unknown, - Method extends RouteMethod = any + Method extends RouteMethod = any, + Context extends RequestHandlerContext = RequestHandlerContext > = ( - context: RequestHandlerContext, + context: Context, request: KibanaRequest, response: KibanaResponseFactory ) => IKibanaResponse | Promise>; diff --git a/src/core/server/http/types.ts b/src/core/server/http/types.ts index 6327844108055..f7243640a026d 100644 --- a/src/core/server/http/types.ts +++ b/src/core/server/http/types.ts @@ -223,7 +223,9 @@ export interface HttpServiceSetup { * ``` * @public */ - createRouter: () => IRouter; + createRouter: () => IRouter< + Context + >; /** * Register a context provider for a route handler. @@ -264,7 +266,10 @@ export interface InternalHttpServiceSetup extends Omit { auth: HttpServerSetup['auth']; server: HttpServerSetup['server']; - createRouter: (path: string, plugin?: PluginOpaqueId) => IRouter; + createRouter: ( + path: string, + plugin?: PluginOpaqueId + ) => IRouter; getAuthHeaders: GetAuthHeaders; registerRouteHandlerContext: ( pluginOpaqueId: PluginOpaqueId, diff --git a/src/core/server/legacy/legacy_service.ts b/src/core/server/legacy/legacy_service.ts index b2501496d87ef..87a87737ee96f 100644 --- a/src/core/server/legacy/legacy_service.ts +++ b/src/core/server/legacy/legacy_service.ts @@ -40,7 +40,7 @@ import { LegacyVars, } from './types'; import { LegacyInternals } from './legacy_internals'; -import { CoreSetup, CoreStart } from '..'; +import { CoreSetup, CoreStart, RequestHandlerContext } from '..'; interface LegacyKbnServer { applyLoggingConfiguration: (settings: Readonly) => void; @@ -281,7 +281,8 @@ export class LegacyService implements CoreService { null, this.legacyId ), - createRouter: () => setupDeps.core.http.createRouter('', this.legacyId), + createRouter: () => + setupDeps.core.http.createRouter('', this.legacyId), registerOnPreAuth: setupDeps.core.http.registerOnPreAuth, registerAuth: setupDeps.core.http.registerAuth, registerOnPostAuth: setupDeps.core.http.registerOnPostAuth, diff --git a/src/core/server/plugins/plugin_context.ts b/src/core/server/plugins/plugin_context.ts index a7b555a9eba01..e11b5bc93ba0f 100644 --- a/src/core/server/plugins/plugin_context.ts +++ b/src/core/server/plugins/plugin_context.ts @@ -35,7 +35,7 @@ import { config as elasticsearchConfig, } from '../elasticsearch/elasticsearch_config'; import { pick, deepFreeze } from '../../utils'; -import { CoreSetup, CoreStart } from '..'; +import { CoreSetup, CoreStart, RequestHandlerContext } from '..'; /** * This returns a facade for `CoreContext` that will be exposed to the plugin initializer. @@ -155,7 +155,8 @@ export function createPluginSetupContext( null, plugin.opaqueId ), - createRouter: () => deps.http.createRouter('', plugin.opaqueId), + createRouter: () => + deps.http.createRouter('', plugin.opaqueId), registerOnPreAuth: deps.http.registerOnPreAuth, registerAuth: deps.http.registerAuth, registerOnPostAuth: deps.http.registerOnPostAuth, From e955f5debd6010e540ce2e1871d3fd844c956ad9 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Fri, 14 Feb 2020 13:08:12 +0100 Subject: [PATCH 2/4] finish generification --- src/core/server/http/http_service.ts | 7 +++++-- src/core/server/http/router/router.ts | 9 ++++----- src/core/server/http/types.ts | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/core/server/http/http_service.ts b/src/core/server/http/http_service.ts index ae9d53f9fd3db..89ea576823f5b 100644 --- a/src/core/server/http/http_service.ts +++ b/src/core/server/http/http_service.ts @@ -100,9 +100,12 @@ export class HttpService implements CoreService { + createRouter: ( + path: string, + pluginId: PluginOpaqueId = this.coreContext.coreId + ) => { const enhanceHandler = this.requestHandlerContext!.createHandler.bind(null, pluginId); - const router = new Router(path, this.log, enhanceHandler); + const router = new Router(path, this.log, enhanceHandler); registerRouter(router); return router; }, diff --git a/src/core/server/http/router/router.ts b/src/core/server/http/router/router.ts index 3a5f6ec61f02e..9b968a3584cbc 100644 --- a/src/core/server/http/router/router.ts +++ b/src/core/server/http/router/router.ts @@ -42,11 +42,10 @@ interface RouterRoute { * * @public */ -export type RouteRegistrar = < - P, - Q, - B ->( +export type RouteRegistrar< + Method extends RouteMethod, + Context extends RequestHandlerContext = RequestHandlerContext +> = ( route: RouteConfig, handler: RequestHandler ) => void; diff --git a/src/core/server/http/types.ts b/src/core/server/http/types.ts index f7243640a026d..95164dac2e926 100644 --- a/src/core/server/http/types.ts +++ b/src/core/server/http/types.ts @@ -266,7 +266,7 @@ export interface InternalHttpServiceSetup extends Omit { auth: HttpServerSetup['auth']; server: HttpServerSetup['server']; - createRouter: ( + createRouter: ( path: string, plugin?: PluginOpaqueId ) => IRouter; From 6cb8485beccb1f035e18e02a193ef811863ab364 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Fri, 14 Feb 2020 13:09:25 +0100 Subject: [PATCH 3/4] update generated doc --- ...gin-server.httpservicesetup.createrouter.md | 2 +- .../kibana-plugin-server.httpservicesetup.md | 2 +- .../kibana-plugin-server.irouter.delete.md | 2 +- .../server/kibana-plugin-server.irouter.get.md | 2 +- .../server/kibana-plugin-server.irouter.md | 12 ++++++------ .../kibana-plugin-server.irouter.patch.md | 2 +- .../kibana-plugin-server.irouter.post.md | 2 +- .../server/kibana-plugin-server.irouter.put.md | 2 +- .../kibana-plugin-server.requesthandler.md | 2 +- .../kibana-plugin-server.routeregistrar.md | 2 +- src/core/server/server.api.md | 18 +++++++++--------- 11 files changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/development/core/server/kibana-plugin-server.httpservicesetup.createrouter.md b/docs/development/core/server/kibana-plugin-server.httpservicesetup.createrouter.md index ea0850fa01c53..fd85f5486e483 100644 --- a/docs/development/core/server/kibana-plugin-server.httpservicesetup.createrouter.md +++ b/docs/development/core/server/kibana-plugin-server.httpservicesetup.createrouter.md @@ -9,7 +9,7 @@ Provides ability to declare a handler function for a particular path and HTTP re Signature: ```typescript -createRouter: () => IRouter; +createRouter: () => IRouter; ``` ## Remarks diff --git a/docs/development/core/server/kibana-plugin-server.httpservicesetup.md b/docs/development/core/server/kibana-plugin-server.httpservicesetup.md index c2d53ec1eaf52..25b3c922dcfba 100644 --- a/docs/development/core/server/kibana-plugin-server.httpservicesetup.md +++ b/docs/development/core/server/kibana-plugin-server.httpservicesetup.md @@ -84,7 +84,7 @@ async (context, request, response) => { | [auth](./kibana-plugin-server.httpservicesetup.auth.md) | {
get: GetAuthState;
isAuthenticated: IsAuthenticated;
} | | | [basePath](./kibana-plugin-server.httpservicesetup.basepath.md) | IBasePath | Access or manipulate the Kibana base path See [IBasePath](./kibana-plugin-server.ibasepath.md). | | [createCookieSessionStorageFactory](./kibana-plugin-server.httpservicesetup.createcookiesessionstoragefactory.md) | <T>(cookieOptions: SessionStorageCookieOptions<T>) => Promise<SessionStorageFactory<T>> | Creates cookie based session storage factory [SessionStorageFactory](./kibana-plugin-server.sessionstoragefactory.md) | -| [createRouter](./kibana-plugin-server.httpservicesetup.createrouter.md) | () => IRouter | Provides ability to declare a handler function for a particular path and HTTP request method. | +| [createRouter](./kibana-plugin-server.httpservicesetup.createrouter.md) | <Context extends RequestHandlerContext = RequestHandlerContext>() => IRouter<Context> | Provides ability to declare a handler function for a particular path and HTTP request method. | | [csp](./kibana-plugin-server.httpservicesetup.csp.md) | ICspConfig | The CSP config used for Kibana. | | [getServerInfo](./kibana-plugin-server.httpservicesetup.getserverinfo.md) | () => HttpServerInfo | Provides common [information](./kibana-plugin-server.httpserverinfo.md) about the running http server. | | [isTlsEnabled](./kibana-plugin-server.httpservicesetup.istlsenabled.md) | boolean | Flag showing whether a server was configured to use TLS connection. | diff --git a/docs/development/core/server/kibana-plugin-server.irouter.delete.md b/docs/development/core/server/kibana-plugin-server.irouter.delete.md index 0b87dbcda3316..b82169deba610 100644 --- a/docs/development/core/server/kibana-plugin-server.irouter.delete.md +++ b/docs/development/core/server/kibana-plugin-server.irouter.delete.md @@ -9,5 +9,5 @@ Register a route handler for `DELETE` request. Signature: ```typescript -delete: RouteRegistrar<'delete'>; +delete: RouteRegistrar<'delete', Context>; ``` diff --git a/docs/development/core/server/kibana-plugin-server.irouter.get.md b/docs/development/core/server/kibana-plugin-server.irouter.get.md index e6f80378e007d..126a9bb634dd2 100644 --- a/docs/development/core/server/kibana-plugin-server.irouter.get.md +++ b/docs/development/core/server/kibana-plugin-server.irouter.get.md @@ -9,5 +9,5 @@ Register a route handler for `GET` request. Signature: ```typescript -get: RouteRegistrar<'get'>; +get: RouteRegistrar<'get', Context>; ``` diff --git a/docs/development/core/server/kibana-plugin-server.irouter.md b/docs/development/core/server/kibana-plugin-server.irouter.md index 3d82cd8245141..b6fea8b134a6c 100644 --- a/docs/development/core/server/kibana-plugin-server.irouter.md +++ b/docs/development/core/server/kibana-plugin-server.irouter.md @@ -9,18 +9,18 @@ Registers route handlers for specified resource path and method. See [RouteConfi Signature: ```typescript -export interface IRouter +export interface IRouter ``` ## Properties | Property | Type | Description | | --- | --- | --- | -| [delete](./kibana-plugin-server.irouter.delete.md) | RouteRegistrar<'delete'> | Register a route handler for DELETE request. | -| [get](./kibana-plugin-server.irouter.get.md) | RouteRegistrar<'get'> | Register a route handler for GET request. | +| [delete](./kibana-plugin-server.irouter.delete.md) | RouteRegistrar<'delete', Context> | Register a route handler for DELETE request. | +| [get](./kibana-plugin-server.irouter.get.md) | RouteRegistrar<'get', Context> | Register a route handler for GET request. | | [handleLegacyErrors](./kibana-plugin-server.irouter.handlelegacyerrors.md) | <P, Q, B>(handler: RequestHandler<P, Q, B>) => RequestHandler<P, Q, B> | Wrap a router handler to catch and converts legacy boom errors to proper custom errors. | -| [patch](./kibana-plugin-server.irouter.patch.md) | RouteRegistrar<'patch'> | Register a route handler for PATCH request. | -| [post](./kibana-plugin-server.irouter.post.md) | RouteRegistrar<'post'> | Register a route handler for POST request. | -| [put](./kibana-plugin-server.irouter.put.md) | RouteRegistrar<'put'> | Register a route handler for PUT request. | +| [patch](./kibana-plugin-server.irouter.patch.md) | RouteRegistrar<'patch', Context> | Register a route handler for PATCH request. | +| [post](./kibana-plugin-server.irouter.post.md) | RouteRegistrar<'post', Context> | Register a route handler for POST request. | +| [put](./kibana-plugin-server.irouter.put.md) | RouteRegistrar<'put', Context> | Register a route handler for PUT request. | | [routerPath](./kibana-plugin-server.irouter.routerpath.md) | string | Resulted path | diff --git a/docs/development/core/server/kibana-plugin-server.irouter.patch.md b/docs/development/core/server/kibana-plugin-server.irouter.patch.md index ef7ea6f18d646..0fe1e19e1cd7f 100644 --- a/docs/development/core/server/kibana-plugin-server.irouter.patch.md +++ b/docs/development/core/server/kibana-plugin-server.irouter.patch.md @@ -9,5 +9,5 @@ Register a route handler for `PATCH` request. Signature: ```typescript -patch: RouteRegistrar<'patch'>; +patch: RouteRegistrar<'patch', Context>; ``` diff --git a/docs/development/core/server/kibana-plugin-server.irouter.post.md b/docs/development/core/server/kibana-plugin-server.irouter.post.md index 6e4a858cd342c..3e6b793cb8cc1 100644 --- a/docs/development/core/server/kibana-plugin-server.irouter.post.md +++ b/docs/development/core/server/kibana-plugin-server.irouter.post.md @@ -9,5 +9,5 @@ Register a route handler for `POST` request. Signature: ```typescript -post: RouteRegistrar<'post'>; +post: RouteRegistrar<'post', Context>; ``` diff --git a/docs/development/core/server/kibana-plugin-server.irouter.put.md b/docs/development/core/server/kibana-plugin-server.irouter.put.md index be6c235250fdd..528c441ec8594 100644 --- a/docs/development/core/server/kibana-plugin-server.irouter.put.md +++ b/docs/development/core/server/kibana-plugin-server.irouter.put.md @@ -9,5 +9,5 @@ Register a route handler for `PUT` request. Signature: ```typescript -put: RouteRegistrar<'put'>; +put: RouteRegistrar<'put', Context>; ``` diff --git a/docs/development/core/server/kibana-plugin-server.requesthandler.md b/docs/development/core/server/kibana-plugin-server.requesthandler.md index d9b6fc4a008e5..796908871b422 100644 --- a/docs/development/core/server/kibana-plugin-server.requesthandler.md +++ b/docs/development/core/server/kibana-plugin-server.requesthandler.md @@ -9,7 +9,7 @@ A function executed when route path matched requested resource path. Request han Signature: ```typescript -export declare type RequestHandler

= (context: RequestHandlerContext, request: KibanaRequest, response: KibanaResponseFactory) => IKibanaResponse | Promise>; +export declare type RequestHandler

= (context: Context, request: KibanaRequest, response: KibanaResponseFactory) => IKibanaResponse | Promise>; ``` ## Example diff --git a/docs/development/core/server/kibana-plugin-server.routeregistrar.md b/docs/development/core/server/kibana-plugin-server.routeregistrar.md index b886305731c3c..306c011a842c9 100644 --- a/docs/development/core/server/kibana-plugin-server.routeregistrar.md +++ b/docs/development/core/server/kibana-plugin-server.routeregistrar.md @@ -9,5 +9,5 @@ Route handler common definition Signature: ```typescript -export declare type RouteRegistrar = (route: RouteConfig, handler: RequestHandler) => void; +export declare type RouteRegistrar = (route: RouteConfig, handler: RequestHandler) => void; ``` diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index 96a93033c7408..439d68bc8160e 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -753,7 +753,7 @@ export interface HttpServiceSetup { }; basePath: IBasePath; createCookieSessionStorageFactory: (cookieOptions: SessionStorageCookieOptions) => Promise>; - createRouter: () => IRouter; + createRouter: () => IRouter; csp: ICspConfig; getServerInfo: () => HttpServerInfo; isTlsEnabled: boolean; @@ -843,17 +843,17 @@ export interface IRenderOptions { } // @public -export interface IRouter { - delete: RouteRegistrar<'delete'>; - get: RouteRegistrar<'get'>; +export interface IRouter { + delete: RouteRegistrar<'delete', Context>; + get: RouteRegistrar<'get', Context>; // Warning: (ae-forgotten-export) The symbol "RouterRoute" needs to be exported by the entry point index.d.ts // // @internal getRoutes: () => RouterRoute[]; handleLegacyErrors: (handler: RequestHandler) => RequestHandler; - patch: RouteRegistrar<'patch'>; - post: RouteRegistrar<'post'>; - put: RouteRegistrar<'put'>; + patch: RouteRegistrar<'patch', Context>; + post: RouteRegistrar<'post', Context>; + put: RouteRegistrar<'put', Context>; routerPath: string; } @@ -1274,7 +1274,7 @@ export interface RenderingServiceSetup { } // @public -export type RequestHandler

= (context: RequestHandlerContext, request: KibanaRequest, response: KibanaResponseFactory) => IKibanaResponse | Promise>; +export type RequestHandler

= (context: Context, request: KibanaRequest, response: KibanaResponseFactory) => IKibanaResponse | Promise>; // @public export interface RequestHandlerContext { @@ -1345,7 +1345,7 @@ export type RouteContentType = 'application/json' | 'application/*+json' | 'appl export type RouteMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options'; // @public -export type RouteRegistrar = (route: RouteConfig, handler: RequestHandler) => void; +export type RouteRegistrar = (route: RouteConfig, handler: RequestHandler) => void; // @public export class RouteValidationError extends SchemaTypeError { From 3c67ddcb78144554320c75333a18963b19d25dfc Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Tue, 25 Feb 2020 08:03:35 +0100 Subject: [PATCH 4/4] update registerRouteHandlerContext signature --- src/core/server/http/http_service.mock.ts | 2 +- src/core/server/http/http_service.ts | 12 ++++++---- src/core/server/http/types.ts | 23 ++++++++++++------- src/core/server/legacy/legacy_service.ts | 19 +++++++++++---- src/core/server/mocks.ts | 2 +- src/core/server/plugins/plugin_context.ts | 13 +++++++---- src/core/server/server.ts | 2 +- .../data/server/search/search_service.ts | 2 +- x-pack/plugins/actions/server/plugin.test.ts | 4 ++-- 9 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/core/server/http/http_service.mock.ts b/src/core/server/http/http_service.mock.ts index 30032ff5da796..139e8d1ec2eea 100644 --- a/src/core/server/http/http_service.mock.ts +++ b/src/core/server/http/http_service.mock.ts @@ -69,7 +69,7 @@ const createSetupContractMock = () => { registerOnPreAuth: jest.fn(), registerAuth: jest.fn(), registerOnPostAuth: jest.fn(), - registerRouteHandlerContext: jest.fn(), + registerRouteHandlerContext: jest.fn() as any, registerOnPreResponse: jest.fn(), createRouter: jest.fn().mockImplementation(() => mockRouter.create({})), basePath: createBasePathMock(), diff --git a/src/core/server/http/http_service.ts b/src/core/server/http/http_service.ts index 89ea576823f5b..fffaec3f5b600 100644 --- a/src/core/server/http/http_service.ts +++ b/src/core/server/http/http_service.ts @@ -110,11 +110,15 @@ export class HttpService implements CoreService( + registerRouteHandlerContext: < + TContextType extends RequestHandlerContext = RequestHandlerContext, + TContextName extends keyof TContextType = 'core' + >( pluginOpaqueId: PluginOpaqueId, - contextName: T, - provider: RequestHandlerContextProvider - ) => this.requestHandlerContext!.registerContext(pluginOpaqueId, contextName, provider), + contextName: TContextName, + provider: RequestHandlerContextProvider + ) => + this.requestHandlerContext!.registerContext(pluginOpaqueId, contextName as any, provider), }; return contract; diff --git a/src/core/server/http/types.ts b/src/core/server/http/types.ts index 95164dac2e926..cbbb74068ef97 100644 --- a/src/core/server/http/types.ts +++ b/src/core/server/http/types.ts @@ -44,8 +44,9 @@ export type RequestHandlerContextContainer = IContextContainer = IContextProvider, TContextName>; + TContextName extends keyof TContextType, + TContextType extends RequestHandlerContext = RequestHandlerContext +> = IContextProvider, TContextName>; /** * Kibana HTTP Service provides own abstraction for work with HTTP stack. @@ -250,9 +251,12 @@ export interface HttpServiceSetup { * ``` * @public */ - registerRouteHandlerContext: ( - contextName: T, - provider: RequestHandlerContextProvider + registerRouteHandlerContext: < + TContextType extends RequestHandlerContext = RequestHandlerContext, + TContextName extends keyof TContextType = 'core' + >( + contextName: TContextName, + provider: RequestHandlerContextProvider ) => RequestHandlerContextContainer; /** @@ -271,10 +275,13 @@ export interface InternalHttpServiceSetup plugin?: PluginOpaqueId ) => IRouter; getAuthHeaders: GetAuthHeaders; - registerRouteHandlerContext: ( + registerRouteHandlerContext: < + TContextType extends RequestHandlerContext = RequestHandlerContext, + TContextName extends keyof TContextType = 'core' + >( pluginOpaqueId: PluginOpaqueId, - contextName: T, - provider: RequestHandlerContextProvider + contextName: TContextName, + provider: RequestHandlerContextProvider ) => RequestHandlerContextContainer; } diff --git a/src/core/server/legacy/legacy_service.ts b/src/core/server/legacy/legacy_service.ts index 87a87737ee96f..3ba7b6e5177f2 100644 --- a/src/core/server/legacy/legacy_service.ts +++ b/src/core/server/legacy/legacy_service.ts @@ -25,7 +25,13 @@ import { Config, ConfigDeprecationProvider } from '../config'; import { CoreContext } from '../core_context'; import { CspConfigType, config as cspConfig } from '../csp'; import { DevConfig, DevConfigType, config as devConfig } from '../dev'; -import { BasePathProxyServer, HttpConfig, HttpConfigType, config as httpConfig } from '../http'; +import { + BasePathProxyServer, + HttpConfig, + HttpConfigType, + config as httpConfig, + RequestHandlerContextProvider, +} from '../http'; import { Logger } from '../logging'; import { PathConfigType } from '../path'; import { findLegacyPluginSpecs } from './plugins'; @@ -277,10 +283,13 @@ export class LegacyService implements CoreService { }, http: { createCookieSessionStorageFactory: setupDeps.core.http.createCookieSessionStorageFactory, - registerRouteHandlerContext: setupDeps.core.http.registerRouteHandlerContext.bind( - null, - this.legacyId - ), + registerRouteHandlerContext: < + TContextType extends RequestHandlerContext = RequestHandlerContext, + TContextName extends keyof TContextType = 'core' + >( + contextName: TContextName, + provider: RequestHandlerContextProvider + ) => setupDeps.core.http.registerRouteHandlerContext(this.legacyId, contextName, provider), createRouter: () => setupDeps.core.http.createRouter('', this.legacyId), registerOnPreAuth: setupDeps.core.http.registerOnPreAuth, diff --git a/src/core/server/mocks.ts b/src/core/server/mocks.ts index d6554babab53e..61c66154c7f1a 100644 --- a/src/core/server/mocks.ts +++ b/src/core/server/mocks.ts @@ -101,7 +101,7 @@ function createCoreSetupMock() { csp: CspConfig.DEFAULT, isTlsEnabled: httpService.isTlsEnabled, createRouter: jest.fn(), - registerRouteHandlerContext: jest.fn(), + registerRouteHandlerContext: jest.fn() as any, auth: { get: httpService.auth.get, isAuthenticated: httpService.auth.isAuthenticated, diff --git a/src/core/server/plugins/plugin_context.ts b/src/core/server/plugins/plugin_context.ts index e11b5bc93ba0f..86dc13bc2a055 100644 --- a/src/core/server/plugins/plugin_context.ts +++ b/src/core/server/plugins/plugin_context.ts @@ -35,7 +35,7 @@ import { config as elasticsearchConfig, } from '../elasticsearch/elasticsearch_config'; import { pick, deepFreeze } from '../../utils'; -import { CoreSetup, CoreStart, RequestHandlerContext } from '..'; +import { CoreSetup, CoreStart, RequestHandlerContext, RequestHandlerContextProvider } from '..'; /** * This returns a facade for `CoreContext` that will be exposed to the plugin initializer. @@ -151,10 +151,13 @@ export function createPluginSetupContext( }, http: { createCookieSessionStorageFactory: deps.http.createCookieSessionStorageFactory, - registerRouteHandlerContext: deps.http.registerRouteHandlerContext.bind( - null, - plugin.opaqueId - ), + registerRouteHandlerContext: < + TContextType extends RequestHandlerContext = RequestHandlerContext, + TContextName extends keyof TContextType = 'core' + >( + contextName: TContextName, + provider: RequestHandlerContextProvider + ) => deps.http.registerRouteHandlerContext(plugin.opaqueId, contextName, provider), createRouter: () => deps.http.createRouter('', plugin.opaqueId), registerOnPreAuth: deps.http.registerOnPreAuth, diff --git a/src/core/server/server.ts b/src/core/server/server.ts index db2493b38d6e0..9d24c459944ca 100644 --- a/src/core/server/server.ts +++ b/src/core/server/server.ts @@ -217,7 +217,7 @@ export class Server { } private registerCoreContext(coreSetup: InternalCoreSetup, rendering: RenderingServiceSetup) { - coreSetup.http.registerRouteHandlerContext( + coreSetup.http.registerRouteHandlerContext( coreId, 'core', async (context, req, res): Promise => { diff --git a/src/plugins/data/server/search/search_service.ts b/src/plugins/data/server/search/search_service.ts index 8ca314ad7bfd8..3400423b51dda 100644 --- a/src/plugins/data/server/search/search_service.ts +++ b/src/plugins/data/server/search/search_service.ts @@ -53,7 +53,7 @@ export class SearchService implements Plugin { this.contextContainer = core.context.createContextContainer(); - core.http.registerRouteHandlerContext<'search'>('search', context => { + core.http.registerRouteHandlerContext('search', context => { return createApi({ caller: context.core!.elasticsearch.dataClient.callAsCurrentUser, searchStrategies: this.searchStrategies, diff --git a/x-pack/plugins/actions/server/plugin.test.ts b/x-pack/plugins/actions/server/plugin.test.ts index 91944dfa8f3ad..2eb1c5e82ea0e 100644 --- a/x-pack/plugins/actions/server/plugin.test.ts +++ b/x-pack/plugins/actions/server/plugin.test.ts @@ -53,7 +53,7 @@ describe('Actions Plugin', () => { const handler = coreSetup.http.registerRouteHandlerContext.mock.calls[0]; expect(handler[0]).toEqual('actions'); - const actionsContextHandler = (await handler[1]( + const actionsContextHandler = (await (handler[1] as Function)( { core: { savedObjects: { @@ -74,7 +74,7 @@ describe('Actions Plugin', () => { const handler = coreSetup.http.registerRouteHandlerContext.mock.calls[0]; expect(handler[0]).toEqual('actions'); - const actionsContextHandler = (await handler[1]( + const actionsContextHandler = (await (handler[1] as Function)( { core: { savedObjects: {