backend-system: initial high-level API surface split

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-06-01 11:24:40 +02:00
parent f19b7e2f68
commit 4ad817262c
6 changed files with 213 additions and 1 deletions
+1 -1
View File
@@ -20,4 +20,4 @@
* @packageDocumentation
*/
export {};
export * from './wiring';
@@ -0,0 +1,52 @@
/*
* Copyright 2022 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.
*/
interface Logger {
log(message: string): void;
child(fields: { [name: string]: string }): Logger;
}
interface ConfigApi {
getString(key: string): string;
}
interface HttpRouterApi {
get(path: string): void;
}
export const loggerApiRef = createServiceRef<Logger>({
id: 'core.logger',
});
export const configApiRef = createServiceRef<ConfigApi>({
id: 'core.config',
});
export const httpRouterApiRef = createServiceRef<HttpRouterApi>({
id: 'core.apiRouter',
});
// export type PluginEnvironment = {
// logger: Logger;
// cache: PluginCacheManager;
// database: PluginDatabaseManager;
// config: Config;
// reader: UrlReader;
// discovery: PluginEndpointDiscovery;
// tokenManager: TokenManager;
// permissions: PermissionEvaluator | PermissionAuthorizer;
// scheduler: PluginTaskScheduler;
// };
@@ -0,0 +1,31 @@
/*
* Copyright 2022 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.
*/
export interface ServiceRef<T> {
id: string;
T: T;
$$ref: 'service';
}
export function createServiceRef<T>(options: { id: string }): ServiceRef<T> {
return {
id: options.id,
get T(): T {
throw Error('NO T');
},
$$ref: 'service', // TODO: declare
};
}
@@ -0,0 +1,17 @@
/*
* Copyright 2022 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.
*/
export { BackendRegistrable } from './types';
@@ -0,0 +1,85 @@
/*
* Copyright 2022 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.
*/
export interface ExtensionPoint<T> {
id: string;
T: T;
$$ref: 'extension-point';
}
export function createExtensionPoint<T>(options: {
id: string;
}): ExtensionPoint<T> {
return {
id: options.id,
get T(): T {
throw Error('NO T');
},
$$ref: 'extension-point', // TODO: declare
};
}
export interface BackendInitRegistry {
registerExtensionPoint<TExtensionPoint>(
ref: ServiceRef<TExtensionPoint>,
impl: TExtensionPoint,
): void;
registerInit<Deps extends { [name in string]: unknown }>(options: {
deps: { [name in keyof Deps]: ServiceRef<Deps[name]> };
init: (deps: Deps) => Promise<void>;
}): void;
}
export interface BackendRegistrable {
id: string;
register(reg: BackendInitRegistry): void;
}
export interface BackendPluginConfig<TOptions> {
id: string;
register(reg: BackendInitRegistry, options: TOptions): void;
}
export function createBackendPlugin<TOptions>(
config: BackendPluginConfig<TOptions>,
): (option: TOptions) => BackendRegistrable {
return options => ({
id: config.id,
register(register) {
return config.register(register, options);
},
});
}
export interface BackendModuleConfig<TOptions> {
pluginId: string;
moduleId: string;
register(
reg: Omit<BackendInitRegistry, 'registerExtensionPoint'>,
options: TOptions,
): void;
}
export function createBackendModule<TOptions>(
config: BackendModuleConfig<TOptions>,
): (option: TOptions) => BackendRegistrable {
return options => ({
id: `${config.pluginId}.${config.moduleId}`,
register(register) {
return config.register(register, options);
},
});
}