diff --git a/packages/backend-plugin-api/src/wiring/factories.test.ts b/packages/backend-plugin-api/src/wiring/factories.test.ts index d92ccc1bf6..6cfcdd06cc 100644 --- a/packages/backend-plugin-api/src/wiring/factories.test.ts +++ b/packages/backend-plugin-api/src/wiring/factories.test.ts @@ -44,6 +44,18 @@ describe('createBackendPlugin', () => { // @ts-expect-error expect(plugin({ b: 'b' })).toBeDefined(); }); + + it('should create plugins with optional options', () => { + const plugin = createBackendPlugin({ + id: 'x', + register(_reg, _options?: { a: string }) {}, + }); + expect(plugin).toBeDefined(); + expect(plugin({ a: 'a' })).toBeDefined(); + expect(plugin()).toBeDefined(); + // @ts-expect-error + expect(plugin({ b: 'b' })).toBeDefined(); + }); }); describe('createBackendModule', () => { @@ -61,4 +73,17 @@ describe('createBackendModule', () => { // @ts-expect-error expect(mod({ b: 'b' })).toBeDefined(); }); + + it('should create modules with optional options', () => { + const mod = createBackendModule({ + pluginId: 'x', + moduleId: 'y', + register(_reg, _options?: { a: string }) {}, + }); + expect(mod).toBeDefined(); + expect(mod({ a: 'a' })).toBeDefined(); + 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 index 9b971311d0..1c3a27bb1c 100644 --- a/packages/backend-plugin-api/src/wiring/factories.ts +++ b/packages/backend-plugin-api/src/wiring/factories.ts @@ -42,15 +42,16 @@ export interface BackendPluginConfig { 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 => ({ +): undefined extends TOptions + ? (options?: TOptions) => BackendRegistrable + : (options: TOptions) => BackendRegistrable { + return (options?: TOptions) => ({ id: config.id, - register(register) { - return config.register(register, options); + register(register: BackendInitRegistry) { + return config.register(register, options!); }, }); } @@ -65,16 +66,17 @@ export interface BackendModuleConfig { ): 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 => ({ +): undefined extends TOptions + ? (options?: TOptions) => BackendRegistrable + : (options: TOptions) => BackendRegistrable { + return (options?: TOptions) => ({ id: `${config.pluginId}.${config.moduleId}`, - register(register) { + register(register: BackendInitRegistry) { // TODO: Hide registerExtensionPoint - return config.register(register, options); + return config.register(register, options!); }, }); }