From cc19abd08bb749c09df89149878ef1543c65c141 Mon Sep 17 00:00:00 2001 From: Jack Palmer Date: Thu, 30 Mar 2023 10:04:36 +0100 Subject: [PATCH] refactor: Use {{pluginId}} pattern and update docs Signed-off-by: Jack Palmer --- packages/backend-common/config.d.ts | 2 +- .../src/discovery/HostDiscovery.test.ts | 8 ++++---- .../src/discovery/HostDiscovery.ts | 19 ++++++++++++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/backend-common/config.d.ts b/packages/backend-common/config.d.ts index c1542c7131..dc2e2fa133 100644 --- a/packages/backend-common/config.d.ts +++ b/packages/backend-common/config.d.ts @@ -217,7 +217,7 @@ export interface Config { * The target baseUrl to use for the plugin * * Can be either a string or an object with internal and external keys. - * Targets with `{pluginId}` in the url will be replaced with the pluginId. + * Targets with `{{pluginId}}` or `{{ pluginId }} in the url will be replaced with the pluginId. */ target: string | { internal: string; external: string }; /** Array of plugins which use the target baseUrl. */ diff --git a/packages/backend-common/src/discovery/HostDiscovery.test.ts b/packages/backend-common/src/discovery/HostDiscovery.test.ts index a288764b4c..381acbde19 100644 --- a/packages/backend-common/src/discovery/HostDiscovery.test.ts +++ b/packages/backend-common/src/discovery/HostDiscovery.test.ts @@ -163,7 +163,7 @@ describe('HostDiscovery', () => { ); }); - it('replaces {pluginId} in the target', async () => { + it('replaces {{pluginId}} or {{ pluginId }} in the target', async () => { const discovery = HostDiscovery.fromConfig( new ConfigReader({ backend: { @@ -173,13 +173,13 @@ describe('HostDiscovery', () => { discovery: { endpoints: [ { - target: 'http://common-backend:8080/api/{pluginId}', + target: 'http://common-backend:8080/api/{{pluginId}}', plugins: ['catalog', 'docs'], }, { target: { - internal: 'http://scaffolder-internal:8080/api/{pluginId}', - external: 'http://scaffolder-external:8080/api/{pluginId}', + internal: 'http://scaffolder-internal:8080/api/{{ pluginId }}', + external: 'http://scaffolder-external:8080/api/{{ pluginId }}', }, plugins: ['scaffolder'], }, diff --git a/packages/backend-common/src/discovery/HostDiscovery.ts b/packages/backend-common/src/discovery/HostDiscovery.ts index e2bd79824a..83def76aae 100644 --- a/packages/backend-common/src/discovery/HostDiscovery.ts +++ b/packages/backend-common/src/discovery/HostDiscovery.ts @@ -37,7 +37,20 @@ export class HostDiscovery implements PluginEndpointDiscovery { * discovering the external URL, and the `.listen` and `.https` config * for the internal one. * - * Can be overridden by in config by providing a `discovery` section with a `` key. + * Can be overridden by in config by providing a target and corresponding plugin in `discovery.endpoint`. + * eg. + * ```yaml + * discovery: + * endpoints: + * - target: https://internal.example.com/internal-catalog + * plugins: [catalog] + * - target: https://internal.example.com/secure/api/{{pluginId}} + * plugins: [auth, permissions] + * - target: + * internal: https://internal.example.com/search + * external: https://example.com/search + * plugins: [search] + * ``` * * The basePath defaults to `/api`, meaning the default full internal * path for the `catalog` plugin will be `http://localhost:7007/api/catalog`. @@ -92,10 +105,10 @@ export class HostDiscovery implements PluginEndpointDiscovery { } if (typeof target === 'string') { - return target.replace('{pluginId}', pluginId); + return target.replace(/\{\{\s*pluginId\s*\}\}/, pluginId); } - return target[type].replace('{pluginId}', pluginId); + return target[type].replace(/\{\{\s*pluginId\s*\}\}/, pluginId); } async getBaseUrl(pluginId: string): Promise {