diff --git a/.changeset/fair-ants-work.md b/.changeset/fair-ants-work.md index ac3a105d52..4cf3c80488 100644 --- a/.changeset/fair-ants-work.md +++ b/.changeset/fair-ants-work.md @@ -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`. diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md index d7150fc9e8..dcf3ced5e8 100644 --- a/packages/backend-app-api/api-report.md +++ b/packages/backend-app-api/api-report.md @@ -71,7 +71,7 @@ export const httpRouterFactory: ( // @public (undocumented) export type HttpRouterFactoryOptions = { - pathPrefix?: string; + getPath(pluginId: string): string; }; // @public diff --git a/packages/backend-app-api/src/services/implementations/httpRouterService.test.ts b/packages/backend-app-api/src/services/implementations/httpRouterService.test.ts new file mode 100644 index 0000000000..89f6db2ac8 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/httpRouterService.test.ts @@ -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, + { 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, { 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, + ); + }); +}); diff --git a/packages/backend-app-api/src/services/implementations/httpRouterService.ts b/packages/backend-app-api/src/services/implementations/httpRouterService.ts index 78d04c988a..f1fc089ea3 100644 --- a/packages/backend-app-api/src/services/implementations/httpRouterService.ts +++ b/packages/backend-app-api/src/services/implementations/httpRouterService.ts @@ -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);