improvement - add request parameter to effects
MODIFY
src/effects/effects.combiner.ts
src/effects/effects.combiner.ts
import * as http from 'http';
import { Observable, merge } from 'rxjs';
import { RequestEffect } from './effects.interface';
-import { HttpRequest } from '../http.interface';
+import { HttpRequest, HttpResponse } from '../http.interface';
-export const combineEffects = (...effects: RequestEffect[]) => (request$: Observable<HttpRequest>) =>
- merge(
- ...effects.map(effect => {
- const output$ = effect(request$);
-
- if (!output$) {
- throw new TypeError(`combineEffects: one of the provided Effects does not return a stream!`);
- }
-
- return output$;
- })
- );
+export const combineEffects = (...effects: RequestEffect[]) => (
+ request$: Observable<HttpRequest>,
+ response: HttpResponse
+) => {
+ const mappedEffects = effects.map(effect => effect(request$, response));
+ return merge(...mappedEffects);
+};

MODIFY
src/effects/effects.interface.ts
src/effects/effects.interface.ts
import { Observable } from 'rxjs';
-import { HttpRequest } from '../http.interface';
+import { HttpRequest, HttpResponse } from '../http.interface';
export type EffectResponse = {
status: number,
@@ -7,4 +7,7 @@ export type EffectResponse = {
headers?: Record<string, string>,
};
-export type RequestEffect = (request$: Observable<HttpRequest>) => Observable<EffectResponse>;
+export type RequestEffect = (
+ request$: Observable<HttpRequest>,
+ response?: HttpResponse,
+) => Observable<EffectResponse>;

MODIFY
src/http.listener.ts
src/http.listener.ts
@@ -8,18 +8,16 @@ import { handleResponse } from './response';
export const httpListener = (...effects: RequestEffect[]) => {
const request$ = new Subject<Http>();
-
- request$
+ const effect$ = request$
.pipe(
map(loggerMiddleware),
- switchMap(http => combineEffects(...effects)(of(http.req))
- .pipe(
- tap(effect => handleResponse(http.res)(effect))
- )
+ switchMap(({ req, res }) =>
+ combineEffects(...effects)(of(req), res)
+ .pipe(tap(effect => handleResponse(res)(effect)))
),
- )
- .subscribe();
+ );
+ effect$.subscribe();
return (req: HttpRequest, res: HttpResponse) => request$.next({ req, res });
};

improvement - add request parameter to effects