@backstage/core-app-api 1.20.3-next.0 → 1.20.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/config.schema.json +1 -1
- package/dist/apis/implementations/DiscoveryApi/FrontendHostDiscovery.esm.js +7 -3
- package/dist/apis/implementations/DiscoveryApi/FrontendHostDiscovery.esm.js.map +1 -1
- package/dist/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.esm.js +40 -5
- package/dist/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.esm.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @backstage/core-app-api
|
|
2
2
|
|
|
3
|
+
## 1.20.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 3ef5974: Added support for wildcard plugin entries in frontend discovery endpoints, matching the backend discovery behavior.
|
|
8
|
+
- 6172725: Fixed the default fetch API to support discovery endpoints that only define an internal target.
|
|
9
|
+
- Updated dependencies
|
|
10
|
+
- @backstage/ui@0.17.0
|
|
11
|
+
- @backstage/core-plugin-api@1.12.8
|
|
12
|
+
|
|
3
13
|
## 1.20.3-next.0
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/config.schema.json
CHANGED
|
@@ -199,7 +199,7 @@
|
|
|
199
199
|
"items": {
|
|
200
200
|
"type": "string"
|
|
201
201
|
},
|
|
202
|
-
"description": "Array of plugins which use the target baseUrl.",
|
|
202
|
+
"description": "Array of plugins which use the target baseUrl. Use `'*'` to configure a fallback target for all plugins without an explicit target.",
|
|
203
203
|
"visibility": "frontend"
|
|
204
204
|
}
|
|
205
205
|
},
|
|
@@ -17,10 +17,14 @@ class FrontendHostDiscovery {
|
|
|
17
17
|
* internal: https://internal.example.com/search
|
|
18
18
|
* external: https://example.com/search
|
|
19
19
|
* plugins: [search]
|
|
20
|
+
* - target: https://example.com/api/{{pluginId}}
|
|
21
|
+
* plugins: ['*']
|
|
20
22
|
* ```
|
|
21
23
|
*
|
|
22
|
-
* If a plugin is not declared in the config,
|
|
23
|
-
*
|
|
24
|
+
* If a plugin is not declared in the config, discovery will first use the
|
|
25
|
+
* wildcard target, if one is configured, and otherwise fall back to using
|
|
26
|
+
* the baseUrl with the provided `pathPattern` appended. The default path
|
|
27
|
+
* pattern is `"/api/{{ pluginId }}"`.
|
|
24
28
|
*/
|
|
25
29
|
static fromConfig(config, options) {
|
|
26
30
|
const path = options?.pathPattern ?? "/api/{{ pluginId }}";
|
|
@@ -45,7 +49,7 @@ class FrontendHostDiscovery {
|
|
|
45
49
|
this.defaultEndpoint = defaultEndpoint;
|
|
46
50
|
}
|
|
47
51
|
async getBaseUrl(pluginId) {
|
|
48
|
-
const endpoint = this.endpoints.get(pluginId);
|
|
52
|
+
const endpoint = this.endpoints.get(pluginId) ?? this.endpoints.get("*");
|
|
49
53
|
if (endpoint) {
|
|
50
54
|
return endpoint.getBaseUrl(pluginId);
|
|
51
55
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FrontendHostDiscovery.esm.js","sources":["../../../../src/apis/implementations/DiscoveryApi/FrontendHostDiscovery.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Config } from '@backstage/config';\nimport { DiscoveryApi } from '@backstage/core-plugin-api';\nimport { UrlPatternDiscovery } from './UrlPatternDiscovery';\n\n/**\n * FrontendHostDiscovery is a config driven DiscoveryApi implementation.\n * It uses the app-config to determine the url for a plugin.\n *\n * @public\n */\nexport class FrontendHostDiscovery implements DiscoveryApi {\n /**\n * Creates a new FrontendHostDiscovery discovery instance by reading\n * the external target URL from the `discovery.endpoints` config section.\n *\n * eg.\n * ```yaml\n * discovery:\n * endpoints:\n * - target: https://internal.example.com/internal-catalog\n * plugins: [catalog]\n * - target: https://internal.example.com/secure/api/{{pluginId}}\n * plugins: [auth, permissions]\n * - target:\n * internal: https://internal.example.com/search\n * external: https://example.com/search\n * plugins: [search]\n * ```\n *\n * If a plugin is not declared in the config,
|
|
1
|
+
{"version":3,"file":"FrontendHostDiscovery.esm.js","sources":["../../../../src/apis/implementations/DiscoveryApi/FrontendHostDiscovery.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Config } from '@backstage/config';\nimport { DiscoveryApi } from '@backstage/core-plugin-api';\nimport { UrlPatternDiscovery } from './UrlPatternDiscovery';\n\n/**\n * FrontendHostDiscovery is a config driven DiscoveryApi implementation.\n * It uses the app-config to determine the url for a plugin.\n *\n * @public\n */\nexport class FrontendHostDiscovery implements DiscoveryApi {\n /**\n * Creates a new FrontendHostDiscovery discovery instance by reading\n * the external target URL from the `discovery.endpoints` config section.\n *\n * eg.\n * ```yaml\n * discovery:\n * endpoints:\n * - target: https://internal.example.com/internal-catalog\n * plugins: [catalog]\n * - target: https://internal.example.com/secure/api/{{pluginId}}\n * plugins: [auth, permissions]\n * - target:\n * internal: https://internal.example.com/search\n * external: https://example.com/search\n * plugins: [search]\n * - target: https://example.com/api/{{pluginId}}\n * plugins: ['*']\n * ```\n *\n * If a plugin is not declared in the config, discovery will first use the\n * wildcard target, if one is configured, and otherwise fall back to using\n * the baseUrl with the provided `pathPattern` appended. The default path\n * pattern is `\"/api/{{ pluginId }}\"`.\n */\n static fromConfig(config: Config, options?: { pathPattern?: string }) {\n const path = options?.pathPattern ?? '/api/{{ pluginId }}';\n const baseUrl = config.getString('backend.baseUrl');\n\n const endpoints = config\n .getOptionalConfigArray('discovery.endpoints')\n ?.flatMap(e => {\n const target =\n typeof e.get('target') === 'object'\n ? e.getOptionalString('target.external')\n : e.getString('target');\n if (!target) {\n return [];\n }\n const discovery = UrlPatternDiscovery.compile(target);\n return e\n .getStringArray('plugins')\n .map(pluginId => [pluginId, discovery] as const);\n });\n\n return new FrontendHostDiscovery(\n new Map(endpoints),\n UrlPatternDiscovery.compile(`${baseUrl}${path}`),\n );\n }\n\n private readonly endpoints: Map<string, DiscoveryApi>;\n private readonly defaultEndpoint: DiscoveryApi;\n\n private constructor(\n endpoints: Map<string, DiscoveryApi>,\n defaultEndpoint: DiscoveryApi,\n ) {\n this.endpoints = endpoints;\n this.defaultEndpoint = defaultEndpoint;\n }\n\n async getBaseUrl(pluginId: string): Promise<string> {\n const endpoint = this.endpoints.get(pluginId) ?? this.endpoints.get('*');\n if (endpoint) {\n return endpoint.getBaseUrl(pluginId);\n }\n return this.defaultEndpoint.getBaseUrl(pluginId);\n }\n}\n"],"names":[],"mappings":";;AAyBO,MAAM,qBAAA,CAA8C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA0BzD,OAAO,UAAA,CAAW,MAAA,EAAgB,OAAA,EAAoC;AACpE,IAAA,MAAM,IAAA,GAAO,SAAS,WAAA,IAAe,qBAAA;AACrC,IAAA,MAAM,OAAA,GAAU,MAAA,CAAO,SAAA,CAAU,iBA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ACF;;;;"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const PLUGIN_ID_TEMPLATE = /\{\{\s*pluginId\s*\}\}/g;
|
|
1
2
|
class IdentityAuthInjectorFetchMiddleware {
|
|
2
3
|
static create(options) {
|
|
3
4
|
const matcher = buildMatcher(options);
|
|
@@ -17,11 +18,17 @@ class IdentityAuthInjectorFetchMiddleware {
|
|
|
17
18
|
static getDiscoveryUrlPrefixes(config) {
|
|
18
19
|
const endpointConfigs = config.getOptionalConfigArray("discovery.endpoints") || [];
|
|
19
20
|
return endpointConfigs.flatMap((c) => {
|
|
20
|
-
const target =
|
|
21
|
+
const target = getExternalTarget(c);
|
|
22
|
+
if (!target) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
21
25
|
const plugins = c.getStringArray("plugins");
|
|
22
|
-
return plugins.
|
|
23
|
-
(pluginId
|
|
24
|
-
|
|
26
|
+
return plugins.flatMap((pluginId) => {
|
|
27
|
+
if (pluginId === "*") {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
return target.replace(PLUGIN_ID_TEMPLATE, pluginId);
|
|
31
|
+
});
|
|
25
32
|
});
|
|
26
33
|
}
|
|
27
34
|
identityApi;
|
|
@@ -52,15 +59,43 @@ function buildMatcher(options) {
|
|
|
52
59
|
} else if (options.urlPrefixAllowlist) {
|
|
53
60
|
return buildPrefixMatcher(options.urlPrefixAllowlist);
|
|
54
61
|
} else if (options.config) {
|
|
55
|
-
|
|
62
|
+
const prefixMatcher = buildPrefixMatcher([
|
|
56
63
|
options.config.getString("backend.baseUrl"),
|
|
57
64
|
...IdentityAuthInjectorFetchMiddleware.getDiscoveryUrlPrefixes(
|
|
58
65
|
options.config
|
|
59
66
|
)
|
|
60
67
|
]);
|
|
68
|
+
const wildcardMatcher = buildWildcardDiscoveryMatcher(options.config);
|
|
69
|
+
return (url) => prefixMatcher(url) || wildcardMatcher(url);
|
|
61
70
|
}
|
|
62
71
|
return () => false;
|
|
63
72
|
}
|
|
73
|
+
function getExternalTarget(config) {
|
|
74
|
+
return typeof config.get("target") === "object" ? config.getOptionalString("target.external") : config.getString("target");
|
|
75
|
+
}
|
|
76
|
+
function buildWildcardDiscoveryMatcher(config) {
|
|
77
|
+
const matchers = config.getOptionalConfigArray("discovery.endpoints")?.flatMap((endpoint) => {
|
|
78
|
+
if (!endpoint.getStringArray("plugins").includes("*")) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
const target = getExternalTarget(endpoint);
|
|
82
|
+
return target ? [buildUrlPatternMatcher(target)] : [];
|
|
83
|
+
}) ?? [];
|
|
84
|
+
return (url) => matchers.some((matcher) => matcher(url));
|
|
85
|
+
}
|
|
86
|
+
function buildUrlPatternMatcher(pattern) {
|
|
87
|
+
const parts = pattern.replace(/\/$/, "").split(PLUGIN_ID_TEMPLATE);
|
|
88
|
+
let source = escapeRegExp(parts[0]);
|
|
89
|
+
for (let i = 1; i < parts.length; i++) {
|
|
90
|
+
source += i === 1 ? "([^/?#]+)" : "\\1";
|
|
91
|
+
source += escapeRegExp(parts[i]);
|
|
92
|
+
}
|
|
93
|
+
const regex = new RegExp(`^${source}(?:/|$|[?#])`);
|
|
94
|
+
return (url) => regex.test(url);
|
|
95
|
+
}
|
|
96
|
+
function escapeRegExp(value) {
|
|
97
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
98
|
+
}
|
|
64
99
|
function buildPrefixMatcher(prefixes) {
|
|
65
100
|
const trimmedPrefixes = prefixes.map((prefix) => prefix.replace(/\/$/, ""));
|
|
66
101
|
return (url) => trimmedPrefixes.some(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdentityAuthInjectorFetchMiddleware.esm.js","sources":["../../../../src/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { IdentityApi } from '@backstage/core-plugin-api';\nimport { FetchMiddleware } from './types';\n\n/**\n * A fetch middleware, which injects a Backstage token header when the user is\n * signed in.\n */\nexport class IdentityAuthInjectorFetchMiddleware implements FetchMiddleware {\n static create(options: {\n identityApi: IdentityApi;\n config?: Config;\n urlPrefixAllowlist?: string[];\n allowUrl?: (url: string) => boolean;\n header?: {\n name: string;\n value: (backstageToken: string) => string;\n };\n }): IdentityAuthInjectorFetchMiddleware {\n const matcher = buildMatcher(options);\n const headerName = options.header?.name || 'authorization';\n const headerValue = options.header?.value || (token => `Bearer ${token}`);\n\n return new IdentityAuthInjectorFetchMiddleware(\n options.identityApi,\n matcher,\n headerName,\n headerValue,\n );\n }\n\n /**\n * Returns an array of plugin URL prefixes derived from the static `discovery`\n * configuration, to be used as `urlPrefixAllowlist` option of {@link create}.\n */\n static getDiscoveryUrlPrefixes(config: Config): string[] {\n const endpointConfigs =\n config.getOptionalConfigArray('discovery.endpoints') || [];\n return endpointConfigs.flatMap(c => {\n const target =\n typeof c.get('target') === 'object'\n ? c.getString('target.external')\n : c.getString('target');\n const plugins = c.getStringArray('plugins');\n return plugins.map(pluginId =>\n target.replace(/\\{\\{\\s*pluginId\\s*\\}\\}/g, pluginId),\n );\n });\n }\n\n public readonly identityApi: IdentityApi;\n public readonly allowUrl: (url: string) => boolean;\n public readonly headerName: string;\n public readonly headerValue: (pluginId: string) => string;\n\n constructor(\n identityApi: IdentityApi,\n allowUrl: (url: string) => boolean,\n headerName: string,\n headerValue: (pluginId: string) => string,\n ) {\n this.identityApi = identityApi;\n this.allowUrl = allowUrl;\n this.headerName = headerName;\n this.headerValue = headerValue;\n }\n\n apply(next: typeof fetch): typeof fetch {\n return async (input, init) => {\n // Skip this middleware if the header already exists, or if the URL\n // doesn't match any of the allowlist items, or if there was no token.\n // NOTE(freben): The \"as any\" casts here and below are because of subtle\n // undici type differences that happened in a node types bump. Those are\n // immaterial to the code at hand at runtime, as the global fetch and\n // Request are always taken from the same place.\n const request = new Request(input as any, init);\n const { token } = await this.identityApi.getCredentials();\n if (\n request.headers.get(this.headerName) ||\n typeof token !== 'string' ||\n !token ||\n !this.allowUrl(request.url)\n ) {\n return next(input as any, init);\n }\n\n request.headers.set(this.headerName, this.headerValue(token));\n return next(request);\n };\n }\n}\n\nfunction buildMatcher(options: {\n config?: Config;\n urlPrefixAllowlist?: string[];\n allowUrl?: (url: string) => boolean;\n}): (url: string) => boolean {\n if (options.allowUrl) {\n return options.allowUrl;\n } else if (options.urlPrefixAllowlist) {\n return buildPrefixMatcher(options.urlPrefixAllowlist);\n } else if (options.config) {\n return buildPrefixMatcher([\n options.config.getString('backend.baseUrl'),\n ...IdentityAuthInjectorFetchMiddleware.getDiscoveryUrlPrefixes(\n options.config,\n ),\n ]);\n }\n return () => false;\n}\n\nfunction buildPrefixMatcher(prefixes: string[]): (url: string) => boolean {\n const trimmedPrefixes = prefixes.map(prefix => prefix.replace(/\\/$/, ''));\n return url =>\n trimmedPrefixes.some(\n prefix => url === prefix || url.startsWith(`${prefix}/`),\n );\n}\n"],"names":[],"mappings":"AAwBO,MAAM,mCAAA,CAA+D;AAAA,EAC1E,OAAO,OAAO,OAAA,EAS0B;AACtC,IAAA,MAAM,OAAA,GAAU,aAAa,OAAO,CAAA;AACpC,IAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,MAAA,EAAQ,IAAA,IAAQ,eAAA;AAC3C,IAAA,MAAM,cAAc,OAAA,CAAQ,MAAA,EAAQ,KAAA,KAAU,CAAA,KAAA,KAAS,UAAU,KAAK,CAAA,CAAA,CAAA;AAEtE,IAAA,OAAO,IAAI,mCAAA;AAAA,MACT,OA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dentityAuthInjectorFetchMiddleware.esm.js","sources":["../../../../src/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { IdentityApi } from '@backstage/core-plugin-api';\nimport { FetchMiddleware } from './types';\n\nconst PLUGIN_ID_TEMPLATE = /\\{\\{\\s*pluginId\\s*\\}\\}/g;\n\n/**\n * A fetch middleware, which injects a Backstage token header when the user is\n * signed in.\n */\nexport class IdentityAuthInjectorFetchMiddleware implements FetchMiddleware {\n static create(options: {\n identityApi: IdentityApi;\n config?: Config;\n urlPrefixAllowlist?: string[];\n allowUrl?: (url: string) => boolean;\n header?: {\n name: string;\n value: (backstageToken: string) => string;\n };\n }): IdentityAuthInjectorFetchMiddleware {\n const matcher = buildMatcher(options);\n const headerName = options.header?.name || 'authorization';\n const headerValue = options.header?.value || (token => `Bearer ${token}`);\n\n return new IdentityAuthInjectorFetchMiddleware(\n options.identityApi,\n matcher,\n headerName,\n headerValue,\n );\n }\n\n /**\n * Returns an array of plugin URL prefixes derived from the static `discovery`\n * configuration, to be used as `urlPrefixAllowlist` option of {@link create}.\n */\n static getDiscoveryUrlPrefixes(config: Config): string[] {\n const endpointConfigs =\n config.getOptionalConfigArray('discovery.endpoints') || [];\n return endpointConfigs.flatMap(c => {\n const target = getExternalTarget(c);\n if (!target) {\n return [];\n }\n const plugins = c.getStringArray('plugins');\n return plugins.flatMap(pluginId => {\n if (pluginId === '*') {\n return [];\n }\n return target.replace(PLUGIN_ID_TEMPLATE, pluginId);\n });\n });\n }\n\n public readonly identityApi: IdentityApi;\n public readonly allowUrl: (url: string) => boolean;\n public readonly headerName: string;\n public readonly headerValue: (pluginId: string) => string;\n\n constructor(\n identityApi: IdentityApi,\n allowUrl: (url: string) => boolean,\n headerName: string,\n headerValue: (pluginId: string) => string,\n ) {\n this.identityApi = identityApi;\n this.allowUrl = allowUrl;\n this.headerName = headerName;\n this.headerValue = headerValue;\n }\n\n apply(next: typeof fetch): typeof fetch {\n return async (input, init) => {\n // Skip this middleware if the header already exists, or if the URL\n // doesn't match any of the allowlist items, or if there was no token.\n // NOTE(freben): The \"as any\" casts here and below are because of subtle\n // undici type differences that happened in a node types bump. Those are\n // immaterial to the code at hand at runtime, as the global fetch and\n // Request are always taken from the same place.\n const request = new Request(input as any, init);\n const { token } = await this.identityApi.getCredentials();\n if (\n request.headers.get(this.headerName) ||\n typeof token !== 'string' ||\n !token ||\n !this.allowUrl(request.url)\n ) {\n return next(input as any, init);\n }\n\n request.headers.set(this.headerName, this.headerValue(token));\n return next(request);\n };\n }\n}\n\nfunction buildMatcher(options: {\n config?: Config;\n urlPrefixAllowlist?: string[];\n allowUrl?: (url: string) => boolean;\n}): (url: string) => boolean {\n if (options.allowUrl) {\n return options.allowUrl;\n } else if (options.urlPrefixAllowlist) {\n return buildPrefixMatcher(options.urlPrefixAllowlist);\n } else if (options.config) {\n const prefixMatcher = buildPrefixMatcher([\n options.config.getString('backend.baseUrl'),\n ...IdentityAuthInjectorFetchMiddleware.getDiscoveryUrlPrefixes(\n options.config,\n ),\n ]);\n const wildcardMatcher = buildWildcardDiscoveryMatcher(options.config);\n return url => prefixMatcher(url) || wildcardMatcher(url);\n }\n return () => false;\n}\n\nfunction getExternalTarget(config: Config): string | undefined {\n return typeof config.get('target') === 'object'\n ? config.getOptionalString('target.external')\n : config.getString('target');\n}\n\nfunction buildWildcardDiscoveryMatcher(\n config: Config,\n): (url: string) => boolean {\n const matchers =\n config.getOptionalConfigArray('discovery.endpoints')?.flatMap(endpoint => {\n if (!endpoint.getStringArray('plugins').includes('*')) {\n return [];\n }\n const target = getExternalTarget(endpoint);\n return target ? [buildUrlPatternMatcher(target)] : [];\n }) ?? [];\n\n return url => matchers.some(matcher => matcher(url));\n}\n\nfunction buildUrlPatternMatcher(pattern: string): (url: string) => boolean {\n const parts = pattern.replace(/\\/$/, '').split(PLUGIN_ID_TEMPLATE);\n let source = escapeRegExp(parts[0]);\n\n for (let i = 1; i < parts.length; i++) {\n source += i === 1 ? '([^/?#]+)' : '\\\\1';\n source += escapeRegExp(parts[i]);\n }\n\n const regex = new RegExp(`^${source}(?:/|$|[?#])`);\n return url => regex.test(url);\n}\n\nfunction escapeRegExp(value: string): string {\n return value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nfunction buildPrefixMatcher(prefixes: string[]): (url: string) => boolean {\n const trimmedPrefixes = prefixes.map(prefix => prefix.replace(/\\/$/, ''));\n return url =>\n trimmedPrefixes.some(\n prefix => url === prefix || url.startsWith(`${prefix}/`),\n );\n}\n"],"names":[],"mappings":"AAoBA,MAAM,kBAAA,GAAqB,yBAAA;AAMpB,MAAM,mCAAA,CAA+D;AAAA,EAC1E,OAAO,OAAO,OAAA,EAS0B;AACtC,IAAA,MAAM,OAAA,GAAU,aAAa,OAAO,CAAA;AACpC,IAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,MAAA,EAAQ,IAAA,IAAQ,eAAA;AAC3C,IAAA,MAAM,cAAc,OAAA,CAAQ,MAAA,EAAQ,KAAA,KAAU,CAAA,KAAA,KAAS,UAAU,KAAK,CAAA,CAAA,CAAA;AAEtE,IAAA,OAAO,IAAI,mCAAA;AAAA,MACT,OAAA,CAAQ,WAAA;AAAA,MACR,OAAA;AAAA,MACA,UAAA;AAAA,MACA;AAAA,KACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,wBAAwB,MAAA,EAA0B;AACvD,IAAA,MAAM,eAAA,GACJ,MAAA,CAAO,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OAAA,CAAQ,KAAA,EAAO,EAAE,CAAC,CAAA;AACxE,EAAA,OAAO,SACL,eAAA,CAAgB,IAAA;AAAA,IACd,YAAU,GAAA,KAAQ,MAAA,IAAU,IAAI,UAAA,CAAW,CAAA,EAAG,MAAM,CAAA,CAAA,CAAG;AAAA,GACzD;AACJ;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -559,10 +559,14 @@ declare class FrontendHostDiscovery implements DiscoveryApi {
|
|
|
559
559
|
* internal: https://internal.example.com/search
|
|
560
560
|
* external: https://example.com/search
|
|
561
561
|
* plugins: [search]
|
|
562
|
+
* - target: https://example.com/api/{{pluginId}}
|
|
563
|
+
* plugins: ['*']
|
|
562
564
|
* ```
|
|
563
565
|
*
|
|
564
|
-
* If a plugin is not declared in the config,
|
|
565
|
-
*
|
|
566
|
+
* If a plugin is not declared in the config, discovery will first use the
|
|
567
|
+
* wildcard target, if one is configured, and otherwise fall back to using
|
|
568
|
+
* the baseUrl with the provided `pathPattern` appended. The default path
|
|
569
|
+
* pattern is `"/api/{{ pluginId }}"`.
|
|
566
570
|
*/
|
|
567
571
|
static fromConfig(config: Config, options?: {
|
|
568
572
|
pathPattern?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/core-app-api",
|
|
3
|
-
"version": "1.20.3
|
|
3
|
+
"version": "1.20.3",
|
|
4
4
|
"description": "Core app API used by Backstage apps",
|
|
5
5
|
"backstage": {
|
|
6
6
|
"role": "web-library"
|
|
@@ -50,11 +50,11 @@
|
|
|
50
50
|
"test": "backstage-cli package test"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@backstage/config": "1.3.8",
|
|
54
|
-
"@backstage/core-plugin-api": "1.12.8
|
|
55
|
-
"@backstage/types": "1.2.2",
|
|
56
|
-
"@backstage/ui": "0.17.0
|
|
57
|
-
"@backstage/version-bridge": "1.0.12",
|
|
53
|
+
"@backstage/config": "^1.3.8",
|
|
54
|
+
"@backstage/core-plugin-api": "^1.12.8",
|
|
55
|
+
"@backstage/types": "^1.2.2",
|
|
56
|
+
"@backstage/ui": "^0.17.0",
|
|
57
|
+
"@backstage/version-bridge": "^1.0.12",
|
|
58
58
|
"@types/prop-types": "^15.7.3",
|
|
59
59
|
"history": "^5.0.0",
|
|
60
60
|
"i18next": "^22.4.15",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
"zod": "^3.25.76 || ^4.0.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
-
"@backstage/cli": "0.36.4
|
|
69
|
-
"@backstage/test-utils": "1.7.20
|
|
68
|
+
"@backstage/cli": "^0.36.4",
|
|
69
|
+
"@backstage/test-utils": "^1.7.20",
|
|
70
70
|
"@testing-library/dom": "^10.0.0",
|
|
71
71
|
"@testing-library/jest-dom": "^6.0.0",
|
|
72
72
|
"@testing-library/react": "^16.0.0",
|