feat(core): continuous HTTP stream (code improvements)
MODIFY
packages/core/src/+internal/utils/array.util.ts
packages/core/src/+internal/utils/array.util.ts
@@ -16,4 +16,4 @@ export const mapArray = <T, R>(f: (v: T) => R) => (array: T[]) =>
array.map(f);
export const insertIf = <T>(condition: boolean, ...elements: T[]) =>
- condition ? elements : [];
+ condition ? elements as NonNullable<T>[] : [];

MODIFY
packages/core/src/http/router/http.router.factory.ts
packages/core/src/http/router/http.router.factory.ts
+import { insertIf } from '../../+internal';
import { HttpMiddlewareEffect } from '../effects/http.effects.interface';
import { isRouteEffectGroup } from './http.router.helpers';
import {
@@ -37,9 +38,10 @@ export const factorizeRouting = (
parameters,
effect: route.effect,
meta: route.meta,
- middlewares: middlewares.length
- ? route.middleware ? [...middlewares, route.middleware] : middlewares
- : route.middleware ? [route.middleware] : [],
+ middlewares: [
+ ...middlewares,
+ ...insertIf(!!route.middleware, route.middleware),
+ ],
};
if (foundRoute) {

MODIFY
packages/core/src/http/server/specs/http.server.listener.spec.ts
packages/core/src/http/server/specs/http.server.listener.spec.ts
import * as http from 'http';
import { throwError } from 'rxjs';
-import { mapTo, mergeMap, map } from 'rxjs/operators';
+import { mapTo, mergeMap } from 'rxjs/operators';
import { r } from '../../router/http.router.ixbuilder';
import { httpListener } from '../http.server.listener';
import { HttpMiddlewareEffect, HttpEffectResponse } from '../../effects/http.effects.interface';
@@ -32,12 +32,9 @@ describe('Http listener', () => {
const effect$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
- r.useEffect(req$ => {
-
- return r.handle(
+ r.useEffect(req$ => req$.pipe(
mapTo(effectResponse),
- )(req$);
- }),
+ )),
);
const middleware$: HttpMiddlewareEffect = req$ => req$;
@@ -59,28 +56,18 @@ describe('Http listener', () => {
// given
const req = { url: '/', method: 'GET' } as http.IncomingMessage;
const res = {} as http.OutgoingMessage;
+ const effectResponse = { body: 'test' } as HttpEffectResponse;
const sender = jest.fn();
const effect$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
- r.useEffect(req$ => {
-
- return r.handle(
- mapTo(1),
- map(x => x + 1),
- map(x => x + 1),
- map(x => x + 1),
- map(x => x + 1),
- map(x => x + 1),
- map(x => x + 1),
- map(x => ({
- body: x + 1,
- })),
- )(req$);
- }),
+ r.useEffect(req$ => req$.pipe(
+ mapTo(effectResponse),
+ )),
);
+
// when
responseHandler.handleResponse = jest.fn(() => () => () => sender);
@@ -90,7 +77,7 @@ describe('Http listener', () => {
})(context)(req, res);
// then
- expect(sender).toHaveBeenCalledWith({ body: 8, request: { ...req, response: res }});
+ expect(sender).toHaveBeenCalledWith({ body: 'test', request: { ...req, response: res }});
done();
});

feat(core): continuous HTTP stream (code improvements)