From 483e907eafc68d57197694b3a6aa76fd4b585d8a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 13 Jan 2023 17:37:08 +0100 Subject: [PATCH] changesets: added changeset for createRootContext Signed-off-by: Patrik Oldsberg --- .changeset/nice-jars-talk.md | 7 +++++ .changeset/real-poets-run.md | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 .changeset/nice-jars-talk.md create mode 100644 .changeset/real-poets-run.md 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() }); + }, +}); +```