[7.x] Simplify generics for IContextContainer (#46538) (#47266)

This commit is contained in:
Josh Dover 2019-10-03 16:53:30 -05:00 committed by GitHub
parent 424440e0fc
commit 003c3b855e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 323 additions and 279 deletions

View file

@ -9,7 +9,7 @@ Register a context provider for application mounting. Will only be available to
<b>Signature:</b>
```typescript
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<AppMountContext, T>): void;
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<App['mount'], T>): void;
```
## Parameters
@ -17,7 +17,7 @@ registerMountContext<T extends keyof AppMountContext>(contextName: T, provider:
| Parameter | Type | Description |
| --- | --- | --- |
| contextName | <code>T</code> | The key of [AppMountContext](./kibana-plugin-public.appmountcontext.md) this provider's return value should be attached to. |
| provider | <code>IContextProvider&lt;AppMountContext, T&gt;</code> | A [IContextProvider](./kibana-plugin-public.icontextprovider.md) function |
| provider | <code>IContextProvider&lt;App['mount'], T&gt;</code> | A [IContextProvider](./kibana-plugin-public.icontextprovider.md) function |
<b>Returns:</b>

View file

@ -9,7 +9,7 @@ Register a context provider for application mounting. Will only be available to
<b>Signature:</b>
```typescript
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<AppMountContext, T>): void;
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<App['mount'], T>): void;
```
## Parameters
@ -17,7 +17,7 @@ registerMountContext<T extends keyof AppMountContext>(contextName: T, provider:
| Parameter | Type | Description |
| --- | --- | --- |
| contextName | <code>T</code> | The key of [AppMountContext](./kibana-plugin-public.appmountcontext.md) this provider's return value should be attached to. |
| provider | <code>IContextProvider&lt;AppMountContext, T&gt;</code> | A [IContextProvider](./kibana-plugin-public.icontextprovider.md) function |
| provider | <code>IContextProvider&lt;App['mount'], T&gt;</code> | A [IContextProvider](./kibana-plugin-public.icontextprovider.md) function |
<b>Returns:</b>

View file

@ -9,9 +9,9 @@ Creates a new [IContextContainer](./kibana-plugin-public.icontextcontainer.md) f
<b>Signature:</b>
```typescript
createContextContainer<TContext extends {}, THandlerReturn, THandlerParmaters extends any[] = []>(): IContextContainer<TContext, THandlerReturn, THandlerParmaters>;
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
```
<b>Returns:</b>
`IContextContainer<TContext, THandlerReturn, THandlerParmaters>`
`IContextContainer<THandler>`

View file

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [HandlerContextType](./kibana-plugin-public.handlercontexttype.md)
## HandlerContextType type
Extracts the type of the first argument of a [HandlerFunction](./kibana-plugin-public.handlerfunction.md) to represent the type of the context.
<b>Signature:</b>
```typescript
export declare type HandlerContextType<T extends HandlerFunction<any>> = T extends HandlerFunction<infer U> ? U : never;
```

View file

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [HandlerFunction](./kibana-plugin-public.handlerfunction.md)
## HandlerFunction type
A function that accepts a context object and an optional number of additional arguments. Used for the generic types in [IContextContainer](./kibana-plugin-public.icontextcontainer.md)
<b>Signature:</b>
```typescript
export declare type HandlerFunction<T extends object> = (context: T, ...args: any[]) => any;
```

View file

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [HandlerParameters](./kibana-plugin-public.handlerparameters.md)
## HandlerParameters type
Extracts the types of the additional arguments of a [HandlerFunction](./kibana-plugin-public.handlerfunction.md)<!-- -->, excluding the [HandlerContextType](./kibana-plugin-public.handlercontexttype.md)<!-- -->.
<b>Signature:</b>
```typescript
export declare type HandlerParameters<T extends HandlerFunction<any>> = T extends (context: any, ...args: infer U) => any ? U : never;
```

View file

@ -9,7 +9,7 @@ Create a new handler function pre-wired to context for the plugin.
<b>Signature:</b>
```typescript
createHandler(pluginOpaqueId: PluginOpaqueId, handler: IContextHandler<TContext, THandlerReturn, THandlerParameters>): (...rest: THandlerParameters) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>;
createHandler(pluginOpaqueId: PluginOpaqueId, handler: THandler): (...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
```
## Parameters
@ -17,11 +17,11 @@ createHandler(pluginOpaqueId: PluginOpaqueId, handler: IContextHandler<TContext,
| Parameter | Type | Description |
| --- | --- | --- |
| pluginOpaqueId | <code>PluginOpaqueId</code> | The plugin opaque ID for the plugin that registers this handler. |
| handler | <code>IContextHandler&lt;TContext, THandlerReturn, THandlerParameters&gt;</code> | Handler function to pass context object to. |
| handler | <code>THandler</code> | Handler function to pass context object to. |
<b>Returns:</b>
`(...rest: THandlerParameters) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>`
`(...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>`
A function that takes `THandlerParameters`<!-- -->, calls `handler` with a new context, and returns a Promise of the `handler` return value.

View file

@ -9,7 +9,7 @@ An object that handles registration of context providers and configuring handler
<b>Signature:</b>
```typescript
export interface IContextContainer<TContext extends {}, THandlerReturn, THandlerParameters extends any[] = []>
export interface IContextContainer<THandler extends HandlerFunction<any>>
```
## Methods

View file

@ -9,7 +9,7 @@ Register a new context provider.
<b>Signature:</b>
```typescript
registerContext<TContextName extends keyof TContext>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<TContext, TContextName, THandlerParameters>): this;
registerContext<TContextName extends keyof HandlerContextType<THandler>>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<THandler, TContextName>): this;
```
## Parameters
@ -18,7 +18,7 @@ registerContext<TContextName extends keyof TContext>(pluginOpaqueId: PluginOpaqu
| --- | --- | --- |
| pluginOpaqueId | <code>PluginOpaqueId</code> | The plugin opaque ID for the plugin that registers this context. |
| contextName | <code>TContextName</code> | The key of the <code>TContext</code> object this provider supplies the value for. |
| provider | <code>IContextProvider&lt;TContext, TContextName, THandlerParameters&gt;</code> | A [IContextProvider](./kibana-plugin-public.icontextprovider.md) to be called each time a new context is created. |
| provider | <code>IContextProvider&lt;THandler, TContextName&gt;</code> | A [IContextProvider](./kibana-plugin-public.icontextprovider.md) to be called each time a new context is created. |
<b>Returns:</b>

View file

@ -1,18 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [IContextHandler](./kibana-plugin-public.icontexthandler.md)
## IContextHandler type
A function registered by a plugin to perform some action.
<b>Signature:</b>
```typescript
export declare type IContextHandler<TContext extends {}, TReturn, THandlerParameters extends any[] = []> = (context: TContext, ...rest: THandlerParameters) => TReturn;
```
## Remarks
A new `TContext` will be built for each handler before invoking.

View file

@ -9,7 +9,7 @@ A function that returns a context value for a specific key of given context type
<b>Signature:</b>
```typescript
export declare type IContextProvider<TContext extends Record<string, any>, TContextName extends keyof TContext, TProviderParameters extends any[] = []> = (context: Partial<TContext>, ...rest: TProviderParameters) => Promise<TContext[TContextName]> | TContext[TContextName];
export declare type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: Partial<HandlerContextType<THandler>>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
```
## Remarks

View file

@ -91,11 +91,13 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [AppUnmount](./kibana-plugin-public.appunmount.md) | A function called when an application should be unmounted from the page. This function should be synchronous. |
| [ChromeHelpExtension](./kibana-plugin-public.chromehelpextension.md) | |
| [ChromeNavLinkUpdateableFields](./kibana-plugin-public.chromenavlinkupdateablefields.md) | |
| [HandlerContextType](./kibana-plugin-public.handlercontexttype.md) | Extracts the type of the first argument of a [HandlerFunction](./kibana-plugin-public.handlerfunction.md) to represent the type of the context. |
| [HandlerFunction](./kibana-plugin-public.handlerfunction.md) | A function that accepts a context object and an optional number of additional arguments. Used for the generic types in [IContextContainer](./kibana-plugin-public.icontextcontainer.md) |
| [HandlerParameters](./kibana-plugin-public.handlerparameters.md) | Extracts the types of the additional arguments of a [HandlerFunction](./kibana-plugin-public.handlerfunction.md)<!-- -->, excluding the [HandlerContextType](./kibana-plugin-public.handlercontexttype.md)<!-- -->. |
| [HttpBody](./kibana-plugin-public.httpbody.md) | |
| [HttpHandler](./kibana-plugin-public.httphandler.md) | |
| [HttpSetup](./kibana-plugin-public.httpsetup.md) | |
| [HttpStart](./kibana-plugin-public.httpstart.md) | |
| [IContextHandler](./kibana-plugin-public.icontexthandler.md) | A function registered by a plugin to perform some action. |
| [IContextProvider](./kibana-plugin-public.icontextprovider.md) | A function that returns a context value for a specific key of given context type. |
| [OverlayBannerMount](./kibana-plugin-public.overlaybannermount.md) | A function that will mount the banner inside the provided element. |
| [OverlayBannerUnmount](./kibana-plugin-public.overlaybannerunmount.md) | A function that will unmount the banner from the element. |

View file

@ -9,5 +9,5 @@ returns `basePath` value, specific for an incoming request.
<b>Signature:</b>
```typescript
get: (request: KibanaRequest<unknown, unknown, unknown> | LegacyRequest) => string;
get: (request: LegacyRequest | KibanaRequest<unknown, unknown, unknown>) => string;
```

View file

@ -16,11 +16,11 @@ export declare class BasePath
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [get](./kibana-plugin-server.basepath.get.md) | | <code>(request: KibanaRequest&lt;unknown, unknown, unknown&gt; &#124; LegacyRequest) =&gt; string</code> | returns <code>basePath</code> value, specific for an incoming request. |
| [get](./kibana-plugin-server.basepath.get.md) | | <code>(request: LegacyRequest &#124; KibanaRequest&lt;unknown, unknown, unknown&gt;) =&gt; string</code> | returns <code>basePath</code> value, specific for an incoming request. |
| [prepend](./kibana-plugin-server.basepath.prepend.md) | | <code>(path: string) =&gt; string</code> | returns a new <code>basePath</code> value, prefixed with passed <code>url</code>. |
| [remove](./kibana-plugin-server.basepath.remove.md) | | <code>(path: string) =&gt; string</code> | returns a new <code>basePath</code> value, cleaned up from passed <code>url</code>. |
| [serverBasePath](./kibana-plugin-server.basepath.serverbasepath.md) | | <code>string</code> | returns the server's basePath<!-- -->See [BasePath.get](./kibana-plugin-server.basepath.get.md) for getting the basePath value for a specific request |
| [set](./kibana-plugin-server.basepath.set.md) | | <code>(request: KibanaRequest&lt;unknown, unknown, unknown&gt; &#124; LegacyRequest, requestSpecificBasePath: string) =&gt; void</code> | sets <code>basePath</code> value, specific for an incoming request. |
| [set](./kibana-plugin-server.basepath.set.md) | | <code>(request: LegacyRequest &#124; KibanaRequest&lt;unknown, unknown, unknown&gt;, requestSpecificBasePath: string) =&gt; void</code> | sets <code>basePath</code> value, specific for an incoming request. |
## Remarks

View file

@ -9,5 +9,5 @@ sets `basePath` value, specific for an incoming request.
<b>Signature:</b>
```typescript
set: (request: KibanaRequest<unknown, unknown, unknown> | LegacyRequest, requestSpecificBasePath: string) => void;
set: (request: LegacyRequest | KibanaRequest<unknown, unknown, unknown>, requestSpecificBasePath: string) => void;
```

View file

@ -9,9 +9,9 @@ Creates a new [IContextContainer](./kibana-plugin-server.icontextcontainer.md) f
<b>Signature:</b>
```typescript
createContextContainer<TContext extends {}, THandlerReturn, THandlerParmaters extends any[] = []>(): IContextContainer<TContext, THandlerReturn, THandlerParmaters>;
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
```
<b>Returns:</b>
`IContextContainer<TContext, THandlerReturn, THandlerParmaters>`
`IContextContainer<THandler>`

View file

@ -14,7 +14,7 @@ http: {
registerOnPostAuth: HttpServiceSetup['registerOnPostAuth'];
basePath: HttpServiceSetup['basePath'];
isTlsEnabled: HttpServiceSetup['isTlsEnabled'];
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(name: T, provider: RequestHandlerContextProvider<RequestHandlerContext>) => RequestHandlerContextContainer<RequestHandlerContext>;
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(name: T, provider: RequestHandlerContextProvider<T>) => RequestHandlerContextContainer;
createRouter: () => IRouter;
};
```

View file

@ -18,5 +18,5 @@ export interface CoreSetup
| --- | --- | --- |
| [context](./kibana-plugin-server.coresetup.context.md) | <code>{</code><br/><code> createContextContainer: ContextSetup['createContextContainer'];</code><br/><code> }</code> | |
| [elasticsearch](./kibana-plugin-server.coresetup.elasticsearch.md) | <code>{</code><br/><code> adminClient$: Observable&lt;ClusterClient&gt;;</code><br/><code> dataClient$: Observable&lt;ClusterClient&gt;;</code><br/><code> createClient: (type: string, clientConfig?: Partial&lt;ElasticsearchClientConfig&gt;) =&gt; ClusterClient;</code><br/><code> }</code> | |
| [http](./kibana-plugin-server.coresetup.http.md) | <code>{</code><br/><code> createCookieSessionStorageFactory: HttpServiceSetup['createCookieSessionStorageFactory'];</code><br/><code> registerOnPreAuth: HttpServiceSetup['registerOnPreAuth'];</code><br/><code> registerAuth: HttpServiceSetup['registerAuth'];</code><br/><code> registerOnPostAuth: HttpServiceSetup['registerOnPostAuth'];</code><br/><code> basePath: HttpServiceSetup['basePath'];</code><br/><code> isTlsEnabled: HttpServiceSetup['isTlsEnabled'];</code><br/><code> registerRouteHandlerContext: &lt;T extends keyof RequestHandlerContext&gt;(name: T, provider: RequestHandlerContextProvider&lt;RequestHandlerContext&gt;) =&gt; RequestHandlerContextContainer&lt;RequestHandlerContext&gt;;</code><br/><code> createRouter: () =&gt; IRouter;</code><br/><code> }</code> | |
| [http](./kibana-plugin-server.coresetup.http.md) | <code>{</code><br/><code> createCookieSessionStorageFactory: HttpServiceSetup['createCookieSessionStorageFactory'];</code><br/><code> registerOnPreAuth: HttpServiceSetup['registerOnPreAuth'];</code><br/><code> registerAuth: HttpServiceSetup['registerAuth'];</code><br/><code> registerOnPostAuth: HttpServiceSetup['registerOnPostAuth'];</code><br/><code> basePath: HttpServiceSetup['basePath'];</code><br/><code> isTlsEnabled: HttpServiceSetup['isTlsEnabled'];</code><br/><code> registerRouteHandlerContext: &lt;T extends keyof RequestHandlerContext&gt;(name: T, provider: RequestHandlerContextProvider&lt;T&gt;) =&gt; RequestHandlerContextContainer;</code><br/><code> createRouter: () =&gt; IRouter;</code><br/><code> }</code> | |

View file

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [HandlerContextType](./kibana-plugin-server.handlercontexttype.md)
## HandlerContextType type
Extracts the type of the first argument of a [HandlerFunction](./kibana-plugin-server.handlerfunction.md) to represent the type of the context.
<b>Signature:</b>
```typescript
export declare type HandlerContextType<T extends HandlerFunction<any>> = T extends HandlerFunction<infer U> ? U : never;
```

View file

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [HandlerFunction](./kibana-plugin-server.handlerfunction.md)
## HandlerFunction type
A function that accepts a context object and an optional number of additional arguments. Used for the generic types in [IContextContainer](./kibana-plugin-server.icontextcontainer.md)
<b>Signature:</b>
```typescript
export declare type HandlerFunction<T extends object> = (context: T, ...args: any[]) => any;
```

View file

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [HandlerParameters](./kibana-plugin-server.handlerparameters.md)
## HandlerParameters type
Extracts the types of the additional arguments of a [HandlerFunction](./kibana-plugin-server.handlerfunction.md)<!-- -->, excluding the [HandlerContextType](./kibana-plugin-server.handlercontexttype.md)<!-- -->.
<b>Signature:</b>
```typescript
export declare type HandlerParameters<T extends HandlerFunction<any>> = T extends (context: any, ...args: infer U) => any ? U : never;
```

View file

@ -10,6 +10,6 @@
```typescript
export declare type HttpServiceSetup = Omit<HttpServerSetup, 'registerRouter'> & {
createRouter: (path: string, plugin?: PluginOpaqueId) => IRouter;
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(pluginOpaqueId: PluginOpaqueId, contextName: T, provider: RequestHandlerContextProvider<RequestHandlerContext>) => RequestHandlerContextContainer<RequestHandlerContext>;
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(pluginOpaqueId: PluginOpaqueId, contextName: T, provider: RequestHandlerContextProvider<T>) => RequestHandlerContextContainer;
};
```

View file

@ -9,7 +9,7 @@ Create a new handler function pre-wired to context for the plugin.
<b>Signature:</b>
```typescript
createHandler(pluginOpaqueId: PluginOpaqueId, handler: IContextHandler<TContext, THandlerReturn, THandlerParameters>): (...rest: THandlerParameters) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>;
createHandler(pluginOpaqueId: PluginOpaqueId, handler: THandler): (...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
```
## Parameters
@ -17,11 +17,11 @@ createHandler(pluginOpaqueId: PluginOpaqueId, handler: IContextHandler<TContext,
| Parameter | Type | Description |
| --- | --- | --- |
| pluginOpaqueId | <code>PluginOpaqueId</code> | The plugin opaque ID for the plugin that registers this handler. |
| handler | <code>IContextHandler&lt;TContext, THandlerReturn, THandlerParameters&gt;</code> | Handler function to pass context object to. |
| handler | <code>THandler</code> | Handler function to pass context object to. |
<b>Returns:</b>
`(...rest: THandlerParameters) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>`
`(...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>`
A function that takes `THandlerParameters`<!-- -->, calls `handler` with a new context, and returns a Promise of the `handler` return value.

View file

@ -9,7 +9,7 @@ An object that handles registration of context providers and configuring handler
<b>Signature:</b>
```typescript
export interface IContextContainer<TContext extends {}, THandlerReturn, THandlerParameters extends any[] = []>
export interface IContextContainer<THandler extends HandlerFunction<any>>
```
## Methods

View file

@ -9,7 +9,7 @@ Register a new context provider.
<b>Signature:</b>
```typescript
registerContext<TContextName extends keyof TContext>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<TContext, TContextName, THandlerParameters>): this;
registerContext<TContextName extends keyof HandlerContextType<THandler>>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<THandler, TContextName>): this;
```
## Parameters
@ -18,7 +18,7 @@ registerContext<TContextName extends keyof TContext>(pluginOpaqueId: PluginOpaqu
| --- | --- | --- |
| pluginOpaqueId | <code>PluginOpaqueId</code> | The plugin opaque ID for the plugin that registers this context. |
| contextName | <code>TContextName</code> | The key of the <code>TContext</code> object this provider supplies the value for. |
| provider | <code>IContextProvider&lt;TContext, TContextName, THandlerParameters&gt;</code> | A [IContextProvider](./kibana-plugin-server.icontextprovider.md) to be called each time a new context is created. |
| provider | <code>IContextProvider&lt;THandler, TContextName&gt;</code> | A [IContextProvider](./kibana-plugin-server.icontextprovider.md) to be called each time a new context is created. |
<b>Returns:</b>

View file

@ -1,18 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [IContextHandler](./kibana-plugin-server.icontexthandler.md)
## IContextHandler type
A function registered by a plugin to perform some action.
<b>Signature:</b>
```typescript
export declare type IContextHandler<TContext extends {}, TReturn, THandlerParameters extends any[] = []> = (context: TContext, ...rest: THandlerParameters) => TReturn;
```
## Remarks
A new `TContext` will be built for each handler before invoking.

View file

@ -9,7 +9,7 @@ A function that returns a context value for a specific key of given context type
<b>Signature:</b>
```typescript
export declare type IContextProvider<TContext extends Record<string, any>, TContextName extends keyof TContext, TProviderParameters extends any[] = []> = (context: Partial<TContext>, ...rest: TProviderParameters) => Promise<TContext[TContextName]> | TContext[TContextName];
export declare type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: Partial<HandlerContextType<THandler>>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
```
## Remarks

View file

@ -116,11 +116,13 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [ElasticsearchClientConfig](./kibana-plugin-server.elasticsearchclientconfig.md) | |
| [GetAuthHeaders](./kibana-plugin-server.getauthheaders.md) | Get headers to authenticate a user against Elasticsearch. |
| [GetAuthState](./kibana-plugin-server.getauthstate.md) | Get authentication state for a request. Returned by <code>auth</code> interceptor. |
| [HandlerContextType](./kibana-plugin-server.handlercontexttype.md) | Extracts the type of the first argument of a [HandlerFunction](./kibana-plugin-server.handlerfunction.md) to represent the type of the context. |
| [HandlerFunction](./kibana-plugin-server.handlerfunction.md) | A function that accepts a context object and an optional number of additional arguments. Used for the generic types in [IContextContainer](./kibana-plugin-server.icontextcontainer.md) |
| [HandlerParameters](./kibana-plugin-server.handlerparameters.md) | Extracts the types of the additional arguments of a [HandlerFunction](./kibana-plugin-server.handlerfunction.md)<!-- -->, excluding the [HandlerContextType](./kibana-plugin-server.handlercontexttype.md)<!-- -->. |
| [Headers](./kibana-plugin-server.headers.md) | Http request headers to read. |
| [HttpResponsePayload](./kibana-plugin-server.httpresponsepayload.md) | Data send to the client as a response payload. |
| [HttpServiceSetup](./kibana-plugin-server.httpservicesetup.md) | |
| [IBasePath](./kibana-plugin-server.ibasepath.md) | Access or manipulate the Kibana base path[BasePath](./kibana-plugin-server.basepath.md) |
| [IContextHandler](./kibana-plugin-server.icontexthandler.md) | A function registered by a plugin to perform some action. |
| [IContextProvider](./kibana-plugin-server.icontextprovider.md) | A function that returns a context value for a specific key of given context type. |
| [IsAuthenticated](./kibana-plugin-server.isauthenticated.md) | Return authentication status for a request. |
| [KibanaResponseFactory](./kibana-plugin-server.kibanaresponsefactory.md) | Creates an object containing request response payload, HTTP headers, error details, and other data transmitted to the client. |
@ -136,8 +138,6 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [RequestHandler](./kibana-plugin-server.requesthandler.md) | A function executed when route path matched requested resource path. Request handler is expected to return a result of one of [KibanaResponseFactory](./kibana-plugin-server.kibanaresponsefactory.md) functions. |
| [RequestHandlerContextContainer](./kibana-plugin-server.requesthandlercontextcontainer.md) | An object that handles registration of http request context providers. |
| [RequestHandlerContextProvider](./kibana-plugin-server.requesthandlercontextprovider.md) | Context provider for request handler. Extends request context object with provided functionality or data. |
| [RequestHandlerParams](./kibana-plugin-server.requesthandlerparams.md) | Parameters passed to the request handler function. |
| [RequestHandlerReturn](./kibana-plugin-server.requesthandlerreturn.md) | Expected outcome the request handler function. |
| [ResponseError](./kibana-plugin-server.responseerror.md) | Error message and optional data send to the client in case of error. |
| [ResponseErrorAttributes](./kibana-plugin-server.responseerrorattributes.md) | Additional data to provide error details. |
| [ResponseHeaders](./kibana-plugin-server.responseheaders.md) | Http response headers to set. |

View file

@ -9,5 +9,5 @@ An object that handles registration of http request context providers.
<b>Signature:</b>
```typescript
export declare type RequestHandlerContextContainer<TContext> = IContextContainer<TContext, RequestHandlerReturn | Promise<RequestHandlerReturn>, RequestHandlerParams>;
export declare type RequestHandlerContextContainer = IContextContainer<RequestHandler<any, any, any>>;
```

View file

@ -9,5 +9,5 @@ Context provider for request handler. Extends request context object with provid
<b>Signature:</b>
```typescript
export declare type RequestHandlerContextProvider<TContext> = IContextProvider<TContext, keyof TContext, RequestHandlerParams>;
export declare type RequestHandlerContextProvider<TContextName extends keyof RequestHandlerContext> = IContextProvider<RequestHandler<any, any, any>, TContextName>;
```

View file

@ -1,13 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RequestHandlerParams](./kibana-plugin-server.requesthandlerparams.md)
## RequestHandlerParams type
Parameters passed to the request handler function.
<b>Signature:</b>
```typescript
export declare type RequestHandlerParams = [KibanaRequest, KibanaResponseFactory];
```

View file

@ -1,13 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [RequestHandlerReturn](./kibana-plugin-server.requesthandlerreturn.md)
## RequestHandlerReturn type
Expected outcome the request handler function.
<b>Signature:</b>
```typescript
export declare type RequestHandlerReturn = KibanaResponse;
```

View file

@ -27,12 +27,9 @@ import { AppRouter } from './ui';
import { HttpStart } from '../http';
import { ContextSetup, IContextContainer } from '../context';
import {
AppMountContext,
App,
LegacyApp,
AppMounter,
AppUnmount,
AppMountParameters,
InternalApplicationSetup,
InternalApplicationStart,
} from './types';
@ -64,11 +61,7 @@ export class ApplicationService {
private readonly apps$ = new BehaviorSubject<ReadonlyMap<string, AppBox>>(new Map());
private readonly legacyApps$ = new BehaviorSubject<ReadonlyMap<string, LegacyApp>>(new Map());
private readonly capabilities = new CapabilitiesService();
private mountContext?: IContextContainer<
AppMountContext,
AppUnmount | Promise<AppUnmount>,
[AppMountParameters]
>;
private mountContext?: IContextContainer<App['mount']>;
public setup({ context }: SetupDeps): InternalApplicationSetup {
this.mountContext = context.createContextContainer();
@ -98,7 +91,7 @@ export class ApplicationService {
this.legacyApps$.next(new Map([...this.legacyApps$.value.entries(), [app.id, app]]));
},
registerMountContext: this.mountContext.registerContext,
registerMountContext: this.mountContext!.registerContext,
};
}

View file

@ -193,7 +193,7 @@ export interface ApplicationSetup {
*/
registerMountContext<T extends keyof AppMountContext>(
contextName: T,
provider: IContextProvider<AppMountContext, T>
provider: IContextProvider<App['mount'], T>
): void;
}
@ -224,7 +224,7 @@ export interface InternalApplicationSetup {
registerMountContext<T extends keyof AppMountContext>(
pluginOpaqueId: PluginOpaqueId,
contextName: T,
provider: IContextProvider<AppMountContext, T>
provider: IContextProvider<App['mount'], T>
): void;
}
@ -261,7 +261,7 @@ export interface ApplicationStart {
*/
registerMountContext<T extends keyof AppMountContext>(
contextName: T,
provider: IContextProvider<AppMountContext, T>
provider: IContextProvider<App['mount'], T>
): void;
}
@ -291,7 +291,7 @@ export interface InternalApplicationStart
registerMountContext<T extends keyof AppMountContext>(
pluginOpaqueId: PluginOpaqueId,
contextName: T,
provider: IContextProvider<AppMountContext, T>
provider: IContextProvider<App['mount'], T>
): void;
// Internal APIs

View file

@ -18,7 +18,7 @@
*/
import { PluginOpaqueId } from '../../server';
import { IContextContainer, ContextContainer } from '../../utils/context';
import { IContextContainer, ContextContainer, HandlerFunction } from '../../utils/context';
import { CoreContext } from '../core_system';
interface StartDeps {
@ -31,15 +31,8 @@ export class ContextService {
public setup({ pluginDependencies }: StartDeps): ContextSetup {
return {
createContextContainer: <
TContext extends {},
THandlerReturn,
THandlerParameters extends any[] = []
>() =>
new ContextContainer<TContext, THandlerReturn, THandlerParameters>(
pluginDependencies,
this.core.coreId
),
createContextContainer: <THandler extends HandlerFunction<any>>() =>
new ContextContainer<THandler>(pluginDependencies, this.core.coreId),
};
}
}
@ -111,9 +104,5 @@ export interface ContextSetup {
/**
* Creates a new {@link IContextContainer} for a service owner.
*/
createContextContainer<
TContext extends {},
THandlerReturn,
THandlerParmaters extends any[] = []
>(): IContextContainer<TContext, THandlerReturn, THandlerParmaters>;
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
}

View file

@ -18,4 +18,10 @@
*/
export { ContextService, ContextSetup } from './context_service';
export { IContextContainer, IContextProvider, IContextHandler } from '../../utils/context';
export {
IContextContainer,
IContextProvider,
HandlerFunction,
HandlerContextType,
HandlerParameters,
} from '../../utils/context';

View file

@ -67,7 +67,14 @@ import { UiSettingsClient, UiSettingsState, UiSettingsClientContract } from './u
import { ApplicationSetup, Capabilities, ApplicationStart } from './application';
import { DocLinksStart } from './doc_links';
import { SavedObjectsStart } from './saved_objects';
import { IContextContainer, IContextProvider, ContextSetup, IContextHandler } from './context';
import {
IContextContainer,
IContextProvider,
ContextSetup,
HandlerFunction,
HandlerContextType,
HandlerParameters,
} from './context';
/** @interal */
export { CoreContext, CoreSystem } from './core_system';
@ -218,7 +225,9 @@ export {
ChromeRecentlyAccessedHistoryItem,
ChromeStart,
IContextContainer,
IContextHandler,
HandlerFunction,
HandlerContextType,
HandlerParameters,
IContextProvider,
ContextSetup,
DocLinksStart,

View file

@ -9,6 +9,7 @@ import { MouseEventHandler } from 'react';
import { Observable } from 'rxjs';
import React from 'react';
import * as Rx from 'rxjs';
import { ShallowPromise } from '@kbn/utility-types';
import { EuiGlobalToastListToast as Toast } from '@elastic/eui';
// @public
@ -31,7 +32,7 @@ export interface AppBase {
// @public (undocumented)
export interface ApplicationSetup {
register(app: App): void;
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<AppMountContext, T>): void;
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<App['mount'], T>): void;
}
// @public (undocumented)
@ -44,7 +45,7 @@ export interface ApplicationStart {
path?: string;
state?: any;
}): void;
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<AppMountContext, T>): void;
registerMountContext<T extends keyof AppMountContext>(contextName: T, provider: IContextProvider<App['mount'], T>): void;
}
// @public
@ -213,7 +214,7 @@ export interface ChromeStart {
// @public
export interface ContextSetup {
createContextContainer<TContext extends {}, THandlerReturn, THandlerParmaters extends any[] = []>(): IContextContainer<TContext, THandlerReturn, THandlerParmaters>;
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
}
// @internal (undocumented)
@ -389,6 +390,15 @@ export interface FatalErrorsSetup {
get$: () => Rx.Observable<FatalErrorInfo>;
}
// @public
export type HandlerContextType<T extends HandlerFunction<any>> = T extends HandlerFunction<infer U> ? U : never;
// @public
export type HandlerFunction<T extends object> = (context: T, ...args: any[]) => any;
// @public
export type HandlerParameters<T extends HandlerFunction<any>> = T extends (context: any, ...args: infer U) => any ? U : never;
// @public (undocumented)
export type HttpBody = BodyInit | null | any;
@ -551,16 +561,13 @@ export interface I18nStart {
}
// @public
export interface IContextContainer<TContext extends {}, THandlerReturn, THandlerParameters extends any[] = []> {
createHandler(pluginOpaqueId: PluginOpaqueId, handler: IContextHandler<TContext, THandlerReturn, THandlerParameters>): (...rest: THandlerParameters) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>;
registerContext<TContextName extends keyof TContext>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<TContext, TContextName, THandlerParameters>): this;
export interface IContextContainer<THandler extends HandlerFunction<any>> {
createHandler(pluginOpaqueId: PluginOpaqueId, handler: THandler): (...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
registerContext<TContextName extends keyof HandlerContextType<THandler>>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<THandler, TContextName>): this;
}
// @public
export type IContextHandler<TContext extends {}, TReturn, THandlerParameters extends any[] = []> = (context: TContext, ...rest: THandlerParameters) => TReturn;
// @public
export type IContextProvider<TContext extends Record<string, any>, TContextName extends keyof TContext, TProviderParameters extends any[] = []> = (context: Partial<TContext>, ...rest: TProviderParameters) => Promise<TContext[TContextName]> | TContext[TContextName];
export type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: Partial<HandlerContextType<THandler>>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
// @public @deprecated
export interface LegacyCoreSetup extends CoreSetup {

View file

@ -18,7 +18,7 @@
*/
import { PluginOpaqueId } from '../../server';
import { IContextContainer, ContextContainer } from '../../utils/context';
import { IContextContainer, ContextContainer, HandlerFunction } from '../../utils/context';
import { CoreContext } from '../core_context';
interface SetupDeps {
@ -31,15 +31,8 @@ export class ContextService {
public setup({ pluginDependencies }: SetupDeps): ContextSetup {
return {
createContextContainer: <
TContext extends {},
THandlerReturn,
THandlerParameters extends any[] = []
>() => {
return new ContextContainer<TContext, THandlerReturn, THandlerParameters>(
pluginDependencies,
this.core.coreId
);
createContextContainer: <THandler extends HandlerFunction<any>>() => {
return new ContextContainer<THandler>(pluginDependencies, this.core.coreId);
},
};
}
@ -112,9 +105,5 @@ export interface ContextSetup {
/**
* Creates a new {@link IContextContainer} for a service owner.
*/
createContextContainer<
TContext extends {},
THandlerReturn,
THandlerParmaters extends any[] = []
>(): IContextContainer<TContext, THandlerReturn, THandlerParmaters>;
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
}

View file

@ -18,4 +18,10 @@
*/
export { ContextService, ContextSetup } from './context_service';
export { IContextContainer, IContextProvider, IContextHandler } from '../../utils/context';
export {
IContextContainer,
IContextProvider,
HandlerFunction,
HandlerContextType,
HandlerParameters,
} from '../../utils/context';

View file

@ -83,8 +83,8 @@ export type HttpServiceSetup = Omit<HttpServerSetup, 'registerRouter'> & {
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(
pluginOpaqueId: PluginOpaqueId,
contextName: T,
provider: RequestHandlerContextProvider<RequestHandlerContext>
) => RequestHandlerContextContainer<RequestHandlerContext>;
provider: RequestHandlerContextProvider<T>
) => RequestHandlerContextContainer;
};
/** @public */
@ -103,7 +103,7 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
private readonly logger: LoggerFactory;
private readonly log: Logger;
private notReadyServer?: Server;
private requestHandlerContext?: RequestHandlerContextContainer<RequestHandlerContext>;
private requestHandlerContext?: RequestHandlerContextContainer;
constructor(private readonly coreContext: CoreContext) {
this.logger = coreContext.logger;
@ -150,7 +150,7 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(
pluginOpaqueId: PluginOpaqueId,
contextName: T,
provider: RequestHandlerContextProvider<RequestHandlerContext>
provider: RequestHandlerContextProvider<T>
) => this.requestHandlerContext!.registerContext(pluginOpaqueId, contextName, provider),
};

View file

@ -16,30 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
import { IContextProvider, IContextContainer } from '../context';
import { KibanaRequest, KibanaResponseFactory, KibanaResponse } from './router';
/**
* Parameters passed to the request handler function.
* @public
*/
export type RequestHandlerParams = [KibanaRequest, KibanaResponseFactory];
/**
* Expected outcome the request handler function.
* @public
*/
export type RequestHandlerReturn = KibanaResponse;
import { RequestHandler } from './router';
import { RequestHandlerContext } from '..';
/**
* An object that handles registration of http request context providers.
* @public
*/
export type RequestHandlerContextContainer<TContext> = IContextContainer<
TContext,
RequestHandlerReturn | Promise<RequestHandlerReturn>,
RequestHandlerParams
>;
export type RequestHandlerContextContainer = IContextContainer<RequestHandler<any, any, any>>;
/**
* Context provider for request handler.
@ -47,8 +33,6 @@ export type RequestHandlerContextContainer<TContext> = IContextContainer<
*
* @public
*/
export type RequestHandlerContextProvider<TContext> = IContextProvider<
TContext,
keyof TContext,
RequestHandlerParams
>;
export type RequestHandlerContextProvider<
TContextName extends keyof RequestHandlerContext
> = IContextProvider<RequestHandler<any, any, any>, TContextName>;

View file

@ -59,7 +59,13 @@ import { SavedObjectsServiceStart } from './saved_objects';
export { bootstrap } from './bootstrap';
export { ConfigPath, ConfigService } from './config';
export { IContextContainer, IContextProvider, IContextHandler } from './context';
export {
IContextContainer,
IContextProvider,
HandlerFunction,
HandlerContextType,
HandlerParameters,
} from './context';
export { CoreId } from './core_context';
export {
CallAPIOptions,
@ -102,8 +108,6 @@ export {
RequestHandler,
RequestHandlerContextContainer,
RequestHandlerContextProvider,
RequestHandlerParams,
RequestHandlerReturn,
ResponseError,
ResponseErrorAttributes,
ResponseHeaders,
@ -212,8 +216,8 @@ export interface CoreSetup {
isTlsEnabled: HttpServiceSetup['isTlsEnabled'];
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(
name: T,
provider: RequestHandlerContextProvider<RequestHandlerContext>
) => RequestHandlerContextContainer<RequestHandlerContext>;
provider: RequestHandlerContextProvider<T>
) => RequestHandlerContextContainer;
createRouter: () => IRouter;
};
}

View file

@ -21,6 +21,7 @@ import { Request } from 'hapi';
import { ResponseObject } from 'hapi';
import { ResponseToolkit } from 'hapi';
import { Server } from 'hapi';
import { ShallowPromise } from '@kbn/utility-types';
import { Stream } from 'stream';
import { Type } from '@kbn/config-schema';
import { TypeOf } from '@kbn/config-schema';
@ -61,11 +62,11 @@ export interface AuthToolkit {
export class BasePath {
// @internal
constructor(serverBasePath?: string);
get: (request: KibanaRequest<unknown, unknown, unknown> | LegacyRequest) => string;
get: (request: LegacyRequest | KibanaRequest<unknown, unknown, unknown>) => string;
prepend: (path: string) => string;
remove: (path: string) => string;
readonly serverBasePath: string;
set: (request: KibanaRequest<unknown, unknown, unknown> | LegacyRequest, requestSpecificBasePath: string) => void;
set: (request: LegacyRequest | KibanaRequest<unknown, unknown, unknown>, requestSpecificBasePath: string) => void;
}
// Warning: (ae-forgotten-export) The symbol "BootstrapArgs" needs to be exported by the entry point index.d.ts
@ -109,7 +110,7 @@ export class ConfigService {
// @public
export interface ContextSetup {
createContextContainer<TContext extends {}, THandlerReturn, THandlerParmaters extends any[] = []>(): IContextContainer<TContext, THandlerReturn, THandlerParmaters>;
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
}
// @internal (undocumented)
@ -135,7 +136,7 @@ export interface CoreSetup {
registerOnPostAuth: HttpServiceSetup['registerOnPostAuth'];
basePath: HttpServiceSetup['basePath'];
isTlsEnabled: HttpServiceSetup['isTlsEnabled'];
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(name: T, provider: RequestHandlerContextProvider<RequestHandlerContext>) => RequestHandlerContextContainer<RequestHandlerContext>;
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(name: T, provider: RequestHandlerContextProvider<T>) => RequestHandlerContextContainer;
createRouter: () => IRouter;
};
}
@ -219,6 +220,15 @@ export type GetAuthState = (request: KibanaRequest | LegacyRequest) => {
state: unknown;
};
// @public
export type HandlerContextType<T extends HandlerFunction<any>> = T extends HandlerFunction<infer U> ? U : never;
// @public
export type HandlerFunction<T extends object> = (context: T, ...args: any[]) => any;
// @public
export type HandlerParameters<T extends HandlerFunction<any>> = T extends (context: any, ...args: infer U) => any ? U : never;
// @public
export type Headers = {
[header in KnownHeaders]?: string | string[] | undefined;
@ -258,7 +268,7 @@ export interface HttpServerSetup {
// @public (undocumented)
export type HttpServiceSetup = Omit<HttpServerSetup, 'registerRouter'> & {
createRouter: (path: string, plugin?: PluginOpaqueId) => IRouter;
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(pluginOpaqueId: PluginOpaqueId, contextName: T, provider: RequestHandlerContextProvider<RequestHandlerContext>) => RequestHandlerContextContainer<RequestHandlerContext>;
registerRouteHandlerContext: <T extends keyof RequestHandlerContext>(pluginOpaqueId: PluginOpaqueId, contextName: T, provider: RequestHandlerContextProvider<T>) => RequestHandlerContextContainer;
};
// @public (undocumented)
@ -270,16 +280,13 @@ export interface HttpServiceStart {
export type IBasePath = Pick<BasePath, keyof BasePath>;
// @public
export interface IContextContainer<TContext extends {}, THandlerReturn, THandlerParameters extends any[] = []> {
createHandler(pluginOpaqueId: PluginOpaqueId, handler: IContextHandler<TContext, THandlerReturn, THandlerParameters>): (...rest: THandlerParameters) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>;
registerContext<TContextName extends keyof TContext>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<TContext, TContextName, THandlerParameters>): this;
export interface IContextContainer<THandler extends HandlerFunction<any>> {
createHandler(pluginOpaqueId: PluginOpaqueId, handler: THandler): (...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
registerContext<TContextName extends keyof HandlerContextType<THandler>>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<THandler, TContextName>): this;
}
// @public
export type IContextHandler<TContext extends {}, TReturn, THandlerParameters extends any[] = []> = (context: TContext, ...rest: THandlerParameters) => TReturn;
// @public
export type IContextProvider<TContext extends Record<string, any>, TContextName extends keyof TContext, TProviderParameters extends any[] = []> = (context: Partial<TContext>, ...rest: TProviderParameters) => Promise<TContext[TContextName]> | TContext[TContextName];
export type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: Partial<HandlerContextType<THandler>>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
// @public
export interface IKibanaSocket {
@ -602,16 +609,10 @@ export interface RequestHandlerContext {
}
// @public
export type RequestHandlerContextContainer<TContext> = IContextContainer<TContext, RequestHandlerReturn | Promise<RequestHandlerReturn>, RequestHandlerParams>;
export type RequestHandlerContextContainer = IContextContainer<RequestHandler<any, any, any>>;
// @public
export type RequestHandlerContextProvider<TContext> = IContextProvider<TContext, keyof TContext, RequestHandlerParams>;
// @public
export type RequestHandlerParams = [KibanaRequest, KibanaResponseFactory];
// @public
export type RequestHandlerReturn = KibanaResponse;
export type RequestHandlerContextProvider<TContextName extends keyof RequestHandlerContext> = IContextProvider<RequestHandler<any, any, any>, TContextName>;
// @public
export type ResponseError = string | Error | {

View file

@ -19,7 +19,7 @@
import { IContextContainer } from './context';
export type ContextContainerMock = jest.Mocked<IContextContainer<any, any, any>>;
export type ContextContainerMock = jest.Mocked<IContextContainer<any>>;
const createContextMock = () => {
const contextMock: ContextContainerMock = {

View file

@ -44,7 +44,7 @@ const coreId = Symbol();
describe('ContextContainer', () => {
it('does not allow the same context to be registered twice', () => {
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(plugins, coreId);
contextContainer.registerContext(coreId, 'ctxFromA', () => 'aString');
expect(() =>
@ -56,7 +56,10 @@ describe('ContextContainer', () => {
describe('registerContext', () => {
it('throws error if called with an unknown symbol', async () => {
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(
plugins,
coreId
);
await expect(() =>
contextContainer.registerContext(Symbol('unknown'), 'ctxFromA', jest.fn())
).toThrowErrorMatchingInlineSnapshot(
@ -67,7 +70,10 @@ describe('ContextContainer', () => {
describe('context building', () => {
it('resolves dependencies', async () => {
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(
plugins,
coreId
);
expect.assertions(8);
contextContainer.registerContext(coreId, 'core1', context => {
expect(context).toEqual({});
@ -118,7 +124,10 @@ describe('ContextContainer', () => {
it('exposes all core context to all providers regardless of registration order', async () => {
expect.assertions(4);
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(
plugins,
coreId
);
contextContainer
.registerContext(pluginA, 'ctxFromA', context => {
expect(context).toEqual({ core1: 'core', core2: 101 });
@ -146,7 +155,10 @@ describe('ContextContainer', () => {
it('exposes all core context to core providers', async () => {
expect.assertions(4);
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(
plugins,
coreId
);
contextContainer
.registerContext(coreId, 'core1', context => {
@ -171,7 +183,10 @@ describe('ContextContainer', () => {
});
it('does not expose plugin contexts to core handler', async () => {
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(
plugins,
coreId
);
contextContainer
.registerContext(coreId, 'core1', context => 'core')
@ -189,10 +204,9 @@ describe('ContextContainer', () => {
it('passes additional arguments to providers', async () => {
expect.assertions(6);
const contextContainer = new ContextContainer<MyContext, string, [string, number]>(
plugins,
coreId
);
const contextContainer = new ContextContainer<
(context: MyContext, arg1: string, arg2: number) => string
>(plugins, coreId);
contextContainer.registerContext(coreId, 'core1', (context, str, num) => {
expect(str).toEqual('passed string');
@ -228,7 +242,10 @@ describe('ContextContainer', () => {
describe('createHandler', () => {
it('throws error if called with an unknown symbol', async () => {
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(
plugins,
coreId
);
await expect(() =>
contextContainer.createHandler(Symbol('unknown'), jest.fn())
).toThrowErrorMatchingInlineSnapshot(
@ -237,8 +254,10 @@ describe('ContextContainer', () => {
});
it('returns value from original handler', async () => {
const contextContainer = new ContextContainer<MyContext, string>(plugins, coreId);
const contextContainer = new ContextContainer<(context: MyContext) => string>(
plugins,
coreId
);
const rawHandler1 = jest.fn(() => 'handler1');
const handler1 = contextContainer.createHandler(pluginA, rawHandler1);
@ -246,10 +265,9 @@ describe('ContextContainer', () => {
});
it('passes additional arguments to handlers', async () => {
const contextContainer = new ContextContainer<MyContext, string, [string, number]>(
plugins,
coreId
);
const contextContainer = new ContextContainer<
(context: MyContext, arg1: string, arg2: number) => string
>(plugins, coreId);
const rawHandler1 = jest.fn<string, [MyContext, string, number]>(() => 'handler1');
const handler1 = contextContainer.createHandler(pluginA, rawHandler1);

View file

@ -18,6 +18,7 @@
*/
import { flatten } from 'lodash';
import { ShallowPromise } from '@kbn/utility-types';
import { pick } from '.';
import { CoreId, PluginOpaqueId } from '../server';
@ -35,26 +36,44 @@ import { CoreId, PluginOpaqueId } from '../server';
* @public
*/
export type IContextProvider<
TContext extends Record<string, any>,
TContextName extends keyof TContext,
TProviderParameters extends any[] = []
THandler extends HandlerFunction<any>,
TContextName extends keyof HandlerContextType<THandler>
> = (
context: Partial<TContext>,
...rest: TProviderParameters
) => Promise<TContext[TContextName]> | TContext[TContextName];
context: Partial<HandlerContextType<THandler>>,
...rest: HandlerParameters<THandler>
) =>
| Promise<HandlerContextType<THandler>[TContextName]>
| HandlerContextType<THandler>[TContextName];
/**
* A function registered by a plugin to perform some action.
*
* @remarks
* A new `TContext` will be built for each handler before invoking.
* A function that accepts a context object and an optional number of additional arguments. Used for the generic types
* in {@link IContextContainer}
*
* @public
*/
export type IContextHandler<TContext extends {}, TReturn, THandlerParameters extends any[] = []> = (
context: TContext,
...rest: THandlerParameters
) => TReturn;
export type HandlerFunction<T extends object> = (context: T, ...args: any[]) => any;
/**
* Extracts the type of the first argument of a {@link HandlerFunction} to represent the type of the context.
*
* @public
*/
export type HandlerContextType<T extends HandlerFunction<any>> = T extends HandlerFunction<infer U>
? U
: never;
/**
* Extracts the types of the additional arguments of a {@link HandlerFunction}, excluding the
* {@link HandlerContextType}.
*
* @public
*/
export type HandlerParameters<T extends HandlerFunction<any>> = T extends (
context: any,
...args: infer U
) => any
? U
: never;
/**
* An object that handles registration of context providers and configuring handlers with context.
@ -123,13 +142,12 @@ export type IContextHandler<TContext extends {}, TReturn, THandlerParameters ext
* }
* ```
*
* @typeParam THandler - the type of {@link HandlerFunction} this container should manage. The first argument of this
* function will be used as the context type.
*
* @public
*/
export interface IContextContainer<
TContext extends {},
THandlerReturn,
THandlerParameters extends any[] = []
> {
export interface IContextContainer<THandler extends HandlerFunction<any>> {
/**
* Register a new context provider.
*
@ -144,10 +162,10 @@ export interface IContextContainer<
* @param provider - A {@link IContextProvider} to be called each time a new context is created.
* @returns The {@link IContextContainer} for method chaining.
*/
registerContext<TContextName extends keyof TContext>(
registerContext<TContextName extends keyof HandlerContextType<THandler>>(
pluginOpaqueId: PluginOpaqueId,
contextName: TContextName,
provider: IContextProvider<TContext, TContextName, THandlerParameters>
provider: IContextProvider<THandler, TContextName>
): this;
/**
@ -160,31 +178,26 @@ export interface IContextContainer<
*/
createHandler(
pluginOpaqueId: PluginOpaqueId,
handler: IContextHandler<TContext, THandlerReturn, THandlerParameters>
): (
...rest: THandlerParameters
) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>;
handler: THandler
): (...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
}
/** @internal */
export class ContextContainer<
TContext extends Record<string, any>,
THandlerReturn,
THandlerParameters extends any[] = []
> implements IContextContainer<TContext, THandlerReturn, THandlerParameters> {
export class ContextContainer<THandler extends HandlerFunction<any>>
implements IContextContainer<THandler> {
/**
* Used to map contexts to their providers and associated plugin. In registration order which is tightly coupled to
* plugin load order.
*/
private readonly contextProviders = new Map<
keyof TContext,
keyof HandlerContextType<THandler>,
{
provider: IContextProvider<TContext, keyof TContext, THandlerParameters>;
provider: IContextProvider<THandler, keyof HandlerContextType<THandler>>;
source: symbol;
}
>();
/** Used to keep track of which plugins registered which contexts for dependency resolution. */
private readonly contextNamesBySource: Map<symbol, Array<keyof TContext>>;
private readonly contextNamesBySource: Map<symbol, Array<keyof HandlerContextType<THandler>>>;
/**
* @param pluginDependencies - A map of plugins to an array of their dependencies.
@ -193,13 +206,15 @@ export class ContextContainer<
private readonly pluginDependencies: ReadonlyMap<PluginOpaqueId, PluginOpaqueId[]>,
private readonly coreId: CoreId
) {
this.contextNamesBySource = new Map<symbol, Array<keyof TContext>>([[coreId, []]]);
this.contextNamesBySource = new Map<symbol, Array<keyof HandlerContextType<THandler>>>([
[coreId, []],
]);
}
public registerContext = <TContextName extends keyof TContext>(
public registerContext = <TContextName extends keyof HandlerContextType<THandler>>(
source: symbol,
contextName: TContextName,
provider: IContextProvider<TContext, TContextName, THandlerParameters>
provider: IContextProvider<THandler, TContextName>
): this => {
if (this.contextProviders.has(contextName)) {
throw new Error(`Context provider for ${contextName} has already been registered.`);
@ -217,27 +232,22 @@ export class ContextContainer<
return this;
};
public createHandler = (
source: symbol,
handler: IContextHandler<TContext, THandlerReturn, THandlerParameters>
) => {
public createHandler = (source: symbol, handler: THandler) => {
if (source !== this.coreId && !this.pluginDependencies.has(source)) {
throw new Error(`Cannot create handler for unknown plugin: ${source.toString()}`);
}
return (async (...args: THandlerParameters) => {
return (async (...args: HandlerParameters<THandler>) => {
const context = await this.buildContext(source, ...args);
return handler(context, ...args);
}) as (
...args: THandlerParameters
) => THandlerReturn extends Promise<any> ? THandlerReturn : Promise<THandlerReturn>;
}) as (...args: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
};
private async buildContext(
source: symbol,
...contextArgs: THandlerParameters
): Promise<TContext> {
const contextsToBuild: ReadonlySet<keyof TContext> = new Set(
...contextArgs: HandlerParameters<THandler>
): Promise<HandlerContextType<THandler>> {
const contextsToBuild: ReadonlySet<keyof HandlerContextType<THandler>> = new Set(
this.getContextNamesForSource(source)
);
@ -252,18 +262,20 @@ export class ContextContainer<
// registered that provider.
const exposedContext = pick(resolvedContext, [
...this.getContextNamesForSource(providerSource),
]);
]) as Partial<HandlerContextType<THandler>>;
return {
...resolvedContext,
[contextName]: await provider(exposedContext as Partial<TContext>, ...contextArgs),
[contextName]: await provider(exposedContext, ...contextArgs),
};
},
Promise.resolve({}) as Promise<TContext>
Promise.resolve({}) as Promise<HandlerContextType<THandler>>
);
}
private getContextNamesForSource(source: symbol): ReadonlySet<keyof TContext> {
private getContextNamesForSource(
source: symbol
): ReadonlySet<keyof HandlerContextType<THandler>> {
if (source === this.coreId) {
return this.getContextNamesForCore();
} else {

View file

@ -15,7 +15,7 @@ describe('licensingRouteHandlerContext', () => {
const context = createRouteHandlerContext(license$);
const { license: contextResult } = await context({});
const { license: contextResult } = await context({}, {} as any, {} as any);
expect(contextResult).toBe(license);
});
@ -29,7 +29,7 @@ describe('licensingRouteHandlerContext', () => {
const latestLicense = (Symbol() as unknown) as ILicense;
license$.next(latestLicense);
const { license: contextResult } = await context({});
const { license: contextResult } = await context({}, {} as any, {} as any);
expect(contextResult).toBe(latestLicense);
});

View file

@ -4,14 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext, IContextProvider } from 'src/core/server';
import { IContextProvider, RequestHandler } from 'src/core/server';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { ILicense } from './types';
export function createRouteHandlerContext(
license$: Observable<ILicense>
): IContextProvider<RequestHandlerContext, 'licensing'> {
): IContextProvider<RequestHandler<any, any, any>, 'licensing'> {
return async function licensingRouteHandlerContext() {
const license = await license$.pipe(take(1)).toPromise();
return { license };

View file

@ -20,6 +20,14 @@ import { LicensingConfig } from './licensing_config';
import { License } from './license';
import { createRouteHandlerContext } from './licensing_route_handler_context';
declare module 'src/core/server' {
interface RequestHandlerContext {
licensing: {
license: ILicense;
};
}
}
export class Plugin implements CorePlugin<LicensingPluginSetup> {
private readonly logger: Logger;
private readonly config$: Observable<LicensingConfig>;