@ngrx/router-store
Version:
Bindings to connect @angular/router to @ngrx/store
294 lines (282 loc) • 12.4 kB
TypeScript
import * as _ngrx_store from '@ngrx/store';
import { Action, Selector, MemoizedSelector } from '@ngrx/store';
import { RouterStateSnapshot, ActivatedRouteSnapshot, NavigationStart, RoutesRecognized, NavigationCancel, NavigationError, NavigationEnd, Params, Data } from '@angular/router';
import * as i0 from '@angular/core';
import { InjectionToken, ModuleWithProviders, EnvironmentProviders } from '@angular/core';
/**
* Simple router state.
* All custom router states / state serializers should have at least
* the properties of this interface.
*/
interface BaseRouterStoreState {
url: string;
}
declare abstract class RouterStateSerializer<T extends BaseRouterStoreState = BaseRouterStoreState> {
abstract serialize(routerState: RouterStateSnapshot): T;
}
interface SerializedRouterStateSnapshot extends BaseRouterStoreState {
root: ActivatedRouteSnapshot;
url: string;
}
declare class FullRouterStateSerializer implements RouterStateSerializer<SerializedRouterStateSnapshot> {
serialize(routerState: RouterStateSnapshot): SerializedRouterStateSnapshot;
private serializeRoute;
}
/**
* An action dispatched when a router navigation request is fired.
*/
declare const ROUTER_REQUEST = "@ngrx/router-store/request";
/**
* Payload of ROUTER_REQUEST
*/
type RouterRequestPayload<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
routerState: T;
event: NavigationStart;
};
/**
* An action dispatched when a router navigation request is fired.
*/
type RouterRequestAction<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
type: typeof ROUTER_REQUEST;
payload: RouterRequestPayload<T>;
};
declare const routerRequestAction: _ngrx_store.ActionCreator<"@ngrx/router-store/request", (props: {
payload: RouterRequestPayload<SerializedRouterStateSnapshot>;
}) => {
payload: RouterRequestPayload<SerializedRouterStateSnapshot>;
} & _ngrx_store.Action<"@ngrx/router-store/request">>;
/**
* An action dispatched when the router navigates.
*/
declare const ROUTER_NAVIGATION = "@ngrx/router-store/navigation";
/**
* Payload of ROUTER_NAVIGATION.
*/
type RouterNavigationPayload<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
routerState: T;
event: RoutesRecognized;
};
/**
* An action dispatched when the router navigates.
*/
type RouterNavigationAction<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
type: typeof ROUTER_NAVIGATION;
payload: RouterNavigationPayload<T>;
};
declare const routerNavigationAction: _ngrx_store.ActionCreator<"@ngrx/router-store/navigation", (props: {
payload: RouterNavigationPayload<SerializedRouterStateSnapshot>;
}) => {
payload: RouterNavigationPayload<SerializedRouterStateSnapshot>;
} & _ngrx_store.Action<"@ngrx/router-store/navigation">>;
/**
* An action dispatched when the router cancels navigation.
*/
declare const ROUTER_CANCEL = "@ngrx/router-store/cancel";
/**
* Payload of ROUTER_CANCEL.
*/
type RouterCancelPayload<T, V extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
routerState: V;
storeState: T;
event: NavigationCancel;
};
/**
* An action dispatched when the router cancels navigation.
*/
type RouterCancelAction<T, V extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
type: typeof ROUTER_CANCEL;
payload: RouterCancelPayload<T, V>;
};
declare const routerCancelAction: _ngrx_store.ActionCreator<"@ngrx/router-store/cancel", (props: {
payload: RouterCancelPayload<SerializedRouterStateSnapshot>;
}) => {
payload: RouterCancelPayload<SerializedRouterStateSnapshot>;
} & _ngrx_store.Action<"@ngrx/router-store/cancel">>;
/**
* An action dispatched when the router errors.
*/
declare const ROUTER_ERROR = "@ngrx/router-store/error";
/**
* Payload of ROUTER_ERROR.
*/
type RouterErrorPayload<T, V extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
routerState: V;
storeState: T;
event: NavigationError;
};
/**
* An action dispatched when the router errors.
*/
type RouterErrorAction<T, V extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
type: typeof ROUTER_ERROR;
payload: RouterErrorPayload<T, V>;
};
declare const routerErrorAction: _ngrx_store.ActionCreator<"@ngrx/router-store/error", (props: {
payload: RouterErrorPayload<SerializedRouterStateSnapshot>;
}) => {
payload: RouterErrorPayload<SerializedRouterStateSnapshot>;
} & _ngrx_store.Action<"@ngrx/router-store/error">>;
/**
* An action dispatched after navigation has ended and new route is active.
*/
declare const ROUTER_NAVIGATED = "@ngrx/router-store/navigated";
/**
* Payload of ROUTER_NAVIGATED.
*/
type RouterNavigatedPayload<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
routerState: T;
event: NavigationEnd;
};
/**
* An action dispatched after navigation has ended and new route is active.
*/
type RouterNavigatedAction<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
type: typeof ROUTER_NAVIGATED;
payload: RouterNavigatedPayload<T>;
};
declare const routerNavigatedAction: _ngrx_store.ActionCreator<"@ngrx/router-store/navigated", (props: {
payload: RouterNavigatedPayload<SerializedRouterStateSnapshot>;
}) => {
payload: RouterNavigatedPayload<SerializedRouterStateSnapshot>;
} & _ngrx_store.Action<"@ngrx/router-store/navigated">>;
/**
* A union type of router actions.
*/
type RouterAction<T, V extends BaseRouterStoreState = SerializedRouterStateSnapshot> = RouterRequestAction<V> | RouterNavigationAction<V> | RouterCancelAction<T, V> | RouterErrorAction<T, V> | RouterNavigatedAction<V>;
type RouterReducerState<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = {
state: T;
navigationId: number;
};
declare function routerReducer<RouterState extends BaseRouterStoreState = SerializedRouterStateSnapshot, Result = RouterReducerState<RouterState>>(state: Result | undefined, action: Action): Result;
type StateKeyOrSelector<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> = string | Selector<any, RouterReducerState<T>>;
declare enum NavigationActionTiming {
PreActivation = 1,
PostActivation = 2
}
declare const DEFAULT_ROUTER_FEATURENAME = "router";
declare const ROUTER_CONFIG: InjectionToken<unknown>;
/**
* Minimal = Serializes the router event with MinimalRouterStateSerializer
* Full = Serializes the router event with FullRouterStateSerializer
*/
declare enum RouterState {
Full = 0,
Minimal = 1
}
interface StoreRouterConfig<T extends BaseRouterStoreState = SerializedRouterStateSnapshot> {
stateKey?: StateKeyOrSelector<T>;
serializer?: new (...args: any[]) => RouterStateSerializer;
/**
* By default, ROUTER_NAVIGATION is dispatched before guards and resolvers run.
* Therefore, the action could run too soon, for example
* there may be a navigation cancel due to a guard saying the navigation is not allowed.
* To run ROUTER_NAVIGATION after guards and resolvers,
* set this property to NavigationActionTiming.PostActivation.
*/
navigationActionTiming?: NavigationActionTiming;
/**
* Decides which router serializer should be used, if there is none provided, and the metadata on the dispatched @ngrx/router-store action payload.
* Set to `Minimal` to use the `MinimalRouterStateSerializer` and to set a minimal router event with the navigation id and url as payload.
* Set to `Full` to use the `FullRouterStateSerializer` and to set the angular router events as payload.
*/
routerState?: RouterState;
}
/**
* Connects RouterModule with StoreModule.
*
* During the navigation, before any guards or resolvers run, the router will dispatch
* a ROUTER_NAVIGATION action, which has the following signature:
*
* ```
* export type RouterNavigationPayload = {
* routerState: SerializedRouterStateSnapshot,
* event: RoutesRecognized
* }
* ```
*
* Either a reducer or an effect can be invoked in response to this action.
* If the invoked reducer throws, the navigation will be canceled.
*
* If navigation gets canceled because of a guard, a ROUTER_CANCEL action will be
* dispatched. If navigation results in an error, a ROUTER_ERROR action will be dispatched.
*
* Both ROUTER_CANCEL and ROUTER_ERROR contain the store state before the navigation
* which can be used to restore the consistency of the store.
*
* Usage:
*
* ```typescript
* @NgModule({
* declarations: [AppCmp, SimpleCmp],
* imports: [
* BrowserModule,
* StoreModule.forRoot(mapOfReducers),
* RouterModule.forRoot([
* { path: '', component: SimpleCmp },
* { path: 'next', component: SimpleCmp }
* ]),
* StoreRouterConnectingModule.forRoot()
* ],
* bootstrap: [AppCmp]
* })
* export class AppModule {
* }
* ```
*/
declare class StoreRouterConnectingModule {
static forRoot<T extends BaseRouterStoreState = SerializedRouterStateSnapshot>(config?: StoreRouterConfig<T>): ModuleWithProviders<StoreRouterConnectingModule>;
static ɵfac: i0.ɵɵFactoryDeclaration<StoreRouterConnectingModule, never>;
static ɵmod: i0.ɵɵNgModuleDeclaration<StoreRouterConnectingModule, never, never, never>;
static ɵinj: i0.ɵɵInjectorDeclaration<StoreRouterConnectingModule>;
}
interface MinimalActivatedRouteSnapshot {
routeConfig: ActivatedRouteSnapshot['routeConfig'];
url: ActivatedRouteSnapshot['url'];
params: ActivatedRouteSnapshot['params'];
queryParams: ActivatedRouteSnapshot['queryParams'];
fragment: ActivatedRouteSnapshot['fragment'];
data: ActivatedRouteSnapshot['data'];
outlet: ActivatedRouteSnapshot['outlet'];
title: ActivatedRouteSnapshot['title'];
firstChild?: MinimalActivatedRouteSnapshot;
children: MinimalActivatedRouteSnapshot[];
}
interface MinimalRouterStateSnapshot extends BaseRouterStoreState {
root: MinimalActivatedRouteSnapshot;
url: string;
}
declare class MinimalRouterStateSerializer implements RouterStateSerializer<MinimalRouterStateSnapshot> {
serialize(routerState: RouterStateSnapshot): MinimalRouterStateSnapshot;
private serializeRoute;
}
type RouterStateSelectors<V> = {
selectCurrentRoute: MemoizedSelector<V, any>;
selectFragment: MemoizedSelector<V, string | undefined>;
selectQueryParams: MemoizedSelector<V, Params>;
selectQueryParam: (param: string) => MemoizedSelector<V, string | string[] | undefined>;
selectRouteParams: MemoizedSelector<V, Params>;
selectRouteParam: (param: string) => MemoizedSelector<V, string | undefined>;
selectRouteData: MemoizedSelector<V, Data>;
selectRouteDataParam: (param: string) => MemoizedSelector<V, string | undefined>;
selectUrl: MemoizedSelector<V, string>;
selectTitle: MemoizedSelector<V, string | undefined>;
};
declare function createRouterSelector<State extends Record<string, any>>(): MemoizedSelector<State, RouterReducerState>;
declare function getRouterSelectors<V extends Record<string, any>>(selectState?: (state: V) => RouterReducerState<any>): RouterStateSelectors<V>;
/**
* Connects the Angular Router to the Store.
*
* @usageNotes
*
* ```ts
* bootstrapApplication(AppComponent, {
* providers: [
* provideStore({ router: routerReducer }),
* provideRouterStore(),
* ],
* });
* ```
*/
declare function provideRouterStore<T extends BaseRouterStoreState = SerializedRouterStateSnapshot>(config?: StoreRouterConfig<T>): EnvironmentProviders;
export { DEFAULT_ROUTER_FEATURENAME, FullRouterStateSerializer, MinimalRouterStateSerializer, NavigationActionTiming, ROUTER_CANCEL, ROUTER_CONFIG, ROUTER_ERROR, ROUTER_NAVIGATED, ROUTER_NAVIGATION, ROUTER_REQUEST, RouterState, RouterStateSerializer, StoreRouterConnectingModule, createRouterSelector, getRouterSelectors, provideRouterStore, routerCancelAction, routerErrorAction, routerNavigatedAction, routerNavigationAction, routerReducer, routerRequestAction };
export type { BaseRouterStoreState, MinimalActivatedRouteSnapshot, MinimalRouterStateSnapshot, RouterAction, RouterCancelAction, RouterCancelPayload, RouterErrorAction, RouterErrorPayload, RouterNavigatedAction, RouterNavigatedPayload, RouterNavigationAction, RouterNavigationPayload, RouterReducerState, RouterRequestAction, RouterRequestPayload, SerializedRouterStateSnapshot, StateKeyOrSelector, StoreRouterConfig };