From dc06dc6ab2963e924714161080b60301fd06a7ca Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 3 Jan 2023 12:20:16 +0100 Subject: [PATCH] backend-plugin-api: doc and type cleanup for wiring API Signed-off-by: Patrik Oldsberg --- packages/backend-plugin-api/src/types.ts | 34 +++++++++++++++++ .../src/wiring/factories.ts | 37 +++++++++---------- .../backend-plugin-api/src/wiring/index.ts | 6 ++- 3 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 packages/backend-plugin-api/src/types.ts diff --git a/packages/backend-plugin-api/src/types.ts b/packages/backend-plugin-api/src/types.ts new file mode 100644 index 0000000000..59ecede753 --- /dev/null +++ b/packages/backend-plugin-api/src/types.ts @@ -0,0 +1,34 @@ +/* + * Copyright 2023 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. + */ + +/** + * Base type for options objects that aren't required. + * + * @internal + * @ignore + */ +export type MaybeOptions = object | undefined; + +/** + * Helper type that makes the options argument optional if options are not required. + * + * @internal + * @ignore + */ +export type FactoryFunctionWithOptions = + undefined extends TOptions + ? (options?: TOptions) => TResult + : (options: TOptions) => TResult; diff --git a/packages/backend-plugin-api/src/wiring/factories.ts b/packages/backend-plugin-api/src/wiring/factories.ts index 831e40f9f8..4c384840cd 100644 --- a/packages/backend-plugin-api/src/wiring/factories.ts +++ b/packages/backend-plugin-api/src/wiring/factories.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { FactoryFunctionWithOptions, MaybeOptions } from '../types'; import { BackendRegistrationPoints, BackendFeature, @@ -21,16 +22,21 @@ import { } from './types'; /** @public */ -export function createExtensionPoint(options: { +export interface ExtensionPointConfig { id: string; -}): ExtensionPoint { +} + +/** @public */ +export function createExtensionPoint( + config: ExtensionPointConfig, +): ExtensionPoint { return { - id: options.id, + id: config.id, get T(): T { throw new Error(`tried to read ExtensionPoint.T of ${this}`); }, toString() { - return `extensionPoint{${options.id}}`; + return `extensionPoint{${config.id}}`; }, $$ref: 'extension-point', // TODO: declare }; @@ -43,14 +49,9 @@ export interface BackendPluginConfig { } /** @public */ -export function createBackendPlugin< - TOptions extends object | undefined = undefined, ->(config: { - id: string; - register(reg: BackendRegistrationPoints, options: TOptions): void; -}): undefined extends TOptions - ? (options?: TOptions) => BackendFeature - : (options: TOptions) => BackendFeature { +export function createBackendPlugin( + config: BackendPluginConfig, +): FactoryFunctionWithOptions { return (options?: TOptions) => ({ id: config.id, register(register: BackendRegistrationPoints) { @@ -70,9 +71,11 @@ export interface BackendModuleConfig { } /** + * Creates a new backend module for a given plugin. + * * @public * - * Creates a new backend module for a given plugin. + * @remarks * * The `moduleId` should be equal to the module-specific prefix of the exported name, such * that the full name is `moduleId + PluginId + "Module"`. For example, a GitHub entity @@ -81,13 +84,9 @@ export interface BackendModuleConfig { * * The `pluginId` should exactly match the `id` of the plugin that the module extends. */ -export function createBackendModule< - TOptions extends object | undefined = undefined, ->( +export function createBackendModule( config: BackendModuleConfig, -): undefined extends TOptions - ? (options?: TOptions) => BackendFeature - : (options: TOptions) => BackendFeature { +): FactoryFunctionWithOptions { return (options?: TOptions) => ({ id: `${config.pluginId}.${config.moduleId}`, register(register: BackendRegistrationPoints) { diff --git a/packages/backend-plugin-api/src/wiring/index.ts b/packages/backend-plugin-api/src/wiring/index.ts index 8f85edb2eb..6d8de91080 100644 --- a/packages/backend-plugin-api/src/wiring/index.ts +++ b/packages/backend-plugin-api/src/wiring/index.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -export type { BackendModuleConfig, BackendPluginConfig } from './factories'; +export type { + BackendModuleConfig, + BackendPluginConfig, + ExtensionPointConfig, +} from './factories'; export { createBackendModule, createBackendPlugin,