Skip to content

Commit

Permalink
Merge pull request #41 from marblejs/feat/matchType-all
Browse files Browse the repository at this point in the history
feat(core): matchType - match all types using * sign
  • Loading branch information
JozefFlakus authored May 30, 2018
2 parents c18ea8e + 40bd854 commit 9edfdc4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
16 changes: 13 additions & 3 deletions example/src/controllers/api.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Effect, combineRoutes, matchPath, matchType } from '@marblejs/core';
import { map } from 'rxjs/operators';
import { Effect, HttpError, HttpStatus, combineRoutes, matchPath, matchType } from '@marblejs/core';
import { throwError } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { user$ } from './user.controller';

const root$: Effect = request$ => request$
Expand All @@ -11,7 +12,16 @@ const root$: Effect = request$ => request$
})),
);

const notFound$: Effect = request$ => request$
.pipe(
matchPath('*'),
matchType('*'),
switchMap(() =>
throwError(new HttpError('Route not found', HttpStatus.NOT_FOUND))
)
);

export const api$ = combineRoutes(
'/api/v1',
[ root$, user$ ],
[ root$, user$, notFound$ ],
);
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ describe('matchType operator', () => {
]);
});

it('matches all HTTP methods', () => {
const operators = [matchType('*')];
Marbles.assert(operators, [
['-a-b---', { a: mockReq('GET'), b: mockReq('POST') }],
['-a-b---', { a: mockReqMatched('GET'), b: mockReqMatched('POST') }],
]);
});

});
4 changes: 2 additions & 2 deletions packages/core/src/operators/matchType/matchType.operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Observable } from 'rxjs';
import { filter, tap } from 'rxjs/operators';
import { HttpMethod, HttpRequest } from '../../http.interface';

export const matchType = (method: HttpMethod) => (source$: Observable<HttpRequest>) =>
export const matchType = (method: HttpMethod | '*') => (source$: Observable<HttpRequest>) =>
source$.pipe(
filter(req => req.method === method),
filter(req => req.method === method || method === '*'),
tap(req => req.matchType = true),
);

0 comments on commit 9edfdc4

Please sign in to comment.