From 04a9387afd9afad709e137cbab532c60c541235f Mon Sep 17 00:00:00 2001 From: Josh Dover Date: Wed, 16 Oct 2019 12:59:54 -0500 Subject: [PATCH] wip example [skip-ci] --- src/core/server/plugins/types.ts | 14 +++---- src/core/server/saved_objects/index.ts | 6 ++- .../saved_objects/saved_objects_service.ts | 7 ++-- src/core/server/server.ts | 4 +- src/core/types/core_service.ts | 42 ++++++++++++++++--- 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/core/server/plugins/types.ts b/src/core/server/plugins/types.ts index 9a3e922b3cb89..70c400b80b0fb 100644 --- a/src/core/server/plugins/types.ts +++ b/src/core/server/plugins/types.ts @@ -22,6 +22,7 @@ import { Type } from '@kbn/config-schema'; import { ConfigPath, EnvironmentMode, PackageInfo } from '../config'; import { LoggerFactory } from '../logging'; +import { Service } from '../../types'; import { CoreSetup, CoreStart } from '..'; export type PluginConfigSchema = Type | null; @@ -137,20 +138,19 @@ export interface DiscoveredPluginInternal extends DiscoveredPlugin { } /** - * The interface that should be returned by a `PluginInitializer`. + * The type that should be returned by a `PluginInitializer`. * * @public */ -export interface Plugin< +export type Plugin< TSetup = void, TStart = void, TPluginsSetup extends object = object, TPluginsStart extends object = object -> { - setup(core: CoreSetup, plugins: TPluginsSetup): TSetup | Promise; - start(core: CoreStart, plugins: TPluginsStart): TStart | Promise; - stop?(): void; -} +> = Service< + (core: CoreSetup, plugins: TPluginsSetup) => TSetup | Promise, + (core: CoreStart, plugins: TPluginsStart) => TStart | Promise +>; /** * Context that's available to plugins during initialization stage. diff --git a/src/core/server/saved_objects/index.ts b/src/core/server/saved_objects/index.ts index 76c62e0841bff..b013994619366 100644 --- a/src/core/server/saved_objects/index.ts +++ b/src/core/server/saved_objects/index.ts @@ -35,6 +35,10 @@ export { SavedObjectsSerializer, RawDoc as SavedObjectsRawDoc } from './serializ export { SavedObjectsMigrationLogger } from './migrations/core/migration_logger'; -export { SavedObjectsService, SavedObjectsServiceStart } from './saved_objects_service'; +export { + ISavedObjectsService, + SavedObjectsService, + SavedObjectsServiceStart, +} from './saved_objects_service'; export { config } from './saved_objects_config'; diff --git a/src/core/server/saved_objects/saved_objects_service.ts b/src/core/server/saved_objects/saved_objects_service.ts index 5ccb02414d043..961fa0f749d7e 100644 --- a/src/core/server/saved_objects/saved_objects_service.ts +++ b/src/core/server/saved_objects/saved_objects_service.ts @@ -17,7 +17,7 @@ * under the License. */ -import { CoreService } from 'src/core/types'; +import { Service, ServiceType } from 'src/core/types'; import { first } from 'rxjs/operators'; import { SavedObjectsClient, @@ -63,8 +63,9 @@ export interface SavedObjectsSetupDeps { // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface SavedObjectsStartDeps {} -export class SavedObjectsService - implements CoreService { +export type ISavedObjectsService = ServiceType; + +export class SavedObjectsService implements Service { private migrator: KibanaMigrator | undefined; private logger: Logger; private clientProvider: ISavedObjectsClientProvider | undefined; diff --git a/src/core/server/server.ts b/src/core/server/server.ts index ffe910a87385a..cc72ea96af0cd 100644 --- a/src/core/server/server.ts +++ b/src/core/server/server.ts @@ -27,7 +27,7 @@ import { LegacyService } from './legacy'; import { Logger, LoggerFactory } from './logging'; import { UiSettingsService } from './ui_settings'; import { PluginsService, config as pluginsConfig } from './plugins'; -import { SavedObjectsService } from '../server/saved_objects'; +import { SavedObjectsService, ISavedObjectsService } from '../server/saved_objects'; import { config as elasticsearchConfig } from './elasticsearch'; import { config as httpConfig } from './http'; @@ -51,7 +51,7 @@ export class Server { private readonly legacy: LegacyService; private readonly log: Logger; private readonly plugins: PluginsService; - private readonly savedObjects: SavedObjectsService; + private readonly savedObjects: ISavedObjectsService; private readonly uiSettings: UiSettingsService; constructor( diff --git a/src/core/types/core_service.ts b/src/core/types/core_service.ts index b99c419b796ba..8c8a4656e4383 100644 --- a/src/core/types/core_service.ts +++ b/src/core/types/core_service.ts @@ -17,9 +17,41 @@ * under the License. */ -/** @internal */ -export interface CoreService { - setup(...params: any[]): Promise; - start(...params: any[]): Promise; - stop(): Promise; +/** @public */ +export type AnyFunction = (...args: any[]) => any; + +/** @public */ +export interface Service< + TSetup extends AnyFunction = AnyFunction, + TStart extends AnyFunction = AnyFunction +> { + setup: TSetup; + start: TStart; + stop(): void; } + +/** + * Utility type for inferring the concrete type of a {@link Service}. + * + * @remarks + * TSDoc comments will be preserved and accessible in editors when using this utility type. + * + * @example + * ```ts + * // Implement the `Service` interface without having to specify the generics type arguments. + * export class MyService implements Service { + * setup(x: number) { return 3 * x } + * start() {} + * stop() {} + * } + * + * // Use `ServiceType` to export an interface that automatically infers the generic type arguments. + * export type IMyService = ServiceType; + * // ⇒ Service<(x: number) => number, () => void> + * ``` + * + * @public + */ +export type ServiceType> = T extends Service + ? Service + : never;