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 0e02f38d3f..4d39ba0f62 100644 --- a/packages/backend-plugin-api/src/services/system/types.test.ts +++ b/packages/backend-plugin-api/src/services/system/types.test.ts @@ -43,13 +43,13 @@ describe('createServiceFactory', () => { it('should create a meta factory with optional options', () => { const ref = createServiceRef({ id: 'x' }); - const metaFactory = createServiceFactory({ + const metaFactory = createServiceFactory((_opts?: { x: number }) => ({ service: ref, deps: {}, - async factory(_deps, _opts?: { x: number }) { + async factory() { return async () => 'x'; }, - }); + })); expect(metaFactory).toEqual(expect.any(Function)); // @ts-expect-error @@ -67,13 +67,13 @@ describe('createServiceFactory', () => { it('should create a meta factory with required options', () => { const ref = createServiceRef({ id: 'x' }); - const metaFactory = createServiceFactory({ + const metaFactory = createServiceFactory((_opts: { x: number }) => ({ service: ref, deps: {}, - async factory(_deps, _opts: { x: number }) { + async factory() { return async () => 'x'; }, - }); + })); expect(metaFactory).toEqual(expect.any(Function)); // @ts-expect-error @@ -96,13 +96,13 @@ describe('createServiceFactory', () => { x: number; } const ref = createServiceRef({ id: 'x' }); - const metaFactory = createServiceFactory({ + const metaFactory = createServiceFactory((_opts?: TestOptions) => ({ service: ref, deps: {}, - async factory(_deps, _opts?: TestOptions) { + async factory() { return async () => 'x'; }, - }); + })); expect(metaFactory).toEqual(expect.any(Function)); // @ts-expect-error @@ -123,13 +123,13 @@ describe('createServiceFactory', () => { x: number; } const ref = createServiceRef({ id: 'x' }); - const metaFactory = createServiceFactory({ + const metaFactory = createServiceFactory((_opts: TestOptions) => ({ service: ref, deps: {}, - async factory(_deps, _opts: TestOptions) { + async factory() { return async () => 'x'; }, - }); + })); expect(metaFactory).toEqual(expect.any(Function)); // @ts-expect-error @@ -149,78 +149,78 @@ describe('createServiceFactory', () => { it('should only allow objects as options', () => { const ref = createServiceRef({ id: 'x' }); - const metaFactory = createServiceFactory({ + // @ts-expect-error + const metaFactory = createServiceFactory((_opts: string) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: string) { + async factory() { return async () => 'x'; }, - }); + })); expect(metaFactory).toEqual(expect.any(Function)); - createServiceFactory({ + // @ts-expect-error + createServiceFactory((_opts: number) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: number) { + async factory() { return async () => 'x'; }, - }); - createServiceFactory({ + })); + // @ts-expect-error + createServiceFactory((_opts: symbol) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: symbol) { + async factory() { return async () => 'x'; }, - }); - createServiceFactory({ + })); + // @ts-expect-error + createServiceFactory((_opts: bigint) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: bigint) { + async factory() { return async () => 'x'; }, - }); - createServiceFactory({ + })); + // @ts-expect-error + createServiceFactory((_opts: 'string') => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: 'string') { + async factory() { return async () => 'x'; }, - }); - createServiceFactory({ + })); + // @ts-expect-error + createServiceFactory((_opts: Array) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: Array) { + async factory() { return async () => 'x'; }, - }); - createServiceFactory({ + })); + // @ts-expect-error + createServiceFactory((_opts: Map) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: Map) { + async factory() { return async () => 'x'; }, - }); - createServiceFactory({ + })); + // @ts-expect-error + createServiceFactory((_opts: Set) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: Set) { + async factory() { return async () => 'x'; }, - }); - createServiceFactory({ + })); + // @ts-expect-error + createServiceFactory((_opts: null) => ({ service: ref, deps: {}, - // @ts-expect-error - async factory(_deps, _opts: null) { + async factory() { return async () => 'x'; }, - }); + })); }); }); diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index ffbbdb5f0c..89528f9fc4 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -import { FactoryFunctionWithOptions, MaybeOptions } from '../../types'; +import { + FactoryFunctionConfig, + FactoryFunctionWithOptions, + MaybeOptions, +} from '../../types'; /** * TODO @@ -144,13 +148,11 @@ export interface ServiceFactoryConfig< TScope extends 'root' | 'plugin', TImpl extends TService, TDeps extends { [name in string]: ServiceRef }, - TOpts extends MaybeOptions = undefined, > { service: ServiceRef; deps: TDeps; factory( deps: ServiceRefsToInstances, - options: TOpts, ): TScope extends 'root' ? Promise : Promise<(deps: ServiceRefsToInstances) => Promise>; @@ -166,15 +168,20 @@ export function createServiceFactory< TDeps extends { [name in string]: ServiceRef }, TOpts extends MaybeOptions = undefined, >( - config: ServiceFactoryConfig, + config: FactoryFunctionConfig< + ServiceFactoryConfig, + TOpts + >, ): FactoryFunctionWithOptions, TOpts> { - return (options?: TOpts) => + if (typeof config === 'function') { + return (opts?: TOpts) => { + const c = config(opts!); + return { ...c, scope: c.service.scope } as ServiceFactory; + }; + } + return () => ({ + ...config, scope: config.service.scope, - service: config.service, - deps: config.deps, - factory(deps: ServiceRefsToInstances) { - return config.factory(deps, options!); - }, } as ServiceFactory); }