diff --git a/modules/router-store/spec/integration.spec.ts b/modules/router-store/spec/integration.spec.ts index 28323ca376..a1c725fb40 100644 --- a/modules/router-store/spec/integration.spec.ts +++ b/modules/router-store/spec/integration.spec.ts @@ -3,14 +3,14 @@ import {TestBed} from '@angular/core/testing'; import {NavigationEnd, Router} from '@angular/router'; import {RouterTestingModule} from '@angular/router/testing'; import {Store, StoreModule} from '@ngrx/store'; -import {ROUTER_CANCEL, ROUTER_ERROR, ROUTER_NAVIGATION, routerReducer, StoreRouterConnectingModule} from '../src/index'; +import { ROUTER_CANCEL, ROUTER_ERROR, ROUTER_NAVIGATION, routerReducer, StoreRouterConnectingModule, RouterNavigationAction, RouterCancelAction, RouterAction } from '../src/index'; import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/first'; import 'rxjs/add/operator/toPromise'; describe('integration spec', () => { it('should work', (done) => { - const reducer = (state: string = '', action: any) => { + const reducer = (state: string = '', action: RouterAction) => { if (action.type === ROUTER_NAVIGATION) { return action.payload.routerState.url.toString(); } else { @@ -49,7 +49,7 @@ describe('integration spec', () => { }); it('should support preventing navigation', (done) => { - const reducer = (state: string = '', action: any) => { + const reducer = (state: string = '', action: RouterAction) => { if (action.type === ROUTER_NAVIGATION && action.payload.routerState.url.toString() === '/next') { throw new Error('You shall not pass!'); } else { @@ -80,7 +80,7 @@ describe('integration spec', () => { }); it('should support rolling back if navigation gets canceled', (done) => { - const reducer = (state: string = '', action: any): any => { + const reducer = (state: string = '', action: RouterAction): any => { if (action.type === ROUTER_NAVIGATION) { return { url: action.payload.routerState.url.toString(), lastAction: ROUTER_NAVIGATION }; @@ -117,7 +117,7 @@ describe('integration spec', () => { }); it('should support rolling back if navigation errors', (done) => { - const reducer = (state: string = '', action: any): any => { + const reducer = (state: string = '', action: RouterAction): any => { if (action.type === ROUTER_NAVIGATION) { return { url: action.payload.routerState.url.toString(), lastAction: ROUTER_NAVIGATION }; @@ -155,7 +155,7 @@ describe('integration spec', () => { }); it('should call navigateByUrl when resetting state of the routerReducer', (done) => { - const reducer = (state: any, action: any) => { + const reducer = (state: any, action: RouterAction) => { const r = routerReducer(state, action); return r && r.state ? ({ url: r.state.url, navigationId: r.navigationId }) : null; }; diff --git a/modules/router-store/src/index.ts b/modules/router-store/src/index.ts index 0460651859..73fd97f27b 100644 --- a/modules/router-store/src/index.ts +++ b/modules/router-store/src/index.ts @@ -2,6 +2,10 @@ export { ROUTER_ERROR, ROUTER_CANCEL, ROUTER_NAVIGATION, + RouterNavigationAction, + RouterCancelAction, + RouterErrorAction, + RouterAction, routerReducer, RouterErrorPayload, RouterReducerState, diff --git a/modules/router-store/src/router_store_module.ts b/modules/router-store/src/router_store_module.ts index dee5fa8439..b3b977f3e1 100644 --- a/modules/router-store/src/router_store_module.ts +++ b/modules/router-store/src/router_store_module.ts @@ -16,6 +16,14 @@ export type RouterNavigationPayload = { event: RoutesRecognized }; +/** + * An action dispatched when the router navigates. + */ +export type RouterNavigationAction = { + type: typeof ROUTER_NAVIGATION, + payload: RouterNavigationPayload +}; + /** * An action dispatched when the router cancels navigation. */ @@ -30,6 +38,14 @@ export type RouterCancelPayload = { event: NavigationCancel }; +/** + * An action dispatched when the router cancel navigation. + */ +export type RouterCancelAction = { + type: typeof ROUTER_CANCEL, + payload: RouterCancelPayload +}; + /** * An action dispatched when the router errors. */ @@ -44,13 +60,29 @@ export type RouterErrorPayload = { event: NavigationError }; +/** + * An action dispatched when the router errors. + */ +export type RouterErrorAction = { + type: typeof ROUTER_ERROR, + payload: RouterErrorPayload +}; + +/** + * An union type of router actions. + */ +export type RouterAction = RouterNavigationAction | RouterCancelAction | RouterErrorAction; + export type RouterReducerState = { state: RouterStateSnapshot, navigationId: number }; -export function routerReducer(state: RouterReducerState, action: any): RouterReducerState { - if (action.type === 'ROUTER_NAVIGATION' || action.type === 'ROUTER_ERROR' || action.type === 'ROUTER_CANCEL') { - return ({ state: action.payload.routerState, navigationId: action.payload.event.id }); - } else { - return state; +export function routerReducer(state: RouterReducerState, action: RouterAction): RouterReducerState { + switch (action.type) { + case ROUTER_NAVIGATION: + case ROUTER_ERROR: + case ROUTER_CANCEL: + return ({ state: action.payload.routerState, navigationId: action.payload.event.id }); + default: + return state; } }