Skip to content

Commit

Permalink
Make context.core required argument to context providers (#59996) (#6…
Browse files Browse the repository at this point in the history
…0061)

* Make context.core required argument to context providers

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

* Update docs
  • Loading branch information
rudolf authored Mar 12, 2020
1 parent 2610d49 commit d0ab211
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/core/utils/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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]>
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
};
});
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d0ab211

Please sign in to comment.