From c49785f00cab0d607fe58ac1a73c2349313d2f5e Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 11 Aug 2023 17:22:16 +0200 Subject: [PATCH] backend-app-api: disallow required service factory options Signed-off-by: Patrik Oldsberg --- .changeset/eighty-swans-sip.md | 5 + .../src/wiring/ServiceRegistry.ts | 4 +- packages/backend-plugin-api/api-report.md | 29 ------ .../src/services/system/types.test.ts | 99 ++----------------- .../src/services/system/types.ts | 33 ------- 5 files changed, 16 insertions(+), 154 deletions(-) create mode 100644 .changeset/eighty-swans-sip.md diff --git a/.changeset/eighty-swans-sip.md b/.changeset/eighty-swans-sip.md new file mode 100644 index 0000000000..240e68c69c --- /dev/null +++ b/.changeset/eighty-swans-sip.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-plugin-api': minor +--- + +**BREAKING**: It is no longer possible to declare options as being required with `createServiceFactory`. diff --git a/packages/backend-app-api/src/wiring/ServiceRegistry.ts b/packages/backend-app-api/src/wiring/ServiceRegistry.ts index b5b55f67cf..61f95ff4a4 100644 --- a/packages/backend-app-api/src/wiring/ServiceRegistry.ts +++ b/packages/backend-app-api/src/wiring/ServiceRegistry.ts @@ -49,10 +49,10 @@ function toInternalServiceFactory( } const pluginMetadataServiceFactory = createServiceFactory( - (options: { pluginId: string }) => ({ + (options?: { pluginId: string }) => ({ service: coreServices.pluginMetadata, deps: {}, - factory: async () => ({ getId: () => options.pluginId }), + factory: async () => ({ getId: () => options?.pluginId! }), }), ); diff --git a/packages/backend-plugin-api/api-report.md b/packages/backend-plugin-api/api-report.md index a1ae6284c6..6dbbee1936 100644 --- a/packages/backend-plugin-api/api-report.md +++ b/packages/backend-plugin-api/api-report.md @@ -152,18 +152,6 @@ export function createServiceFactory< config: (options?: TOpts) => RootServiceFactoryConfig, ): (options?: TOpts) => ServiceFactory; -// @public -export function createServiceFactory< - TService, - TImpl extends TService, - TDeps extends { - [name in string]: ServiceRef; - }, - TOpts extends object | undefined = undefined, ->( - config: (options: TOpts) => RootServiceFactoryConfig, -): (options: TOpts) => ServiceFactory; - // @public export function createServiceFactory< TService, @@ -192,23 +180,6 @@ export function createServiceFactory< ) => PluginServiceFactoryConfig, ): (options?: TOpts) => ServiceFactory; -// @public -export function createServiceFactory< - TService, - TImpl extends TService, - TDeps extends { - [name in string]: ServiceRef; - }, - TContext = undefined, - TOpts extends object | undefined = undefined, ->( - config: - | PluginServiceFactoryConfig - | (( - options: TOpts, - ) => PluginServiceFactoryConfig), -): (options: TOpts) => ServiceFactory; - // @public export function createServiceRef( config: ServiceRefConfig, diff --git a/packages/backend-plugin-api/src/services/system/types.test.ts b/packages/backend-plugin-api/src/services/system/types.test.ts index d3e9b9dd4e..4d0432505e 100644 --- a/packages/backend-plugin-api/src/services/system/types.test.ts +++ b/packages/backend-plugin-api/src/services/system/types.test.ts @@ -26,7 +26,7 @@ interface TestOptions { function unused(..._any: any[]) {} describe('createServiceFactory', () => { - it('should create a sync factory with no options', () => { + it('should create a sync factory', () => { const metaFactory = createServiceFactory({ service: ref, deps: {}, @@ -51,7 +51,7 @@ describe('createServiceFactory', () => { metaFactory(); }); - it('should create a sync root factory with no options', () => { + it('should create a sync root factory', () => { const metaFactory = createServiceFactory({ service: rootDep, deps: {}, @@ -75,7 +75,7 @@ describe('createServiceFactory', () => { metaFactory(); }); - it('should create a factory with no options', () => { + it('should create a factory', () => { const metaFactory = createServiceFactory({ service: ref, deps: {}, @@ -100,7 +100,7 @@ describe('createServiceFactory', () => { metaFactory(); }); - it('should create a factory with optional options', () => { + it('should create a factory with options', () => { const metaFactory = createServiceFactory((_opts?: { x: number }) => ({ service: ref, deps: {}, @@ -124,7 +124,8 @@ describe('createServiceFactory', () => { metaFactory(); }); - it('should create a factory with required options', () => { + it('should not be allowed to require options', () => { + // @ts-expect-error const metaFactory = createServiceFactory((_opts: { x: number }) => ({ service: ref, deps: {}, @@ -134,23 +135,9 @@ describe('createServiceFactory', () => { }, })); expect(metaFactory).toEqual(expect.any(Function)); - - // @ts-expect-error - metaFactory('string'); - // @ts-expect-error - metaFactory({}); - metaFactory({ x: 1 }); - // @ts-expect-error - metaFactory({ x: 1, y: 2 }); - // @ts-expect-error - metaFactory(null); - // @ts-expect-error - metaFactory(undefined); - // @ts-expect-error - metaFactory(); }); - it('should create a factory with optional options as interface', () => { + it('should create a factory with options as interface', () => { const metaFactory = createServiceFactory((_opts?: TestOptions) => ({ service: ref, deps: {}, @@ -174,32 +161,6 @@ describe('createServiceFactory', () => { metaFactory(); }); - it('should create a factory with required options as interface', () => { - const metaFactory = createServiceFactory((_opts: TestOptions) => ({ - service: ref, - deps: {}, - async createRootContext() {}, - async factory() { - return 'x'; - }, - })); - expect(metaFactory).toEqual(expect.any(Function)); - - // @ts-expect-error - metaFactory('string'); - // @ts-expect-error - metaFactory({}); - metaFactory({ x: 1 }); - // @ts-expect-error - metaFactory({ x: 1, y: 2 }); - // @ts-expect-error - metaFactory(null); - // @ts-expect-error - metaFactory(undefined); - // @ts-expect-error - metaFactory(); - }); - it('should create root scoped factory with dependencies', () => { const metaFactory = createServiceFactory({ service: createServiceRef({ id: 'foo', scope: 'root' }), @@ -226,7 +187,7 @@ describe('createServiceFactory', () => { metaFactory(); }); - it('should create root scoped factory with dependencies and optional options', () => { + it('should create root scoped factory with dependencies and options', () => { const metaFactory = createServiceFactory((_options?: TestOptions) => ({ service: createServiceRef({ id: 'foo', scope: 'root' }), deps: { @@ -325,49 +286,7 @@ describe('createServiceFactory', () => { metaFactory(); }); - it('should create factory with required options and dependencies', () => { - const metaFactory = createServiceFactory((_opts: TestOptions) => ({ - service: ref, - deps: { - root: rootDep, - plugin: pluginDep, - }, - async createRootContext({ root }) { - const root1: number = root; - // @ts-expect-error - const root2: string = root; - unused(root1, root2); - return { root }; - }, - async factory({ plugin }, { root }) { - const root1: number = root; - // @ts-expect-error - const root2: string = root; - const plugin3: boolean = plugin; - // @ts-expect-error - const plugin4: number = plugin; - unused(root1, root2, plugin3, plugin4); - return 'x'; - }, - })); - expect(metaFactory).toEqual(expect.any(Function)); - - // @ts-expect-error - metaFactory('string'); - // @ts-expect-error - metaFactory({}); - metaFactory({ x: 1 }); - // @ts-expect-error - metaFactory({ x: 1, y: 2 }); - // @ts-expect-error - metaFactory(null); - // @ts-expect-error - metaFactory(undefined); - // @ts-expect-error - metaFactory(); - }); - - it('should create factory with optional options and dependencies', () => { + it('should create factory with options and dependencies', () => { const metaFactory = createServiceFactory((_opts?: TestOptions) => ({ service: ref, deps: { diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index 190ef7c13e..9465d4a9cb 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -191,20 +191,6 @@ export function createServiceFactory< >( config: (options?: TOpts) => RootServiceFactoryConfig, ): (options?: TOpts) => ServiceFactory; -/** - * Creates a root scoped service factory with required options. - * - * @public - * @param config - The service factory configuration. - */ -export function createServiceFactory< - TService, - TImpl extends TService, - TDeps extends { [name in string]: ServiceRef }, - TOpts extends object | undefined = undefined, ->( - config: (options: TOpts) => RootServiceFactoryConfig, -): (options: TOpts) => ServiceFactory; /** * Creates a plugin scoped service factory without options. * @@ -237,25 +223,6 @@ export function createServiceFactory< options?: TOpts, ) => PluginServiceFactoryConfig, ): (options?: TOpts) => ServiceFactory; -/** - * Creates a plugin scoped service factory with required options. - * - * @public - * @param config - The service factory configuration. - */ -export function createServiceFactory< - TService, - TImpl extends TService, - TDeps extends { [name in string]: ServiceRef }, - TContext = undefined, - TOpts extends object | undefined = undefined, ->( - config: - | PluginServiceFactoryConfig - | (( - options: TOpts, - ) => PluginServiceFactoryConfig), -): (options: TOpts) => ServiceFactory; export function createServiceFactory< TService, TImpl extends TService,