Skip to content

Commit 5c814a9

Browse files
dummdidummbrandonroberts
authored andcommitted
feat(router-store): Add custom serializer to config object
Part of #1010, fixes #1262
1 parent 9f731c3 commit 5c814a9

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

modules/router-store/spec/integration.spec.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ describe('integration spec', () => {
393393
});
394394
});
395395

396-
it('should support a custom RouterStateSnapshot serializer ', (done: any) => {
396+
function shouldSupportCustomSerializer(
397+
serializerThroughConfig: boolean,
398+
done: Function
399+
) {
397400
interface SerializedState {
398401
url: string;
399402
params: any;
@@ -422,11 +425,17 @@ describe('integration spec', () => {
422425
}
423426
}
424427

425-
const providers = [
426-
{ provide: RouterStateSerializer, useClass: CustomSerializer },
427-
];
428-
429-
createTestModule({ reducers: { routerReducer, reducer }, providers });
428+
if (serializerThroughConfig) {
429+
createTestModule({
430+
reducers: { routerReducer, reducer },
431+
config: { serializer: CustomSerializer },
432+
});
433+
} else {
434+
const providers = [
435+
{ provide: RouterStateSerializer, useClass: CustomSerializer },
436+
];
437+
createTestModule({ reducers: { routerReducer, reducer }, providers });
438+
}
430439

431440
const router = TestBed.get(Router);
432441
const log = logOfRouterAndActionsAndStore();
@@ -464,6 +473,14 @@ describe('integration spec', () => {
464473
log.splice(0);
465474
done();
466475
});
476+
}
477+
478+
it('should support a custom RouterStateSnapshot serializer via provider', (done: any) => {
479+
shouldSupportCustomSerializer(false, done);
480+
});
481+
482+
it('should support a custom RouterStateSnapshot serializer via config', (done: any) => {
483+
shouldSupportCustomSerializer(true, done);
467484
});
468485

469486
it('should support event during an async canActivate guard', (done: any) => {

modules/router-store/src/router_store_module.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
DefaultRouterStateSerializer,
1919
RouterStateSerializer,
2020
SerializedRouterStateSnapshot,
21+
RouterState,
2122
} from './serializer';
2223

2324
/**
@@ -146,14 +147,6 @@ export type RouterAction<
146147
| RouterErrorAction<T, V>
147148
| RouterNavigatedAction;
148149

149-
/**
150-
* Simple router state.
151-
* All custom router states should have at least this property.
152-
*/
153-
export type RouterState = {
154-
url: string;
155-
};
156-
157150
export type RouterReducerState<
158151
T extends RouterState = SerializedRouterStateSnapshot
159152
> = {
@@ -182,6 +175,7 @@ export function routerReducer<
182175

183176
export interface StoreRouterConfig {
184177
stateKey?: string;
178+
serializer?: new () => RouterStateSerializer;
185179
}
186180

187181
export const _ROUTER_CONFIG = new InjectionToken(
@@ -192,7 +186,7 @@ export const ROUTER_CONFIG = new InjectionToken(
192186
);
193187
export const DEFAULT_ROUTER_FEATURENAME = 'router';
194188

195-
export function _createDefaultRouterConfig(
189+
export function _createRouterConfig(
196190
config: StoreRouterConfig | StoreRouterConfigFunction
197191
): StoreRouterConfig {
198192
let _config: StoreRouterConfig;
@@ -205,10 +199,19 @@ export function _createDefaultRouterConfig(
205199

206200
return {
207201
stateKey: DEFAULT_ROUTER_FEATURENAME,
202+
serializer: DefaultRouterStateSerializer,
208203
..._config,
209204
};
210205
}
211206

207+
export function _createSerializer(
208+
config: StoreRouterConfig
209+
): RouterStateSerializer {
210+
// This function gets handed a complete config-object from _createRouterConfig,
211+
// so we know the serializer property exists
212+
return new config.serializer!();
213+
}
214+
212215
export type StoreRouterConfigFunction = () => StoreRouterConfig;
213216

214217
enum RouterTrigger {
@@ -261,16 +264,23 @@ enum RouterTrigger {
261264
*/
262265
@NgModule({
263266
providers: [
264-
{ provide: RouterStateSerializer, useClass: DefaultRouterStateSerializer },
265267
{
266268
provide: _ROUTER_CONFIG,
267-
useValue: { stateKey: DEFAULT_ROUTER_FEATURENAME },
269+
useValue: {
270+
stateKey: DEFAULT_ROUTER_FEATURENAME,
271+
serializer: DefaultRouterStateSerializer,
272+
},
268273
},
269274
{
270275
provide: ROUTER_CONFIG,
271-
useFactory: _createDefaultRouterConfig,
276+
useFactory: _createRouterConfig,
272277
deps: [_ROUTER_CONFIG],
273278
},
279+
{
280+
provide: RouterStateSerializer,
281+
deps: [ROUTER_CONFIG],
282+
useFactory: _createSerializer,
283+
},
274284
],
275285
})
276286
export class StoreRouterConnectingModule {
@@ -282,14 +292,7 @@ export class StoreRouterConnectingModule {
282292
): ModuleWithProviders {
283293
return {
284294
ngModule: StoreRouterConnectingModule,
285-
providers: [
286-
{ provide: _ROUTER_CONFIG, useValue: config },
287-
{
288-
provide: ROUTER_CONFIG,
289-
useFactory: _createDefaultRouterConfig,
290-
deps: [_ROUTER_CONFIG],
291-
},
292-
],
295+
providers: [{ provide: _ROUTER_CONFIG, useValue: config }],
293296
};
294297
}
295298

modules/router-store/src/serializer.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
22

3-
export abstract class RouterStateSerializer<T> {
3+
/**
4+
* Simple router state.
5+
* All custom router states / state serializers should have at least this property.
6+
*/
7+
export interface RouterState {
8+
url: string;
9+
}
10+
11+
export abstract class RouterStateSerializer<T extends RouterState = RouterState> {
412
abstract serialize(routerState: RouterStateSnapshot): T;
513
}
614

7-
export interface SerializedRouterStateSnapshot {
15+
export interface SerializedRouterStateSnapshot extends RouterState {
816
root: ActivatedRouteSnapshot;
917
url: string;
1018
}

0 commit comments

Comments
 (0)