Address PR review comments
- Make ExtensionPointFactoryMiddleware an opaque type via new @internal/backend package using OpaqueType from @internal/opaque - Use options object for createExtensionPointFactoryMiddleware - Make middleware function async (returns Promise<T>) - Remove extensionPointFactoryMiddleware from createBackend, keep only on createSpecializedBackend - Export defaultServiceFactories from @backstage/backend-defaults Signed-off-by: Jack Palmer <jackpalmer@spotify.com>
This commit is contained in:
@@ -2,4 +2,4 @@
|
||||
'@backstage/backend-defaults': patch
|
||||
---
|
||||
|
||||
Added `extensionPointFactoryMiddleware` option to `createBackend()` to reimplement extension point outputs at backend initialization time. Also re-exports `ExtensionPointFactoryMiddleware` type and `createExtensionPointFactoryMiddleware` helper from `@backstage/backend-app-api`.
|
||||
Exported `defaultServiceFactories` to allow use with `createSpecializedBackend` for advanced configuration like `extensionPointFactoryMiddleware`.
|
||||
|
||||
@@ -44,10 +44,10 @@ export interface BackendStartupResult {
|
||||
}
|
||||
|
||||
// @public
|
||||
export function createExtensionPointFactoryMiddleware<T>(
|
||||
extensionPoint: ExtensionPoint<T>,
|
||||
middleware: (original: T) => T,
|
||||
): ExtensionPointFactoryMiddleware;
|
||||
export function createExtensionPointFactoryMiddleware<T>(options: {
|
||||
extensionPoint: ExtensionPoint<T>;
|
||||
middleware: (original: T) => Promise<T>;
|
||||
}): ExtensionPointFactoryMiddleware<T>;
|
||||
|
||||
// @public (undocumented)
|
||||
export function createSpecializedBackend(
|
||||
@@ -63,11 +63,9 @@ export interface CreateSpecializedBackendOptions {
|
||||
}
|
||||
|
||||
// @public
|
||||
export interface ExtensionPointFactoryMiddleware<T = unknown> {
|
||||
export interface ExtensionPointFactoryMiddleware<_T = unknown> {
|
||||
// (undocumented)
|
||||
extensionPoint: ExtensionPoint<T>;
|
||||
// (undocumented)
|
||||
middleware: (original: T) => T;
|
||||
$$type: '@backstage/ExtensionPointFactoryMiddleware';
|
||||
}
|
||||
|
||||
// @public
|
||||
|
||||
@@ -2122,10 +2122,13 @@ describe('BackendInitializer', () => {
|
||||
});
|
||||
|
||||
const init = new BackendInitializer(baseFactories, [
|
||||
createExtensionPointFactoryMiddleware(extensionPoint, original => ({
|
||||
...original,
|
||||
values: [...original.values, 'from-middleware'],
|
||||
})),
|
||||
createExtensionPointFactoryMiddleware({
|
||||
extensionPoint,
|
||||
middleware: async original => ({
|
||||
...original,
|
||||
values: [...original.values, 'from-middleware'],
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
|
||||
init.add(testPlugin);
|
||||
@@ -2170,10 +2173,13 @@ describe('BackendInitializer', () => {
|
||||
});
|
||||
|
||||
const init = new BackendInitializer(baseFactories, [
|
||||
createExtensionPointFactoryMiddleware(extensionPointA, original => ({
|
||||
...original,
|
||||
values: [...original.values, 'wrapped'],
|
||||
})),
|
||||
createExtensionPointFactoryMiddleware({
|
||||
extensionPoint: extensionPointA,
|
||||
middleware: async original => ({
|
||||
...original,
|
||||
values: [...original.values, 'wrapped'],
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
|
||||
init.add(testPlugin);
|
||||
@@ -2215,14 +2221,20 @@ describe('BackendInitializer', () => {
|
||||
});
|
||||
|
||||
const init = new BackendInitializer(baseFactories, [
|
||||
createExtensionPointFactoryMiddleware(extensionPoint, original => ({
|
||||
...original,
|
||||
values: [...original.values, 'first'],
|
||||
})),
|
||||
createExtensionPointFactoryMiddleware(extensionPoint, original => ({
|
||||
...original,
|
||||
values: [...original.values, 'second'],
|
||||
})),
|
||||
createExtensionPointFactoryMiddleware({
|
||||
extensionPoint,
|
||||
middleware: async original => ({
|
||||
...original,
|
||||
values: [...original.values, 'first'],
|
||||
}),
|
||||
}),
|
||||
createExtensionPointFactoryMiddleware({
|
||||
extensionPoint,
|
||||
middleware: async original => ({
|
||||
...original,
|
||||
values: [...original.values, 'second'],
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
|
||||
init.add(testPlugin);
|
||||
@@ -2262,13 +2274,13 @@ describe('BackendInitializer', () => {
|
||||
});
|
||||
|
||||
const init = new BackendInitializer(baseFactories, [
|
||||
createExtensionPointFactoryMiddleware(
|
||||
unregisteredExtensionPoint,
|
||||
original => ({
|
||||
createExtensionPointFactoryMiddleware({
|
||||
extensionPoint: unregisteredExtensionPoint,
|
||||
middleware: async original => ({
|
||||
...original,
|
||||
values: [...original.values, 'never-applied'],
|
||||
}),
|
||||
),
|
||||
}),
|
||||
]);
|
||||
|
||||
init.add(testPlugin);
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
ExtensionPointFactoryMiddleware,
|
||||
ServiceOrExtensionPoint,
|
||||
} from './types';
|
||||
import { OpaqueExtensionPointFactoryMiddleware } from '@internal/backend';
|
||||
// Direct internal import to avoid duplication
|
||||
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
|
||||
import type {
|
||||
@@ -210,8 +211,9 @@ export class BackendInitializer {
|
||||
},
|
||||
});
|
||||
for (const mw of this.#extensionPointFactoryMiddleware) {
|
||||
if (mw.extensionPoint.id === ref.id) {
|
||||
epImpl = (mw.middleware as (original: unknown) => unknown)(epImpl);
|
||||
const internal = OpaqueExtensionPointFactoryMiddleware.toInternal(mw);
|
||||
if (internal.extensionPointId === ref.id) {
|
||||
epImpl = await internal.middleware(epImpl);
|
||||
}
|
||||
}
|
||||
result.set(name, epImpl);
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
ServiceRef,
|
||||
ServiceFactory,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { OpaqueExtensionPointFactoryMiddleware } from '@internal/backend';
|
||||
|
||||
/**
|
||||
* A middleware entry that reimplements a specific extension point's output.
|
||||
@@ -28,9 +29,8 @@ import {
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface ExtensionPointFactoryMiddleware<T = unknown> {
|
||||
extensionPoint: ExtensionPoint<T>;
|
||||
middleware: (original: T) => T;
|
||||
export interface ExtensionPointFactoryMiddleware<_T = unknown> {
|
||||
$$type: '@backstage/ExtensionPointFactoryMiddleware';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,14 +39,14 @@ export interface ExtensionPointFactoryMiddleware<T = unknown> {
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export function createExtensionPointFactoryMiddleware<T>(
|
||||
extensionPoint: ExtensionPoint<T>,
|
||||
middleware: (original: T) => T,
|
||||
): ExtensionPointFactoryMiddleware {
|
||||
return {
|
||||
extensionPoint: extensionPoint as ExtensionPoint<unknown>,
|
||||
middleware: middleware as (original: unknown) => unknown,
|
||||
};
|
||||
export function createExtensionPointFactoryMiddleware<T>(options: {
|
||||
extensionPoint: ExtensionPoint<T>;
|
||||
middleware: (original: T) => Promise<T>;
|
||||
}): ExtensionPointFactoryMiddleware<T> {
|
||||
return OpaqueExtensionPointFactoryMiddleware.createInstance('v1', {
|
||||
extensionPointId: options.extensionPoint.id,
|
||||
middleware: options.middleware as (original: unknown) => Promise<unknown>,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,18 +5,14 @@
|
||||
```ts
|
||||
import { Backend } from '@backstage/backend-app-api';
|
||||
import { BackendFeature } from '@backstage/backend-plugin-api';
|
||||
import { createExtensionPointFactoryMiddleware } from '@backstage/backend-app-api';
|
||||
import { ExtensionPointFactoryMiddleware } from '@backstage/backend-app-api';
|
||||
import { ServiceFactory } from '@backstage/backend-plugin-api';
|
||||
|
||||
// @public (undocumented)
|
||||
export function createBackend(options?: {
|
||||
extensionPointFactoryMiddleware?: ExtensionPointFactoryMiddleware[];
|
||||
}): Backend;
|
||||
export function createBackend(): Backend;
|
||||
|
||||
export { createExtensionPointFactoryMiddleware };
|
||||
// @public (undocumented)
|
||||
export const defaultServiceFactories: ServiceFactory[];
|
||||
|
||||
// @public
|
||||
export const discoveryFeatureLoader: BackendFeature;
|
||||
|
||||
export { ExtensionPointFactoryMiddleware };
|
||||
```
|
||||
|
||||
@@ -14,11 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
Backend,
|
||||
createSpecializedBackend,
|
||||
ExtensionPointFactoryMiddleware,
|
||||
} from '@backstage/backend-app-api';
|
||||
import { Backend, createSpecializedBackend } from '@backstage/backend-app-api';
|
||||
import { ServiceFactory } from '@backstage/backend-plugin-api';
|
||||
import { auditorServiceFactory } from '@backstage/backend-defaults/auditor';
|
||||
import { authServiceFactory } from '@backstage/backend-defaults/auth';
|
||||
import { cacheServiceFactory } from '@backstage/backend-defaults/cache';
|
||||
@@ -46,7 +43,8 @@ import {
|
||||
} from '@backstage/backend-defaults/alpha';
|
||||
import { instanceMetadataServiceFactory } from './alpha/entrypoints/instanceMetadata/instanceMetadataServiceFactory';
|
||||
|
||||
export const defaultServiceFactories = [
|
||||
/** @public */
|
||||
export const defaultServiceFactories: ServiceFactory[] = [
|
||||
auditorServiceFactory,
|
||||
authServiceFactory,
|
||||
cacheServiceFactory,
|
||||
@@ -80,11 +78,6 @@ export const defaultServiceFactories = [
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export function createBackend(options?: {
|
||||
extensionPointFactoryMiddleware?: ExtensionPointFactoryMiddleware[];
|
||||
}): Backend {
|
||||
return createSpecializedBackend({
|
||||
defaultServiceFactories,
|
||||
extensionPointFactoryMiddleware: options?.extensionPointFactoryMiddleware,
|
||||
});
|
||||
export function createBackend(): Backend {
|
||||
return createSpecializedBackend({ defaultServiceFactories });
|
||||
}
|
||||
|
||||
@@ -20,7 +20,5 @@
|
||||
* @packageDocumentation
|
||||
*/
|
||||
|
||||
export { createBackend } from './CreateBackend';
|
||||
export type { ExtensionPointFactoryMiddleware } from '@backstage/backend-app-api';
|
||||
export { createExtensionPointFactoryMiddleware } from '@backstage/backend-app-api';
|
||||
export { createBackend, defaultServiceFactories } from './CreateBackend';
|
||||
export { discoveryFeatureLoader } from './discoveryFeatureLoader';
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname, {
|
||||
rules: {
|
||||
'@backstage/no-top-level-material-ui-4-imports': 'error',
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
# @internal/backend
|
||||
|
||||
This is an internal package used by the other backend packages. It does not get published to NPM, but instead inlined into consuming packages due to the `backstage.inline` flag in `package.json`.
|
||||
@@ -0,0 +1,9 @@
|
||||
apiVersion: backstage.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: internal-backend
|
||||
title: '@internal/backend'
|
||||
spec:
|
||||
lifecycle: experimental
|
||||
type: backstage-node-library
|
||||
owner: framework-maintainers
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "@internal/backend",
|
||||
"version": "0.0.1",
|
||||
"backstage": {
|
||||
"role": "node-library",
|
||||
"inline": true
|
||||
},
|
||||
"private": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/backstage/backstage",
|
||||
"directory": "packages/backend-internal"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"sideEffects": false,
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"lint": "backstage-cli package lint",
|
||||
"test": "backstage-cli package test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "workspace:^"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2026 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { OpaqueExtensionPointFactoryMiddleware } from './wiring';
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2026 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { OpaqueType } from '@internal/opaque';
|
||||
|
||||
export const OpaqueExtensionPointFactoryMiddleware = OpaqueType.create<{
|
||||
public: { $$type: '@backstage/ExtensionPointFactoryMiddleware' };
|
||||
versions: {
|
||||
readonly version: 'v1';
|
||||
readonly extensionPointId: string;
|
||||
readonly middleware: (original: unknown) => Promise<unknown>;
|
||||
};
|
||||
}>({
|
||||
type: '@backstage/ExtensionPointFactoryMiddleware',
|
||||
versions: ['v1'],
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2026 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { OpaqueExtensionPointFactoryMiddleware } from './OpaqueExtensionPointFactoryMiddleware';
|
||||
@@ -9977,6 +9977,14 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@internal/backend@workspace:packages/backend-internal":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@internal/backend@workspace:packages/backend-internal"
|
||||
dependencies:
|
||||
"@backstage/cli": "workspace:^"
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@internal/cli@workspace:packages/cli-internal":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@internal/cli@workspace:packages/cli-internal"
|
||||
|
||||
Reference in New Issue
Block a user