Clean up API surface
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
@@ -46,11 +46,15 @@ export function createPublicSignInApp(options?: CreateAppOptions): {
|
||||
};
|
||||
|
||||
// @public (undocumented)
|
||||
export function getAvailableFeatures(config: Config): FrontendFeature[];
|
||||
export function discoverAvailableFeatures(config: Config): {
|
||||
features: FrontendFeature[];
|
||||
};
|
||||
|
||||
// @public (undocumented)
|
||||
export function resolveFeatures(options: {
|
||||
export function resolveAsyncFeatures(options: {
|
||||
config: Config;
|
||||
features?: (FrontendFeature | CreateAppFeatureLoader)[];
|
||||
}): Promise<FrontendFeature[]>;
|
||||
}): Promise<{
|
||||
features: FrontendFeature[];
|
||||
}>;
|
||||
```
|
||||
|
||||
@@ -28,8 +28,8 @@ import {
|
||||
createSpecializedApp,
|
||||
} from '@backstage/frontend-app-api';
|
||||
import appPlugin from '@backstage/plugin-app';
|
||||
import { getAvailableFeatures } from './discovery';
|
||||
import { resolveFeatures } from './resolveFeatures';
|
||||
import { discoverAvailableFeatures } from './discovery';
|
||||
import { resolveAsyncFeatures } from './resolution';
|
||||
|
||||
/**
|
||||
* A source of dynamically loaded frontend features.
|
||||
@@ -89,8 +89,8 @@ export function createApp(options?: CreateAppOptions): {
|
||||
overrideBaseUrlConfigs(defaultConfigLoaderSync()),
|
||||
);
|
||||
|
||||
const discoveredFeatures = getAvailableFeatures(config);
|
||||
const providedFeatures = await resolveFeatures({
|
||||
const { features: discoveredFeatures } = discoverAvailableFeatures(config);
|
||||
const { features: providedFeatures } = await resolveAsyncFeatures({
|
||||
config,
|
||||
features: options?.features,
|
||||
});
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import { createFrontendPlugin } from '@backstage/frontend-plugin-api';
|
||||
import { getAvailableFeatures } from './discovery';
|
||||
import { discoverAvailableFeatures } from './discovery';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
|
||||
const globalSpy = jest.fn();
|
||||
@@ -27,18 +27,18 @@ const config = new ConfigReader({
|
||||
app: { experimental: { packages: 'all' } },
|
||||
});
|
||||
|
||||
describe('getAvailableFeatures', () => {
|
||||
describe('discoverAvailableFeatures', () => {
|
||||
afterEach(jest.resetAllMocks);
|
||||
|
||||
it('should discover nothing with undefined global', () => {
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
});
|
||||
|
||||
it('should discover nothing with empty global', () => {
|
||||
globalSpy.mockReturnValue({
|
||||
modules: [],
|
||||
});
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
});
|
||||
|
||||
it('should discover a plugin', () => {
|
||||
@@ -46,24 +46,26 @@ describe('getAvailableFeatures', () => {
|
||||
globalSpy.mockReturnValue({
|
||||
modules: [{ default: testPlugin }],
|
||||
});
|
||||
expect(getAvailableFeatures(config)).toEqual([testPlugin]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({
|
||||
features: [testPlugin],
|
||||
});
|
||||
});
|
||||
|
||||
it('should ignore garbage', () => {
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: null }] });
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: undefined }] });
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: Symbol() }] });
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: () => {} }] });
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: 0 }] });
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: false }] });
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: true }] });
|
||||
expect(getAvailableFeatures(config)).toEqual([]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({ features: [] });
|
||||
});
|
||||
|
||||
it('should discover multiple plugins', () => {
|
||||
@@ -77,10 +79,8 @@ describe('getAvailableFeatures', () => {
|
||||
{ default: test3Plugin },
|
||||
],
|
||||
});
|
||||
expect(getAvailableFeatures(config)).toEqual([
|
||||
test1Plugin,
|
||||
test2Plugin,
|
||||
test3Plugin,
|
||||
]);
|
||||
expect(discoverAvailableFeatures(config)).toEqual({
|
||||
features: [test1Plugin, test2Plugin, test3Plugin],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -55,30 +55,33 @@ function readPackageDetectionConfig(config: Config) {
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export function getAvailableFeatures(config: Config): FrontendFeature[] {
|
||||
export function discoverAvailableFeatures(config: Config): {
|
||||
features: FrontendFeature[];
|
||||
} {
|
||||
const discovered = (
|
||||
window as { '__@backstage/discovered__'?: DiscoveryGlobal }
|
||||
)['__@backstage/discovered__'];
|
||||
|
||||
const detection = readPackageDetectionConfig(config);
|
||||
if (!detection) {
|
||||
return [];
|
||||
return { features: [] };
|
||||
}
|
||||
|
||||
return (
|
||||
discovered?.modules
|
||||
.filter(({ name }) => {
|
||||
if (detection.exclude?.includes(name)) {
|
||||
return false;
|
||||
}
|
||||
if (detection.include && !detection.include.includes(name)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.map(m => m.default)
|
||||
.filter(isBackstageFeature) ?? []
|
||||
);
|
||||
return {
|
||||
features:
|
||||
discovered?.modules
|
||||
.filter(({ name }) => {
|
||||
if (detection.exclude?.includes(name)) {
|
||||
return false;
|
||||
}
|
||||
if (detection.include && !detection.include.includes(name)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.map(m => m.default)
|
||||
.filter(isBackstageFeature) ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
function isBackstageFeature(obj: unknown): obj is FrontendFeature {
|
||||
|
||||
@@ -26,5 +26,5 @@ export {
|
||||
type CreateAppFeatureLoader,
|
||||
} from './createApp';
|
||||
export { createPublicSignInApp } from './createPublicSignInApp';
|
||||
export { getAvailableFeatures } from './discovery';
|
||||
export { resolveFeatures } from './resolveFeatures';
|
||||
export { discoverAvailableFeatures } from './discovery';
|
||||
export { resolveAsyncFeatures } from './resolution';
|
||||
|
||||
+6
-6
@@ -19,12 +19,12 @@ import {
|
||||
PageBlueprint,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { CreateAppFeatureLoader } from './createApp';
|
||||
import { resolveFeatures } from './resolveFeatures';
|
||||
import { resolveAsyncFeatures } from './resolution';
|
||||
import { mockApis } from '@backstage/test-utils';
|
||||
|
||||
describe('resolveFeatures', () => {
|
||||
describe('resolveAsyncFeatures', () => {
|
||||
it('returns empty array when no features are provided', async () => {
|
||||
const features = await resolveFeatures({
|
||||
const { features } = await resolveAsyncFeatures({
|
||||
config: mockApis.config(),
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@ describe('resolveFeatures', () => {
|
||||
});
|
||||
|
||||
it('returns expected array when features are directly provided', async () => {
|
||||
const features = await resolveFeatures({
|
||||
const { features } = await resolveAsyncFeatures({
|
||||
config: mockApis.config(),
|
||||
features: [
|
||||
createFrontendPlugin({
|
||||
@@ -93,7 +93,7 @@ describe('resolveFeatures', () => {
|
||||
},
|
||||
};
|
||||
|
||||
const features = await resolveFeatures({
|
||||
const { features } = await resolveAsyncFeatures({
|
||||
config: mockApis.config(),
|
||||
features: [loader],
|
||||
});
|
||||
@@ -129,7 +129,7 @@ describe('resolveFeatures', () => {
|
||||
};
|
||||
|
||||
await expect(() =>
|
||||
resolveFeatures({
|
||||
resolveAsyncFeatures({
|
||||
config: mockApis.config(),
|
||||
features: [loader],
|
||||
}),
|
||||
+3
-3
@@ -20,10 +20,10 @@ import { FrontendFeature } from '@backstage/frontend-app-api';
|
||||
import { CreateAppFeatureLoader } from './createApp';
|
||||
|
||||
/** @public */
|
||||
export async function resolveFeatures(options: {
|
||||
export async function resolveAsyncFeatures(options: {
|
||||
config: Config;
|
||||
features?: (FrontendFeature | CreateAppFeatureLoader)[];
|
||||
}): Promise<FrontendFeature[]> {
|
||||
}): Promise<{ features: FrontendFeature[] }> {
|
||||
const features = [];
|
||||
for (const entry of options.features ?? []) {
|
||||
if ('load' in entry) {
|
||||
@@ -41,5 +41,5 @@ export async function resolveFeatures(options: {
|
||||
features.push(entry);
|
||||
}
|
||||
}
|
||||
return features;
|
||||
return { features };
|
||||
}
|
||||
Reference in New Issue
Block a user