core-compat-api: added convertLegacyRouteRefs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-01-19 11:17:56 +01:00
parent 2a284475af
commit 7155c30a88
4 changed files with 68 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/core-compat-api': patch
---
Added `convertLegacyRouteRefs` for bulk conversion of plugin routes.
+21
View File
@@ -40,5 +40,26 @@ export function convertLegacyRouteRef<
ref: ExternalRouteRef<TParams, TOptional>,
): ExternalRouteRef_2<TParams, TOptional>;
// @public
export function convertLegacyRouteRefs<
TRefs extends {
[name in string]: RouteRef | SubRouteRef | ExternalRouteRef;
},
>(
refs: TRefs,
): {
[KName in keyof TRefs]: ToNewRouteRef<TRefs[KName]>;
};
// @public
export type ToNewRouteRef<T extends RouteRef | SubRouteRef | ExternalRouteRef> =
T extends RouteRef<infer IParams>
? RouteRef_2<IParams>
: T extends SubRouteRef<infer IParams>
? SubRouteRef_2<IParams>
: T extends ExternalRouteRef<infer IParams, infer IOptional>
? ExternalRouteRef_2<IParams, IOptional>
: never;
// (No @packageDocumentation comment for this package)
```
@@ -40,6 +40,43 @@ import { toInternalSubRouteRef } from '../../frontend-plugin-api/src/routing/Sub
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { toInternalExternalRouteRef } from '../../frontend-plugin-api/src/routing/ExternalRouteRef';
/**
* Converts a legacy route ref type to the new system.
*
* @public
*/
export type ToNewRouteRef<
T extends LegacyRouteRef | LegacySubRouteRef | LegacyExternalRouteRef,
> = T extends LegacyRouteRef<infer IParams>
? RouteRef<IParams>
: T extends LegacySubRouteRef<infer IParams>
? SubRouteRef<IParams>
: T extends LegacyExternalRouteRef<infer IParams, infer IOptional>
? ExternalRouteRef<IParams, IOptional>
: never;
/**
* Converts a collection of legacy route refs to the new system.
* This is particularly useful when defining plugin `routes` and `externalRoutes`.
*
* @public
*/
export function convertLegacyRouteRefs<
TRefs extends {
[name in string]:
| LegacyRouteRef
| LegacySubRouteRef
| LegacyExternalRouteRef;
},
>(refs: TRefs): { [KName in keyof TRefs]: ToNewRouteRef<TRefs[KName]> } {
return Object.fromEntries(
Object.entries(refs).map(([name, ref]) => [
name,
convertLegacyRouteRef(ref as LegacyRouteRef),
]),
) as { [KName in keyof TRefs]: ToNewRouteRef<TRefs[KName]> };
}
/**
* A temporary helper to convert a legacy route ref to the new system.
*
+5 -1
View File
@@ -16,4 +16,8 @@
export * from './compatWrapper';
export { convertLegacyApp } from './convertLegacyApp';
export { convertLegacyRouteRef } from './convertLegacyRouteRef';
export {
convertLegacyRouteRef,
convertLegacyRouteRefs,
type ToNewRouteRef,
} from './convertLegacyRouteRef';