backend-defaults: remove options from createBackend
Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
committed by
Patrik Oldsberg
parent
90fdcfb403
commit
7af5f7320f
@@ -22,60 +22,63 @@ 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 () => ({
|
||||
addStartupHook: () => {},
|
||||
addShutdownHook: () => {},
|
||||
}),
|
||||
const backend = createBackend();
|
||||
|
||||
expect(() => {
|
||||
backend.add(
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLifecycle,
|
||||
deps: {},
|
||||
factory: async () => ({
|
||||
addStartupHook: () => {},
|
||||
addShutdownHook: () => {},
|
||||
}),
|
||||
],
|
||||
}),
|
||||
).not.toThrow();
|
||||
}),
|
||||
);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should throw on duplicate service implementations', () => {
|
||||
expect(() =>
|
||||
createBackend({
|
||||
services: [
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLifecycle,
|
||||
deps: {},
|
||||
factory: async () => ({
|
||||
addStartupHook: () => {},
|
||||
addShutdownHook: () => {},
|
||||
}),
|
||||
}),
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLifecycle,
|
||||
deps: {},
|
||||
factory: async () => ({
|
||||
addStartupHook: () => {},
|
||||
addShutdownHook: () => {},
|
||||
}),
|
||||
}),
|
||||
],
|
||||
const backend = createBackend();
|
||||
|
||||
backend.add(
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLifecycle,
|
||||
deps: {},
|
||||
factory: async () => ({
|
||||
addStartupHook: () => {},
|
||||
addShutdownHook: () => {},
|
||||
}),
|
||||
}),
|
||||
).toThrow(
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
backend.add(
|
||||
createServiceFactory({
|
||||
service: coreServices.rootLifecycle,
|
||||
deps: {},
|
||||
factory: async () => ({
|
||||
addStartupHook: () => {},
|
||||
addShutdownHook: () => {},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
}).toThrow(
|
||||
'Duplicate service implementations provided for core.rootLifecycle',
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw when providing a plugin metadata service implementation', () => {
|
||||
const backend = createBackend();
|
||||
|
||||
expect(() =>
|
||||
createBackend({
|
||||
services: [
|
||||
createServiceFactory({
|
||||
service: coreServices.pluginMetadata,
|
||||
deps: {},
|
||||
factory: async () => ({ getId: () => 'test' }),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
backend.add(
|
||||
createServiceFactory({
|
||||
service: coreServices.pluginMetadata,
|
||||
deps: {},
|
||||
factory: async () => ({ getId: () => 'test' }),
|
||||
}),
|
||||
),
|
||||
).toThrow('The core.pluginMetadata service cannot be overridden');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,10 +33,6 @@ import {
|
||||
urlReaderServiceFactory,
|
||||
identityServiceFactory,
|
||||
} from '@backstage/backend-app-api';
|
||||
import {
|
||||
ServiceFactory,
|
||||
ServiceFactoryOrFunction,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
|
||||
export const defaultServiceFactories = [
|
||||
cacheServiceFactory(),
|
||||
@@ -59,27 +55,6 @@ export const defaultServiceFactories = [
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface CreateBackendOptions {
|
||||
services?: ServiceFactoryOrFunction[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export function createBackend(options?: CreateBackendOptions): Backend {
|
||||
const services = new Array<ServiceFactory>();
|
||||
|
||||
// Highest priority: Services passed directly to createBackend
|
||||
const providedServices = (options?.services ?? []).map(sf =>
|
||||
typeof sf === 'function' ? sf() : sf,
|
||||
);
|
||||
services.push(...providedServices);
|
||||
|
||||
// Lowest priority: Default services that are not already provided by environment or directly to createBackend
|
||||
const defaultServices = defaultServiceFactories.filter(
|
||||
sf => !services.some(({ service }) => service.id === sf.service.id),
|
||||
);
|
||||
services.push(...defaultServices);
|
||||
|
||||
return createSpecializedBackend({ services });
|
||||
export function createBackend(): Backend {
|
||||
return createSpecializedBackend({ defaultServiceFactories });
|
||||
}
|
||||
|
||||
@@ -25,5 +25,4 @@
|
||||
// TODO(Rugvip): Remove this once backend-common is no longer used by backend-app-api
|
||||
import '@backstage/backend-common';
|
||||
|
||||
export type { CreateBackendOptions } from './CreateBackend';
|
||||
export { createBackend } from './CreateBackend';
|
||||
|
||||
Reference in New Issue
Block a user