diff --git a/.changeset/nice-jars-talk.md b/.changeset/nice-jars-talk.md new file mode 100644 index 0000000000..9b92f67042 --- /dev/null +++ b/.changeset/nice-jars-talk.md @@ -0,0 +1,7 @@ +--- +'@backstage/backend-test-utils': patch +'@backstage/backend-app-api': patch +'@backstage/plugin-catalog-node': patch +--- + +Internal updates of `createServiceFactory` from `@backstage/backend-plugin-api`. diff --git a/.changeset/real-poets-run.md b/.changeset/real-poets-run.md new file mode 100644 index 0000000000..f1bef6ed68 --- /dev/null +++ b/.changeset/real-poets-run.md @@ -0,0 +1,56 @@ +--- +'@backstage/backend-plugin-api': patch +--- + +The `createServiceFactory` function has been updated to no longer use a duplicate callback pattern for plugin scoped services. The outer callback is now replaced by an optional `createRootContext` method. This change was made in order to support TypeScript 4.9, but it also simplifies the API surface a bit, especially for plugin scoped service factories that don't need to create a root context. + +A factory that previously would have looked like this: + +```ts +createServiceFactory({ + service: coreServices.cache, + deps: { + config: coreServices.config, + plugin: coreServices.pluginMetadata, + }, + async factory({ config }) { + const cacheManager = CacheManager.fromConfig(config); + return async ({ plugin }) => { + return cacheManager.forPlugin(plugin.getId()); + }; + }, +}); +``` + +Now instead looks like this: + +```ts +createServiceFactory({ + service: coreServices.cache, + deps: { + config: coreServices.config, + plugin: coreServices.pluginMetadata, + }, + async createRootContext({ config }) { + return CacheManager.fromConfig(config); + }, + async factory({ plugin }, manager) { + return manager.forPlugin(plugin.getId()); + }, +}); +``` + +Although in many cases the `createRootContext` isn't needed, for example: + +```ts +createServiceFactory({ + service: coreServices.logger, + deps: { + rootLogger: coreServices.rootLogger, + plugin: coreServices.pluginMetadata, + }, + async factory({ rootLogger, plugin }) { + return rootLogger.child({ plugin: plugin.getId() }); + }, +}); +```