Make context.core required argument to context providers (#59996)

* Make context.core required argument to context providers

* Refactor plugins: context.core isn't optional anymore

* Update docs
This commit is contained in:
Rudolf Meijering 2020-03-12 20:39:29 +01:00 committed by GitHub
parent 9cd6e32de0
commit 80e6ff729a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 22 additions and 12 deletions

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<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: Partial<HandlerContextType<THandler>>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
export declare type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: PartialExceptFor<HandlerContextType<THandler>, 'core'>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
```
## Remarks

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<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: Partial<HandlerContextType<THandler>>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
export declare type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: PartialExceptFor<HandlerContextType<THandler>, 'core'>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
```
## Remarks

View file

@ -735,8 +735,10 @@ export interface IContextContainer<THandler extends HandlerFunction<any>> {
registerContext<TContextName extends keyof HandlerContextType<THandler>>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<THandler, TContextName>): this;
}
// Warning: (ae-forgotten-export) The symbol "PartialExceptFor" needs to be exported by the entry point index.d.ts
//
// @public
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];
export type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: PartialExceptFor<HandlerContextType<THandler>, 'core'>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
// @public (undocumented)
export interface IHttpFetchError extends Error {

View file

@ -882,8 +882,10 @@ export interface IContextContainer<THandler extends HandlerFunction<any>> {
registerContext<TContextName extends keyof HandlerContextType<THandler>>(pluginOpaqueId: PluginOpaqueId, contextName: TContextName, provider: IContextProvider<THandler, TContextName>): this;
}
// Warning: (ae-forgotten-export) The symbol "PartialExceptFor" needs to be exported by the entry point index.d.ts
//
// @public
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];
export type IContextProvider<THandler extends HandlerFunction<any>, TContextName extends keyof HandlerContextType<THandler>> = (context: PartialExceptFor<HandlerContextType<THandler>, 'core'>, ...rest: HandlerParameters<THandler>) => Promise<HandlerContextType<THandler>[TContextName]> | HandlerContextType<THandler>[TContextName];
// @public
export interface ICspConfig {

View file

@ -22,6 +22,11 @@ import { ShallowPromise } from '@kbn/utility-types';
import { pick } from '.';
import { CoreId, PluginOpaqueId } from '../server';
/**
* Make all properties in T optional, except for the properties whose keys are in the union K
*/
type PartialExceptFor<T, K extends keyof T> = Partial<T> & Pick<T, K>;
/**
* A function that returns a context value for a specific key of given context type.
*
@ -39,7 +44,8 @@ export type IContextProvider<
THandler extends HandlerFunction<any>,
TContextName extends keyof HandlerContextType<THandler>
> = (
context: Partial<HandlerContextType<THandler>>,
// context.core will always be available, but plugin contexts are typed as optional
context: PartialExceptFor<HandlerContextType<THandler>, 'core'>,
...rest: HandlerParameters<THandler>
) =>
| Promise<HandlerContextType<THandler>[TContextName]>
@ -261,7 +267,7 @@ export class ContextContainer<THandler extends HandlerFunction<any>>
// registered that provider.
const exposedContext = pick(resolvedContext, [
...this.getContextNamesForSource(providerSource),
]) as Partial<HandlerContextType<THandler>>;
]) as PartialExceptFor<HandlerContextType<THandler>, 'core'>;
return {
...resolvedContext,

View file

@ -55,7 +55,7 @@ export class SearchService implements Plugin<ISearchSetup, void> {
core.http.registerRouteHandlerContext<'search'>('search', context => {
return createApi({
caller: context.core!.elasticsearch.dataClient.callAsCurrentUser,
caller: context.core.elasticsearch.dataClient.callAsCurrentUser,
searchStrategies: this.searchStrategies,
});
});

View file

@ -34,7 +34,7 @@ export class CorePluginAPlugin implements Plugin {
core.http.registerRouteHandlerContext('pluginA', context => {
return {
ping: () =>
context.core!.elasticsearch.adminClient.callAsInternalUser('ping') as Promise<string>,
context.core.elasticsearch.adminClient.callAsInternalUser('ping') as Promise<string>,
};
});
}

View file

@ -282,7 +282,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
);
}
return new ActionsClient({
savedObjectsClient: context.core!.savedObjects.client,
savedObjectsClient: context.core.savedObjects.client,
actionTypeRegistry: actionTypeRegistry!,
defaultKibanaIndex,
scopedClusterClient: adminClient!.asScoped(request),

View file

@ -244,7 +244,7 @@ export class AlertingPlugin {
return async function alertsRouteHandlerContext(context, request) {
return {
getAlertsClient: () => {
return alertsClientFactory!.create(request, context.core!.savedObjects.client);
return alertsClientFactory!.create(request, context.core.savedObjects.client);
},
listTypes: alertTypeRegistry!.list.bind(alertTypeRegistry!),
};

View file

@ -17,9 +17,9 @@ describe('createRouteHandlerContext', () => {
const routeHandler = createRouteHandlerContext(license$);
const firstCtx = await routeHandler({}, {} as any, {} as any);
const firstCtx = await routeHandler({} as any, {} as any, {} as any);
license$.next(secondLicense);
const secondCtx = await routeHandler({}, {} as any, {} as any);
const secondCtx = await routeHandler({} as any, {} as any, {} as any);
expect(firstCtx.license).toBe(firstLicense);
expect(secondCtx.license).toBe(secondLicense);