diff --git a/packages/backend-plugin-api/src/wiring/factories.test.ts b/packages/backend-plugin-api/src/wiring/factories.test.ts new file mode 100644 index 0000000000..d92ccc1bf6 --- /dev/null +++ b/packages/backend-plugin-api/src/wiring/factories.test.ts @@ -0,0 +1,64 @@ +/* + * 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. + */ + +import { + createBackendModule, + createBackendPlugin, + createExtensionPoint, +} from './factories'; + +describe('createExtensionPoint', () => { + it('should create an ExtensionPoint', () => { + const extensionPoint = createExtensionPoint({ id: 'x' }); + expect(extensionPoint).toBeDefined(); + expect(extensionPoint.id).toBe('x'); + expect(() => extensionPoint.T).toThrow(); + expect(String(extensionPoint)).toBe('extensionPoint{x}'); + }); +}); + +describe('createBackendPlugin', () => { + it('should create an BackendPlugin', () => { + const plugin = createBackendPlugin({ + id: 'x', + register(_reg, _options: { a: string }) {}, + }); + expect(plugin).toBeDefined(); + expect(plugin({ a: 'a' })).toBeDefined(); + expect(plugin({ a: 'a' }).id).toBe('x'); + // @ts-expect-error + expect(plugin()).toBeDefined(); + // @ts-expect-error + expect(plugin({ b: 'b' })).toBeDefined(); + }); +}); + +describe('createBackendModule', () => { + it('should create an BackendModule', () => { + const mod = createBackendModule({ + pluginId: 'x', + moduleId: 'y', + register(_reg, _options: { a: string }) {}, + }); + expect(mod).toBeDefined(); + expect(mod({ a: 'a' })).toBeDefined(); + expect(mod({ a: 'a' }).id).toBe('x.y'); + // @ts-expect-error + expect(mod()).toBeDefined(); + // @ts-expect-error + expect(mod({ b: 'b' })).toBeDefined(); + }); +}); diff --git a/packages/backend-plugin-api/src/wiring/factories.ts b/packages/backend-plugin-api/src/wiring/factories.ts new file mode 100644 index 0000000000..9b971311d0 --- /dev/null +++ b/packages/backend-plugin-api/src/wiring/factories.ts @@ -0,0 +1,80 @@ +/* + * 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. + */ + +import { + BackendInitRegistry, + BackendRegistrable, + ExtensionPoint, +} from './types'; + +/** @public */ +export function createExtensionPoint(options: { + id: string; +}): ExtensionPoint { + return { + id: options.id, + get T(): T { + throw new Error(`tried to read ExtensionPoint.T of ${this}`); + }, + toString() { + return `extensionPoint{${options.id}}`; + }, + $$ref: 'extension-point', // TODO: declare + }; +} + +/** @public */ +export interface BackendPluginConfig { + id: string; + register(reg: BackendInitRegistry, options: TOptions): void; +} + +// TODO: Make option optional in the returned factory if they are indeed optional +/** @public */ +export function createBackendPlugin( + config: BackendPluginConfig, +): (option: TOptions) => BackendRegistrable { + return options => ({ + id: config.id, + register(register) { + return config.register(register, options); + }, + }); +} + +/** @public */ +export interface BackendModuleConfig { + pluginId: string; + moduleId: string; + register( + reg: Omit, + options: TOptions, + ): void; +} + +// TODO: Make option optional in the returned factory if they are indeed optional +/** @public */ +export function createBackendModule( + config: BackendModuleConfig, +): (option: TOptions) => BackendRegistrable { + return options => ({ + id: `${config.pluginId}.${config.moduleId}`, + register(register) { + // TODO: Hide registerExtensionPoint + return config.register(register, options); + }, + }); +} diff --git a/packages/backend-plugin-api/src/wiring/index.ts b/packages/backend-plugin-api/src/wiring/index.ts index 9340dc8fa9..342a922dbd 100644 --- a/packages/backend-plugin-api/src/wiring/index.ts +++ b/packages/backend-plugin-api/src/wiring/index.ts @@ -14,5 +14,14 @@ * limitations under the License. */ -export type { BackendRegistrable } from './types'; -export * from './types'; +export type { BackendModuleConfig, BackendPluginConfig } from './factories'; +export { + createBackendModule, + createBackendPlugin, + createExtensionPoint, +} from './factories'; +export type { + BackendInitRegistry, + BackendRegistrable, + ExtensionPoint, +} from './types'; diff --git a/packages/backend-plugin-api/src/wiring/types.ts b/packages/backend-plugin-api/src/wiring/types.ts index 2b0bef16f7..65525fd8aa 100644 --- a/packages/backend-plugin-api/src/wiring/types.ts +++ b/packages/backend-plugin-api/src/wiring/types.ts @@ -35,22 +35,6 @@ export type ExtensionPoint = { $$ref: 'extension-point'; }; -/** @public */ -export function createExtensionPoint(options: { - id: string; -}): ExtensionPoint { - return { - id: options.id, - get T(): T { - throw new Error(`tried to read ExtensionPoint.T of ${this}`); - }, - toString() { - return `extensionPoint{${options.id}}`; - }, - $$ref: 'extension-point', // TODO: declare - }; -} - /** @public */ export interface BackendInitRegistry { registerExtensionPoint( @@ -68,46 +52,3 @@ export interface BackendRegistrable { id: string; register(reg: BackendInitRegistry): void; } - -/** @public */ -export interface BackendPluginConfig { - id: string; - register(reg: BackendInitRegistry, options: TOptions): void; -} - -// TODO: Make option optional in the returned factory if they are indeed optional -/** @public */ -export function createBackendPlugin( - config: BackendPluginConfig, -): (option: TOptions) => BackendRegistrable { - return options => ({ - id: config.id, - register(register) { - return config.register(register, options); - }, - }); -} - -/** @public */ -export interface BackendModuleConfig { - pluginId: string; - moduleId: string; - register( - reg: Omit, - options: TOptions, - ): void; -} - -// TODO: Make option optional in the returned factory if they are indeed optional -/** @public */ -export function createBackendModule( - config: BackendModuleConfig, -): (option: TOptions) => BackendRegistrable { - return options => ({ - id: `${config.pluginId}.${config.moduleId}`, - register(register) { - // TODO: Hide registerExtensionPoint - return config.register(register, options); - }, - }); -}