feat(core): match all paths for * sign
MODIFY
packages/core/src/operators/matchPath/matchPath.operator.ts
packages/core/src/operators/matchPath/matchPath.operator.ts
@@ -21,8 +21,11 @@ export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Obs
tap(req => (req.matchers = req.matchers || [])),
filter(isRequestMatched),
filter(req => {
+ if (path === '*') { return true; }
+
const matcher = matcherFactory(req.matchers!, path, opts.suffix);
const url = removeQueryParams(req.url!);
+
return pathToRegexp(matcher).test(url);
}),
tap(req => (req.params = urlParamsFactory(req, path))),

MODIFY
packages/spec/routing.integration.spec.ts
packages/spec/routing.integration.spec.ts
import { delay, map, tap } from 'rxjs/operators';
import * as request from 'supertest';
-import { Effect, httpListener, matchPath, matchType } from '../core/src';
+import { Effect, combineRoutes, httpListener, matchPath, matchType } from '../core/src';
describe('Routing integration', async () => {
@@ -25,13 +25,23 @@ describe('Routing integration', async () => {
matchPath('/p3'), matchType('GET'), map(req => ({ body: req.test }))
);
- const app = httpListener({ effects: [p1$, p2$, p3$ ] });
+ const all$: Effect = req$ => req$.pipe(
+ tap(req => req.test = req.test || []),
+ tap(req => req.test.push('all')),
+ matchPath('*'), matchType('GET'), map(req => ({ body: req.test }))
+ );
+
+ const combined$ = combineRoutes('/combined', [p1$]);
+
+ const app = httpListener({ effects: [p1$, p2$, combined$, p3$, all$] });
// when
return Promise.all([
request(app).get('/p1').expect(200, [1]),
+ request(app).get('/combined/p1').expect(200, [1, 2, 1]),
request(app).get('/p2').expect(200, [1, 2]),
request(app).get('/p3').expect(200, [1, 2, 3]),
+ request(app).get('/combined/not-found').expect(200, [1, 2, 1, 3, 'all']),
]);
});
@@ -91,4 +101,24 @@ describe('Routing integration', async () => {
]);
});
+ test('when no paths are matched - executes last "*" handler', () => {
+ // given
+ const p1$: Effect = req$ => req$.pipe(
+ matchPath('/p1'), matchType('GET'), map(req => ({ body: req.test }))
+ );
+
+ const p2$: Effect = req$ => req$.pipe(
+ matchPath('/p2'), matchType('GET'), map(req => ({ body: req.test }))
+ );
+
+ const notFound$: Effect = req$ => req$.pipe(
+ matchPath('*'), matchType('GET'), map(req => ({ body: 'not-found' }))
+ );
+
+ const app = httpListener({ effects: [p1$, p2$, notFound$ ] });
+
+ // when
+ return request(app).get('/test').expect(200, '"not-found"');
+ });
+
});

feat(core): match all paths for * sign