improvement - curried combiners
MODIFY
src/effects/effects.combiner.ts
src/effects/effects.combiner.ts
-import { Observable, merge, of } from 'rxjs';
+import { merge, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
-import { Effect, EffectResponse } from './effects.interface';
-import { HttpRequest, HttpResponse } from '../http.interface';
+import { EffectCombiner, MiddlewareCombiner } from './effects.interface';
+import { HttpRequest } from '../http.interface';
-export const combineEffects = (...effects: Effect<EffectResponse>[]) => (
- request$: Observable<HttpRequest>,
- response: HttpResponse
-) => {
- const mappedEffects = effects.map(effect => effect(request$, response));
+export const combineEffects: EffectCombiner = effects => res => req => {
+ const req$ = of(req);
+ const mappedEffects = effects.map(effect => effect(req$, res));
return merge(...mappedEffects);
};
-export const combineMiddlewareEffects = (...effects: Effect<HttpRequest>[]) => (
- request$: Observable<HttpRequest>,
- response: HttpResponse
-) => {
+export const combineMiddlewareEffects: MiddlewareCombiner = effects => res => req => {
+ const req$ = of(req);
const mappedEffects = effects.map(effect =>
- switchMap((mappedReqest: HttpRequest) => effect(of(mappedReqest), response))
+ switchMap((mappedReqest: HttpRequest) => effect(of(mappedReqest), res))
);
- return request$.pipe(...mappedEffects);
+ return req$.pipe(...mappedEffects);
};

MODIFY
src/effects/effects.interface.ts
src/effects/effects.interface.ts
@@ -12,3 +12,10 @@ export type Effect<T = EffectResponse> = (
request$: Observable<HttpRequest>,
response: HttpResponse,
) => Observable<T>;
+
+
+export type EffectCombiner = (effects: Effect<EffectResponse>[]) => (res: HttpResponse) => (req: HttpRequest) =>
+ Observable<EffectResponse>;
+
+export type MiddlewareCombiner = (effects: Effect<HttpRequest>[]) => (res: HttpResponse) => (req: HttpRequest) =>
+ Observable<HttpRequest>;

MODIFY
src/http.listener.ts
src/http.listener.ts
-import { Subject, of } from 'rxjs';
+import { Subject } from 'rxjs';
import { defaultIfEmpty, tap, switchMap } from 'rxjs/operators';
import { Http, HttpRequest, HttpResponse } from './http.interface';
import { combineEffects, Effect, combineMiddlewareEffects } from './effects';
@@ -10,9 +10,9 @@ export const httpListener = (middlewares: Effect<HttpRequest>[], effects: Effect
const effect$ = request$
.pipe(
switchMap(({ req, res }) =>
- combineMiddlewareEffects(...middlewares)(of(req), res)
+ combineMiddlewareEffects(middlewares)(res)(req)
.pipe(
- switchMap(req => combineEffects(...effects)(of(req), res)),
+ switchMap(combineEffects(effects)(res)),
defaultIfEmpty({ status: StatusCode.NOT_FOUND }),
tap(handleResponse(res))
)

MODIFY
src/middlewares/index.ts
src/middlewares/index.ts
export * from './logger.middleware';
-export * from './middleware.interface';

DELETE
src/middlewares/middleware.interface.ts
-import { Http } from '../http.interface';
-
-export type Middleware = (http: Http) => Http;

improvement - curried combiners