Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make context.core required argument to context providers #59996

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine not to expose PartialExceptFor as plugins would never need to use this type directly.

//
// @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 @@ -258,7 +258,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 @@ -203,7 +203,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