-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
With the new`AndGuard`, devs can now set up cases where they want to use an `OrGuard` to handle an `(A && B) || C` kind of condition, or any other complex logical approach they'd like. Devs are also now able to correctly pass injection tokens to the `AndGuard` and `OrGuard` and use custom providers for their guards that are registered as providers
- Loading branch information
Showing
12 changed files
with
2,141 additions
and
1,940 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@nest-lab/or-guard': minor | ||
--- | ||
|
||
Add a new AndGuard to handle complex logical cases | ||
|
||
With the new `AndGuard` it is now possible to create logical cases like | ||
`(A && B) || C` using the `OrGuard` and a composite guard approach. Check out | ||
the docs for more info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{ | ||
"singleQuote": true | ||
} | ||
"printWidth": 80, | ||
"singleQuote": true, | ||
"proseWrap": "always" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './lib/and.guard'; | ||
export * from './lib/or.guard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Inject, | ||
InjectionToken, | ||
mixin, | ||
Type, | ||
} from '@nestjs/common'; | ||
import { ModuleRef } from '@nestjs/core'; | ||
import { | ||
defer, | ||
from, | ||
Observable, | ||
of, | ||
OperatorFunction, | ||
throwError, | ||
} from 'rxjs'; | ||
import { catchError, last, mergeMap, every } from 'rxjs/operators'; | ||
|
||
interface OrGuardOptions { | ||
throwOnFirstError?: boolean; | ||
} | ||
|
||
export function AndGuard( | ||
guards: Array<Type<CanActivate> | InjectionToken>, | ||
orGuardOptions?: OrGuardOptions | ||
) { | ||
class AndMixinGuard implements CanActivate { | ||
private guards: CanActivate[] = []; | ||
constructor(@Inject(ModuleRef) private readonly modRef: ModuleRef) {} | ||
canActivate(context: ExecutionContext): Observable<boolean> { | ||
this.guards = guards.map((guard) => this.modRef.get(guard)); | ||
const canActivateReturns: Array<Observable<boolean>> = this.guards.map( | ||
(guard) => this.deferGuard(guard, context) | ||
); | ||
return from(canActivateReturns).pipe( | ||
mergeMap((obs) => { | ||
return obs.pipe(this.handleError()); | ||
}), | ||
every((val) => val === true), | ||
last() | ||
); | ||
} | ||
|
||
private deferGuard( | ||
guard: CanActivate, | ||
context: ExecutionContext | ||
): Observable<boolean> { | ||
return defer(() => { | ||
const guardVal = guard.canActivate(context); | ||
if (this.guardIsPromise(guardVal)) { | ||
return from(guardVal); | ||
} | ||
if (this.guardIsObservable(guardVal)) { | ||
return guardVal; | ||
} | ||
return of(guardVal); | ||
}); | ||
} | ||
|
||
private handleError(): OperatorFunction<boolean, boolean> { | ||
return catchError((err) => { | ||
if (orGuardOptions?.throwOnFirstError) { | ||
return throwError(() => err); | ||
} | ||
return of(false); | ||
}); | ||
} | ||
|
||
private guardIsPromise( | ||
guard: boolean | Promise<boolean> | Observable<boolean> | ||
): guard is Promise<boolean> { | ||
return !!(guard as Promise<boolean>).then; | ||
} | ||
|
||
private guardIsObservable( | ||
guard: boolean | Observable<boolean> | ||
): guard is Observable<boolean> { | ||
return !!(guard as Observable<boolean>).pipe; | ||
} | ||
} | ||
|
||
const Guard = mixin(AndMixinGuard); | ||
return Guard as Type<CanActivate>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.