npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ngworker/router-component-store

v15.0.0-rc.1

Published

A strictly typed lightweight alternative to NgRx Router Store (@ngrx/router-store) and ActivatedRoute

Downloads

990

Readme

Router Component Store

@ngworker/router-component-store

A strictly typed lightweight alternative to NgRx Router Store (@ngrx/router-store) and ActivatedRoute.

Compatibility

Required peer dependencies:

  • Angular >=15.0
  • NgRx Component Store >=15.0
  • RxJS >=7.5
  • TypeScript >=4.8

Published with partial Ivy compilation.

Find additional documentation in the github.com/ngworker/router-component-store/docs directory.

Guiding principles

Router Component Store is meant as a lightweight alternative to NgRx Router Store that additionaly can be used as a replacement for ActivatedRoute at any route level.

The following principles guide the development of Router Component Store.

  • The global router store closely matches NgRx Router Store selectors
  • Local router stores closely match ActivatedRoute observable properties
  • Router state is immutable and serializable
  • The API is strictly and strongly typed

API

RouterStore

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<StrictRouteParams> | Select the current route query parameters. | | routeData$: Observable<StrictRouteData> | Select the current route data. | | routeParams$: Observable<StrictRouteParams> | Select the current route parameters. | | title$: Observable<string \| undefined> | Select the resolved route title. | | url$: Observable<string> | Select the current URL. | | selectQueryParam(param: string): Observable<string \| undefined> | Select the specified query parameter. | | selectRouteData(key: string): Observable<unknown> | Select the specified route data. | | selectRouteParam(param: string): Observable<string \| undefined> | Select the specified route parameter. | | selectRouterEvents(...acceptedRouterEvents: RouterEvent[]): Observable<RouterEvent> | Select router events of the specified router event types. |

A RouterStore service is provided by using either provideGlobalRouterStoreor provideLocalRouterStore.

The global RouterStore service is provided in a root environment injector and is never destroyed but can be injected in any injection context.

It emits values similar to @ngrx/router-store selectors. A comparison is in the documentation.

A local RouterStore requires a component-level provider, follows the lifecycle of that component, and can be injected in declarables as well as other component-level services.

It emits values similar to ActivatedRoute. A comparison is in the documentation.

Global router store

An application-wide router store that can be injected in any injection context. Use provideGlobalRouterStore to provide it in a root environment injector.

Use a global router store instead of NgRx Router Store.

Providing in a standalone Angular application:

// main.ts
// (...)
import { provideGlobalRouterStore } from '@ngworker/router-component-store';

bootstrapApplication(AppComponent, {
  providers: [provideGlobalRouterStore()],
}).catch((error) => console.error(error));

Providing in a classic Angular application:

// app.module.ts
// (...)
import { provideGlobalRouterStore } from '@ngworker/router-component-store';

@NgModule({
  // (...)
  providers: [provideGlobalRouterStore()],
})
export class AppModule {}

Usage in service:

// hero.service.ts
// (...)
import { RouterStore } from '@ngworker/router-component-store';

@Injectable({
  providedIn: 'root',
})
export class HeroService {
  #routerStore = inject(RouterStore);

  activeHeroId$: Observable<string | undefined> =
    this.#routerStore.selectRouteParam('id');
}

Usage in component:

// hero-detail.component.ts
// (...)
import { RouterStore } from '@ngworker/router-component-store';

@Component({
  // (...)
})
export class HeroDetailComponent {
  #routerStore = inject(RouterStore);

  heroId$: Observable<string | undefined> =
    this.#routerStore.selectRouteParam('id');
}

Local router store

A component-level router store. Can be injected in any directive, component, pipe, or component-level service. Explicitly provided in a component sub-tree using Component.providers or Component.viewProviders.

Use a local router store instead of ActivatedRoute.

Usage in component:

// hero-detail.component.ts
// (...)
import {
  provideLocalRouterStore,
  RouterStore,
} from '@ngworker/router-component-store';

@Component({
  // (...)
  providers: [provideLocalRouterStore()],
})
export class HeroDetailComponent {
  #routerStore = inject(RouterStore);

  heroId$: Observable<string | undefined> =
    this.#routerStore.selectRouteParam('id');
}

Serializable router state

Several of the Angular Router's types are recursive which means that they aren't serializable. The router stores exclusively use serializable types to support advanced state synchronization strategies.

MinimalActivatedRouteSnapshot

The MinimalActivatedRouteSnapshot interface is used for the observable RouterStore#currentRoute$ property. This interface is a serializable subset of the Angular Router's ActivatedRouteSnapshot class and has the following public properties.

| API | Description | | --------------------------------------------------- | ------------------------------------------------ | | children: MinimalActivatedRouteSnapshot[] | The children of this route in the route tree. | | data: StrictRouteData | The static and resolved data of this route. | | firstChild: MinimalActivatedRouteSnapshot \| null | The first child of this route in the route tree. | | fragment: string \| null | The URL fragment shared by all routes. | | outlet: string | The outlet name of the route. | | params: StrictRouteParams | The matrix parameters scoped to this route. | | queryParams: StrictRouteParams | The query parameters shared by all routes. | | routeConfig: Route \| null | The configuration used to match this route. | | title: string \| undefined | The resolved route title. | | url: UrlSegment[] | The URL segments matched by this route. |

StrictRouteData

The StrictRouteData interface is used for the MinimalActivatedRouteSnapshot#data and RouterStore#routeData$ properties. This interface is a serializable subset of the Angular Router's Data type. In particular, the symbol index in the Angular Router's Data type is removed. Additionally, the any member type is replaced with unknown for stricter typing.

StrictRouteData has the following signature.

export type StrictRouteData = {
  readonly [key: string]: unknown;
};

StrictRouteParams

The StrictRouteParams type is used for route parameters in the MinimalActivatedRouteSnapshot#params and RouterStore#routeParams$ properties and for query parameters in the MinimalActivatedRouteSnapshot#queryParams and RouterStore#queryParams$ properties. It is a strictly typed version of the Angular Router's Params type where members are read-only and the any member type is replaced with string | undefined.

StrictRouteParams has the following signature.

export type StrictRouteParams = {
  readonly [key: string]: string | undefined;
};