core-api: make it possible for plugins to include ApiFactories

This commit is contained in:
Patrik Oldsberg
2020-09-04 16:10:47 +02:00
parent c8faa6a73d
commit 1067ae85e7
3 changed files with 20 additions and 0 deletions
+12
View File
@@ -324,6 +324,18 @@ export class PrivateAppImpl implements BackstageApp {
registry.register('default', factory);
}
for (const plugin of this.plugins) {
for (const factory of plugin.getApis()) {
if (!registry.register('default', factory)) {
throw new Error(
`Plugin ${plugin.getId()} tried to register duplicate or forbidden API factory for ${
factory.implements
}`,
);
}
}
}
for (const factory of this.apis) {
if (!registry.register('app', factory)) {
throw new Error(
+6
View File
@@ -24,9 +24,11 @@ import {
} from './types';
import { validateBrowserCompat, validateFlagName } from '../app/FeatureFlags';
import { RouteRef } from '../routing';
import { AnyApiFactory } from '../apis';
export type PluginConfig = {
id: string;
apis?: Iterable<AnyApiFactory>;
register?(hooks: PluginHooks): void;
};
@@ -65,6 +67,10 @@ export class PluginImpl {
return this.config.id;
}
getApis(): Iterable<AnyApiFactory> {
return this.config.apis ?? [];
}
output(): PluginOutput[] {
if (this.storedOutput) {
return this.storedOutput;
+2
View File
@@ -16,6 +16,7 @@
import { ComponentType } from 'react';
import { RouteRef } from '../routing';
import { AnyApiFactory } from '../apis';
export type RouteOptions = {
// Whether the route path must match exactly, defaults to true.
@@ -70,4 +71,5 @@ export type PluginOutput =
export type BackstagePlugin = {
getId(): string;
output(): PluginOutput[];
getApis(): Iterable<AnyApiFactory>;
};