feat(core): reusable EffectResponse interface
MODIFY
packages/core/src/effects/effects.interface.ts
packages/core/src/effects/effects.interface.ts
import { Observable } from 'rxjs';
-import { HttpRequest, HttpResponse, HttpStatus } from '../http.interface';
+import { HttpRequest, HttpResponse, HttpStatus, HttpHeaders } from '../http.interface';
-export interface EffectResponse {
+export interface EffectResponse<T = any> {
+ body?: T;
+}
+
+export interface EffectHttpResponse<T = any> extends EffectResponse<T> {
status?: HttpStatus;
- body?: any;
- headers?: Record<string, string>;
+ headers?: HttpHeaders;
}
export interface Middleware<
@@ -13,8 +16,12 @@ export interface Middleware<
> extends Effect<I, O> {}
export interface ErrorEffect<T extends Error = Error>
- extends Effect<HttpRequest, EffectResponse, T> {}
+ extends Effect<HttpRequest, EffectHttpResponse, T> {}
-export interface Effect<T extends HttpRequest = HttpRequest, U = EffectResponse, V = any> {
+export interface Effect<
+ T extends HttpRequest = HttpRequest,
+ U extends EffectResponse | HttpRequest = EffectHttpResponse,
+ V = any
+> {
(req$: Observable<T>, res: HttpResponse, meta: V): Observable<U>;
}

MODIFY
packages/core/src/http.interface.ts
packages/core/src/http.interface.ts
import * as http from 'http';
import { Observable } from 'rxjs';
-import { EffectResponse } from './effects/effects.interface';
+import { EffectHttpResponse } from './effects/effects.interface';
export interface HttpRequest<
TBody = any,
@@ -24,7 +24,7 @@ export interface QueryParameters {
}
export interface HttpResponse extends http.ServerResponse {
- send: (effect: EffectResponse) => Observable<never>;
+ send: (response: EffectHttpResponse) => Observable<never>;
}
export interface HttpHeaders extends Record<string, string> {}

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

MODIFY
packages/core/src/response/response.handler.ts
packages/core/src/response/response.handler.ts
import { EMPTY } from 'rxjs';
-import { EffectResponse } from '../effects/effects.interface';
+import { EffectHttpResponse } from '../effects/effects.interface';
import { HttpRequest, HttpResponse, HttpStatus } from '../http.interface';
import { bodyFactory } from './responseBody.factory';
import { headersFactory } from './responseHeaders.factory';
-export const handleResponse = (res: HttpResponse) => (req: HttpRequest) => (effect: EffectResponse) => {
+export const handleResponse = (res: HttpResponse) => (req: HttpRequest) => (effect: EffectHttpResponse) => {
if (res.finished) { return EMPTY; }
const status = effect.status || HttpStatus.OK;

MODIFY
packages/core/src/router/router.interface.ts
packages/core/src/router/router.interface.ts
import { HttpMethod, HttpRequest } from '../http.interface';
-import { Effect, Middleware, EffectResponse } from '../effects/effects.interface';
+import { Effect, Middleware, EffectHttpResponse } from '../effects/effects.interface';
// Route
export interface RouteEffect<T extends HttpRequest = HttpRequest> {
path: string;
method: HttpMethod;
- effect: Effect<T, EffectResponse>;
+ effect: Effect<T, EffectHttpResponse>;
middleware?: Middleware;
}

MODIFY
packages/core/src/router/router.resolver.ts
packages/core/src/router/router.resolver.ts
import { EMPTY, Observable, of } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { HttpMethod, HttpRequest, HttpResponse } from '../http.interface';
-import { EffectResponse } from '../effects/effects.interface';
+import { EffectHttpResponse } from '../effects/effects.interface';
import { RouteMatched, Routing, RoutingItem } from './router.interface';
import { queryParamsFactory } from './router.query.factory';
export { RoutingItem };
@@ -38,7 +38,7 @@ export const findRoute = (
export const resolveRouting =
(routing: Routing) =>
(res: HttpResponse) =>
- (req: HttpRequest): Observable<EffectResponse> => {
+ (req: HttpRequest): Observable<EffectHttpResponse> => {
if (res.finished) { return EMPTY; }
const [urlPath, urlQuery] = req.url.split('?');

feat(core): reusable EffectResponse interface