diff --git a/packages/backend-plugin-api/src/types.ts b/packages/backend-plugin-api/src/types.ts index 6da3bafe17..1bd9d43390 100644 --- a/packages/backend-plugin-api/src/types.ts +++ b/packages/backend-plugin-api/src/types.ts @@ -21,6 +21,14 @@ */ export type MaybeOptions = object | undefined; +/** + * @ignore + */ +export type FactoryFunctionConfig = + | TConfig + | ((options: TOptions) => TConfig) + | (() => TConfig); + /** * Helper type that makes the options argument optional if options are not required. * diff --git a/packages/backend-plugin-api/src/wiring/factories.test.ts b/packages/backend-plugin-api/src/wiring/factories.test.ts index cf67e411d5..dbdf0ead14 100644 --- a/packages/backend-plugin-api/src/wiring/factories.test.ts +++ b/packages/backend-plugin-api/src/wiring/factories.test.ts @@ -32,10 +32,10 @@ describe('createExtensionPoint', () => { describe('createBackendPlugin', () => { it('should create a BackendPlugin', () => { - const plugin = createBackendPlugin({ + const plugin = createBackendPlugin((_options: { a: string }) => ({ id: 'x', - register(_reg, _options: { a: string }) {}, - }); + register() {}, + })); expect(plugin).toBeDefined(); expect(plugin({ a: 'a' })).toBeDefined(); expect(plugin({ a: 'a' }).id).toBe('x'); @@ -46,10 +46,10 @@ describe('createBackendPlugin', () => { }); it('should create plugins with optional options', () => { - const plugin = createBackendPlugin({ + const plugin = createBackendPlugin((_options?: { a: string }) => ({ id: 'x', - register(_reg, _options?: { a: string }) {}, - }); + register() {}, + })); expect(plugin).toBeDefined(); expect(plugin({ a: 'a' })).toBeDefined(); expect(plugin()).toBeDefined(); @@ -73,10 +73,10 @@ describe('createBackendPlugin', () => { interface TestOptions { a: string; } - const plugin = createBackendPlugin({ + const plugin = createBackendPlugin((_options: TestOptions) => ({ id: 'x', - register(_reg, _options: TestOptions) {}, - }); + register() {}, + })); expect(plugin).toBeDefined(); expect(plugin({ a: 'a' })).toBeDefined(); expect(plugin({ a: 'a' }).id).toBe('x'); @@ -90,10 +90,10 @@ describe('createBackendPlugin', () => { interface TestOptions { a: string; } - const plugin = createBackendPlugin({ + const plugin = createBackendPlugin((_options?: TestOptions) => ({ id: 'x', - register(_reg, _options?: TestOptions) {}, - }); + register() {}, + })); expect(plugin).toBeDefined(); expect(plugin({ a: 'a' })).toBeDefined(); expect(plugin()).toBeDefined(); @@ -104,11 +104,11 @@ describe('createBackendPlugin', () => { describe('createBackendModule', () => { it('should create a BackendModule', () => { - const mod = createBackendModule({ + const mod = createBackendModule((_options: { a: string }) => ({ pluginId: 'x', moduleId: 'y', - register(_reg, _options: { a: string }) {}, - }); + register() {}, + })); expect(mod).toBeDefined(); expect(mod({ a: 'a' })).toBeDefined(); expect(mod({ a: 'a' }).id).toBe('x.y'); @@ -119,11 +119,11 @@ describe('createBackendModule', () => { }); it('should create modules with optional options', () => { - const mod = createBackendModule({ + const mod = createBackendModule((_options?: { a: string }) => ({ pluginId: 'x', moduleId: 'y', - register(_reg, _options?: { a: string }) {}, - }); + register() {}, + })); expect(mod).toBeDefined(); expect(mod({ a: 'a' })).toBeDefined(); expect(mod()).toBeDefined(); @@ -148,11 +148,11 @@ describe('createBackendModule', () => { interface TestOptions { a: string; } - const mod = createBackendModule({ + const mod = createBackendModule((_options: TestOptions) => ({ pluginId: 'x', moduleId: 'y', - register(_reg, _options: TestOptions) {}, - }); + register() {}, + })); expect(mod).toBeDefined(); expect(mod({ a: 'a' })).toBeDefined(); expect(mod({ a: 'a' }).id).toBe('x.y'); @@ -166,11 +166,11 @@ describe('createBackendModule', () => { interface TestOptions { a: string; } - const mod = createBackendModule({ + const mod = createBackendModule((_options?: TestOptions) => ({ pluginId: 'x', moduleId: 'y', - register(_reg, _options?: TestOptions) {}, - }); + register() {}, + })); expect(mod).toBeDefined(); expect(mod({ a: 'a' })).toBeDefined(); expect(mod()).toBeDefined(); diff --git a/packages/backend-plugin-api/src/wiring/factories.ts b/packages/backend-plugin-api/src/wiring/factories.ts index 4c384840cd..c9ccc9c0aa 100644 --- a/packages/backend-plugin-api/src/wiring/factories.ts +++ b/packages/backend-plugin-api/src/wiring/factories.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -import { FactoryFunctionWithOptions, MaybeOptions } from '../types'; +import { + FactoryFunctionWithOptions, + MaybeOptions, + FactoryFunctionConfig, +} from '../types'; import { BackendRegistrationPoints, BackendFeature, @@ -43,30 +47,28 @@ export function createExtensionPoint( } /** @public */ -export interface BackendPluginConfig { +export interface BackendPluginConfig { id: string; - register(reg: BackendRegistrationPoints, options: TOptions): void; + register(reg: BackendRegistrationPoints): void; } /** @public */ export function createBackendPlugin( - config: BackendPluginConfig, + config: FactoryFunctionConfig, ): FactoryFunctionWithOptions { - return (options?: TOptions) => ({ - id: config.id, - register(register: BackendRegistrationPoints) { - return config.register(register, options!); - }, - }); + if (typeof config === 'function') { + return config as FactoryFunctionWithOptions; + } + + return () => config; } /** @public */ -export interface BackendModuleConfig { +export interface BackendModuleConfig { pluginId: string; moduleId: string; register( reg: Omit, - options: TOptions, ): void; } @@ -85,13 +87,22 @@ export interface BackendModuleConfig { * The `pluginId` should exactly match the `id` of the plugin that the module extends. */ export function createBackendModule( - config: BackendModuleConfig, + config: FactoryFunctionConfig, ): FactoryFunctionWithOptions { - return (options?: TOptions) => ({ + if (typeof config === 'function') { + return (options?: TOptions) => { + const c = config(options!); + return { + id: `${c.pluginId}.${c.moduleId}`, + register: c.register, + }; + }; + } + return () => ({ id: `${config.pluginId}.${config.moduleId}`, register(register: BackendRegistrationPoints) { // TODO: Hide registerExtensionPoint - return config.register(register, options!); + return config.register(register); }, }); }