Improvement - changes in params structure in matchType operator
MODIFY
packages/core/src/http.interface.ts
packages/core/src/http.interface.ts
@@ -3,17 +3,12 @@ import * as http from 'http';
export interface HttpRequest extends http.IncomingMessage {
body?: any;
matchers?: string[];
- route?: HttpRoute;
+ params?: RouteParameters;
[key: string]: any;
}
export type RouteParameters = Record<string, string | number>;
-export interface HttpRoute {
- url: string;
- params?: RouteParameters;
-}
-
export interface HttpResponse extends http.ServerResponse {}
export type HttpHeaders = Record<string, string>;

ADD
packages/core/src/operators/index.ts
+export { matchPath } from './matchPath/matchPath.operator';
+export { use } from './use/use.operator';

DELETE
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, HttpRoute, RouteParameters } from '../http.interface';
-
-type MatcherOpts = {
- suffix?: string,
- combiner?: boolean,
-};
-
-const removeTrailingSlash = (path: string): string =>
- path.replace(/\/$/, '');
-
-const removeQueryParams = (path: string): string =>
- path.split('?')[0];
-
-const pathFactory = (matchers: string[], path: string, suffix?: string): string =>
- removeTrailingSlash(
- matchers.reduce((prev, cur) => prev + cur, '')
- + path + (suffix || '')
- );
-
-const urlFactory = (path: string): string =>
- removeQueryParams(path);
-
-const paramFactory = (params: RegExpMatchArray | null, routes: pathToRegexp.Token[]): RouteParameters => {
- if (!params || !routes) { return {}; }
-
- return routes
- .filter(route => route.hasOwnProperty('name'))
- .reduce((obj, route, index) => ({
- ...obj,
- [route['name']]: params[++index],
- }), {});
-};
-
-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);
-
- return {
- url,
- params: paramFactory(params, routes),
- };
-};
-
-export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Observable<HttpRequest>) =>
- source$.pipe(
- tap(req => req.matchers = req.matchers || []),
- filter(req => {
- const match = pathFactory(req.matchers!, path, opts.suffix);
- const url = urlFactory(req.url!);
- return pathToRegexp(match).test(url);
- }),
- tap(req => req.route = {
- ...req.route,
- ...routeFactory(req, path),
- }),
- tap(req => opts.combiner && req.matchers!.push(path))
- );

ADD
packages/core/src/operators/matchPath/matchPath.factory.ts
+const removeTrailingSlash = (path: string): string =>
+ path.replace(/\/$/, '');
+
+const removeQueryParams = (path: string): string =>
+ path.split('?')[0];
+
+export const pathFactory = (matchers: string[], path: string, suffix?: string): string =>
+ removeTrailingSlash(
+ matchers.reduce((prev, cur) => prev + cur, '')
+ + path + (suffix || '')
+ );
+
+export const urlFactory = (path: string): string =>
+ removeQueryParams(path);

RENAME
packages/core/src/operators/matchPath.operator.spec.ts
packages/core/src/operators/matchPath/matchPath.operator.spec.ts
-import { HttpRequest, HttpRoute, RouteParameters } from '../http.interface';
-import { Marbles } from '../util/marbles.spec-util';
+import { HttpRequest, RouteParameters } from '../../http.interface';
+import { Marbles } from '../../util/marbles.spec-util';
import { matchPath } from './matchPath.operator';
const req = (url: string) => ({ url } as HttpRequest);
-const reqRoute = (url: string, params: RouteParameters = {}) => ({ url, params }) as HttpRoute;
const reqMatched = (url: string, query = '', matchers: string[] = [], params?: RouteParameters) => ({
url: !!query ? `${url}?${query}` : url,
matchers,
- route: reqRoute(url, params),
+ params: params,
} as HttpRequest);
describe('matchPath operator', () => {

ADD
packages/core/src/operators/matchPath/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 { pathFactory, urlFactory } from './matchPath.factory';
+import { urlParamsFactory } from './urlParams.factory';
+
+type MatcherOpts = {
+ suffix?: string,
+ combiner?: boolean,
+};
+
+export const matchPath = (path: string, opts: MatcherOpts = {}) => (source$: Observable<HttpRequest>) =>
+ source$.pipe(
+ tap(req => req.matchers = req.matchers || []),
+ filter(req => {
+ const match = pathFactory(req.matchers!, path, opts.suffix);
+ const url = urlFactory(req.url!);
+ return pathToRegexp(match).test(url);
+ }),
+ tap(req => req.params = urlParamsFactory(req, path)),
+ tap(req => opts.combiner && req.matchers!.push(path))
+ );

RENAME
packages/core/src/operators/matchType.operator.spec.ts
packages/core/src/operators/matchPath/matchType.operator.spec.ts
-import { HttpRequest } from '../http.interface';
-import { Marbles } from '../util/marbles.spec-util';
+import { HttpRequest } from '../../http.interface';
+import { Marbles } from '../../util/marbles.spec-util';
import { matchType } from './matchType.operator';
const createMockReq = (method: string) => ({ method } as HttpRequest);

RENAME
packages/core/src/operators/matchType.operator.ts
packages/core/src/operators/matchPath/matchType.operator.ts
import { Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
-import { HttpMethod, HttpRequest } from '../http.interface';
+import { HttpMethod, HttpRequest } from '../../http.interface';
export const matchType = (method: HttpMethod) => (source$: Observable<HttpRequest>) =>
source$.pipe(

ADD
packages/core/src/operators/matchPath/urlParams.factory.ts
+import * as pathToRegexp from 'path-to-regexp';
+import { HttpRequest, RouteParameters } from '../../http.interface';
+import { pathFactory, urlFactory } from './matchPath.factory';
+
+export const urlParamsFactory = (req: HttpRequest, path: string): RouteParameters => {
+ const match = pathFactory(req.matchers!, path);
+ const url = urlFactory(req.url!);
+ const params = pathToRegexp(match).exec(url);
+ const routes = pathToRegexp.parse(match);
+
+ if (!params || !routes) { return {}; }
+
+ return routes
+ .filter(route => route.hasOwnProperty('name'))
+ .reduce((obj, route, index) => ({
+ ...obj,
+ [route['name']]: params[++index],
+ }), {});
+};

RENAME
packages/core/src/operators/use.operator.spec.ts
packages/core/src/operators/use/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 { 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);

RENAME
packages/core/src/operators/use.operator.ts
packages/core/src/operators/use/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';
+import { Effect } from '../../effects/effects.interface';
+import { HttpRequest, HttpResponse } from '../../http.interface';
export const use =
(middleware: Effect<HttpRequest>, res?: HttpResponse) =>

Improvement - changes in params structure in matchType operator