changesets: added changeset for createRootContext
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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`.
|
||||
@@ -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() });
|
||||
},
|
||||
});
|
||||
```
|
||||
Reference in New Issue
Block a user