style(core): combineMiddlewareEffects -> combineMiddlewares
MODIFY
packages/core/src/effects/effects.combiner.ts
packages/core/src/effects/effects.combiner.ts
@@ -4,21 +4,11 @@ import { HttpRequest, HttpResponse } from '../http.interface';
import { Middleware } from './effects.interface';
export { HttpRequest, HttpResponse, Observable };
-export const combineMiddlewareEffects = (effects: Middleware[]): Middleware => {
- const middlewaresObservable = from(middlewaresGuard(effects));
-
- return (req$, res, metadata) => {
- return middlewaresObservable.pipe(
+export const combineMiddlewares = (middlewares: Middleware[] = []): Middleware => (req$, res, metadata) =>
+ middlewares.length
+ ? from(middlewares).pipe(
takeWhile(() => !res.finished),
- concatMap(effect => effect(req$, res, metadata)),
+ concatMap(middleware => middleware(req$, res, metadata)),
last(),
- );
- };
-};
-
-const middlewaresGuard = (middlewares: Middleware[]) => {
- const emptyMiddleware: Middleware = req$ => req$;
- return middlewares.length
- ? middlewares
- : [emptyMiddleware];
-};
+ )
+ : req$;

MODIFY
packages/core/src/effects/specs/effects.combiner.spec.ts
packages/core/src/effects/specs/effects.combiner.spec.ts
import { of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Middleware } from '../effects.interface';
-import { combineMiddlewareEffects } from '../effects.combiner';
+import { combineMiddlewares } from '../effects.combiner';
import { HttpResponse, HttpRequest } from '../../http.interface';
import { Marbles } from '../../+internal';
const createMockRes = () => ({} as HttpResponse);
const createMockReq = (url = '/') => ({ url } as HttpRequest);
-describe('Effects combiner', () => {
-
- test('#combineMiddlewareEffects combines middlewares into one stream', () => {
+describe('#combineMiddlewares', () => {
+ test('combines middlewares into one stream', () => {
// given
const a$: Middleware = req$ => req$.pipe(tap(req => { req.test = 1; }));
const b$: Middleware = req$ => req$.pipe(tap(req => { req.test = req.test + 1; }));
@@ -19,8 +18,8 @@ describe('Effects combiner', () => {
// when
const req = createMockReq();
const res = createMockRes();
- const combinedEffect = combineMiddlewareEffects([ a$, b$, c$ ]);
- const http$ = combinedEffect(of(req), res, undefined);
+ const combinedMiddlewares = combineMiddlewares([ a$, b$, c$ ]);
+ const http$ = combinedMiddlewares(of(req), res, undefined);
// then
Marbles.assertCombinedEffects(http$, [
@@ -30,15 +29,12 @@ describe('Effects combiner', () => {
]);
});
- test('#combineMiddlewareEffects returns stream even if middlewares are not provided', () => {
- // given
- const emptyMiddlewaresCollection = [];
-
+ test('returns stream even if middlewares are not provided', () => {
// when
const req = createMockReq();
const res = createMockRes();
- const combinedEffect = combineMiddlewareEffects(emptyMiddlewaresCollection);
- const http$ = combinedEffect(of(req), res, undefined);
+ const combinedMiddlewares = combineMiddlewares();
+ const http$ = combinedMiddlewares(of(req), res, undefined);
// then
Marbles.assertCombinedEffects(http$, [
@@ -47,5 +43,4 @@ describe('Effects combiner', () => {
}
]);
});
-
});

MODIFY
packages/core/src/http.listener.spec.ts
packages/core/src/http.listener.spec.ts
@@ -40,7 +40,7 @@ describe('Http listener', () => {
const res = {} as OutgoingMessage;
// when
- effectsCombiner.combineMiddlewareEffects = jest.fn(() => () => of(req));
+ effectsCombiner.combineMiddlewares = jest.fn(() => () => of(req));
routerFactory.factorizeRouting = jest.fn(() => []);
router.resolveRouting = jest.fn(() => () => () => of({ body: 'test' }));
responseHandler.handleResponse = jest.fn(() => () => () => undefined);
@@ -53,7 +53,7 @@ describe('Http listener', () => {
// then
setTimeout(() => {
- expect(effectsCombiner.combineMiddlewareEffects).toHaveBeenCalledWith([middleware$]);
+ expect(effectsCombiner.combineMiddlewares).toHaveBeenCalledWith([middleware$]);
expect(routerFactory.factorizeRouting).toHaveBeenCalledWith([effect$]);
expect(router.resolveRouting).toHaveBeenCalled();
expect(responseHandler.handleResponse).toHaveBeenCalled();
@@ -66,7 +66,7 @@ describe('Http listener', () => {
const req = {} as IncomingMessage;
// when
- effectsCombiner.combineMiddlewareEffects = jest.fn(() => () => of(req));
+ effectsCombiner.combineMiddlewares = jest.fn(() => () => of(req));
routerFactory.factorizeRouting = jest.fn(() => []);
errorEffect.errorEffectProvider = jest.fn(() => errorMiddleware$);
@@ -75,7 +75,7 @@ describe('Http listener', () => {
});
// then
- expect(effectsCombiner.combineMiddlewareEffects).toHaveBeenCalledWith([]);
+ expect(effectsCombiner.combineMiddlewares).toHaveBeenCalledWith([]);
});
test('#httpListener catches error', done => {
@@ -86,7 +86,7 @@ describe('Http listener', () => {
const errorHandler = jest.fn(() => of({ body: 'error' }));
// when
- effectsCombiner.combineMiddlewareEffects = jest.fn(() => () => of(req));
+ effectsCombiner.combineMiddlewares = jest.fn(() => () => of(req));
routerFactory.factorizeRouting = jest.fn(() => []);
router.resolveRouting = jest.fn(() => () => () => of({ body: 'test' }).pipe(switchMap(() => throwError(error))));
responseHandler.handleResponse = jest.fn(() => () => () => undefined);

MODIFY
packages/core/src/http.listener.ts
packages/core/src/http.listener.ts
import { IncomingMessage, OutgoingMessage } from 'http';
import { of, Subject } from 'rxjs';
import { catchError, defaultIfEmpty, mergeMap, switchMap, tap, takeWhile } from 'rxjs/operators';
-import { combineMiddlewareEffects } from './effects/effects.combiner';
+import { combineMiddlewares } from './effects/effects.combiner';
import { EffectResponse, Middleware, ErrorEffect } from './effects/effects.interface';
import { errorEffectProvider } from './error/error.effect';
import { Http, HttpRequest, HttpResponse, HttpStatus } from './http.interface';
@@ -23,7 +23,7 @@ export const httpListener = ({
}: HttpListenerConfig) => {
const request$ = new Subject<Http>();
- const combinedMiddlewares = combineMiddlewareEffects(middlewares);
+ const combinedMiddlewares = combineMiddlewares(middlewares);
const routerEffects = factorizeRouting(effects);
const providedErrorEffect = errorEffectProvider(errorEffect);
const defaultResponse = { status: HttpStatus.NOT_FOUND } as EffectResponse;

MODIFY
packages/core/src/router/router.factory.ts
packages/core/src/router/router.factory.ts
@@ -10,7 +10,7 @@ import {
RouteCombinerConfig,
} from './router.interface';
import { factorizeRegExpWithParams } from './urlParams.factory';
-import { combineMiddlewareEffects } from '../effects/effects.combiner';
+import { combineMiddlewares } from '../effects/effects.combiner';
export const factorizeRouting = (
routes: (RouteEffect | RouteEffectGroup)[],
@@ -35,7 +35,7 @@ export const factorizeRouting = (
const method: RoutingMethod = {
effect: route.effect,
middleware: middleware.length > 0
- ? middleware.length > 1 ? combineMiddlewareEffects(middleware) : middleware[0]
+ ? middleware.length > 1 ? combineMiddlewares(middleware) : middleware[0]
: undefined,
parameters,
};
@@ -58,10 +58,13 @@ export const factorizeRouting = (
return routing;
};
-export const combineRoutes = (
+export function combineRoutes(path: string, config: RouteCombinerConfig): RouteEffectGroup;
+export function combineRoutes(path: string, effects: (RouteEffect | RouteEffectGroup)[]): RouteEffectGroup;
+export function combineRoutes(
path: string,
configOrEffects: RouteCombinerConfig | (RouteEffect | RouteEffectGroup)[]
-): RouteEffectGroup => ({
+): RouteEffectGroup {
+ return {
path,
effects: isRouteCombinerConfig(configOrEffects)
? configOrEffects.effects
@@ -69,4 +72,6 @@ export const combineRoutes = (
middlewares: isRouteCombinerConfig(configOrEffects)
? (configOrEffects.middlewares || [])
: [],
-});
+ };
+}
+

MODIFY
packages/core/src/router/specs/router.factory.spec.ts
packages/core/src/router/specs/router.factory.spec.ts
@@ -44,7 +44,7 @@ describe('Router factory', () => {
];
// when
- effectsCombiner.combineMiddlewareEffects = jest.fn(() => m$);
+ effectsCombiner.combineMiddlewares = jest.fn(() => m$);
const factorizedRouting = factorizeRouting(routing);
// then

style(core): combineMiddlewareEffects -> combineMiddlewares