Skip to content

Commit 74a25f5

Browse files
authored
refactor: extract types and utilities from minimal serializer ES module (#292)
# Build - Add Browserslist configuration matching Angular CLI defaults - Configure TypeScript target and lib types # Refactors - Extract types from `minimal_serializer.ts` to separate files: - `OmitSymbolIndex` utility type - `MinimalRouteData` - `MinimalActivatedRouteSnapshot` - `MinimalRouterStateSnapshot`
1 parent 21a941e commit 74a25f5

File tree

12 files changed

+196
-102
lines changed

12 files changed

+196
-102
lines changed

.browserslistrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Matches Angular CLI's default Browserslist configuration
2+
# See https://angular.io/guide/build#configuring-browser-compatibility
3+
last 1 Chrome version
4+
last 1 Firefox version
5+
last 2 Edge major versions
6+
last 2 Safari major versions
7+
last 2 iOS major versions
8+
Firefox ESR

packages/router-component-store/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export * from './lib/local-router-store/provide-local-router-store';
88
export * from './lib/router-store';
99

1010
// Serializable route state
11-
export { MinimalActivatedRouteSnapshot } from './lib/@ngrx/router-store/minimal_serializer';
11+
export * from './lib/@ngrx/router-store/minimal-activated-route-state-snapshot';
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @license
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2017 Brandon Roberts, Mike Ryan, Victor Savkin, Rob Wormald
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
/**
26+
* @license
27+
* Copyright Google LLC All Rights Reserved.
28+
*
29+
* Use of this source code is governed by an MIT-style license that can be
30+
* found in the LICENSE file at https://angular.io/license
31+
*/
32+
import { ActivatedRouteSnapshot } from '@angular/router';
33+
import { OmitSymbolIndex } from '../../util-types/omit-symbol-index';
34+
35+
/**
36+
* Contains the information about a route associated with a component loaded in
37+
* an outlet at a particular moment in time. MinimalActivatedRouteSnapshot can
38+
* also be used to traverse the router state tree.
39+
*/
40+
export interface MinimalActivatedRouteSnapshot {
41+
/**
42+
* The configuration used to match this route.
43+
*/
44+
readonly routeConfig: ActivatedRouteSnapshot['routeConfig'];
45+
/**
46+
* The URL segments matched by this route.
47+
*/
48+
readonly url: ActivatedRouteSnapshot['url'];
49+
/**
50+
* The matrix parameters scoped to this route.
51+
*/
52+
readonly params: ActivatedRouteSnapshot['params'];
53+
/**
54+
* The query parameters shared by all the routes.
55+
*/
56+
readonly queryParams: ActivatedRouteSnapshot['queryParams'];
57+
/**
58+
* The URL fragment shared by all the routes.
59+
*/
60+
readonly fragment: ActivatedRouteSnapshot['fragment'];
61+
/**
62+
* The static and resolved data of this route.
63+
*
64+
* @remarks
65+
* Contains serializable route `Data` without its symbol index, in particular
66+
* without the `Symbol.for(RouteTitle)` key as this is an internal value for
67+
* the Angular `Router`. Instead, we access the resolved route title through
68+
* `MinimalActivatedRouteSnapshot['title']`.
69+
*/
70+
readonly data: OmitSymbolIndex<ActivatedRouteSnapshot['data']>;
71+
/**
72+
* The outlet name of the route.
73+
*/
74+
readonly outlet: ActivatedRouteSnapshot['outlet'];
75+
/**
76+
* The resolved route title.
77+
*/
78+
readonly title: ActivatedRouteSnapshot['title'];
79+
/**
80+
* The first child of this route in the router state tree
81+
*/
82+
readonly firstChild?: MinimalActivatedRouteSnapshot;
83+
/**
84+
* The children of this route in the router state tree.
85+
*/
86+
readonly children: MinimalActivatedRouteSnapshot[];
87+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2017 Brandon Roberts, Mike Ryan, Victor Savkin, Rob Wormald
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
import { MinimalActivatedRouteSnapshot } from './minimal-activated-route-state-snapshot';
26+
27+
export interface MinimalRouterStateSnapshot {
28+
readonly root: MinimalActivatedRouteSnapshot;
29+
readonly url: string;
30+
}

packages/router-component-store/src/lib/@ngrx/router-store/minimal_serializer.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* @license
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2017 Brandon Roberts, Mike Ryan, Victor Savkin, Rob Wormald
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
125
import { RouterStateSnapshot } from '@angular/router';
226

327
import { MinimalRouterStateSerializer } from './minimal_serializer';

packages/router-component-store/src/lib/@ngrx/router-store/minimal_serializer.ts

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -22,100 +22,15 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
* SOFTWARE.
2424
*/
25-
/**
26-
* @license
27-
* Copyright Google LLC All Rights Reserved.
28-
*
29-
* Use of this source code is governed by an MIT-style license that can be
30-
* found in the LICENSE file at https://angular.io/license
31-
*/
3225
import { Injectable } from '@angular/core';
3326
import {
3427
ActivatedRouteSnapshot,
3528
Data,
3629
RouterStateSnapshot,
3730
} from '@angular/router';
38-
39-
type OmitSymbolIndex<TShape> = {
40-
[TShapeKey in keyof TShape as symbol extends TShapeKey
41-
? never
42-
: TShapeKey]: TShape[TShapeKey];
43-
};
44-
45-
/**
46-
* Serializable route `Data` without its symbol index, in particular without the
47-
* `Symbol.for(RouteTitle)` key as this is an internal value for the Angular
48-
* `Router`.
49-
*/
50-
export type MinimalRouteData = OmitSymbolIndex<Data>;
51-
52-
/**
53-
* Contains the information about a route associated with a component loaded in
54-
* an outlet at a particular moment in time. MinimalActivatedRouteSnapshot can
55-
* also be used to traverse the router state tree.
56-
*/
57-
export interface MinimalActivatedRouteSnapshot {
58-
/**
59-
* The configuration used to match this route.
60-
*/
61-
readonly routeConfig: ActivatedRouteSnapshot['routeConfig'];
62-
/**
63-
* The URL segments matched by this route.
64-
*/
65-
readonly url: ActivatedRouteSnapshot['url'];
66-
/**
67-
* The matrix parameters scoped to this route.
68-
*/
69-
readonly params: ActivatedRouteSnapshot['params'];
70-
/**
71-
* The query parameters shared by all the routes.
72-
*/
73-
readonly queryParams: ActivatedRouteSnapshot['queryParams'];
74-
/**
75-
* The URL fragment shared by all the routes.
76-
*/
77-
readonly fragment: ActivatedRouteSnapshot['fragment'];
78-
/**
79-
* The static and resolved data of this route.
80-
*
81-
* @remarks
82-
* Contains serializable route `Data` without its symbol index, in particular
83-
* without the `Symbol.for(RouteTitle)` key as this is an internal value for
84-
* the Angular `Router`. Instead, we access the resolved route title through
85-
* `MinimalActivatedRouteSnapshot['title']`.
86-
*/
87-
readonly data: OmitSymbolIndex<ActivatedRouteSnapshot['data']>;
88-
/**
89-
* The outlet name of the route.
90-
*/
91-
readonly outlet: ActivatedRouteSnapshot['outlet'];
92-
/**
93-
* The resolved route title.
94-
*/
95-
readonly title: ActivatedRouteSnapshot['title'];
96-
/**
97-
* The first child of this route in the router state tree
98-
*/
99-
readonly firstChild?: MinimalActivatedRouteSnapshot;
100-
/**
101-
* The children of this route in the router state tree.
102-
*/
103-
readonly children: MinimalActivatedRouteSnapshot[];
104-
}
105-
106-
export interface MinimalRouterStateSnapshot {
107-
readonly root: MinimalActivatedRouteSnapshot;
108-
readonly url: string;
109-
}
110-
111-
function objectFromEntries<TValue>(entries: [string, TValue][]): {
112-
[key: string]: TValue;
113-
} {
114-
return entries.reduce(
115-
(object, [key, value]) => ({ ...object, [key]: value }),
116-
{}
117-
);
118-
}
31+
import { MinimalRouteData } from '../../minimal-route-data';
32+
import { MinimalActivatedRouteSnapshot } from './minimal-activated-route-state-snapshot';
33+
import { MinimalRouterStateSnapshot } from './minimal-router-state-snapshot';
11934

12035
@Injectable({
12136
providedIn: 'root',
@@ -129,7 +44,7 @@ export class MinimalRouterStateSerializer {
12944
}
13045

13146
#serializeRouteData(routeData: Data): MinimalRouteData {
132-
return objectFromEntries(Object.entries(routeData));
47+
return Object.fromEntries(Object.entries(routeData));
13348
}
13449

13550
#serializeRouteSnapshot(

packages/router-component-store/src/lib/global-router-store/global-router-store.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { Injectable } from '@angular/core';
22
import { Data, Params, Router } from '@angular/router';
33
import { ComponentStore } from '@ngrx/component-store';
44
import { map, Observable } from 'rxjs';
5-
import {
6-
MinimalActivatedRouteSnapshot,
7-
MinimalRouterStateSerializer,
8-
MinimalRouterStateSnapshot,
9-
} from '../@ngrx/router-store/minimal_serializer';
5+
import { MinimalActivatedRouteSnapshot } from '../@ngrx/router-store/minimal-activated-route-state-snapshot';
6+
import { MinimalRouterStateSnapshot } from '../@ngrx/router-store/minimal-router-state-snapshot';
7+
import { MinimalRouterStateSerializer } from '../@ngrx/router-store/minimal_serializer';
108
import { RouterStore } from '../router-store';
119

1210
interface GlobalRouterState {

packages/router-component-store/src/lib/local-router-store/local-router-store.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { Injectable } from '@angular/core';
22
import { ActivatedRoute, Data, Params, Router } from '@angular/router';
33
import { ComponentStore } from '@ngrx/component-store';
44
import { map, Observable } from 'rxjs';
5-
import {
6-
MinimalActivatedRouteSnapshot,
7-
MinimalRouterStateSerializer,
8-
MinimalRouterStateSnapshot,
9-
} from '../@ngrx/router-store/minimal_serializer';
5+
import { MinimalActivatedRouteSnapshot } from '../@ngrx/router-store/minimal-activated-route-state-snapshot';
6+
import { MinimalRouterStateSnapshot } from '../@ngrx/router-store/minimal-router-state-snapshot';
7+
import { MinimalRouterStateSerializer } from '../@ngrx/router-store/minimal_serializer';
108
import { RouterStore } from '../router-store';
119

1210
interface LocalRouterState {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Data } from '@angular/router';
2+
import { OmitSymbolIndex } from './util-types/omit-symbol-index';
3+
4+
/**
5+
* Serializable route `Data` without its symbol index, in particular without the
6+
* `Symbol.for(RouteTitle)` key as this is an internal value for the Angular
7+
* `Router`.
8+
*/
9+
export type MinimalRouteData = OmitSymbolIndex<Data>;

packages/router-component-store/src/lib/router-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { Data, Params } from '@angular/router';
33
import { Observable } from 'rxjs';
4-
import { MinimalActivatedRouteSnapshot } from './@ngrx/router-store/minimal_serializer';
4+
import { MinimalActivatedRouteSnapshot } from './@ngrx/router-store/minimal-activated-route-state-snapshot';
55

66
/**
77
* An Angular Router-connecting NgRx component store.

0 commit comments

Comments
 (0)