diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts index d578434170..1c6426ecec 100644 --- a/packages/backend-plugin-api/src/services/system/types.ts +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -28,6 +28,14 @@ export type ServiceRef = { */ T: T; + /** + * The default factory that will be used to create service + * instances if not other factory is provided. + */ + defaultFactory?: ( + service: ServiceRef, + ) => Promise>; + toString(): string; $$ref: 'service'; @@ -65,12 +73,19 @@ export type AnyServiceFactory = ServiceFactory< /** * @public */ -export function createServiceRef(options: { id: string }): ServiceRef { +export function createServiceRef(options: { + id: string; + defaultFactory?: ( + service: ServiceRef, + ) => Promise>; +}): ServiceRef { + const { id, defaultFactory } = options; return { - id: options.id, + id, get T(): T { throw new Error(`tried to read ServiceRef.T of ${this}`); }, + defaultFactory, toString() { return `serviceRef{${options.id}}`; },