From eef91a2558e8ea66b973e5330da94aab7be4d7a3 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 17 Aug 2022 18:11:41 +0200 Subject: [PATCH] backend-plugin-api: simplified ServiceFactory type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .changeset/old-tables-joke.md | 5 ++ packages/backend-app-api/api-report.md | 49 +++++-------------- .../src/wiring/BackendInitializer.ts | 4 +- .../src/wiring/BackstageBackend.ts | 7 +-- packages/backend-app-api/src/wiring/types.ts | 4 +- packages/backend-defaults/api-report.md | 4 +- .../backend-defaults/src/CreateBackend.ts | 8 +-- packages/backend-plugin-api/api-report.md | 45 ++++++++--------- .../src/services/system/index.ts | 1 - .../src/services/system/types.ts | 41 ++++++---------- packages/backend-test-utils/api-report.md | 4 +- .../src/next/wiring/TestBackend.ts | 6 +-- 12 files changed, 70 insertions(+), 108 deletions(-) create mode 100644 .changeset/old-tables-joke.md diff --git a/.changeset/old-tables-joke.md b/.changeset/old-tables-joke.md new file mode 100644 index 0000000000..d458244b3a --- /dev/null +++ b/.changeset/old-tables-joke.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-plugin-api': patch +--- + +Simplified the `ServiceFactory` type and removed `AnyServiceFactory`. diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md index 26c8179771..0f8806870d 100644 --- a/packages/backend-app-api/api-report.md +++ b/packages/backend-app-api/api-report.md @@ -3,7 +3,6 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts -import { AnyServiceFactory } from '@backstage/backend-plugin-api'; import { BackendFeature } from '@backstage/backend-plugin-api'; import { Config } from '@backstage/config'; import { ExtensionPoint } from '@backstage/backend-plugin-api'; @@ -29,14 +28,10 @@ export interface Backend { } // @public (undocumented) -export const cacheFactory: ServiceFactory< - PluginCacheManager, - PluginCacheManager, - {} ->; +export const cacheFactory: ServiceFactory; // @public (undocumented) -export const configFactory: ServiceFactory; +export const configFactory: ServiceFactory; // @public (undocumented) export function createSpecializedBackend( @@ -46,46 +41,28 @@ export function createSpecializedBackend( // @public (undocumented) export interface CreateSpecializedBackendOptions { // (undocumented) - services: AnyServiceFactory[]; + services: ServiceFactory[]; } // @public (undocumented) -export const databaseFactory: ServiceFactory< - PluginDatabaseManager, - PluginDatabaseManager, - {} ->; +export const databaseFactory: ServiceFactory; // @public (undocumented) -export const discoveryFactory: ServiceFactory< - PluginEndpointDiscovery, - PluginEndpointDiscovery, - {} ->; +export const discoveryFactory: ServiceFactory; // @public (undocumented) -export const httpRouterFactory: ServiceFactory< - HttpRouterService, - HttpRouterService, - {} ->; +export const httpRouterFactory: ServiceFactory; // @public (undocumented) -export const loggerFactory: ServiceFactory; +export const loggerFactory: ServiceFactory; // @public (undocumented) export const permissionsFactory: ServiceFactory< - PermissionAuthorizer | PermissionEvaluator, - PermissionAuthorizer | PermissionEvaluator, - {} + PermissionAuthorizer | PermissionEvaluator >; // @public (undocumented) -export const schedulerFactory: ServiceFactory< - PluginTaskScheduler, - PluginTaskScheduler, - {} ->; +export const schedulerFactory: ServiceFactory; // @public (undocumented) export type ServiceOrExtensionPoint = @@ -93,12 +70,8 @@ export type ServiceOrExtensionPoint = | ServiceRef; // @public (undocumented) -export const tokenManagerFactory: ServiceFactory< - TokenManager, - TokenManager, - {} ->; +export const tokenManagerFactory: ServiceFactory; // @public (undocumented) -export const urlReaderFactory: ServiceFactory; +export const urlReaderFactory: ServiceFactory; ``` diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts index afb5250504..355fa6001d 100644 --- a/packages/backend-app-api/src/wiring/BackendInitializer.ts +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -108,7 +108,9 @@ export class BackendInitializer { id: feature.id, provides, consumes: new Set(Object.values(registerOptions.deps)), - deps: registerOptions.deps, + deps: registerOptions.deps as { + [name: string]: ServiceOrExtensionPoint; + }, init: registerOptions.init as BackendRegisterInit['init'], }; }, diff --git a/packages/backend-app-api/src/wiring/BackstageBackend.ts b/packages/backend-app-api/src/wiring/BackstageBackend.ts index 1e09ed77ba..1d76161bfe 100644 --- a/packages/backend-app-api/src/wiring/BackstageBackend.ts +++ b/packages/backend-app-api/src/wiring/BackstageBackend.ts @@ -14,10 +14,7 @@ * limitations under the License. */ -import { - AnyServiceFactory, - BackendFeature, -} from '@backstage/backend-plugin-api'; +import { ServiceFactory, BackendFeature } from '@backstage/backend-plugin-api'; import { BackendInitializer } from './BackendInitializer'; import { ServiceRegistry } from './ServiceRegistry'; import { Backend } from './types'; @@ -26,7 +23,7 @@ export class BackstageBackend implements Backend { #services: ServiceRegistry; #initializer: BackendInitializer; - constructor(apiFactories: AnyServiceFactory[]) { + constructor(apiFactories: ServiceFactory[]) { this.#services = new ServiceRegistry(apiFactories); this.#initializer = new BackendInitializer(this.#services); } diff --git a/packages/backend-app-api/src/wiring/types.ts b/packages/backend-app-api/src/wiring/types.ts index 0ca40b5660..55009edbdf 100644 --- a/packages/backend-app-api/src/wiring/types.ts +++ b/packages/backend-app-api/src/wiring/types.ts @@ -15,7 +15,7 @@ */ import { - AnyServiceFactory, + ServiceFactory, BackendFeature, ExtensionPoint, FactoryFunc, @@ -43,7 +43,7 @@ export interface BackendRegisterInit { * @public */ export interface CreateSpecializedBackendOptions { - services: AnyServiceFactory[]; + services: ServiceFactory[]; } export type ServiceHolder = { diff --git a/packages/backend-defaults/api-report.md b/packages/backend-defaults/api-report.md index 4c0fb80714..b5f1e154bb 100644 --- a/packages/backend-defaults/api-report.md +++ b/packages/backend-defaults/api-report.md @@ -3,8 +3,8 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts -import { AnyServiceFactory } from '@backstage/backend-plugin-api'; import { Backend } from '@backstage/backend-app-api'; +import { ServiceFactory } from '@backstage/backend-plugin-api'; // @public (undocumented) export function createBackend(options?: CreateBackendOptions): Backend; @@ -12,6 +12,6 @@ export function createBackend(options?: CreateBackendOptions): Backend; // @public (undocumented) export interface CreateBackendOptions { // (undocumented) - services?: AnyServiceFactory[]; + services?: ServiceFactory[]; } ``` diff --git a/packages/backend-defaults/src/CreateBackend.ts b/packages/backend-defaults/src/CreateBackend.ts index 7cc116a052..819a77ea11 100644 --- a/packages/backend-defaults/src/CreateBackend.ts +++ b/packages/backend-defaults/src/CreateBackend.ts @@ -28,7 +28,7 @@ import { tokenManagerFactory, urlReaderFactory, } from '@backstage/backend-app-api'; -import { AnyServiceFactory } from '@backstage/backend-plugin-api'; +import { ServiceFactory } from '@backstage/backend-plugin-api'; export const defaultServiceFactories = [ cacheFactory, @@ -47,15 +47,15 @@ export const defaultServiceFactories = [ * @public */ export interface CreateBackendOptions { - services?: AnyServiceFactory[]; + services?: ServiceFactory[]; } /** * @public */ export function createBackend(options?: CreateBackendOptions): Backend { - const services = new Map( - defaultServiceFactories.map(sf => [sf.service.id, sf]), + const services = new Map( + defaultServiceFactories.map(sf => [sf.service.id, sf as ServiceFactory]), ); if (options?.services) { diff --git a/packages/backend-plugin-api/api-report.md b/packages/backend-plugin-api/api-report.md index fa5097be64..01423390e6 100644 --- a/packages/backend-plugin-api/api-report.md +++ b/packages/backend-plugin-api/api-report.md @@ -16,15 +16,6 @@ import { TokenManager } from '@backstage/backend-common'; import { TransportStreamOptions } from 'winston-transport'; import { UrlReader } from '@backstage/backend-common'; -// @public (undocumented) -export type AnyServiceFactory = ServiceFactory< - unknown, - unknown, - { - [key in string]: unknown; - } ->; - // @public (undocumented) export interface BackendFeature { // (undocumented) @@ -101,15 +92,22 @@ export function createExtensionPoint(options: { // @public (undocumented) export function createServiceFactory< - Api, - Impl extends Api, - Deps extends { + TService, + TImpl extends TService, + TDeps extends { [name in string]: unknown; }, ->(factory: ServiceFactory): ServiceFactory; +>(factory: { + service: ServiceRef; + deps: TypesToServiceRef; + factory(deps: DepsToDepFactories): Promise>; +}): ServiceFactory; // @public (undocumented) -export function createServiceRef(options: { id: string }): ServiceRef; +export function createServiceRef(options: { + id: string; + defaultFactory?: (service: ServiceRef) => Promise>; +}): ServiceRef; // @public (undocumented) export const databaseServiceRef: ServiceRef; @@ -168,22 +166,21 @@ export const permissionsServiceRef: ServiceRef< export const schedulerServiceRef: ServiceRef; // @public (undocumented) -export type ServiceFactory< - TApi, - TImpl extends TApi, - TDeps extends { - [name in string]: unknown; - }, -> = { - service: ServiceRef; - deps: TypesToServiceRef; - factory(deps: DepsToDepFactories): Promise>; +export type ServiceFactory = { + service: ServiceRef; + deps: { + [key in string]: ServiceRef; + }; + factory(deps: { + [key in string]: unknown; + }): Promise>; }; // @public export type ServiceRef = { id: string; T: T; + defaultFactory?: (service: ServiceRef) => Promise>; toString(): string; $$ref: 'service'; }; diff --git a/packages/backend-plugin-api/src/services/system/index.ts b/packages/backend-plugin-api/src/services/system/index.ts index eead2297a5..817b0a590f 100644 --- a/packages/backend-plugin-api/src/services/system/index.ts +++ b/packages/backend-plugin-api/src/services/system/index.ts @@ -20,6 +20,5 @@ export type { DepsToDepFactories, FactoryFunc, ServiceFactory, - AnyServiceFactory, } from './types'; export { createServiceRef, createServiceFactory } from './types'; diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 1c6426ecec..dcd047c63d 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -32,9 +32,7 @@ export type ServiceRef = { * The default factory that will be used to create service * instances if not other factory is provided. */ - defaultFactory?: ( - service: ServiceRef, - ) => Promise>; + defaultFactory?: (service: ServiceRef) => Promise>; toString(): string; @@ -53,31 +51,18 @@ export type DepsToDepFactories = { export type FactoryFunc = (pluginId: string) => Promise; /** @public */ -export type ServiceFactory< - TApi, - TImpl extends TApi, - TDeps extends { [name in string]: unknown }, -> = { - service: ServiceRef; - deps: TypesToServiceRef; - factory(deps: DepsToDepFactories): Promise>; +export type ServiceFactory = { + service: ServiceRef; + deps: { [key in string]: ServiceRef }; + factory(deps: { [key in string]: unknown }): Promise>; }; -/** @public */ -export type AnyServiceFactory = ServiceFactory< - unknown, - unknown, - { [key in string]: unknown } ->; - /** * @public */ export function createServiceRef(options: { id: string; - defaultFactory?: ( - service: ServiceRef, - ) => Promise>; + defaultFactory?: (service: ServiceRef) => Promise>; }): ServiceRef { const { id, defaultFactory } = options; return { @@ -97,9 +82,13 @@ export function createServiceRef(options: { * @public */ export function createServiceFactory< - Api, - Impl extends Api, - Deps extends { [name in string]: unknown }, ->(factory: ServiceFactory): ServiceFactory { - return factory; + TService, + TImpl extends TService, + TDeps extends { [name in string]: unknown }, +>(factory: { + service: ServiceRef; + deps: TypesToServiceRef; + factory(deps: DepsToDepFactories): Promise>; +}): ServiceFactory { + return factory as ServiceFactory; } diff --git a/packages/backend-test-utils/api-report.md b/packages/backend-test-utils/api-report.md index b8881e659d..c0f6a43321 100644 --- a/packages/backend-test-utils/api-report.md +++ b/packages/backend-test-utils/api-report.md @@ -3,10 +3,10 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts -import { AnyServiceFactory } from '@backstage/backend-plugin-api'; import { BackendFeature } from '@backstage/backend-plugin-api'; import { ExtensionPoint } from '@backstage/backend-plugin-api'; import { Knex } from 'knex'; +import { ServiceFactory } from '@backstage/backend-plugin-api'; import { ServiceRef } from '@backstage/backend-plugin-api'; // @public (undocumented) @@ -45,7 +45,7 @@ export interface TestBackendOptions< services?: readonly [ ...{ [index in keyof TServices]: - | AnyServiceFactory + | ServiceFactory | [ServiceRef, Partial]; }, ]; diff --git a/packages/backend-test-utils/src/next/wiring/TestBackend.ts b/packages/backend-test-utils/src/next/wiring/TestBackend.ts index b9276fa9b9..98e979793e 100644 --- a/packages/backend-test-utils/src/next/wiring/TestBackend.ts +++ b/packages/backend-test-utils/src/next/wiring/TestBackend.ts @@ -16,7 +16,7 @@ import { createSpecializedBackend } from '@backstage/backend-app-api'; import { - AnyServiceFactory, + ServiceFactory, ServiceRef, createServiceFactory, BackendFeature, @@ -31,7 +31,7 @@ export interface TestBackendOptions< services?: readonly [ ...{ [index in keyof TServices]: - | AnyServiceFactory + | ServiceFactory | [ServiceRef, Partial]; }, ]; @@ -68,7 +68,7 @@ export async function startTestBackend< factory: async () => async () => serviceDef[1], }); } - return serviceDef as AnyServiceFactory; + return serviceDef as ServiceFactory; }); const backend = createSpecializedBackend({