From 4ad817262cd48445bc0260c621405a0fc5621717 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 1 Jun 2022 11:24:40 +0200 Subject: [PATCH] backend-system: initial high-level API surface split Signed-off-by: Patrik Oldsberg --- packages/backend-app-api/src/wiring/types.ts | 27 ++++++ packages/backend-plugin-api/src/index.ts | 2 +- .../src/services/definitions/types.ts | 52 ++++++++++++ .../src/services/system/types.ts | 31 +++++++ .../backend-plugin-api/src/wiring/index.ts | 17 ++++ .../backend-plugin-api/src/wiring/types.ts | 85 +++++++++++++++++++ 6 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 packages/backend-app-api/src/wiring/types.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/types.ts create mode 100644 packages/backend-plugin-api/src/services/system/types.ts create mode 100644 packages/backend-plugin-api/src/wiring/index.ts create mode 100644 packages/backend-plugin-api/src/wiring/types.ts diff --git a/packages/backend-app-api/src/wiring/types.ts b/packages/backend-app-api/src/wiring/types.ts new file mode 100644 index 0000000000..cf600ba1d0 --- /dev/null +++ b/packages/backend-app-api/src/wiring/types.ts @@ -0,0 +1,27 @@ +/* + * 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 BackendApp { + add(registrable: BackendRegistrable): void; +} + +interface CreateBackendOptions { + apis: AnyServiceFactory[]; +} + +export function createBackend(options?: CreateBackendOptions): Backend { + return new BackstageBackend(options?.apis ?? []); +} diff --git a/packages/backend-plugin-api/src/index.ts b/packages/backend-plugin-api/src/index.ts index caef25e2ad..da51896950 100644 --- a/packages/backend-plugin-api/src/index.ts +++ b/packages/backend-plugin-api/src/index.ts @@ -20,4 +20,4 @@ * @packageDocumentation */ -export {}; +export * from './wiring'; diff --git a/packages/backend-plugin-api/src/services/definitions/types.ts b/packages/backend-plugin-api/src/services/definitions/types.ts new file mode 100644 index 0000000000..8e85e1198d --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/types.ts @@ -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({ + id: 'core.logger', +}); + +export const configApiRef = createServiceRef({ + id: 'core.config', +}); + +export const httpRouterApiRef = createServiceRef({ + 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; +// }; diff --git a/packages/backend-plugin-api/src/services/system/types.ts b/packages/backend-plugin-api/src/services/system/types.ts new file mode 100644 index 0000000000..4b6b284a1d --- /dev/null +++ b/packages/backend-plugin-api/src/services/system/types.ts @@ -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 { + id: string; + T: T; + $$ref: 'service'; +} + +export function createServiceRef(options: { id: string }): ServiceRef { + return { + id: options.id, + get T(): T { + throw Error('NO T'); + }, + $$ref: 'service', // TODO: declare + }; +} diff --git a/packages/backend-plugin-api/src/wiring/index.ts b/packages/backend-plugin-api/src/wiring/index.ts new file mode 100644 index 0000000000..259fe2b2ea --- /dev/null +++ b/packages/backend-plugin-api/src/wiring/index.ts @@ -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'; diff --git a/packages/backend-plugin-api/src/wiring/types.ts b/packages/backend-plugin-api/src/wiring/types.ts new file mode 100644 index 0000000000..32a5afba0c --- /dev/null +++ b/packages/backend-plugin-api/src/wiring/types.ts @@ -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 { + id: string; + T: T; + $$ref: 'extension-point'; +} + +export function createExtensionPoint(options: { + id: string; +}): ExtensionPoint { + return { + id: options.id, + get T(): T { + throw Error('NO T'); + }, + $$ref: 'extension-point', // TODO: declare + }; +} + +export interface BackendInitRegistry { + registerExtensionPoint( + ref: ServiceRef, + impl: TExtensionPoint, + ): void; + registerInit(options: { + deps: { [name in keyof Deps]: ServiceRef }; + init: (deps: Deps) => Promise; + }): void; +} + +export interface BackendRegistrable { + id: string; + register(reg: BackendInitRegistry): void; +} + +export interface BackendPluginConfig { + id: string; + register(reg: BackendInitRegistry, options: TOptions): void; +} + +export function createBackendPlugin( + config: BackendPluginConfig, +): (option: TOptions) => BackendRegistrable { + return options => ({ + id: config.id, + register(register) { + return config.register(register, options); + }, + }); +} + +export interface BackendModuleConfig { + pluginId: string; + moduleId: string; + register( + reg: Omit, + options: TOptions, + ): void; +} + +export function createBackendModule( + config: BackendModuleConfig, +): (option: TOptions) => BackendRegistrable { + return options => ({ + id: `${config.pluginId}.${config.moduleId}`, + register(register) { + return config.register(register, options); + }, + }); +}