backend-plugin-api: properly forward lack of feature options

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-09-02 16:51:56 +02:00
parent b9d94774fd
commit 91eed37a39
9 changed files with 55 additions and 9 deletions
+14 -2
View File
@@ -72,14 +72,26 @@ export const cacheServiceRef: ServiceRef<PluginCacheManager>;
export const configServiceRef: ServiceRef<Config>;
// @public (undocumented)
export function createBackendModule<TOptions>(
export function createBackendModule<
TOptions extends
| {
[name: string]: unknown;
}
| undefined = undefined,
>(
config: BackendModuleConfig<TOptions>,
): undefined extends TOptions
? (options?: TOptions) => BackendFeature
: (options: TOptions) => BackendFeature;
// @public (undocumented)
export function createBackendPlugin<TOptions>(
export function createBackendPlugin<
TOptions extends
| {
[name: string]: unknown;
}
| undefined = undefined,
>(
config: BackendPluginConfig<TOptions>,
): undefined extends TOptions
? (options?: TOptions) => BackendFeature
@@ -56,6 +56,18 @@ describe('createBackendPlugin', () => {
// @ts-expect-error
expect(plugin({ b: 'b' })).toBeDefined();
});
it('should create plugins without options', () => {
const plugin = createBackendPlugin({
id: 'x',
register() {},
});
expect(plugin).toBeDefined();
// @ts-expect-error
expect(plugin({ a: 'a' })).toBeDefined();
// @ts-expect-error
expect(plugin({})).toBeDefined();
});
});
describe('createBackendModule', () => {
@@ -86,4 +98,17 @@ describe('createBackendModule', () => {
// @ts-expect-error
expect(mod({ b: 'b' })).toBeDefined();
});
it('should create modules without options', () => {
const mod = createBackendModule({
pluginId: 'x',
moduleId: 'y',
register() {},
});
expect(mod).toBeDefined();
// @ts-expect-error
expect(mod({ a: 'a' })).toBeDefined();
// @ts-expect-error
expect(mod({})).toBeDefined();
});
});
@@ -43,7 +43,9 @@ export interface BackendPluginConfig<TOptions> {
}
/** @public */
export function createBackendPlugin<TOptions>(
export function createBackendPlugin<
TOptions extends { [name: string]: unknown } | undefined = undefined,
>(
config: BackendPluginConfig<TOptions>,
): undefined extends TOptions
? (options?: TOptions) => BackendFeature
@@ -67,7 +69,9 @@ export interface BackendModuleConfig<TOptions> {
}
/** @public */
export function createBackendModule<TOptions>(
export function createBackendModule<
TOptions extends { [name: string]: unknown } | undefined = undefined,
>(
config: BackendModuleConfig<TOptions>,
): undefined extends TOptions
? (options?: TOptions) => BackendFeature