improvements - intercepting url parameters - ingore suffixes + added test cases
MODIFY
packages/core/src/http.listener.spec.ts
packages/core/src/http.listener.spec.ts
import { map, mapTo, tap } from 'rxjs/operators';
import * as request from 'supertest';
+import { combineRoutes } from './effects/effects.combiner';
import { Effect } from './effects/effects.interface';
import { HttpRequest } from './http.interface';
import { httpListener } from './http.listener';
@@ -52,4 +53,55 @@ describe('Http listener', () => {
.expect(200, { test: 'test' });
});
+ it('integrates with "matchPath" operator', async () => {
+ const effect$1: Effect = req$ => req$.pipe(
+ matchPath('/test/:id/foo'),
+ map(req => req.route.params.id),
+ map(id => ({ status: 200, body: { id } })),
+ );
+
+ const effect$2: Effect = req$ => req$.pipe(
+ matchPath('/test/:id/foo/:id/test'),
+ map(req => req.route.params),
+ map(params => ({ status: 200, body: { id: params.id } })),
+ );
+
+ const effect$3: Effect = req$ => req$.pipe(
+ matchPath('/test/:id/bar/:type/test'),
+ map(req => req.route.params),
+ map(params => ({ status: 200, body: { id: params.id, type: params.type } })),
+ );
+
+ const app = httpListener({ effects: [effect$1, effect$2, effect$3] });
+
+ return Promise.all([
+ request(app)
+ .get('/test/2/foo')
+ .expect(200, { id: '2' }),
+
+ request(app)
+ .get('/test/2/foo/marble/test')
+ .expect(200, { id: 'marble' }),
+
+ request(app)
+ .get('/test/2/bar/marble/test')
+ .expect(200, { id: '2', type: 'marble' }),
+ ]);
+ });
+
+ it('integrates with "combineRoutes"', async () => {
+ const effect$: Effect = req$ => req$.pipe(
+ matchPath('/user/:id'),
+ map(req => req.route.params),
+ map(params => ({ status: 200, body: { id: params.id, version: params.version } })),
+ );
+
+ const api$ = combineRoutes('/api/:version', [ effect$ ]);
+ const app = httpListener({ effects: [api$] });
+
+ return request(app)
+ .get('/api/v1/user/1')
+ .expect(200, { version: 'v1', id: '1' });
+ });
+
});

MODIFY
packages/core/src/operators/matchPath.operator.spec.ts
packages/core/src/operators/matchPath.operator.spec.ts
-import { HttpRequest, RouteParameters, HttpRoute } from '../http.interface';
+import { HttpRequest, HttpRoute, RouteParameters } from '../http.interface';
import { Marbles } from '../util/marbles.spec-util';
import { matchPath } from './matchPath.operator';
@@ -74,9 +74,9 @@ describe('matchPath operator', () => {
c: req('/test/url/bar'),
}],
['-a-b-c-', {
- a: reqMatched('/test', null, [], { foo: undefined }),
- b: reqMatched('/test/bar', null, [], { foo: 'bar' }),
- c: reqMatched('/test/url/bar', null, [], { foo: 'url/bar' }),
+ a: reqMatched('/test'),
+ b: reqMatched('/test/bar'),
+ c: reqMatched('/test/url/bar'),
}],
]);
});
@@ -87,6 +87,12 @@ describe('matchPath operator', () => {
['-a--', { a: req('/test') }],
['-a--', { a: reqMatched('/test', null, ['/test']) }],
]);
+
+ operators = [matchPath('/test/:id/foo', { combiner: true, suffix: '/:foo*' })];
+ Marbles.assert(operators, [
+ ['-a--', { a: req('/test/2/foo') }],
+ ['-a--', { a: reqMatched('/test/2/foo', null, ['/test/:id/foo'], { id: '2' }) }],
+ ]);
});
});

MODIFY
packages/core/src/operators/matchPath.operator.ts
packages/core/src/operators/matchPath.operator.ts
@@ -34,8 +34,8 @@ const paramFactory = (params: RegExpMatchArray | null, routes: pathToRegexp.Toke
}), {});
};
-const routeFactory = (req: HttpRequest, path: string, opts: MatcherOpts = {}): HttpRoute => {
- const match = pathFactory(req.matchers!, path, opts.suffix);
+const routeFactory = (req: HttpRequest, path: string): HttpRoute => {
+ const match = pathFactory(req.matchers!, path);
const url = urlFactory(req.url!);
const params = pathToRegexp(match).exec(url);
const routes = pathToRegexp.parse(match);
@@ -56,7 +56,7 @@ export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Obs
}),
tap(req => req.route = {
...req.route,
- ...routeFactory(req, path, opts),
+ ...routeFactory(req, path),
}),
tap(req => opts.combiner && req.matchers!.push(path))
);

improvements - intercepting url parameters - ingore suffixes + added test cases