backend-app-api: add add method to ServiceRegistry

Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
Vincenzo Scamporlino
2023-08-10 23:57:32 +02:00
committed by Patrik Oldsberg
parent 966e376056
commit 108728f5fe
2 changed files with 10 additions and 1 deletions
@@ -39,7 +39,7 @@ function toInternalServiceFactory<TService, TScope extends 'plugin' | 'root'>(
factory: ServiceFactory<TService, TScope>,
): InternalServiceFactory<TService, TScope> {
const f = factory as InternalServiceFactory<TService, TScope>;
if (f.$$type !== '@backstage/ServiceFactory') {
if (f.$$type !== '@backstage/BackendFeature') {
throw new Error(`Invalid service factory, bad type '${f.$$type}'`);
}
if (f.version !== 'v1') {
@@ -82,6 +82,13 @@ export class ServiceRegistry implements EnumerableServiceHolder {
this.#implementations = new Map();
}
add(serviceFactory: ServiceFactory) {
this.#providedFactories.set(
serviceFactory.service.id,
toInternalServiceFactory(serviceFactory),
);
}
#resolveFactory(
ref: ServiceRef<unknown>,
pluginId: string,
@@ -19,6 +19,7 @@ import {
ExtensionPoint,
ServiceRef,
ServiceFactoryOrFunction,
ServiceFactory,
} from '@backstage/backend-plugin-api';
/**
@@ -39,6 +40,7 @@ export interface CreateSpecializedBackendOptions {
export interface ServiceHolder {
get<T>(api: ServiceRef<T>, pluginId: string): Promise<T> | undefined;
add(serviceFactory: ServiceFactory): void;
}
/**