backend-plugin-api: handle optional options for plugins and modules
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -42,15 +42,16 @@ export interface BackendPluginConfig<TOptions> {
|
||||
register(reg: BackendInitRegistry, options: TOptions): void;
|
||||
}
|
||||
|
||||
// TODO: Make option optional in the returned factory if they are indeed optional
|
||||
/** @public */
|
||||
export function createBackendPlugin<TOptions>(
|
||||
config: BackendPluginConfig<TOptions>,
|
||||
): (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<TOptions> {
|
||||
): void;
|
||||
}
|
||||
|
||||
// TODO: Make option optional in the returned factory if they are indeed optional
|
||||
/** @public */
|
||||
export function createBackendModule<TOptions>(
|
||||
config: BackendModuleConfig<TOptions>,
|
||||
): (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!);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user