feat: loosen entityRouteParams to accept references

Signed-off-by: Tim Klever <tim.v.klever@aexp.com>
This commit is contained in:
Tim Klever
2025-04-01 19:15:30 -07:00
parent def696c310
commit 1f1f8de2c1
2 changed files with 51 additions and 8 deletions
+17 -1
View File
@@ -14,7 +14,11 @@
* limitations under the License.
*/
import { DEFAULT_NAMESPACE, Entity } from '@backstage/catalog-model';
import {
DEFAULT_NAMESPACE,
Entity,
getCompoundEntityRef,
} from '@backstage/catalog-model';
import { entityRouteParams } from './routes';
const entity: Entity = {
@@ -49,11 +53,23 @@ const expectedNamespacedEntityRouteParams = {
describe('entityRouteParams', () => {
it.each([
['Entity', entity, expectedEntityRouteParams],
['ComponentRef', getCompoundEntityRef(entity), expectedEntityRouteParams],
['string', 'component:Test-Component', expectedEntityRouteParams],
[
'namespaced Entity',
namespacedEntity,
expectedNamespacedEntityRouteParams,
],
[
'namespaced ComponentRef',
getCompoundEntityRef(namespacedEntity),
expectedNamespacedEntityRouteParams,
],
[
'namespaced string',
'component:Test-Namespace/Test-Namespaced-Component',
expectedNamespacedEntityRouteParams,
],
])(
'should return correct route params for %s',
(_type, entityOrRef, expectedRouteParams) => {
+34 -7
View File
@@ -14,7 +14,12 @@
* limitations under the License.
*/
import { Entity, DEFAULT_NAMESPACE } from '@backstage/catalog-model';
import {
Entity,
DEFAULT_NAMESPACE,
CompoundEntityRef,
parseEntityRef,
} from '@backstage/catalog-model';
import { createRouteRef } from '@backstage/core-plugin-api';
import { getOrCreateGlobalSingleton } from '@backstage/version-bridge';
@@ -41,12 +46,34 @@ export const entityRouteRef = getOrCreateGlobalSingleton(
* Utility function to get suitable route params for entityRoute, given an
* @public
*/
export function entityRouteParams(entity: Entity) {
export function entityRouteParams(
entityOrRef: Entity | CompoundEntityRef | string,
) {
let kind;
let namespace;
let name;
if (typeof entityOrRef === 'string') {
const parsed = parseEntityRef(entityOrRef);
kind = parsed.kind;
namespace = parsed.namespace;
name = parsed.name;
} else if ('metadata' in entityOrRef) {
kind = entityOrRef.kind;
namespace = entityOrRef.metadata.namespace;
name = entityOrRef.metadata.name;
} else {
kind = entityOrRef.kind;
namespace = entityOrRef.namespace;
name = entityOrRef.name;
}
kind = kind.toLocaleLowerCase('en-US');
namespace = namespace?.toLocaleLowerCase('en-US') ?? DEFAULT_NAMESPACE;
return {
kind: entity.kind.toLocaleLowerCase('en-US'),
namespace:
entity.metadata.namespace?.toLocaleLowerCase('en-US') ??
DEFAULT_NAMESPACE,
name: entity.metadata.name,
kind,
namespace,
name,
} as const;
}