frontend-plugin-api: refactor to use opaque type for route refs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-11-17 10:35:21 +01:00
parent 766215437e
commit 4d03f08d19
19 changed files with 282 additions and 278 deletions
@@ -16,8 +16,7 @@
import { RouteRef } from '@backstage/frontend-plugin-api';
import { RouteRefsById } from './collectRouteIds';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { toInternalRouteRef } from '../../../frontend-plugin-api/src/routing/RouteRef';
import { OpaqueRouteRef } from '@internal/frontend';
/**
* @internal
@@ -41,7 +40,7 @@ export function createRouteAliasResolver(
let currentRef = routeRef;
for (let i = 0; i < 100; i++) {
const alias = toInternalRouteRef(currentRef).alias;
const alias = OpaqueRouteRef.toInternal(currentRef).alias;
if (alias) {
if (pluginId) {
const [aliasPluginId] = alias.split('.');
@@ -25,18 +25,11 @@ import {
} from '@backstage/frontend-plugin-api';
import mapValues from 'lodash/mapValues';
import { AnyRouteRef, BackstageRouteObject } from './types';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { isRouteRef } from '../../../frontend-plugin-api/src/routing/RouteRef';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import {
isSubRouteRef,
toInternalSubRouteRef,
} from '../../../frontend-plugin-api/src/routing/SubRouteRef';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import {
isExternalRouteRef,
toInternalExternalRouteRef,
} from '../../../frontend-plugin-api/src/routing/ExternalRouteRef';
OpaqueRouteRef,
OpaqueExternalRouteRef,
OpaqueSubRouteRef,
} from '@internal/frontend';
import { RouteAliasResolver } from './RouteAliasResolver';
// Joins a list of paths together, avoiding trailing and duplicate slashes
@@ -65,10 +58,10 @@ function resolveTargetRef(
let ref: AnyRouteRef = targetRouteRef;
let path = '';
if (isExternalRouteRef(ref)) {
if (OpaqueExternalRouteRef.isType(ref)) {
let resolvedRoute = routeBindings.get(ref);
if (!resolvedRoute) {
const internal = toInternalExternalRouteRef(ref);
const internal = OpaqueExternalRouteRef.toInternal(ref);
const defaultTarget = internal.getDefaultTarget();
if (defaultTarget) {
resolvedRoute = routeRefsById.get(defaultTarget);
@@ -80,13 +73,13 @@ function resolveTargetRef(
ref = resolvedRoute;
}
if (isSubRouteRef(ref)) {
const internal = toInternalSubRouteRef(ref);
if (OpaqueSubRouteRef.isType(ref)) {
const internal = OpaqueSubRouteRef.toInternal(ref);
path = ref.path;
ref = internal.getParent();
}
if (!isRouteRef(ref)) {
if (!OpaqueRouteRef.isType(ref)) {
throw new Error(
`Unexpectedly resolved ${targetRouteRef} to a non-route ref ${ref}`,
);
@@ -20,16 +20,12 @@ import {
ExternalRouteRef,
FrontendFeature,
} from '@backstage/frontend-plugin-api';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import {
isRouteRef,
toInternalRouteRef,
} from '../../../frontend-plugin-api/src/routing/RouteRef';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { toInternalExternalRouteRef } from '../../../frontend-plugin-api/src/routing/ExternalRouteRef';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { toInternalSubRouteRef } from '../../../frontend-plugin-api/src/routing/SubRouteRef';
import { OpaqueFrontendPlugin } from '@internal/frontend';
OpaqueRouteRef,
OpaqueSubRouteRef,
OpaqueExternalRouteRef,
OpaqueFrontendPlugin,
} from '@internal/frontend';
import { ErrorCollector } from '../wiring/createErrorCollector';
/** @internal */
@@ -62,12 +58,12 @@ export function collectRouteIds(
continue;
}
if (isRouteRef(ref)) {
const internalRef = toInternalRouteRef(ref);
if (OpaqueRouteRef.isType(ref)) {
const internalRef = OpaqueRouteRef.toInternal(ref);
internalRef.setId(refId);
routesById.set(refId, ref);
} else {
const internalRef = toInternalSubRouteRef(ref);
const internalRef = OpaqueSubRouteRef.toInternal(ref);
routesById.set(refId, internalRef);
}
}
@@ -82,7 +78,7 @@ export function collectRouteIds(
continue;
}
const internalRef = toInternalExternalRouteRef(ref);
const internalRef = OpaqueExternalRouteRef.toInternal(ref);
internalRef.setId(refId);
externalRoutesById.set(refId, ref);
}
@@ -23,8 +23,7 @@ import { RouteRefsById } from './collectRouteIds';
import { ErrorCollector } from '../wiring/createErrorCollector';
import { Config } from '@backstage/config';
import { JsonObject } from '@backstage/types';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { toInternalExternalRouteRef } from '../../../frontend-plugin-api/src/routing/ExternalRouteRef';
import { OpaqueExternalRouteRef } from '@internal/frontend';
/**
* Extracts a union of the keys in a map whose value extends the given type
@@ -166,7 +165,7 @@ export function resolveRouteBindings(
for (const externalRef of routesById.externalRoutes.values()) {
if (!result.has(externalRef) && !disabledExternalRefs.has(externalRef)) {
const defaultRefId =
toInternalExternalRouteRef(externalRef).getDefaultTarget();
OpaqueExternalRouteRef.toInternal(externalRef).getDefaultTarget();
if (defaultRefId) {
const defaultRef = routesById.routes.get(defaultRefId);
if (defaultRef) {