Skip to content

Commit

Permalink
feat!: make parameter selector types stricter (#278)
Browse files Browse the repository at this point in the history
# Feature
- Remove type parameter from `selectQueryParam`
- Specify strict observable type returned from `selectQueryParam`
- Remove type parameter from `selectRouteParam`
- Specify strict observable type returned from `selectRouteParam`

BREAKING CHANGE:
- Stricter types for `selectQueryParam`

Before:
```typescript
// Actual emitted values are of type `string | undefined` regardless of what we specify
const filter$ = routerStore.selectQueryParam<string | null>('filter');
```

After:
```typescript
// Emitted values are implicitly of type `string | undefined` and are only changeable through operators
const filter$ = routerStore.selectQueryParam('filter').pipe(
  map(filter => filter ?? null),
);
```

- Stricter types for `selectRouteParam`

Before:
```typescript
// Actual emitted values are of type `string | undefined` regardless of what we specify
const id$ = routerStore.selectRouteParam<number>('id');
```

After:
```typescript
// Emitted values are implicitly of type `string | undefined` and are only changeable through operators
const id$ = routerStore.selectRouteParam('id').pipe(
  map(id => id === undefined ? undefined : Number(id),
);
```
  • Loading branch information
LayZeeDK authored Oct 24, 2022
1 parent c46e777 commit f314c63
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ Published with partial Ivy compilation.

A `RouterStore` service has the following public properties:

| API | Description |
| ----------------------------------------------------------- | ------------------------------------------ |
| currentRoute$: Observable<MinimalActivatedRouteSnapshot> | Select the current route. |
| fragment$: Observable<string \| null> | Select the current route fragment. |
| queryParams$: Observable<Params> | Select the current route query parameters. |
| routeData$: Observable<Data> | Select the current route data. |
| routeParams$: Observable<Params> | Select the current route parameters. |
| url$: Observable<string> | Select the current URL. |
| selectQueryParam<TValue>(param: string): Observable<TValue> | Select the specified query parameter. |
| selectRouteParam<TValue>(param: string): Observable<TValue> | Select the specified route parameter. |
| API | Description |
| ---------------------------------------------------------------- | ------------------------------------------ |
| currentRoute$: Observable<MinimalActivatedRouteSnapshot> | Select the current route. |
| fragment$: Observable<string \| null> | Select the current route fragment. |
| queryParams$: Observable<Params> | Select the current route query parameters. |
| routeData$: Observable<Data> | Select the current route data. |
| routeParams$: Observable<Params> | Select the current route parameters. |
| url$: Observable<string> | Select the current URL. |
| selectQueryParam(param: string): Observable<string \| undefined> | Select the specified query parameter. |
| selectRouteParam(param: string): Observable<string \| undefined> | Select the specified route parameter. |

A `RouterStore` service is provided by using either `provideGlobalRouterStore` or `provideLocalRouterStore`.

Expand Down
2 changes: 1 addition & 1 deletion packages/router-component-store/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngworker/router-component-store",
"version": "0.1.2-alpha.8",
"version": "0.2.0-alpha.0",
"description": "An Angular Router-connecting NgRx component store.",
"license": "MIT",
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function setup({
`,
})
class ClassicRoutedComponent {
id$: Observable<string>;
id$: Observable<string | undefined>;
url$: Observable<string>;

constructor(routerStore: RouterStore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ export class GlobalRouterStore
})
);

selectQueryParam<TValue>(param: string): Observable<TValue> {
selectQueryParam(param: string): Observable<string | undefined> {
return this.select(this.queryParams$, (params) => params[param]);
}

selectRouteParam<TValue>(param: string): Observable<TValue> {
selectRouteParam(param: string): Observable<string | undefined> {
return this.select(this.routeParams$, (params) => params[param]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ export class LocalRouterStore
})
);

selectQueryParam<TValue>(param: string): Observable<TValue> {
selectQueryParam(param: string): Observable<string | undefined> {
return this.select(this.queryParams$, (params) => params[param]);
}

selectRouteParam<TValue>(param: string): Observable<TValue> {
selectRouteParam(param: string): Observable<string | undefined> {
return this.select(this.routeParams$, (params) => params[param]);
}
}
4 changes: 2 additions & 2 deletions packages/router-component-store/src/lib/router-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export abstract class RouterStore {
* @example <caption>Usage</caption>
* const order$ = routerStore.selectQueryParam('order');
*/
abstract selectQueryParam<TValue>(param: string): Observable<TValue>;
abstract selectQueryParam(param: string): Observable<string | undefined>;
/**
* Select the specified route parameter.
*
Expand All @@ -74,5 +74,5 @@ export abstract class RouterStore {
* @example <caption>Usage</caption>
* const id$ = routerStore.selectRouteParam('id');
*/
abstract selectRouteParam<TValue>(param: string): Observable<TValue>;
abstract selectRouteParam(param: string): Observable<string | undefined>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function setup({
`,
})
class StandaloneRoutedComponent {
id$: Observable<string>;
id$: Observable<string | undefined>;
url$: Observable<string>;

constructor(routerStore: RouterStore) {
Expand Down

0 comments on commit f314c63

Please sign in to comment.