diff --git a/.changeset/metal-kings-argue.md b/.changeset/metal-kings-argue.md index d3edf502f0..0019139362 100644 --- a/.changeset/metal-kings-argue.md +++ b/.changeset/metal-kings-argue.md @@ -2,4 +2,4 @@ '@backstage/plugin-search-backend-module-catalog': patch --- -Breaking change in the alpha export `searchModuleCatalogCollator`: Moved collator settings from module options into app-config. You are now expected to set up the catalog collator under the `search.collators.catalog` configuration key. +Breaking change in the alpha export `searchModuleCatalogCollator`: Moved collator settings from module options into app-config. You are now expected to set up the catalog collator under the `search.collators.catalog` configuration key. There is also a new `catalogCollatorExtensionPoint` extension point for the module, wherein you can set custom transformers. diff --git a/plugins/search-backend-module-catalog/alpha-api-report.md b/plugins/search-backend-module-catalog/alpha-api-report.md index 3db9fe97a6..658c1aec15 100644 --- a/plugins/search-backend-module-catalog/alpha-api-report.md +++ b/plugins/search-backend-module-catalog/alpha-api-report.md @@ -5,16 +5,18 @@ ```ts import { BackendFeature } from '@backstage/backend-plugin-api'; import { CatalogCollatorEntityTransformer } from '@backstage/plugin-search-backend-module-catalog'; +import { ExtensionPoint } from '@backstage/backend-plugin-api'; // @alpha -export const searchModuleCatalogCollator: ( - options?: SearchModuleCatalogCollatorOptions | undefined, -) => BackendFeature; - -// @alpha -export type SearchModuleCatalogCollatorOptions = { - entityTransformer?: CatalogCollatorEntityTransformer; +export type CatalogCollatorExtensionPoint = { + setEntityTransformer(transformer: CatalogCollatorEntityTransformer): void; }; +// @alpha +export const catalogCollatorExtensionPoint: ExtensionPoint; + +// @alpha +export const searchModuleCatalogCollator: () => BackendFeature; + // (No @packageDocumentation comment for this package) ``` diff --git a/plugins/search-backend-module-catalog/src/alpha.ts b/plugins/search-backend-module-catalog/src/alpha.ts index 8d2a7f4ff1..0edf90fc7f 100644 --- a/plugins/search-backend-module-catalog/src/alpha.ts +++ b/plugins/search-backend-module-catalog/src/alpha.ts @@ -22,6 +22,7 @@ import { coreServices, createBackendModule, + createExtensionPoint, } from '@backstage/backend-plugin-api'; import { catalogServiceRef } from '@backstage/plugin-catalog-node/alpha'; import { @@ -32,57 +33,78 @@ import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-back import { readScheduleConfigOptions } from './collators/config'; /** - * Options for {@link searchModuleCatalogCollator}. + * Options for {@link catalogCollatorExtensionPoint}. * * @alpha */ -export type SearchModuleCatalogCollatorOptions = { +export type CatalogCollatorExtensionPoint = { /** * Allows you to customize how entities are shaped into documents. */ - entityTransformer?: CatalogCollatorEntityTransformer; + setEntityTransformer(transformer: CatalogCollatorEntityTransformer): void; }; +/** + * Extension point for customizing how catalog entities are shaped into + * documents for the search backend, when using + * {@link searchModuleCatalogCollator}. + * + * @alpha + */ +export const catalogCollatorExtensionPoint = + createExtensionPoint({ + id: 'search.catalogCollator.extension', + }); + /** * Search backend module for the Catalog index. * * @alpha */ -export const searchModuleCatalogCollator = createBackendModule( - (options?: SearchModuleCatalogCollatorOptions) => ({ - moduleId: 'catalogCollator', - pluginId: 'search', - register(env) { - env.registerInit({ - deps: { - config: coreServices.rootConfig, - discovery: coreServices.discovery, - tokenManager: coreServices.tokenManager, - scheduler: coreServices.scheduler, - indexRegistry: searchIndexRegistryExtensionPoint, - catalog: catalogServiceRef, - }, - async init({ - config, - discovery, - tokenManager, - scheduler, - indexRegistry, - catalog, - }) { - indexRegistry.addCollator({ - schedule: scheduler.createScheduledTaskRunner( - readScheduleConfigOptions(config), - ), - factory: DefaultCatalogCollatorFactory.fromConfig(config, { - entityTransformer: options?.entityTransformer, - discovery, - tokenManager, - catalogClient: catalog, - }), - }); - }, - }); - }, - }), -); +export const searchModuleCatalogCollator = createBackendModule({ + moduleId: 'catalogCollator', + pluginId: 'search', + register(env) { + let entityTransformer: CatalogCollatorEntityTransformer | undefined; + + env.registerExtensionPoint(catalogCollatorExtensionPoint, { + setEntityTransformer(transformer) { + if (entityTransformer) { + throw new Error('setEntityTransformer can only be called once'); + } + entityTransformer = transformer; + }, + }); + + env.registerInit({ + deps: { + config: coreServices.rootConfig, + discovery: coreServices.discovery, + tokenManager: coreServices.tokenManager, + scheduler: coreServices.scheduler, + indexRegistry: searchIndexRegistryExtensionPoint, + catalog: catalogServiceRef, + }, + async init({ + config, + discovery, + tokenManager, + scheduler, + indexRegistry, + catalog, + }) { + indexRegistry.addCollator({ + schedule: scheduler.createScheduledTaskRunner( + readScheduleConfigOptions(config), + ), + factory: DefaultCatalogCollatorFactory.fromConfig(config, { + entityTransformer, + discovery, + tokenManager, + catalogClient: catalog, + }), + }); + }, + }); + }, +});