diff --git a/packages/backend-plugin-api/src/services/system/index.ts b/packages/backend-plugin-api/src/services/system/index.ts index ca1090fbeb..d00c2da4bd 100644 --- a/packages/backend-plugin-api/src/services/system/index.ts +++ b/packages/backend-plugin-api/src/services/system/index.ts @@ -16,6 +16,7 @@ export type { ServiceRef, + ServiceRefConfig, TypesToServiceRef, ServiceFactory, ServiceFactoryConfig, diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index c2302ae9d4..7c3a9547b6 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -72,48 +72,50 @@ export type ServiceFactory = }; /** @public */ -export function createServiceRef(options: { +export interface ServiceRefConfig { id: string; - scope?: 'plugin'; + scope?: TScope; defaultFactory?: ( - service: ServiceRef, - ) => Promise | (() => ServiceFactory)>; -}): ServiceRef; -/** @public */ -export function createServiceRef(options: { - id: string; - scope: 'root'; - defaultFactory?: ( - service: ServiceRef, - ) => Promise | (() => ServiceFactory)>; -}): ServiceRef; -export function createServiceRef(options: { - id: string; - scope?: 'plugin' | 'root'; - defaultFactory?: - | (( - service: ServiceRef, - ) => Promise | (() => ServiceFactory)>) - | (( - service: ServiceRef, - ) => Promise | (() => ServiceFactory)>); -}): ServiceRef { - const { id, scope = 'plugin', defaultFactory } = options; + service: ServiceRef, + ) => Promise | (() => ServiceFactory)>; +} + +/** + * Creates a new service definition. This overload is used to create plugin scoped services. + * + * @public + */ +export function createServiceRef( + config: ServiceRefConfig, +): ServiceRef; + +/** + * Creates a new service definition. This overload is used to create root scoped services. + * + * @public + */ +export function createServiceRef( + config: ServiceRefConfig, +): ServiceRef; +export function createServiceRef( + config: ServiceRefConfig, +): ServiceRef { + const { id, scope = 'plugin', defaultFactory } = config; return { id, scope, - get T(): T { + get T(): TService { throw new Error(`tried to read ServiceRef.T of ${this}`); }, toString() { - return `serviceRef{${options.id}}`; + return `serviceRef{${config.id}}`; }, $$ref: 'service', // TODO: declare __defaultFactory: defaultFactory, - } as ServiceRef & { + } as ServiceRef & { __defaultFactory?: ( - service: ServiceRef, - ) => Promise | (() => ServiceFactory)>; + service: ServiceRef, + ) => Promise | (() => ServiceFactory)>; }; }