dev-utils: remove separate ApiTestRegistry setup

This commit is contained in:
Patrik Oldsberg
2020-09-04 17:23:12 +02:00
parent bcaa188860
commit e7c04aedab
6 changed files with 9 additions and 179 deletions
@@ -1,36 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* 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 * as apiFactories from './apiFactories';
import { ApiTestRegistry, ApiFactory } from '@backstage/core';
describe('apiFactories', () => {
it('should be possible to get an instance of each API', () => {
const registry = new ApiTestRegistry();
const factories: ApiFactory<unknown, unknown, unknown>[] = Object.values(
apiFactories,
);
for (const factory of factories) {
registry.register(factory);
}
for (const factory of factories) {
const api = registry.get(factory.implements);
expect(api).toBeDefined();
}
});
});
@@ -1,108 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* 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 {
alertApiRef,
errorApiRef,
ErrorApiForwarder,
AlertApi,
createApiFactory,
ErrorAlerter,
AlertApiForwarder,
oauthRequestApiRef,
OAuthRequestManager,
UrlPatternDiscovery,
discoveryApiRef,
GoogleAuth,
googleAuthApiRef,
GithubAuth,
githubAuthApiRef,
GitlabAuth,
gitlabAuthApiRef,
Auth0Auth,
auth0AuthApiRef,
} from '@backstage/core';
// TODO(rugvip): We should likely figure out how to reuse all of these between apps
// and plugin serve with minimal boilerplate. For example we might move everything
// to DI, and provide factories for the default implementations, so this just becomes
// a list of things like `[ErrorApiForwarder.factory, AlertApiDialog.factory]`.
export const alertApiFactory = createApiFactory({
implements: alertApiRef,
deps: {},
factory: (): AlertApi => new AlertApiForwarder(),
});
export const errorApiFactory = createApiFactory({
implements: errorApiRef,
deps: { alertApi: alertApiRef },
factory: ({ alertApi }) =>
new ErrorAlerter(alertApi, new ErrorApiForwarder()),
});
export const oauthRequestApiFactory = createApiFactory({
implements: oauthRequestApiRef,
deps: {},
factory: () => new OAuthRequestManager(),
});
export const discoveryApiFactory = createApiFactory({
implements: discoveryApiRef,
deps: {},
factory: () =>
UrlPatternDiscovery.compile(`http://localhost:7000/{{ pluginId }}`),
});
export const googleAuthApiFactory = createApiFactory({
implements: googleAuthApiRef,
deps: { discoveryApi: discoveryApiRef, oauthRequestApi: oauthRequestApiRef },
factory: ({ discoveryApi, oauthRequestApi }) =>
GoogleAuth.create({
discoveryApi,
oauthRequestApi,
}),
});
export const githubAuthApiFactory = createApiFactory({
implements: githubAuthApiRef,
deps: { discoveryApi: discoveryApiRef, oauthRequestApi: oauthRequestApiRef },
factory: ({ discoveryApi, oauthRequestApi }) =>
GithubAuth.create({
discoveryApi,
oauthRequestApi,
}),
});
export const gitlabAuthApiFactory = createApiFactory({
implements: gitlabAuthApiRef,
deps: { discoveryApi: discoveryApiRef, oauthRequestApi: oauthRequestApiRef },
factory: ({ discoveryApi, oauthRequestApi }) =>
GitlabAuth.create({
discoveryApi,
oauthRequestApi,
}),
});
export const auth0AuthApiFactory = createApiFactory({
implements: auth0AuthApiRef,
deps: { discoveryApi: discoveryApiRef, oauthRequestApi: oauthRequestApiRef },
factory: ({ discoveryApi, oauthRequestApi }) =>
Auth0Auth.create({
discoveryApi,
oauthRequestApi,
}),
});
+6 -32
View File
@@ -26,12 +26,10 @@ import {
SidebarSpacer,
ApiFactory,
createPlugin,
ApiTestRegistry,
ApiHolder,
AlertDisplay,
OAuthRequestDialog,
AnyApiFactory,
} from '@backstage/core';
import * as defaultApiFactories from './apiFactories';
import SentimentDissatisfiedIcon from '@material-ui/icons/SentimentDissatisfied';
// TODO(rugvip): export proper plugin type from core that isn't the plugin class
@@ -43,7 +41,7 @@ type BackstagePlugin = ReturnType<typeof createPlugin>;
*/
class DevAppBuilder {
private readonly plugins = new Array<BackstagePlugin>();
private readonly factories = new Array<ApiFactory<any, any, any>>();
private readonly apis = new Array<AnyApiFactory>();
private readonly rootChildren = new Array<ReactNode>();
/**
@@ -57,10 +55,10 @@ class DevAppBuilder {
/**
* Register an API factory to add to the app
*/
registerApiFactory<Api, Impl, Deps>(
factory: ApiFactory<Api, Impl, Deps>,
registerApi<Api, Deps extends { [name in string]: unknown }>(
factory: ApiFactory<Api, Deps>,
): DevAppBuilder {
this.factories.push(factory);
this.apis.push(factory);
return this;
}
@@ -79,7 +77,7 @@ class DevAppBuilder {
*/
build(): ComponentType<{}> {
const app = createApp({
apis: this.setupApiRegistry(this.factories),
apis: this.apis,
plugins: this.plugins,
});
@@ -170,30 +168,6 @@ class DevAppBuilder {
);
}
// Set up an API registry that merges together default implementations with ones provided through config.
private setupApiRegistry(
providedFactories: ApiFactory<any, any, any>[],
): ApiHolder {
const providedApis = new Set(
providedFactories.map(factory => factory.implements),
);
// Exlude any default API factory that we receive a factory for in the config
const defaultFactories = Object.values(defaultApiFactories).filter(
factory => !providedApis.has(factory.implements),
);
const allFactories = [...defaultFactories, ...providedFactories];
// Use a test registry with dependency injection so that the consumer
// can override APIs but still depend on the default implementations.
const registry = new ApiTestRegistry();
for (const factory of allFactories) {
registry.register(factory);
}
return registry;
}
private findPluginPaths(plugins: BackstagePlugin[]) {
const paths = new Array<string>();
+1 -1
View File
@@ -20,7 +20,7 @@ import { plugin, GraphQLEndpoints, graphQlBrowseApiRef } from '../src';
createDevApp()
.registerPlugin(plugin)
.registerApiFactory({
.registerApi({
implements: graphQlBrowseApiRef,
deps: {
errorApi: errorApiRef,
+1 -1
View File
@@ -20,7 +20,7 @@ import { lighthouseApiRef, LighthouseRestApi } from '../src';
createDevApp()
.registerPlugin(plugin)
.registerApiFactory({
.registerApi({
implements: lighthouseApiRef,
deps: {},
factory: () => new LighthouseRestApi('http://localhost:3003'),
+1 -1
View File
@@ -20,7 +20,7 @@ import { TechDocsDevStorageApi } from './api';
import { techdocsStorageApiRef } from '../src';
createDevApp()
.registerApiFactory({
.registerApi({
deps: {},
factory: () =>
new TechDocsDevStorageApi({