@hmcts/opal-frontend-common 0.0.77 → 0.0.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,22 @@
1
1
  import { inject } from '@angular/core';
2
+ import { Router } from '@angular/router';
3
+ import { resolveGuardResult } from '@hmcts/opal-frontend-common/guards/helpers';
4
+ import { PAGES_ROUTING_PATHS } from '@hmcts/opal-frontend-common/pages/routing/constants';
2
5
  import { LaunchDarklyService } from '@hmcts/opal-frontend-common/services/launch-darkly-service';
3
6
  import { GlobalStore } from '@hmcts/opal-frontend-common/stores/global';
4
7
 
5
8
  const isFeatureFlagPopulated = (featureFlags, flagKey) => featureFlags?.[flagKey] !== undefined;
6
9
  const isFeatureFlagEnabled = (featureFlags, flagKey) => featureFlags?.[flagKey] === true;
10
+ /**
11
+ * Resolves a feature flag route guard to a boolean result.
12
+ *
13
+ * @param flagKey - The key of the feature flag to check
14
+ * @param route - The activated route snapshot passed to the guard
15
+ * @param state - The router state snapshot passed to the guard
16
+ * @returns A boolean that is true only when the feature flag guard allows access
17
+ *
18
+ */
19
+ const resolveFeatureFlagGuard = async (flagKey, route, state) => (await resolveGuardResult(featureFlagGuard(flagKey)(route, state))) === true;
7
20
  /**
8
21
  * Creates a route guard that checks if a specific feature flag is enabled before allowing access.
9
22
  *
@@ -33,10 +46,25 @@ function featureFlagGuard(flagKey) {
33
46
  }
34
47
  };
35
48
  }
49
+ /**
50
+ * Creates a route guard that redirects when a specific feature flag is not enabled.
51
+ *
52
+ * @param flagKey - The key of the feature flag to check
53
+ * @param redirectPath - The path to redirect to when the feature flag is not enabled
54
+ * @returns A CanActivateFn that returns true if the feature flag is enabled, otherwise a UrlTree
55
+ *
56
+ */
57
+ function featureFlagRedirectGuard(flagKey, redirectPath = `/${PAGES_ROUTING_PATHS.children.accessDenied}`) {
58
+ return async (route, state) => {
59
+ const router = inject(Router);
60
+ const canActivate = await resolveFeatureFlagGuard(flagKey, route, state);
61
+ return canActivate ? true : router.createUrlTree([redirectPath]);
62
+ };
63
+ }
36
64
 
37
65
  /**
38
66
  * Generated bundle index. Do not edit.
39
67
  */
40
68
 
41
- export { featureFlagGuard };
69
+ export { featureFlagGuard, featureFlagRedirectGuard, resolveFeatureFlagGuard };
42
70
  //# sourceMappingURL=hmcts-opal-frontend-common-guards-feature-flag.mjs.map
@@ -1,6 +1,6 @@
1
1
  import { ActivatedRouteSnapshot, UrlSegment } from '@angular/router';
2
+ import { isObservable, firstValueFrom, Observable } from 'rxjs';
2
3
  import { TestBed } from '@angular/core/testing';
3
- import { Observable } from 'rxjs';
4
4
 
5
5
  /**
6
6
  * Returns a function that invokes the specified guard with a dummy route and state.
@@ -33,6 +33,11 @@ function handleObservableResult(result) {
33
33
  });
34
34
  }
35
35
 
36
+ /**
37
+ * Converts any valid Angular guard result into a promise so callers can await the final value consistently.
38
+ */
39
+ const resolveGuardResult = (result) => isObservable(result) ? firstValueFrom(result) : Promise.resolve(result);
40
+
36
41
  /**
37
42
  * Runs an authentication guard function within the injection context of TestBed.
38
43
  *
@@ -55,5 +60,5 @@ async function runHasFlowStateGuardWithContext(hasFlowStateGuard) {
55
60
  * Generated bundle index. Do not edit.
56
61
  */
