Remove shared environment code & documentation

Signed-off-by: Philipp Hugenroth <philipph@spotify.com>
This commit is contained in:
Philipp Hugenroth
2023-07-28 14:22:57 +02:00
parent 425b386e13
commit 5cf35d1948
8 changed files with 26 additions and 320 deletions
@@ -1,100 +0,0 @@
/*
* 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 {
createServiceFactory,
createServiceRef,
ServiceFactoryOrFunction,
} from '../services';
import {
createSharedEnvironment,
InternalSharedBackendEnvironment,
} from './createSharedEnvironment';
const fooService = createServiceRef<string>({ id: 'foo', scope: 'root' });
const fooFactory = createServiceFactory({
service: fooService,
deps: {},
async factory() {
return 'foo';
},
});
const barService = createServiceRef<number>({ id: 'bar', scope: 'root' });
const barFactory = createServiceFactory({
service: barService,
deps: {},
async factory() {
return 0xba5;
},
});
describe('createSharedEnvironment', () => {
it('should create an empty shared environment', () => {
const env = createSharedEnvironment({});
expect(env).toBeDefined();
const internalEnv = env() as unknown as InternalSharedBackendEnvironment;
expect(internalEnv).toEqual({
$$type: '@backstage/SharedBackendEnvironment',
version: 'v1',
services: undefined,
});
});
it('should create a shared environment with services', () => {
const env = createSharedEnvironment({
services: [fooFactory, barFactory()],
});
const internalEnv = env() as unknown as InternalSharedBackendEnvironment;
expect(internalEnv.version).toBe('v1');
expect(internalEnv.services?.length).toBe(2);
expect(internalEnv.services?.[0]?.service.id).toBe('foo');
expect(internalEnv.services?.[1]?.service.id).toBe('bar');
});
it('should create a shared environment with options', () => {
const env = createSharedEnvironment((options?: { withFoo?: boolean }) => {
const services = new Array<ServiceFactoryOrFunction>();
if (options?.withFoo) {
services.push(fooFactory());
}
services.push(barFactory);
return { services };
});
const internalEnv1 = env() as unknown as InternalSharedBackendEnvironment;
expect(internalEnv1.version).toBe('v1');
expect(internalEnv1.services?.length).toBe(1);
expect(internalEnv1.services?.[0]?.service.id).toBe('bar');
const internalEnv2 = env({
withFoo: true,
}) as unknown as InternalSharedBackendEnvironment;
expect(internalEnv2.version).toBe('v1');
expect(internalEnv2.services?.length).toBe(2);
expect(internalEnv2.services?.[0]?.service.id).toBe('foo');
expect(internalEnv2.services?.[1]?.service.id).toBe('bar');
});
it('should not allow duplicate service factories', () => {
expect(() =>
createSharedEnvironment({
services: [fooFactory, fooFactory()],
})(),
).toThrow(
"Duplicate service implementations provided in shared environment for 'foo'",
);
});
});
@@ -1,102 +0,0 @@
/*
* 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 { ServiceFactory, ServiceFactoryOrFunction } from '../services';
/**
* The configuration options passed to {@link createSharedEnvironment}.
*
* @public
*/
export interface SharedBackendEnvironmentConfig {
services?: ServiceFactoryOrFunction[];
}
/**
* An opaque type that represents the contents of a shared backend environment.
*
* @public
*/
export interface SharedBackendEnvironment {
$$type: '@backstage/SharedBackendEnvironment';
// NOTE: This type is opaque in order to allow for future API evolution without
// cluttering the external API. For example we might want to add support
// for more powerful callback based backend modifications.
//
// By making this opaque we also ensure that the type doesn't become an input
// type that we need to care about, as it would otherwise be possible to pass
// a custom environment definition to `createBackend`, which we don't want.
}
/**
* This type is NOT supposed to be used by anyone except internally by the
* backend-app-api package.
*
* @internal
*/
export interface InternalSharedBackendEnvironment {
version: 'v1';
services?: ServiceFactory[];
}
/**
* Creates a shared backend environment which can be used to create multiple
* backends.
*
* @public
*/
export function createSharedEnvironment<
TOptions extends [options?: object] = [],
>(
config:
| SharedBackendEnvironmentConfig
| ((...params: TOptions) => SharedBackendEnvironmentConfig),
): (...options: TOptions) => SharedBackendEnvironment {
const configCallback = typeof config === 'function' ? config : () => config;
return (...options) => {
const actualConfig = configCallback(...options);
const services = actualConfig?.services?.map(sf =>
typeof sf === 'function' ? sf() : sf,
);
const exists = new Set<string>();
const duplicates = new Set<string>();
for (const { service } of services ?? []) {
if (exists.has(service.id)) {
duplicates.add(service.id);
} else {
exists.add(service.id);
}
}
if (duplicates.size > 0) {
const dupStr = [...duplicates].map(id => `'${id}'`).join(', ');
throw new Error(
`Duplicate service implementations provided in shared environment for ${dupStr}`,
);
}
// Here to ensure type safety in this internal implementation.
const env: SharedBackendEnvironment & InternalSharedBackendEnvironment = {
$$type: '@backstage/SharedBackendEnvironment',
version: 'v1',
services,
};
return env;
};
}
@@ -14,11 +14,6 @@
* limitations under the License.
*/
export { createSharedEnvironment } from './createSharedEnvironment';
export type {
SharedBackendEnvironment,
SharedBackendEnvironmentConfig,
} from './createSharedEnvironment';
export type {
BackendModuleConfig,
BackendPluginConfig,