refactor(core): removed dead code
MODIFY
packages/core/src/http.interface.ts
packages/core/src/http.interface.ts
@@ -6,9 +6,6 @@ export interface HttpRequest extends http.IncomingMessage {
body?: any;
params: RouteParameters;
query: QueryParameters;
- matchers?: string[];
- matchPath: boolean;
- matchType: boolean;
[key: string]: any;
}

DELETE
packages/core/src/operators/matchPath/matchPath.factory.spec.ts
-import { matchPathFactory, removeQueryParams, removeTrailingSlash } from './matchPath.factory';
-
-describe('matchPath factory', () => {
-
- it('#removeQueryParams removes query parameters from url', () => {
- // given
- const input = 'api/v1/foo?testParam';
- const result = 'api/v1/foo';
-
- // when
- const urlWithoutQuery = removeQueryParams(input);
-
- // then
- expect(urlWithoutQuery).toEqual(result);
- });
-
- it('#removeTrailingSlash removes trailing slash from url', () => {
- // given
- const input = 'api/v1/foo/';
- const result = 'api/v1/foo';
-
- // when
- const urlWithoutTrailingSlash = removeTrailingSlash(input);
-
- // then
- expect(urlWithoutTrailingSlash).toEqual(result);
- });
-
- it('#matcherFactory factorizes matcher url based on matching history', () => {
- // given
- const matchingHistory = ['/api/v1', '/user'];
- const pathToMatch = '/foo/';
-
- // when
- const matcher = matchPathFactory(matchingHistory, pathToMatch);
-
- // then
- expect(matcher).toEqual('/api/v1/user/foo');
- });
-
- it('#matcherFactory factorizes matcher url based on matching history', () => {
- // given
- const matchingHistory = ['/api/v1', '/user'];
- const pathToMatch = '/foo/';
-
- // when
- const matcher = matchPathFactory(matchingHistory, pathToMatch);
-
- // then
- expect(matcher).toEqual('/api/v1/user/foo');
- });
-
- it('#matcherFactory factorizes matcher with additional suffix', () => {
- // given
- const matchingHistory = ['/api/:version', '/user'];
- const pathToMatch = '/foo/';
- const suffix = '/:foo*';
-
- // when
- const matcher = matchPathFactory(matchingHistory, pathToMatch, suffix);
-
- // then
- expect(matcher).toEqual('/api/:version/user/foo/:foo*');
- });
-
-});

DELETE
packages/core/src/operators/matchPath/matchPath.factory.ts
-import { compose } from '../../util/compose.util';
-
-export const applyPath = (path: string) => (url: string) => url + path;
-
-export const applySuffix = (suffix?: string) => (url: string) => url + (suffix || '');
-
-export const removeQueryParams = (url: string) => url.split('?')[0];
-
-export const removeTrailingSlash = (url: string) => url.replace(/\/$/, '');
-
-export const constructMatcher = (history: string[]) => history.reduce((prev, cur) => prev + cur, '');
-
-export const matchPathFactory = (
- matchingHistory: string[],
- pathToMatch: string,
- suffix?: string
-) =>
- compose(
- applySuffix(suffix),
- removeTrailingSlash,
- applyPath(pathToMatch),
- constructMatcher,
- )(matchingHistory);

DELETE
packages/core/src/operators/matchPath/urlParams.factory.spec.ts
-import { urlParamsFactory } from './urlParams.factory';
-import { HttpRequest } from '../../http.interface';
-
-describe('urlParamsFactory', () => {
- const req = {
- matchers: ['/api/v1', '/user'],
- url: '/api/v1/user/bob/12',
- } as HttpRequest;
-
- test('should return empty object when passed path without params', () => {
- // given
- const invalidPath = '/name/id';
-
- // when
- const params = urlParamsFactory(req, invalidPath);
-
- // then
- expect(params).toEqual({});
- });
-
- test('should return empty object when in path missing params', () => {
- // given
- const exampleReq = { ...req, url: '/api/v1/user/bob' } as HttpRequest;
- const examplePath = '/:name/:id';
-
- // when
- const params = urlParamsFactory(exampleReq, examplePath);
-
- // then
- expect(params).toEqual({});
- });
-
- test('should return object with multiple params', () => {
- // given
- const examplePath = '/:name/:id';
- const expectedParamsObj = { name: 'bob', id: '12' };
-
- // when
- const params = urlParamsFactory(req, examplePath);
-
- // then
- expect(params).toEqual(expectedParamsObj);
- });
-
- test('should return object with optional params', () => {
- // given
- const examplePath = '/:name/:id?';
- const expectedParamsObj = { name: 'bob', id: '12' };
-
- // when
- const params = urlParamsFactory(req, examplePath);
-
- // then
- expect(params).toEqual(expectedParamsObj);
- });
-
- test('should return object without optional params', () => {
- // given
- const exampleReq = { ...req, url: '/api/v1/user/bob' } as HttpRequest;
- const examplePath = '/:name/:id?';
- const expectedParamsObj = { name: 'bob' };
-
- // when
- const params = urlParamsFactory(exampleReq, examplePath);
-
- // then
- expect(params).toEqual(expectedParamsObj);
- });
-});

DELETE
packages/core/src/operators/matchPath/urlParams.factory.ts
-import * as pathToRegexp from 'path-to-regexp';
-import { HttpRequest, RouteParameters } from '../../http.interface';
-import { matchPathFactory, removeQueryParams } from './matchPath.factory';
-
-export const urlParamsFactory = (req: HttpRequest, path: string): RouteParameters => {
- const match = matchPathFactory(req.matchers!, path);
- const url = removeQueryParams(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]
- }),
- {}
- );
-};

refactor(core): removed dead code