improvements - intercepting url parameters
MODIFY
packages/core/src/http.interface.ts
packages/core/src/http.interface.ts
@@ -3,9 +3,20 @@ import * as http from 'http';
export interface HttpRequest extends http.IncomingMessage {
body?: any;
matchers?: string[];
+ route?: HttpRoute;
[key: string]: any;
}
+export interface HttpRoute {
+ url?: string;
+ params?: HttpParameters;
+ query?: HttpParameters;
+}
+
+export interface HttpParameters {
+ [key: string]: string | number;
+}
+
export interface HttpResponse extends http.ServerResponse {}
export type HttpHeaders = Record<string, string>;

MODIFY
packages/core/src/operators/matchPath.operator.ts
packages/core/src/operators/matchPath.operator.ts
import * as pathToRegexp from 'path-to-regexp';
import { Observable } from 'rxjs';
import { filter, tap } from 'rxjs/operators';
-import { HttpRequest } from '../http.interface';
+import { HttpRequest, HttpParameters, HttpRoute } from '../http.interface';
type MatcherOpts = {
suffix?: string,
@@ -23,6 +23,29 @@ const pathFactory = (matchers: string[], path: string, suffix?: string): string
const urlFactory = (path: string): string =>
removeQueryParams(path);
+const getParams = (params: RegExpMatchArray | null, routes: pathToRegexp.Token[]): HttpParameters => {
+ if (!params || !routes) { return {}; }
+
+ return routes
+ .filter(route => route.hasOwnProperty('name'))
+ .reduce((obj, route, index) => ({
+ ...obj,
+ [route['name']]: params[index],
+ }), {});
+};
+
+const getRoute = (req: HttpRequest, path: string, opts: MatcherOpts = {}): HttpRoute => {
+ const match = pathFactory(req.matchers!, path, opts.suffix);
+ const url = urlFactory(req.url!);
+ const params = pathToRegexp(match).exec(url);
+ const routes = pathToRegexp.parse(match);
+
+ return {
+ url: req.url,
+ params: getParams(params, routes),
+ };
+};
+
export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Observable<HttpRequest>) =>
source$.pipe(
tap(req => req.matchers = req.matchers || []),
@@ -31,5 +54,6 @@ export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Obs
const url = urlFactory(req.url!);
return pathToRegexp(match).test(url);
}),
+ tap(req => req.route = getRoute(req, path, opts)),
tap(req => opts.combiner && req.matchers!.push(path))
);

improvements - intercepting url parameters