backend-app-api: switch out http router pathPrefix option for getPath + tests

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-01-03 10:16:56 +01:00
parent eaa705f754
commit 0d5c70d1f6
4 changed files with 76 additions and 6 deletions
+1 -1
View File
@@ -2,6 +2,6 @@
'@backstage/backend-app-api': minor
---
**BREAKING**: The `httpRouterFactory` now accepts a `pathPrefix` option rather than `indexPlugin`. To set up custom index path, configure the new `rootHttpRouterFactory` with a custom `indexPath` instead.
**BREAKING**: The `httpRouterFactory` now accepts a `getPath` option rather than `indexPlugin`. To set up custom index path, configure the new `rootHttpRouterFactory` with a custom `indexPath` instead.
Added an implementation for the new `rootHttpRouterServiceRef`.
+1 -1
View File
@@ -71,7 +71,7 @@ export const httpRouterFactory: (
// @public (undocumented)
export type HttpRouterFactoryOptions = {
pathPrefix?: string;
getPath(pluginId: string): string;
};
// @public
@@ -0,0 +1,70 @@
/*
* Copyright 2022 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 {
HttpRouterService,
ServiceFactory,
} from '@backstage/backend-plugin-api';
import { httpRouterFactory } from './httpRouterService';
describe('httpRouterFactory', () => {
it('should register plugin paths', async () => {
const rootHttpRouter = { use: jest.fn() };
const factory = httpRouterFactory() as Exclude<
ServiceFactory<HttpRouterService>,
{ scope: 'root' }
>;
const innerFactory = await factory.factory({ rootHttpRouter });
const handler1 = () => {};
const router1 = await innerFactory({ plugin: { getId: () => 'test1' } });
router1.use(handler1);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(1);
expect(rootHttpRouter.use).toHaveBeenCalledWith('/api/test1', handler1);
const handler2 = () => {};
const router2 = await innerFactory({ plugin: { getId: () => 'test2' } });
router2.use(handler2);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(2);
expect(rootHttpRouter.use).toHaveBeenCalledWith('/api/test2', handler2);
});
it('should use custom path generator', async () => {
const rootHttpRouter = { use: jest.fn() };
const factory = httpRouterFactory({
getPath: id => `/some/${id}/path`,
}) as Exclude<ServiceFactory<HttpRouterService>, { scope: 'root' }>;
const innerFactory = await factory.factory({ rootHttpRouter });
const handler1 = () => {};
const router1 = await innerFactory({ plugin: { getId: () => 'test1' } });
router1.use(handler1);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(1);
expect(rootHttpRouter.use).toHaveBeenCalledWith(
'/some/test1/path',
handler1,
);
const handler2 = () => {};
const router2 = await innerFactory({ plugin: { getId: () => 'test2' } });
router2.use(handler2);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(2);
expect(rootHttpRouter.use).toHaveBeenCalledWith(
'/some/test2/path',
handler2,
);
});
});
@@ -25,9 +25,9 @@ import { Handler } from 'express';
*/
export type HttpRouterFactoryOptions = {
/**
* The path prefix used for each plugin, defaults to `/api/`.
* A callback used to generate the path for each plugin, defaults to `/api/{pluginId}`.
*/
pathPrefix?: string;
getPath(pluginId: string): string;
};
/** @public */
@@ -38,10 +38,10 @@ export const httpRouterFactory = createServiceFactory({
rootHttpRouter: coreServices.rootHttpRouter,
},
async factory({ rootHttpRouter }, options?: HttpRouterFactoryOptions) {
const pathPrefix = options?.pathPrefix ?? '/api/';
const getPath = options?.getPath ?? (id => `/api/${id}`);
return async ({ plugin }) => {
const path = pathPrefix + plugin.getId();
const path = getPath(plugin.getId());
return {
use(handler: Handler) {
rootHttpRouter.use(path, handler);