UNPKG

@karelics/angular-unleash-proxy-client

Version:

Angular wrapper for [unleash-proxy-client](https://github.com/Unleash/unleash-proxy-client-js).

213 lines (201 loc) 10.2 kB
import * as i0 from '@angular/core'; import { InjectionToken, makeEnvironmentProviders, APP_INITIALIZER, inject, Injectable, Inject, DestroyRef, Directive, Input } from '@angular/core'; import { UnleashClient, EVENTS } from 'unleash-proxy-client'; import { take, tap, fromEvent, shareReplay, startWith, map, distinctUntilChanged, ReplaySubject, switchMap, combineLatest } from 'rxjs'; import * as i1 from '@angular/common'; import { NgIf } from '@angular/common'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { Router } from '@angular/router'; const UNLEASH_CONFIG = new InjectionToken('Unleash config'); function provideUnleashProxy(config) { return makeEnvironmentProviders([ { provide: UNLEASH_CONFIG, useValue: config }, { provide: APP_INITIALIZER, useFactory: () => { const unleashService = inject(UnleashService); return () => unleashService.initialized$.pipe(take(1), tap(() => unleashService.unleash.start())); }, multi: true, }, ]); } class UnleashService { constructor(config) { this.config = config; this.unleash = new UnleashClient(this.config); this.initialized$ = fromEvent(this.unleash, EVENTS.INIT).pipe(shareReplay({ bufferSize: 1, refCount: true })); this.error$ = fromEvent(this.unleash, EVENTS.ERROR).pipe(shareReplay({ bufferSize: 1, refCount: true })); this.ready$ = fromEvent(this.unleash, EVENTS.READY).pipe(shareReplay({ bufferSize: 1, refCount: true })); this.update$ = fromEvent(this.unleash, EVENTS.UPDATE).pipe(shareReplay({ bufferSize: 1, refCount: true })); this.impression$ = fromEvent(this.unleash, EVENTS.IMPRESSION).pipe(shareReplay({ bufferSize: 1, refCount: true })); } isEnabled(featureFlag) { return this.unleash.isEnabled(featureFlag); } isDisabled(featureFlag) { return !this.isEnabled(featureFlag); } isEnabled$(featureFlag) { return this.update$.pipe(startWith(null), map(() => this.isEnabled(featureFlag)), distinctUntilChanged()); } isDisabled$(featureFlag) { return this.isEnabled$(featureFlag).pipe(map((state) => !state)); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: UnleashService, deps: [{ token: UNLEASH_CONFIG }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: UnleashService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: UnleashService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [{ type: undefined, decorators: [{ type: Inject, args: [UNLEASH_CONFIG] }] }] }); class FeatureDirective { constructor() { this.unleashService = inject(UnleashService); this.destroyRef = inject(DestroyRef); this.ngIf = inject(NgIf); this.toggleNamesSubject = new ReplaySubject(1); } ngOnInit() { this.toggleNamesSubject.pipe(switchMap((toggleNames) => this.toggleState$(toggleNames)), tap((state) => this.update(state)), takeUntilDestroyed(this.destroyRef)).subscribe(); } setToggleNames(names) { this.toggleNamesSubject.next(names); } update(state) { this.ngIf.ngIf = state; } setElseTemplate(templateRef) { this.ngIf.ngIfElse = templateRef; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: FeatureDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.11", type: FeatureDirective, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: FeatureDirective, decorators: [{ type: Directive }] }); class FeatureEnabledDirective extends FeatureDirective { constructor() { super(...arguments); this._operator = 'or'; this.toggleState$ = (toggleNames) => { return combineLatest(toggleNames.map(name => this.unleashService.isEnabled$(name))).pipe(map(states => this._operator === 'and' ? states.every(state => state) : states.some(state => state))); }; } set toggleNames(val) { this.setToggleNames(Array.isArray(val) ? val : [val]); } set operator(val) { this._operator = val.toLowerCase(); } set else(templateRef) { this.setElseTemplate(templateRef); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: FeatureEnabledDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.11", type: FeatureEnabledDirective, isStandalone: true, selector: "[featureEnabled]", inputs: { toggleNames: ["featureEnabled", "toggleNames"], operator: ["featureEnabledOperator", "operator"], else: ["featureEnabledElse", "else"] }, usesInheritance: true, hostDirectives: [{ directive: i1.NgIf }], ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: FeatureEnabledDirective, decorators: [{ type: Directive, args: [{ selector: '[featureEnabled]', standalone: true, hostDirectives: [NgIf], }] }], propDecorators: { toggleNames: [{ type: Input, args: ['featureEnabled'] }], operator: [{ type: Input, args: ['featureEnabledOperator'] }], else: [{ type: Input, args: ['featureEnabledElse'] }] } }); class FeatureDisabledDirective extends FeatureDirective { constructor() { super(...arguments); this._operator = 'or'; this.toggleState$ = (toggleNames) => { return combineLatest(toggleNames.map(name => this.unleashService.isDisabled$(name))).pipe(map(states => this._operator === 'and' ? states.every(state => state) : states.some(state => state))); }; } set toggleNames(val) { this.setToggleNames(Array.isArray(val) ? val : [val]); } set operator(val) { this._operator = val.toLowerCase(); } set else(templateRef) { this.setElseTemplate(templateRef); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: FeatureDisabledDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.11", type: FeatureDisabledDirective, isStandalone: true, selector: "[featureDisabled]", inputs: { toggleNames: ["featureDisabled", "toggleNames"], operator: ["featureDisabledOperator", "operator"], else: ["featureDisabledElse", "else"] }, usesInheritance: true, hostDirectives: [{ directive: i1.NgIf }], ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.11", ngImport: i0, type: FeatureDisabledDirective, decorators: [{ type: Directive, args: [{ selector: '[featureDisabled]', standalone: true, hostDirectives: [NgIf], }] }], propDecorators: { toggleNames: [{ type: Input, args: ['featureDisabled'] }], operator: [{ type: Input, args: ['featureDisabledOperator'] }], else: [{ type: Input, args: ['featureDisabledElse'] }] } }); function redirectResult(redirectUrl) { if (redirectUrl) { if (typeof redirectUrl === 'string') { const router = inject(Router); return router.navigateByUrl(redirectUrl); } else { return redirectUrl; } } return false; } function featureEnabled(toggleNames, operatorOrRedirectUrl, redirectUrl) { return () => { const unleashService = inject(UnleashService); const effectiveToggleNames = Array.isArray(toggleNames) ? toggleNames : [toggleNames]; const effectiveOperator = Array.isArray(toggleNames) ? (operatorOrRedirectUrl ?? 'or') : 'or'; const effectiveRedirectUrl = Array.isArray(toggleNames) ? redirectUrl : operatorOrRedirectUrl; const states = effectiveToggleNames.map(toggle => unleashService.isEnabled(toggle)); const enabledState = effectiveOperator === 'and' ? states.every(Boolean) : states.some(Boolean); return enabledState || redirectResult(effectiveRedirectUrl); }; } function featureDisabled(toggleNames, operatorOrRedirectUrl, redirectUrl) { return () => { const unleashService = inject(UnleashService); const effectiveToggleNames = Array.isArray(toggleNames) ? toggleNames : [toggleNames]; const effectiveOperator = Array.isArray(toggleNames) ? (operatorOrRedirectUrl ?? 'or') : 'or'; const effectiveRedirectUrl = Array.isArray(toggleNames) ? redirectUrl : operatorOrRedirectUrl; const states = effectiveToggleNames.map(toggle => unleashService.isDisabled(toggle)); const disabledState = effectiveOperator === 'and' ? states.every(Boolean) : states.some(Boolean); return disabledState || redirectResult(effectiveRedirectUrl); }; } /* * Public API Surface of angular-unleash-proxy-client */ /** * Generated bundle index. Do not edit. */ export { FeatureDisabledDirective, FeatureEnabledDirective, UNLEASH_CONFIG, UnleashService, featureDisabled, featureEnabled, provideUnleashProxy }; //# sourceMappingURL=karelics-angular-unleash-proxy-client.mjs.map