Skip to content

Commit

Permalink
refactor(Guards): Improve guards API based on common usage (#62)
Browse files Browse the repository at this point in the history
Change guard factory from `createGuard` to `provideGuard` to clarify what the factory is doing. Re-order guard params to receive route params first.

BREAKING CHANGE:

  Before:
  ```ts
  const auth = createGuard(function(http: Http) {

    return function(route: Route, params: any, isTerminal: boolean) {
      return http.get(...);
    };

  }, [ Http ]);
  ```

  After:
  ```ts
  const auth = provideGuard(function(http: Http) {

    return function(params: any, route: Route, isTerminal: boolean) {
      return http.get(...);
    };

  }, [ Http ]);
  ```
  • Loading branch information
MikeRyanDev authored and brandonroberts committed Apr 22, 2016
1 parent 562c77b commit 1ea2806
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import { useTraversalMiddleware, TraversalCandidate } from './route-traverser';
import { createMiddleware } from './middleware';

export interface Guard {
(route: Route, params: any, isTerminal: boolean): Observable<boolean>;
(params: any, route: Route, isTerminal: boolean): Observable<boolean>;
}

export const createGuard = createProviderFactory<Guard>('@ngrx/router Guard');
export const provideGuard = createProviderFactory<Guard>('@ngrx/router Guard');

export const guardMiddleware = createMiddleware(function(injector: Injector) {
return (route$: Observable<TraversalCandidate>) => route$
Expand All @@ -35,7 +35,7 @@ export const guardMiddleware = createMiddleware(function(injector: Injector) {
const guards: Guard[] = route.guards
.map(provider => injector.resolveAndInstantiate(provider));

const resolved = guards.map(guard => guard(route, params, isTerminal));
const resolved = guards.map(guard => guard(params, route, isTerminal));

return Observable.merge(...resolved)
.every(value => !!value)
Expand Down
14 changes: 7 additions & 7 deletions spec/guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Injector } from 'angular2/core';
import { Route } from '../lib/route';
import { Middleware } from '../lib/middleware';
import { TraversalCandidate } from '../lib/route-traverser';
import { Guard, createGuard, guardMiddleware } from '../lib/guard';
import { Guard, provideGuard, guardMiddleware } from '../lib/guard';


describe('Guard Middleware', function() {
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('Guard Middleware', function() {

it('should resolve all guards in the context of the injector', function() {
spyOn(injector, 'resolveAndInstantiate').and.callThrough();
const guard = createGuard(() => () => Observable.of(true));
const guard = provideGuard(() => () => Observable.of(true));

route({ guards: [ guard ] }).let(guardRunner).subscribe();

Expand All @@ -53,18 +53,18 @@ describe('Guard Middleware', function() {
it('should provide guards with the route it has matched', function() {
const testGuard = { run: () => Observable.of(true) };
spyOn(testGuard, 'run').and.callThrough();
const guard = createGuard(() => testGuard.run);
const guard = provideGuard(() => testGuard.run);
const nextRoute = { guards: [ guard ] };
const params = { abc: 123 };
const isTerminal = true;

route(nextRoute, params, isTerminal).let(guardRunner).subscribe();

expect(testGuard.run).toHaveBeenCalledWith(nextRoute, params, isTerminal);
expect(testGuard.run).toHaveBeenCalledWith(params, nextRoute, isTerminal);
});

it('should return true if all of the guards return true', function(done) {
const pass = createGuard(() => () => Observable.of(true));
const pass = provideGuard(() => () => Observable.of(true));

route({ guards: [ pass ] })
.let<TraversalCandidate>(guardRunner)
Expand All @@ -76,8 +76,8 @@ describe('Guard Middleware', function() {
});

it('should return false if just one guard returns false', function(done) {
const pass = createGuard(() => () => Observable.of(true));
const fail = createGuard(() => () => Observable.of(false));
const pass = provideGuard(() => () => Observable.of(true));
const fail = provideGuard(() => () => Observable.of(false));

route({ guards: [ pass, fail ] })
.let(guardRunner)
Expand Down

0 comments on commit 1ea2806

Please sign in to comment.