refactor(core): `combineRoutes` -> `router.factory` file
MODIFY
packages/core/src/index.ts
packages/core/src/index.ts
export { EffectFactory } from './effects/effects.factory';
export { error$ } from './error/error.middleware';
export { HttpError } from './error/error.model';
-export { combineRoutes } from './router/router';
+export { combineRoutes } from './router/router.factory';
export * from './effects/effects.interface';
export * from './router/router.interface';
export * from './http.interface';

MODIFY
packages/core/src/router/router.factory.spec.ts
packages/core/src/router/router.factory.spec.ts
import { mapTo } from 'rxjs/operators';
+import { EffectFactory } from '../effects/effects.factory';
import { Effect, Middleware } from '../effects/effects.interface';
import { RouteEffect, RouteEffectGroup, Routing } from './router.interface';
-import { factorizeRouting } from './router.factory';
+import { factorizeRouting, combineRoutes } from './router.factory';
describe('Router factory', () => {
let effectsCombiner;
@@ -83,4 +84,65 @@ describe('Router factory', () => {
expect(error).toThrowError('Redefinition of route at "GET: /test"');
});
+ describe('#combineRoutes', () => {
+ test('factorizes combined routes for effects only', () => {
+ // given
+ const effect$ = req$ => req$.pipe(mapTo({}));
+ const a$ = EffectFactory.matchPath('/a').matchType('GET').use(effect$);
+ const b$ = EffectFactory.matchPath('/b').matchType('GET').use(effect$);
+
+ // when
+ const combiner = combineRoutes('/test', [a$, b$]);
+
+ // then
+ expect(combiner).toEqual({
+ path: '/test',
+ middlewares: [],
+ effects: [a$, b$],
+ });
+ });
+
+ test('factorizes combined routes for effects with middlewares', () => {
+ // given
+ const effect$ = req$ => req$.pipe(mapTo({}));
+ const a$ = EffectFactory.matchPath('/a').matchType('GET').use(effect$);
+ const b$ = EffectFactory.matchPath('/b').matchType('GET').use(effect$);
+
+ const m1$: Middleware = req$ => req$;
+ const m2$: Middleware = req$ => req$;
+
+ // when
+ const combiner = combineRoutes('/test', {
+ middlewares: [m1$, m2$],
+ effects: [a$, b$],
+ });
+
+ // then
+ expect(combiner).toEqual({
+ path: '/test',
+ middlewares: [m1$, m2$],
+ effects: [a$, b$],
+ });
+ });
+
+ test('factorizes combined routes for effects with empty middlewares', () => {
+ // given
+ const effect$ = req$ => req$.pipe(mapTo({}));
+ const a$ = EffectFactory.matchPath('/a').matchType('GET').use(effect$);
+ const b$ = EffectFactory.matchPath('/b').matchType('GET').use(effect$);
+
+ // when
+ const combiner = combineRoutes('/test', {
+ effects: [a$, b$],
+ });
+
+ // then
+ expect(combiner).toEqual({
+ path: '/test',
+ middlewares: [],
+ effects: [a$, b$],
+ });
+ });
+ });
+
});

MODIFY
packages/core/src/router/router.factory.ts
packages/core/src/router/router.factory.ts
import { Effect } from '../effects/effects.interface';
import { HttpRequest } from '../http.interface';
-import { isRouteEffectGroup } from './router.helpers';
-import { Routing, RoutingMethod, RoutingItem, RouteEffect, RouteEffectGroup } from './router.interface';
+import { isRouteEffectGroup, isRouteCombinerConfig } from './router.helpers';
+import {
+ Routing,
+ RoutingMethod,
+ RoutingItem,
+ RouteEffect,
+ RouteEffectGroup,
+ RouteCombinerConfig,
+} from './router.interface';
import { factorizeRegExpWithParams } from './urlParams.factory';
import { combineMiddlewareEffects } from '../effects/effects.combiner';
@@ -47,3 +54,16 @@ export const factorizeRouting = (
return routing;
};
+
+export const combineRoutes = (
+ path: string,
+ configOrEffects: RouteCombinerConfig | (RouteEffect | RouteEffectGroup)[]
+): RouteEffectGroup => ({
+ path,
+ effects: isRouteCombinerConfig(configOrEffects)
+ ? configOrEffects.effects
+ : configOrEffects,
+ middlewares: isRouteCombinerConfig(configOrEffects)
+ ? (configOrEffects.middlewares || [])
+ : [],
+});

MODIFY
packages/core/src/router/router.helpers.ts
packages/core/src/router/router.helpers.ts
@@ -7,5 +7,4 @@ export const isRouteEffectGroup = (item): item is RouteEffectGroup =>
Array.isArray(item.middlewares);
export const isRouteCombinerConfig = (item): item is RouteCombinerConfig =>
- Array.isArray(item.effects) &&
- Array.isArray(item.middlewares);
+ Array.isArray(item.effects);

MODIFY
packages/core/src/router/router.ts
packages/core/src/router/router.ts
import { EMPTY, Observable, of } from 'rxjs';
import { HttpMethod, HttpRequest, HttpResponse } from '../http.interface';
import { EffectResponse } from '../effects/effects.interface';
-import { isRouteCombinerConfig } from './router.helpers';
-import { RouteCombinerConfig, RouteEffect, RouteEffectGroup, RouteMatched, Routing } from './router.interface';
+import { RouteMatched, Routing } from './router.interface';
import { queryParamsFactory } from '../router/queryParams.factory';
-export const combineRoutes = (
- path: string,
- configOrEffects: RouteCombinerConfig | (RouteEffect | RouteEffectGroup)[]
-): RouteEffectGroup => ({
- path,
- effects: isRouteCombinerConfig(configOrEffects) ? configOrEffects.effects : configOrEffects,
- middlewares: isRouteCombinerConfig(configOrEffects) ? (configOrEffects.middlewares || []) : [],
-});
-
export const findRoute = (
routing: Routing,
url: string,

refactor(core): `combineRoutes` -> `router.factory` file