UNPKG

@everllence/ngx-chain-functional-guards

Version:

An Angular guard that executes functional guards in a serial manner, waiting for each one to complete before proceeding to the next.

1 lines 4.3 kB
{"version":3,"file":"everllence-ngx-chain-functional-guards.mjs","sources":["../../../../libs/ngx-chain-functional-guards/src/lib/utils/rxjs.utils.ts","../../../../libs/ngx-chain-functional-guards/src/lib/chain.guard.ts","../../../../libs/ngx-chain-functional-guards/src/everllence-ngx-chain-functional-guards.ts"],"sourcesContent":["import { Observable, from, of } from 'rxjs'\n\n/**\n * Helper to transform value to an Observable\n * @param value - either a value, a promise or an observable\n * @returns an observable of the value\n */\nexport function wrapIntoObservable<T>(value: T | Promise<T> | Observable<T>): Observable<T> {\n if (value instanceof Observable) {\n return value\n } else if (value instanceof Promise) {\n return from(value)\n } else {\n return of(value)\n }\n}\n","import { EnvironmentInjector, inject, runInInjectionContext } from '@angular/core'\nimport {\n ActivatedRouteSnapshot,\n CanActivateChildFn,\n CanActivateFn,\n CanDeactivateFn,\n RouterStateSnapshot\n} from '@angular/router'\n\nimport { concatMap, first, from, last, takeWhile } from 'rxjs'\n\nimport { wrapIntoObservable } from './utils/rxjs.utils'\n\n/**\n * Chains route configured guards on canActivate or canActivateChild, calling them after each other, waiting for each one before calling the next.\n *\n * @param guards The array of guards to process.\n * @returns The result of the first guard that returns `false` or an instance of a `UrlTree`, otherwise `true`.\n */\nexport function chainActivationGuards(\n guards: CanActivateFn[] | CanActivateChildFn[]\n): CanActivateFn | CanActivateChildFn {\n return (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {\n const injector = inject(EnvironmentInjector)\n\n return from(guards).pipe(\n concatMap(guard => {\n const guardResult = runInInjectionContext(injector, () => guard(route, state))\n return wrapIntoObservable(guardResult).pipe(first())\n }),\n takeWhile(val => val === true, true),\n last()\n )\n }\n}\n\n/**\n * Chains route configured guards on canDeactivate, calling them after each other, waiting for each one before calling the next.\n *\n * @param guards The array of guards to process.\n * @returns The result of the first guard that returns `false` or an instance of a `UrlTree`, otherwise `true`.\n */\nexport function chainDeactivationGuards(guards: CanDeactivateFn<never>[]): CanDeactivateFn<never> {\n return (\n component: never,\n route: ActivatedRouteSnapshot,\n state: RouterStateSnapshot,\n nextState: RouterStateSnapshot\n ) => {\n const injector = inject(EnvironmentInjector)\n\n return from(guards).pipe(\n concatMap(guard => {\n const guardResult = runInInjectionContext(injector, () =>\n guard(component, route, state, nextState)\n )\n return wrapIntoObservable(guardResult).pipe(first())\n }),\n takeWhile(val => val === true, true),\n last()\n )\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAEA;;;;AAIG;AACG,SAAU,kBAAkB,CAAI,KAAqC,EAAA;AACzE,IAAA,IAAI,KAAK,YAAY,UAAU,EAAE;AAC/B,QAAA,OAAO,KAAK;;AACP,SAAA,IAAI,KAAK,YAAY,OAAO,EAAE;AACnC,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC;;SACb;AACL,QAAA,OAAO,EAAE,CAAC,KAAK,CAAC;;AAEpB;;ACFA;;;;;AAKG;AACG,SAAU,qBAAqB,CACnC,MAA8C,EAAA;AAE9C,IAAA,OAAO,CAAC,KAA6B,EAAE,KAA0B,KAAI;AACnE,QAAA,MAAM,QAAQ,GAAG,MAAM,CAAC,mBAAmB,CAAC;QAE5C,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CACtB,SAAS,CAAC,KAAK,IAAG;AAChB,YAAA,MAAM,WAAW,GAAG,qBAAqB,CAAC,QAAQ,EAAE,MAAM,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YAC9E,OAAO,kBAAkB,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;AACtD,SAAC,CAAC,EACF,SAAS,CAAC,GAAG,IAAI,GAAG,KAAK,IAAI,EAAE,IAAI,CAAC,EACpC,IAAI,EAAE,CACP;AACH,KAAC;AACH;AAEA;;;;;AAKG;AACG,SAAU,uBAAuB,CAAC,MA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