feat(core): improved HttpRequest types interence for middlewares composition
MODIFY
packages/core/src/effects/effects.interface.ts
packages/core/src/effects/effects.interface.ts
@@ -7,7 +7,11 @@ export interface EffectResponse {
headers?: Record<string, string>;
}
-export type Middleware = Effect<HttpRequest>;
+export type Middleware<
+ TBody = any,
+ TParams = any,
+ TQuery = any,
+> = Effect<HttpRequest<TBody, TParams, TQuery>>;
export type ErrorEffect = Effect<EffectResponse, Error>;

MODIFY
packages/core/src/http.interface.ts
packages/core/src/http.interface.ts
@@ -2,17 +2,26 @@ import * as http from 'http';
import { Observable } from 'rxjs';
import { EffectResponse } from './effects/effects.interface';
-export interface HttpRequest extends http.IncomingMessage {
+export interface HttpRequest<
+ TBody = any,
+ TParams = any,
+ TQuery = any,
+> extends http.IncomingMessage {
url: string;
method: HttpMethod;
- body?: any;
- params: RouteParameters;
- query: QueryParameters;
+ body?: TBody;
+ params: TParams;
+ query: TQuery;
[key: string]: any;
}
-export type RouteParameters = Record<string, string>;
-export type QueryParameters = Record<string, string | number | object>;
+export interface RouteParameters {
+ [key: string]: any;
+}
+
+export interface QueryParameters {
+ [key: string]: any;
+}
export interface HttpResponse extends http.ServerResponse {
send: (effect: EffectResponse) => Observable<never>;

MODIFY
packages/core/src/operators/use/use.operator.ts
packages/core/src/operators/use/use.operator.ts
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
-import { Effect } from '../../effects/effects.interface';
+import { Middleware } from '../../effects/effects.interface';
import { HttpRequest, HttpResponse } from '../../http.interface';
-export const use =
- (middleware: Effect<HttpRequest>, res?: HttpResponse) =>
- (source$: Observable<HttpRequest>) =>
+export const use = <T, U, V>
+ (middleware: Middleware<T, U, V>, res?: HttpResponse) =>
+ (source$: Observable<HttpRequest>): Observable<HttpRequest<T, U, V>> =>
source$.pipe(
switchMap(req => middleware(of(req), res!, {}))
);

feat(core): improved HttpRequest types interence for middlewares composition