backend-plugin-api,backend-defaults: add createSharedEnvironment
Co-authored-by: blam <ben@blam.sh> Co-authored-by: Johan Haals <johan.haals@gmail.com> Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -33,7 +33,15 @@ import {
|
||||
urlReaderFactory,
|
||||
identityFactory,
|
||||
} from '@backstage/backend-app-api';
|
||||
import { ServiceFactoryOrFunction } from '@backstage/backend-plugin-api';
|
||||
import {
|
||||
ServiceFactory,
|
||||
ServiceFactoryOrFunction,
|
||||
SharedBackendEnvironment,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
|
||||
// Internal import of the type to avoid needing to export this.
|
||||
// eslint-disable-next-line monorepo/no-internal-import
|
||||
import type { InternalSharedBackendEnvironment } from '@backstage/backend-plugin-api/src/wiring/createSharedEnvironment';
|
||||
|
||||
export const defaultServiceFactories = [
|
||||
cacheFactory(),
|
||||
@@ -57,6 +65,7 @@ export const defaultServiceFactories = [
|
||||
* @public
|
||||
*/
|
||||
export interface CreateBackendOptions {
|
||||
env?: SharedBackendEnvironment;
|
||||
services?: ServiceFactoryOrFunction[];
|
||||
}
|
||||
|
||||
@@ -64,15 +73,33 @@ export interface CreateBackendOptions {
|
||||
* @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,
|
||||
);
|
||||
const providedIds = new Set(providedServices.map(sf => sf.service.id));
|
||||
const neededDefaultFactories = defaultServiceFactories.filter(
|
||||
sf => !providedIds.has(sf.service.id),
|
||||
);
|
||||
services.push(...providedServices);
|
||||
|
||||
return createSpecializedBackend({
|
||||
services: [...neededDefaultFactories, ...providedServices],
|
||||
});
|
||||
// Middle priority: Services from the shared environment
|
||||
if (options?.env) {
|
||||
const env = options.env as unknown as InternalSharedBackendEnvironment;
|
||||
if (env.version !== 'v1') {
|
||||
throw new Error(`Unsupported shared environment version: ${env.version}`);
|
||||
}
|
||||
|
||||
const environmentServices =
|
||||
env.services?.filter(
|
||||
sf => !services.some(({ service }) => sf.service.id === service.id),
|
||||
) ?? [];
|
||||
services.push(...environmentServices);
|
||||
}
|
||||
|
||||
// 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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user