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
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-plugin-api': patch
---
Updated `createBackendPlugin` and `createBackendModule` to properly forward lack of options.
+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
@@ -89,7 +89,7 @@ describe('TestBackend', () => {
await startTestBackend({
services: [sf],
features: [testModule({})],
features: [testModule()],
});
expect(testFn).toHaveBeenCalledWith('winning');
+1 -1
View File
@@ -225,7 +225,7 @@ export type CatalogPermissionRule<TParams extends unknown[] = unknown[]> =
PermissionRule<Entity, EntitiesSearchFilter, 'catalog-entity', TParams>;
// @alpha
export const catalogPlugin: (options?: unknown) => BackendFeature;
export const catalogPlugin: (options?: undefined) => BackendFeature;
// @public (undocumented)
export interface CatalogProcessingEngine {
@@ -53,7 +53,7 @@ describe('catalogServiceRef', () => {
await startTestBackend({
services: [mockDiscoveryFactory],
features: [testModule({})],
features: [testModule()],
});
});
});
+1 -1
View File
@@ -556,7 +556,7 @@ export type RunCommandOptions = {
};
// @alpha
export const scaffolderCatalogModule: (options?: unknown) => BackendFeature;
export const scaffolderCatalogModule: (options?: undefined) => BackendFeature;
// @public (undocumented)
export class ScaffolderEntitiesProcessor implements CatalogProcessor {
@@ -24,7 +24,7 @@ describe('ScaffolderCatalogModule', () => {
const extensionPoint = { addProcessor: jest.fn() };
await startTestBackend({
extensionPoints: [[catalogProcessingExtensionPoint, extensionPoint]],
features: [scaffolderCatalogModule({})],
features: [scaffolderCatalogModule()],
});
expect(extensionPoint.addProcessor).toHaveBeenCalledWith(