frontend-plugin-api: rename AnyRouteParams -> AnyRouteRefParams

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-12 19:56:51 +02:00
parent a285d8c000
commit 5b665a3810
13 changed files with 54 additions and 46 deletions
@@ -19,7 +19,7 @@ import {
RouteRef,
ExternalRouteRef,
SubRouteRef,
AnyRouteParams,
AnyRouteRefParams,
RouteFunc,
} from '@backstage/frontend-plugin-api';
import mapValues from 'lodash/mapValues';
@@ -189,7 +189,7 @@ export class RouteResolver {
private readonly appBasePath: string, // base path without a trailing slash
) {}
resolve<Params extends AnyRouteParams>(
resolve<Params extends AnyRouteRefParams>(
anyRouteRef:
| RouteRef<Params>
| SubRouteRef<Params>
@@ -18,7 +18,7 @@ import React from 'react';
import { BackstagePlugin } from '@backstage/core-plugin-api';
import { extractRouteInfoFromInstanceTree } from './extractRouteInfoFromInstanceTree';
import {
AnyRouteParams,
AnyRouteRefParams,
Extension,
RouteRef,
coreExtensionData,
@@ -35,7 +35,7 @@ const ref2 = createRouteRef();
const ref3 = createRouteRef();
const ref4 = createRouteRef();
const ref5 = createRouteRef();
const refOrder: RouteRef<AnyRouteParams>[] = [ref1, ref2, ref3, ref4, ref5];
const refOrder: RouteRef<AnyRouteRefParams>[] = [ref1, ref2, ref3, ref4, ref5];
function createTestExtension(options: {
id: string;
+14 -10
View File
@@ -44,7 +44,7 @@ export type AnyExternalRoutes = {
};
// @public
export type AnyRouteParams =
export type AnyRouteRefParams =
| {
[param in string]: string;
}
@@ -93,7 +93,7 @@ export const coreExtensionData: {
reactElement: ConfigurableExtensionDataRef<JSX_2.Element, {}>;
routePath: ConfigurableExtensionDataRef<string, {}>;
apiFactory: ConfigurableExtensionDataRef<AnyApiFactory, {}>;
routeRef: ConfigurableExtensionDataRef<RouteRef<AnyRouteParams>, {}>;
routeRef: ConfigurableExtensionDataRef<RouteRef<AnyRouteRefParams>, {}>;
navTarget: ConfigurableExtensionDataRef<NavTarget, {}>;
theme: ConfigurableExtensionDataRef<AppTheme, {}>;
};
@@ -288,7 +288,7 @@ export function createSchemaFromZod<TOutput, TInput>(
// @public
export function createSubRouteRef<
Path extends string,
ParentParams extends AnyRouteParams = never,
ParentParams extends AnyRouteRefParams = never,
>(config: {
path: Path;
parent: RouteRef<ParentParams>;
@@ -414,7 +414,7 @@ export interface ExtensionOverridesOptions {
// @public
export interface ExternalRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
TOptional extends boolean = boolean,
> {
// (undocumented)
@@ -454,14 +454,16 @@ export type PortableSchema<TOutput> = {
};
// @public
export type RouteFunc<TParams extends AnyRouteParams> = (
export type RouteFunc<TParams extends AnyRouteRefParams> = (
...[params]: TParams extends undefined
? readonly []
: readonly [params: TParams]
) => string;
// @public
export interface RouteRef<TParams extends AnyRouteParams = AnyRouteParams> {
export interface RouteRef<
TParams extends AnyRouteRefParams = AnyRouteRefParams,
> {
// (undocumented)
readonly $$type: '@backstage/RouteRef';
// (undocumented)
@@ -469,7 +471,9 @@ export interface RouteRef<TParams extends AnyRouteParams = AnyRouteParams> {
}
// @public
export interface SubRouteRef<TParams extends AnyRouteParams = AnyRouteParams> {
export interface SubRouteRef<
TParams extends AnyRouteRefParams = AnyRouteRefParams,
> {
// (undocumented)
readonly $$type: '@backstage/SubRouteRef';
// (undocumented)
@@ -481,18 +485,18 @@ export interface SubRouteRef<TParams extends AnyRouteParams = AnyRouteParams> {
// @public
export function useRouteRef<
TOptional extends boolean,
TParams extends AnyRouteParams,
TParams extends AnyRouteRefParams,
>(
routeRef: ExternalRouteRef<TParams, TOptional>,
): TParams extends true ? RouteFunc<TParams> | undefined : RouteFunc<TParams>;
// @public
export function useRouteRef<TParams extends AnyRouteParams>(
export function useRouteRef<TParams extends AnyRouteRefParams>(
routeRef: RouteRef<TParams> | SubRouteRef<TParams>,
): RouteFunc<TParams>;
// @public
export function useRouteRefParams<Params extends AnyRouteParams>(
export function useRouteRefParams<Params extends AnyRouteRefParams>(
_routeRef: RouteRef<Params> | SubRouteRef<Params>,
): Params;
```
@@ -19,7 +19,7 @@ import {
createExternalRouteRef,
toInternalExternalRouteRef,
} from './ExternalRouteRef';
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
describe('ExternalRouteRef', () => {
it('should be created', () => {
@@ -67,7 +67,7 @@ describe('ExternalRouteRef', () => {
it('should properly infer and validate parameter types and assignments', () => {
function checkRouteRef<
T extends AnyRouteParams,
T extends AnyRouteRefParams,
TOptional extends boolean,
TCheck extends TOptional,
>(
@@ -16,7 +16,7 @@
import { RouteRefImpl } from './RouteRef';
import { describeParentCallSite } from './describeParentCallSite';
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
/**
* Route descriptor, to be later bound to a concrete route by the app. Used to implement cross-plugin route references.
@@ -28,7 +28,7 @@ import { AnyRouteParams } from './types';
* @public
*/
export interface ExternalRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
TOptional extends boolean = boolean,
> {
readonly $$type: '@backstage/ExternalRouteRef';
@@ -38,7 +38,7 @@ export interface ExternalRouteRef<
/** @internal */
export interface InternalExternalRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
TOptional extends boolean = boolean,
> extends ExternalRouteRef<TParams, TOptional> {
readonly version: 'v1';
@@ -50,7 +50,7 @@ export interface InternalExternalRouteRef<
/** @internal */
export function toInternalExternalRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
TOptional extends boolean = boolean,
>(
resource: ExternalRouteRef<TParams, TOptional>,
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
import { RouteRef, createRouteRef, toInternalRouteRef } from './RouteRef';
describe('RouteRef', () => {
@@ -54,7 +54,7 @@ describe('RouteRef', () => {
});
it('should properly infer and validate parameter types and assignments', () => {
function checkRouteRef<T extends AnyRouteParams>(
function checkRouteRef<T extends AnyRouteRefParams>(
_ref: RouteRef<T>,
_params: T extends undefined ? undefined : T,
) {}
@@ -15,7 +15,7 @@
*/
import { describeParentCallSite } from './describeParentCallSite';
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
/**
* Absolute route reference.
@@ -26,14 +26,16 @@ import { AnyRouteParams } from './types';
*
* @public
*/
export interface RouteRef<TParams extends AnyRouteParams = AnyRouteParams> {
export interface RouteRef<
TParams extends AnyRouteRefParams = AnyRouteRefParams,
> {
readonly $$type: '@backstage/RouteRef';
readonly T: TParams;
}
/** @internal */
export interface InternalRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
> extends RouteRef<TParams> {
readonly version: 'v1';
getParams(): string[];
@@ -44,7 +46,7 @@ export interface InternalRouteRef<
/** @internal */
export function toInternalRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
>(resource: RouteRef<TParams>): InternalRouteRef<TParams> {
const r = resource as InternalRouteRef<TParams>;
if (r.$$type !== '@backstage/RouteRef') {
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
import {
SubRouteRef,
createSubRouteRef,
@@ -96,7 +96,7 @@ describe('SubRouteRef', () => {
});
it('should properly infer and parse path parameters', () => {
function checkSubRouteRef<T extends AnyRouteParams>(
function checkSubRouteRef<T extends AnyRouteRefParams>(
_ref: SubRouteRef<T>,
_params: T extends undefined ? undefined : T,
) {}
@@ -15,7 +15,7 @@
*/
import { RouteRef, toInternalRouteRef } from './RouteRef';
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
// Should match the pattern in react-router
const PARAM_PATTERN = /^\w+$/;
@@ -29,7 +29,9 @@ const PARAM_PATTERN = /^\w+$/;
*
* @public
*/
export interface SubRouteRef<TParams extends AnyRouteParams = AnyRouteParams> {
export interface SubRouteRef<
TParams extends AnyRouteRefParams = AnyRouteRefParams,
> {
readonly $$type: '@backstage/SubRouteRef';
readonly T: TParams;
@@ -39,7 +41,7 @@ export interface SubRouteRef<TParams extends AnyRouteParams = AnyRouteParams> {
/** @internal */
export interface InternalSubRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
> extends SubRouteRef<TParams> {
readonly version: 'v1';
@@ -50,7 +52,7 @@ export interface InternalSubRouteRef<
/** @internal */
export function toInternalSubRouteRef<
TParams extends AnyRouteParams = AnyRouteParams,
TParams extends AnyRouteRefParams = AnyRouteRefParams,
>(resource: SubRouteRef<TParams>): InternalSubRouteRef<TParams> {
const r = resource as InternalSubRouteRef<TParams>;
if (r.$$type !== '@backstage/SubRouteRef') {
@@ -68,7 +70,7 @@ export function isSubRouteRef(opaque: {
}
/** @internal */
export class SubRouteRefImpl<TParams extends AnyRouteParams>
export class SubRouteRefImpl<TParams extends AnyRouteRefParams>
implements SubRouteRef<TParams>
{
readonly $$type = '@backstage/SubRouteRef';
@@ -127,7 +129,7 @@ type PathParams<S extends string> = { [name in ParamNames<S>]: string };
*/
type MergeParams<
P1 extends { [param in string]: string },
P2 extends AnyRouteParams,
P2 extends AnyRouteRefParams,
> = (P1[keyof P1] extends never ? {} : P1) & (P2 extends undefined ? {} : P2);
/**
@@ -145,7 +147,7 @@ type TrimEmptyParams<Params extends { [param in string]: string }> =
*/
type MakeSubRouteRef<
Params extends { [param in string]: string },
ParentParams extends AnyRouteParams,
ParentParams extends AnyRouteRefParams,
> = keyof Params & keyof ParentParams extends never
? SubRouteRef<TrimEmptyParams<MergeParams<Params, ParentParams>>>
: never;
@@ -158,7 +160,7 @@ type MakeSubRouteRef<
*/
export function createSubRouteRef<
Path extends string,
ParentParams extends AnyRouteParams = never,
ParentParams extends AnyRouteRefParams = never,
>(config: {
path: Path;
parent: RouteRef<ParentParams>;
@@ -14,7 +14,7 @@
* limitations under the License.
*/
export type { AnyRouteParams } from './types';
export type { AnyRouteRefParams } from './types';
export { createRouteRef, type RouteRef } from './RouteRef';
export { createSubRouteRef, type SubRouteRef } from './SubRouteRef';
export {
@@ -19,4 +19,4 @@
*
* @public
*/
export type AnyRouteParams = { [param in string]: string } | undefined;
export type AnyRouteRefParams = { [param in string]: string } | undefined;
@@ -17,7 +17,7 @@
import { useMemo } from 'react';
import { matchRoutes, useLocation } from 'react-router-dom';
import { useVersionedContext } from '@backstage/version-bridge';
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
import { RouteRef } from './RouteRef';
import { SubRouteRef } from './SubRouteRef';
import { ExternalRouteRef } from './ExternalRouteRef';
@@ -35,7 +35,7 @@ import { ExternalRouteRef } from './ExternalRouteRef';
*
* @public
*/
export type RouteFunc<TParams extends AnyRouteParams> = (
export type RouteFunc<TParams extends AnyRouteRefParams> = (
...[params]: TParams extends undefined
? readonly []
: readonly [params: TParams]
@@ -45,7 +45,7 @@ export type RouteFunc<TParams extends AnyRouteParams> = (
* @internal
*/
export interface RouteResolver {
resolve<TParams extends AnyRouteParams>(
resolve<TParams extends AnyRouteRefParams>(
anyRouteRef:
| RouteRef<TParams>
| SubRouteRef<TParams>
@@ -67,7 +67,7 @@ export interface RouteResolver {
*/
export function useRouteRef<
TOptional extends boolean,
TParams extends AnyRouteParams,
TParams extends AnyRouteRefParams,
>(
routeRef: ExternalRouteRef<TParams, TOptional>,
): TParams extends true ? RouteFunc<TParams> | undefined : RouteFunc<TParams>;
@@ -83,7 +83,7 @@ export function useRouteRef<
* @returns A function that will in turn return the concrete URL of the `routeRef`.
* @public
*/
export function useRouteRef<TParams extends AnyRouteParams>(
export function useRouteRef<TParams extends AnyRouteRefParams>(
routeRef: RouteRef<TParams> | SubRouteRef<TParams>,
): RouteFunc<TParams>;
@@ -98,7 +98,7 @@ export function useRouteRef<TParams extends AnyRouteParams>(
* @returns A function that will in turn return the concrete URL of the `routeRef`.
* @public
*/
export function useRouteRef<TParams extends AnyRouteParams>(
export function useRouteRef<TParams extends AnyRouteRefParams>(
routeRef:
| RouteRef<TParams>
| SubRouteRef<TParams>
@@ -15,7 +15,7 @@
*/
import { useParams } from 'react-router-dom';
import { AnyRouteParams } from './types';
import { AnyRouteRefParams } from './types';
import { RouteRef } from './RouteRef';
import { SubRouteRef } from './SubRouteRef';
@@ -24,7 +24,7 @@ import { SubRouteRef } from './SubRouteRef';
* @param _routeRef - Ref of the current route.
* @public
*/
export function useRouteRefParams<Params extends AnyRouteParams>(
export function useRouteRefParams<Params extends AnyRouteRefParams>(
_routeRef: RouteRef<Params> | SubRouteRef<Params>,
): Params {
return useParams() as Params;