Skip to content

Commit

Permalink
refactor: replace external StrictRouteData with simple type (#326)
Browse files Browse the repository at this point in the history
Closes #328.

### Refactors

- Rename `StrictRouteData` to `InternalStrictRouteData` and use it
internally to enforce compatibility with Angular's `Data` type
- Replace the exposed `StrictRouteData` with a simple type
  • Loading branch information
LayZeeDK authored Sep 16, 2024
2 parents f7ea0d7 + 807b447 commit 44b1d0c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
Data,
RouterStateSnapshot,
} from '@angular/router';
import { InternalStrictRouteData } from '../../internal-strict-route-data';
import { InternalStrictRouteParams } from '../../internal-strict-route-params';
import { StrictRouteData } from '../../strict-route-data';
import { MinimalActivatedRouteSnapshot } from './minimal-activated-route-state-snapshot';
import { MinimalRouterStateSnapshot } from './minimal-router-state-snapshot';

Expand All @@ -44,7 +44,7 @@ export class MinimalRouterStateSerializer {
};
}

#serializeRouteData(routeData: Data): StrictRouteData {
#serializeRouteData(routeData: Data): InternalStrictRouteData {
return Object.fromEntries(Object.entries(routeData));
}

Expand All @@ -56,7 +56,9 @@ export class MinimalRouterStateSerializer {
);
return {
params: routeSnapshot.params as InternalStrictRouteParams,
data: this.#serializeRouteData(routeSnapshot.data),
data: this.#serializeRouteData(
routeSnapshot.data
) as InternalStrictRouteData,
url: routeSnapshot.url,
outlet: routeSnapshot.outlet,
title: routeSnapshot.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { MinimalActivatedRouteSnapshot } from '../@ngrx/router-store/minimal-act
import { MinimalRouterStateSnapshot } from '../@ngrx/router-store/minimal-router-state-snapshot';
import { MinimalRouterStateSerializer } from '../@ngrx/router-store/minimal_serializer';
import { filterRouterEvents } from '../filter-router-event.operator';
import { InternalStrictRouteData } from '../internal-strict-route-data';
import { InternalStrictRouteParams } from '../internal-strict-route-params';
import { RouterStore } from '../router-store';
import { StrictRouteData } from '../strict-route-data';

interface GlobalRouterState {
readonly routerState: MinimalRouterStateSnapshot;
Expand Down Expand Up @@ -56,7 +56,7 @@ export class GlobalRouterStore
this.#rootRoute$,
(route) => route.queryParams
);
routeData$: Observable<StrictRouteData> = this.select(
routeData$: Observable<InternalStrictRouteData> = this.select(
this.currentRoute$,
(route) => route.data
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Data } from '@angular/router';
import { OmitSymbolIndex } from './util-types/omit-symbol-index';
import { StrictNoAny } from './util-types/strict-no-any';

/**
* @remarks We use this type to ensure compatibility with {@link Data}.
* @internal
*/
export type InternalStrictRouteData = Readonly<
StrictNoAny<OmitSymbolIndex<Data>>
>;
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { MinimalActivatedRouteSnapshot } from '../@ngrx/router-store/minimal-act
import { MinimalRouterStateSnapshot } from '../@ngrx/router-store/minimal-router-state-snapshot';
import { MinimalRouterStateSerializer } from '../@ngrx/router-store/minimal_serializer';
import { filterRouterEvents } from '../filter-router-event.operator';
import { InternalStrictRouteData } from '../internal-strict-route-data';
import { InternalStrictRouteParams } from '../internal-strict-route-params';
import { RouterStore } from '../router-store';
import { StrictRouteData } from '../strict-route-data';

interface LocalRouterState {
readonly routerState: MinimalRouterStateSnapshot;
Expand All @@ -45,7 +45,7 @@ export class LocalRouterStore
currentRoute$: Observable<MinimalActivatedRouteSnapshot> = this.#localRoute;
fragment$: Observable<string | null>;
queryParams$: Observable<InternalStrictRouteParams>;
routeData$: Observable<StrictRouteData>;
routeData$: Observable<InternalStrictRouteData>;
routeParams$: Observable<InternalStrictRouteParams>;
title$: Observable<string | undefined>;
url$: Observable<string> = this.select(
Expand Down
8 changes: 3 additions & 5 deletions packages/router-component-store/src/lib/strict-route-data.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Data } from '@angular/router';
import { OmitSymbolIndex } from './util-types/omit-symbol-index';
import { StrictNoAny } from './util-types/strict-no-any';

/**
* Serializable route `Data` without its symbol index, in particular without the
* `Symbol(RouteTitle)` key as this is an internal value for the Angular
* `Router`.
*
* Additionally, the `any` member type is converted to `unknown`.
*/
export type StrictRouteData = Readonly<StrictNoAny<OmitSymbolIndex<Data>>>;
export interface StrictRouteData {
readonly [key: string]: unknown;
}

0 comments on commit 44b1d0c

Please sign in to comment.