mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
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:
parent
9cd6e32de0
commit
80e6ff729a
10 changed files with 22 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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>,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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!),
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue