Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use strict false when getting guard providers #38

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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());
@@ -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);
4 changes: 3 additions & 1 deletion packages/or-guard/src/lib/or.guard.ts
Original file line number Diff line number Diff line change
@@ -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)
);
Loading