feature - added `use.operator` + updated test cases
MODIFY
packages/core/src/http.listener.spec.ts
packages/core/src/http.listener.spec.ts
-import { mapTo, tap } from 'rxjs/operators';
+import { map, mapTo, tap } from 'rxjs/operators';
import * as request from 'supertest';
import { Effect } from './effects/effects.interface';
+import { HttpRequest } from './http.interface';
import { httpListener } from './http.listener';
import { matchPath } from './operators/matchPath.operator';
+import { use } from './operators/use.operator';
describe('Http listener', () => {
@@ -11,6 +13,7 @@ describe('Http listener', () => {
matchPath('/test'),
mapTo({ status: 200 })
);
+
const app = httpListener({ effects: [effect$] });
return request(app)
@@ -24,6 +27,7 @@ describe('Http listener', () => {
tap(() => { throw new Error(); }),
mapTo({ status: 200 })
);
+
const app = httpListener({ effects: [effect$] });
return request(app)
@@ -31,4 +35,21 @@ describe('Http listener', () => {
.expect(500);
});
+ it('integrates with "use" operator', async () => {
+ const middleware$: Effect<HttpRequest> = req$ => req$
+ .pipe(tap(req => req.test = 'test'));
+
+ const effect$: Effect = req$ => req$.pipe(
+ matchPath('/test'),
+ use(middleware$),
+ map(req => ({ status: 200, body: { test: req.test } })),
+ );
+
+ const app = httpListener({ effects: [effect$] });
+
+ return request(app)
+ .get('/test')
+ .expect(200, { test: 'test' });
+ });
+
});

ADD
packages/core/src/operators/use.operator.spec.ts
+import { tap } from 'rxjs/operators';
+import { Effect } from '../effects/effects.interface';
+import { HttpRequest } from '../http.interface';
+import { Marbles } from '../util/marbles.spec-util';
+import { use } from './use.operator';
+
+const createMockReq = (test = 0) => ({ test } as any as HttpRequest);
+
+const middleware$: Effect<HttpRequest> = request$ => request$
+ .pipe(tap(req => req.test++));
+
+describe('Use operator', () => {
+
+ it('applies middlewares to the request pipeline', () => {
+ const operators = [
+ use(middleware$),
+ use(middleware$)
+ ];
+
+ Marbles.assert(operators, [
+ ['-a---', { a: createMockReq() }],
+ ['-a---', { a: createMockReq(2) }],
+ ]);
+ });
+
+});

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

feature - added `use.operator` + updated test cases