Skip to content

Commit 21a218a

Browse files
authored
feat: add RouterStore#selectRouteData selector factory (#290)
# Features - Add factory for selecting specific route data
1 parent 5aeca7f commit 21a218a

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ describe(`${GlobalRouterStore.name} selectors`, () => {
126126
});
127127
});
128128

129+
it('creates a selector for specific route data', async () => {
130+
await router.navigateByUrl('/login/etyDDwAAQBAJ?ref=ngrx.io#test-fragment');
131+
132+
await expect(
133+
firstValueFrom(store.selectRouteData<string>('testData'))
134+
).resolves.toBe('test-data');
135+
});
136+
129137
it('exposes a selector for the URL', async () => {
130138
await router.navigateByUrl('/login/etyDDwAAQBAJ?ref=ngrx.io#test-fragment');
131139

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export class GlobalRouterStore
7878
return this.select(this.queryParams$, (params) => params[param]);
7979
}
8080

81+
selectRouteData<TValue>(key: string): Observable<TValue | undefined> {
82+
return this.select(this.routeData$, (data) => data[key]);
83+
}
84+
8185
selectRouteParam(param: string): Observable<string | undefined> {
8286
return this.select(this.routeParams$, (params) => params[param]);
8387
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ describe(`${LocalRouterStore.name} selectors`, () => {
134134
});
135135
});
136136

137+
it('creates a selector for specific route data', async () => {
138+
await router.navigateByUrl('/login/etyDDwAAQBAJ?ref=ngrx.io#test-fragment');
139+
140+
await expect(
141+
firstValueFrom(getStore().selectRouteData<string>('testData'))
142+
).resolves.toBe('test-data');
143+
});
144+
137145
it('exposes a selector for the URL', async () => {
138146
await router.navigateByUrl('/login/etyDDwAAQBAJ?ref=ngrx.io#test-fragment');
139147

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ export class LocalRouterStore
7777
return this.select(this.queryParams$, (params) => params[param]);
7878
}
7979

80+
selectRouteData<TValue>(key: string): Observable<TValue | undefined> {
81+
return this.select(this.routeData$, (data) => data[key]);
82+
}
83+
8084
selectRouteParam(param: string): Observable<string | undefined> {
8185
return this.select(this.routeParams$, (params) => params[param]);
8286
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ export abstract class RouterStore {
5757
* Select the current URL.
5858
*/
5959
abstract readonly url$: Observable<string>;
60+
/**
61+
* Select the specified route data.
62+
*
63+
* @param key The route data key.
64+
*
65+
* @example <caption>Usage</caption>
66+
* const limit$ = routerStore.selectRouteData<number>('limit');
67+
*/
68+
abstract selectRouteData<TValue>(key: string): Observable<TValue | undefined>;
6069
/**
6170
* Select the specified query parameter.
6271
*

0 commit comments

Comments
 (0)