backend-defaults: removed the deprecated getPath option from httpRouterServiceFactory

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-07-10 11:19:10 +02:00
parent 53c8e39dd4
commit 1cb84d7085
9 changed files with 48 additions and 118 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/backend-defaults': minor
'@backstage/backend-app-api': minor
---
**BREAKING**: Removed the depreacted `getPath` option from `httpRouterServiceFactory`, as well as the `HttpRouterFactoryOptions` type.
+4 -8
View File
@@ -146,15 +146,11 @@ export const httpAuthServiceFactory: () => ServiceFactory<
'plugin'
>;
// Warning: (ae-forgotten-export) The symbol "HttpRouterFactoryOptions_2" needs to be exported by the entry point index.d.ts
//
// @public @deprecated (undocumented)
export type HttpRouterFactoryOptions = HttpRouterFactoryOptions_2;
// @public @deprecated
export const httpRouterServiceFactory: (
options?: HttpRouterFactoryOptions_2 | undefined,
) => ServiceFactory<HttpRouterService, 'plugin'>;
export const httpRouterServiceFactory: () => ServiceFactory<
HttpRouterService,
'plugin'
>;
// Warning: (ae-forgotten-export) The symbol "HttpServerCertificateOptions_2" needs to be exported by the entry point index.d.ts
//
@@ -15,16 +15,7 @@
*/
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import {
httpRouterServiceFactory as _httpRouterServiceFactory,
type HttpRouterFactoryOptions as _HttpRouterFactoryOptions,
} from '../../../../../backend-defaults/src/entrypoints/httpRouter/httpRouterServiceFactory';
/**
* @public
* @deprecated Please import from `@backstage/backend-defaults/httpRouter` instead.
*/
export type HttpRouterFactoryOptions = _HttpRouterFactoryOptions;
import { httpRouterServiceFactory as _httpRouterServiceFactory } from '../../../../../backend-defaults/src/entrypoints/httpRouter/httpRouterServiceFactory';
/**
* HTTP route registration for plugins.
@@ -15,6 +15,5 @@
*/
export { httpRouterServiceFactory } from './httpRouterServiceFactory';
export type { HttpRouterFactoryOptions } from './httpRouterServiceFactory';
export { createLifecycleMiddleware } from './createLifecycleMiddleware';
export type { LifecycleMiddlewareOptions } from './createLifecycleMiddleware';
@@ -14,15 +14,11 @@ export function createLifecycleMiddleware(
options: LifecycleMiddlewareOptions,
): RequestHandler;
// @public (undocumented)
export interface HttpRouterFactoryOptions {
getPath?(pluginId: string): string;
}
// @public
export const httpRouterServiceFactory: (
options?: HttpRouterFactoryOptions | undefined,
) => ServiceFactory<HttpRouterService, 'plugin'>;
export const httpRouterServiceFactory: () => ServiceFactory<
HttpRouterService,
'plugin'
>;
// @public
export interface LifecycleMiddlewareOptions {
@@ -52,32 +52,6 @@ describe('httpRouterFactory', () => {
);
});
it('should use custom path generator', async () => {
const rootHttpRouter = mockServices.rootHttpRouter.mock();
const tester = ServiceFactoryTester.from(
httpRouterServiceFactory({
getPath: id => `/some/${id}/path`,
}),
{ dependencies: [rootHttpRouter.factory] },
);
const router1 = await tester.get('test1');
router1.use(() => {});
expect(rootHttpRouter.use).toHaveBeenCalledTimes(1);
expect(rootHttpRouter.use).toHaveBeenCalledWith(
'/some/test1/path',
expect.any(Function),
);
const router2 = await tester.get('test2');
router2.use(() => {});
expect(rootHttpRouter.use).toHaveBeenCalledTimes(2);
expect(rootHttpRouter.use).toHaveBeenCalledWith(
'/some/test2/path',
expect.any(Function),
);
});
describe('auth services', () => {
const pluginSubject = createBackendPlugin({
pluginId: 'test',
@@ -26,16 +26,6 @@ import { createCredentialsBarrier } from './createCredentialsBarrier';
import { createAuthIntegrationRouter } from './createAuthIntegrationRouter';
import { createCookieAuthRefreshMiddleware } from './createCookieAuthRefreshMiddleware';
/**
* @public
*/
export interface HttpRouterFactoryOptions {
/**
* A callback used to generate the path for each plugin, defaults to `/api/{pluginId}`.
*/
getPath?(pluginId: string): string;
}
/**
* HTTP route registration for plugins.
*
@@ -45,57 +35,39 @@ export interface HttpRouterFactoryOptions {
*
* @public
*/
export const httpRouterServiceFactory = createServiceFactory(
(options?: HttpRouterFactoryOptions) => ({
service: coreServices.httpRouter,
initialization: 'always',
deps: {
plugin: coreServices.pluginMetadata,
config: coreServices.rootConfig,
logger: coreServices.logger,
lifecycle: coreServices.lifecycle,
rootHttpRouter: coreServices.rootHttpRouter,
auth: coreServices.auth,
httpAuth: coreServices.httpAuth,
},
async factory({
auth,
export const httpRouterServiceFactory = createServiceFactory({
service: coreServices.httpRouter,
initialization: 'always',
deps: {
plugin: coreServices.pluginMetadata,
config: coreServices.rootConfig,
lifecycle: coreServices.lifecycle,
rootHttpRouter: coreServices.rootHttpRouter,
auth: coreServices.auth,
httpAuth: coreServices.httpAuth,
},
async factory({ auth, httpAuth, config, plugin, rootHttpRouter, lifecycle }) {
const router = PromiseRouter();
rootHttpRouter.use(`/api/${plugin.getId()}`, router);
const credentialsBarrier = createCredentialsBarrier({
httpAuth,
config,
logger,
plugin,
rootHttpRouter,
lifecycle,
}) {
if (options?.getPath) {
logger.warn(
`DEPRECATION WARNING: The 'getPath' option for HttpRouterService is deprecated. The ability to reconfigure the '/api/' path prefix for plugins will be removed in the future.`,
);
}
const getPath = options?.getPath ?? (id => `/api/${id}`);
const path = getPath(plugin.getId());
});
const router = PromiseRouter();
rootHttpRouter.use(path, router);
router.use(createAuthIntegrationRouter({ auth }));
router.use(createLifecycleMiddleware({ lifecycle }));
router.use(credentialsBarrier.middleware);
router.use(createCookieAuthRefreshMiddleware({ auth, httpAuth }));
const credentialsBarrier = createCredentialsBarrier({
httpAuth,
config,
});
router.use(createAuthIntegrationRouter({ auth }));
router.use(createLifecycleMiddleware({ lifecycle }));
router.use(credentialsBarrier.middleware);
router.use(createCookieAuthRefreshMiddleware({ auth, httpAuth }));
return {
use(handler: Handler): void {
router.use(handler);
},
addAuthPolicy(policy: HttpRouterServiceAuthPolicy): void {
credentialsBarrier.addAuthPolicy(policy);
},
};
},
}),
);
return {
use(handler: Handler): void {
router.use(handler);
},
addAuthPolicy(policy: HttpRouterServiceAuthPolicy): void {
credentialsBarrier.addAuthPolicy(policy);
},
};
},
});
@@ -15,6 +15,5 @@
*/
export { httpRouterServiceFactory } from './httpRouterServiceFactory';
export type { HttpRouterFactoryOptions } from './httpRouterServiceFactory';
export { createLifecycleMiddleware } from './createLifecycleMiddleware';
export type { LifecycleMiddlewareOptions } from './createLifecycleMiddleware';
+1 -4
View File
@@ -22,7 +22,6 @@ import { EventsService } from '@backstage/plugin-events-node';
import { ExtendedHttpServer } from '@backstage/backend-app-api';
import { ExtensionPoint } from '@backstage/backend-plugin-api';
import { HttpAuthService } from '@backstage/backend-plugin-api';
import { HttpRouterFactoryOptions } from '@backstage/backend-defaults/httpRouter';
import { HttpRouterService } from '@backstage/backend-plugin-api';
import { IdentityService } from '@backstage/backend-plugin-api';
import { JsonObject } from '@backstage/types';
@@ -221,9 +220,7 @@ export namespace mockServices {
// (undocumented)
export namespace httpRouter {
const // (undocumented)
factory: (
options?: HttpRouterFactoryOptions | undefined,
) => ServiceFactory<HttpRouterService, 'plugin'>;
factory: () => ServiceFactory<HttpRouterService, 'plugin'>;
const // (undocumented)
mock: (
partialImpl?: Partial<HttpRouterService> | undefined,