Skip to content

Commit

Permalink
fix: use strict false when getting guard providers (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 authored Mar 7, 2024
2 parents 65e4a3d + 7972281 commit a101917
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/polite-planes-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@nest-lab/or-guard': patch
---

Use `{strict: false}` when calling modRef.get() to ensure guards can come from
other modules
10 changes: 6 additions & 4 deletions packages/or-guard/src/lib/and.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ interface AndGuardOptions {

export function AndGuard(
guards: Array<Type<CanActivate> | InjectionToken>,
orGuardOptions?: AndGuardOptions
andGuardOptions?: AndGuardOptions
) {
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));
this.guards = guards.map((guard) =>
this.modRef.get(guard, { strict: false })
);
const canActivateReturns: Array<() => Observable<boolean>> =
this.guards.map((guard) => () => this.deferGuard(guard, context));
const mapOperator = orGuardOptions?.sequential ? concatMap : mergeMap;
const mapOperator = andGuardOptions?.sequential ? concatMap : mergeMap;
return from(canActivateReturns).pipe(
mapOperator((obs) => {
return obs().pipe(this.handleError());
Expand Down Expand Up @@ -61,7 +63,7 @@ export function AndGuard(

private handleError(): OperatorFunction<boolean, boolean> {
return catchError((err) => {
if (orGuardOptions?.throwOnFirstError) {
if (andGuardOptions?.throwOnFirstError) {
return throwError(() => err);
}
return of(false);
Expand Down
4 changes: 3 additions & 1 deletion packages/or-guard/src/lib/or.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export function OrGuard(
private guards: CanActivate[] = [];
constructor(@Inject(ModuleRef) private readonly modRef: ModuleRef) {}
canActivate(context: ExecutionContext): Observable<boolean> {
this.guards = guards.map((guard) => this.modRef.get(guard));
this.guards = guards.map((guard) =>
this.modRef.get(guard, { strict: false })
);
const canActivateReturns: Array<Observable<boolean>> = this.guards.map(
(guard) => this.deferGuard(guard, context)
);
Expand Down

0 comments on commit a101917

Please sign in to comment.