57
62
 
58
- export { getGuardWithDummyUrl, handleObservableResult, runAuthGuardWithContext, runHasFlowStateGuardWithContext };
63
+ export { getGuardWithDummyUrl, handleObservableResult, resolveGuardResult, runAuthGuardWithContext, runHasFlowStateGuardWithContext };
59
64
  //# sourceMappingURL=hmcts-opal-frontend-common-guards-helpers.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmcts/opal-frontend-common",
3
- "version": "0.0.77",
3
+ "version": "0.0.78",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,5 +1,15 @@
1
- import { CanActivateFn } from '@angular/router';
1
+ import { CanActivateFn, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
2
2
 
3
+ /**
4
+ * Resolves a feature flag route guard to a boolean result.
5
+ *
6
+ * @param flagKey - The key of the feature flag to check
7
+ * @param route - The activated route snapshot passed to the guard
8
+ * @param state - The router state snapshot passed to the guard
9
+ * @returns A boolean that is true only when the feature flag guard allows access
10
+ *
11
+ */
12
+ declare const resolveFeatureFlagGuard: (flagKey: string, route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => Promise<boolean>;
3
13
  /**
4
14
  * Creates a route guard that checks if a specific feature flag is enabled before allowing access.
5
15
  *
@@ -8,5 +18,14 @@ import { CanActivateFn } from '@angular/router';
8
18
  *
9
19
  */
10
20
  declare function featureFlagGuard(flagKey: string): CanActivateFn;
21
+ /**
22
+ * Creates a route guard that redirects when a specific feature flag is not enabled.
23
+ *
24
+ * @param flagKey - The key of the feature flag to check
25
+ * @param redirectPath - The path to redirect to when the feature flag is not enabled
26
+ * @returns A CanActivateFn that returns true if the feature flag is enabled, otherwise a UrlTree
27
+ *
28
+ */
29
+ declare function featureFlagRedirectGuard(flagKey: string, redirectPath?: string): CanActivateFn;
11
30
 
12
- export { featureFlagGuard };
31
+ export { featureFlagGuard, featureFlagRedirectGuard, resolveFeatureFlagGuard };
@@ -1,4 +1,4 @@
1
- import { CanActivateFn } from '@angular/router';
1
+ import { CanActivateFn, MaybeAsync, GuardResult } from '@angular/router';
2
2
  import { Observable } from 'rxjs';
3
3
  import { GuardReturnType } from '@hmcts/opal-frontend-common/guards/types';
4
4
 
@@ -19,6 +19,11 @@ declare function getGuardWithDummyUrl(guard: CanActivateFn, urlPath: string): ()
19
19
  */
20
20
  declare function handleObservableResult(result: Observable<GuardReturnType>): Promise<GuardReturnType>;
21
21
 
22
+ /**
23
+ * Converts any valid Angular guard result into a promise so callers can await the final value consistently.
24
+ */
25
+ declare const resolveGuardResult: (result: MaybeAsync<GuardResult>) => Promise<GuardResult>;
26
+
22
27
  /**
23
28
  * Runs an authentication guard function within the injection context of TestBed.
24
29
  *
@@ -29,4 +34,4 @@ declare function runAuthGuardWithContext(authGuard: () => GuardReturnType | Prom
29
34
 
30
35
  declare function runHasFlowStateGuardWithContext(hasFlowStateGuard: () => GuardReturnType | Promise<GuardReturnType> | Observable<GuardReturnType>): Promise<GuardReturnType>;
31
36
 
32
- export { getGuardWithDummyUrl, handleObservableResult, runAuthGuardWithContext, runHasFlowStateGuardWithContext };
37
+ export { getGuardWithDummyUrl, handleObservableResult, resolveGuardResult, runAuthGuardWithContext, runHasFlowStateGuardWithContext };