backend-plugin-api: made defaultFactories internal

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-09-02 10:57:50 +02:00
parent c2b7cb1373
commit 2c57c0c499
4 changed files with 28 additions and 9 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/backend-app-api': patch
'@backstage/backend-plugin-api': patch
---
Made `ApiRef.defaultFactory` internal.
@@ -13,12 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
ServiceFactory,
FactoryFunc,
ServiceRef,
} from '@backstage/backend-plugin-api';
/**
* Keep in sync with `@backstage/backend-plugin-api/src/services/system/types.ts`
* @internal
*/
export type InternalServiceRef<T> = ServiceRef<T> & {
__defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
};
export class ServiceRegistry {
readonly #providedFactories: Map<string, ServiceFactory>;
readonly #loadedDefaultFactories: Map<Function, Promise<ServiceFactory>>;
@@ -38,7 +47,7 @@ export class ServiceRegistry {
get<T>(ref: ServiceRef<T>): FactoryFunc<T> | undefined {
let factory = this.#providedFactories.get(ref.id);
const { defaultFactory } = ref;
const { __defaultFactory: defaultFactory } = ref as InternalServiceRef<T>;
if (!factory && !defaultFactory) {
return undefined;
}
@@ -180,7 +180,6 @@ export type ServiceFactory<TService = unknown> = {
export type ServiceRef<T> = {
id: string;
T: T;
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
toString(): string;
$$ref: 'service';
};
@@ -28,15 +28,20 @@ export type ServiceRef<T> = {
*/
T: T;
toString(): string;
$$ref: 'service';
};
/**
* @internal
*/
export type InternalServiceRef<T> = ServiceRef<T> & {
/**
* The default factory that will be used to create service
* instances if no other factory is provided.
*/
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
toString(): string;
$$ref: 'service';
__defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
};
/** @public */
@@ -70,12 +75,12 @@ export function createServiceRef<T>(options: {
get T(): T {
throw new Error(`tried to read ServiceRef.T of ${this}`);
},
defaultFactory,
toString() {
return `serviceRef{${options.id}}`;
},
$$ref: 'service', // TODO: declare
};
__defaultFactory: defaultFactory,
} as InternalServiceRef<T>;
}
/**