backend-app-api: throw error if duplicate service implementations are provided

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-01-03 11:39:44 +01:00
parent 2ba88fe2df
commit 015a6dced6
7 changed files with 142 additions and 45 deletions
@@ -0,0 +1,58 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
coreServices,
createServiceFactory,
} from '@backstage/backend-plugin-api';
import { createBackend } from './CreateBackend';
describe('createBackend', () => {
it('should not throw when overriding a default service implementation', () => {
expect(() =>
createBackend({
services: [
createServiceFactory({
service: coreServices.rootLifecycle,
deps: {},
factory: async () => ({ addShutdownHook: () => {} }),
}),
],
}),
).not.toThrow();
});
it('should throw on duplicate service implementations', () => {
expect(() =>
createBackend({
services: [
createServiceFactory({
service: coreServices.rootLifecycle,
deps: {},
factory: async () => ({ addShutdownHook: () => {} }),
}),
createServiceFactory({
service: coreServices.rootLifecycle,
deps: {},
factory: async () => ({ addShutdownHook: () => {} }),
}),
],
}),
).toThrow(
'Duplicate service implementations provided for core.rootLifecycle',
);
});
});
+23 -15
View File
@@ -35,20 +35,20 @@ import {
import { ServiceFactory } from '@backstage/backend-plugin-api';
export const defaultServiceFactories = [
cacheFactory,
configFactory,
databaseFactory,
discoveryFactory,
loggerFactory,
rootLoggerFactory,
permissionsFactory,
schedulerFactory,
tokenManagerFactory,
urlReaderFactory,
httpRouterFactory,
rootHttpRouterFactory,
lifecycleFactory,
rootLifecycleFactory,
cacheFactory(),
configFactory(),
databaseFactory(),
discoveryFactory(),
loggerFactory(),
rootLoggerFactory(),
permissionsFactory(),
schedulerFactory(),
tokenManagerFactory(),
urlReaderFactory(),
httpRouterFactory(),
rootHttpRouterFactory(),
lifecycleFactory(),
rootLifecycleFactory(),
];
/**
@@ -62,7 +62,15 @@ export interface CreateBackendOptions {
* @public
*/
export function createBackend(options?: CreateBackendOptions): Backend {
const providedServices = (options?.services ?? []).map(sf =>
typeof sf === 'function' ? sf() : sf,
);
const providedIds = new Set(providedServices.map(sf => sf.service.id));
const neededDefaultFactories = defaultServiceFactories.filter(
sf => !providedIds.has(sf.service.id),
);
return createSpecializedBackend({
services: [...defaultServiceFactories, ...(options?.services ?? [])],
services: [...neededDefaultFactories, ...providedServices],
});
}