ng-config-cat
Version:
An Angular service for ConfigCat
252 lines (235 loc) • 9.37 kB
JavaScript
import { LogLevel } from 'configcat-common';
export { LogLevel } from 'configcat-common';
import { InjectionToken, ɵɵdefineInjectable, ɵɵinject, Injectable, Inject, Directive, ViewContainerRef, TemplateRef, Input, NgModule, Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { createConsoleLogger, createClientWithAutoPoll, createClientWithLazyLoad, createClientWithManualPoll } from 'configcat-js';
import { Subject, merge } from 'rxjs';
import { switchMap, distinctUntilChanged, takeUntil, map } from 'rxjs/operators';
import { fromPromise } from 'rxjs/internal-compatibility';
const NG_CONFIG_CAT_OPTIONS_TOKEN = new InjectionToken('NgConfigCatOptions');
class PollingModeConfiguration {
constructor(options) {
if (options.logLevel && options.logLevel !== LogLevel.Off) {
this.logger = createConsoleLogger(options.logLevel);
}
this.requestTimeoutMs = options.requestTimeout;
}
}
class AutoPollingModeConfiguration extends PollingModeConfiguration {
constructor(options = {}) {
super(options);
this.pollIntervalSeconds = options.pollInterval;
}
}
class ConfigCatClient {
getValue(key, defaultValue, user) {
return fromPromise(this.client.getValueAsync(key, defaultValue, user));
}
getAllKeys() {
return this.client.getAllKeysAsync();
}
forceRefresh() {
return this.client.forceRefreshAsync();
}
}
class ConfigCatAutoModeClient extends ConfigCatClient {
constructor(sdkKey, configCatConfiguration) {
super();
this.configChangeNotificator = new Subject();
this.client = createClientWithAutoPoll(sdkKey, {
pollIntervalSeconds: configCatConfiguration.pollIntervalSeconds,
configChanged: () => this.configChangeNotificator.next(),
logger: configCatConfiguration.logger,
requestTimeoutMs: configCatConfiguration.requestTimeoutMs,
});
}
getValue(key, defaultValue, user) {
return merge(super.getValue(key, defaultValue, user), this.configChangeNotificator.pipe(switchMap(() => super.getValue(key, defaultValue, user)))).pipe(distinctUntilChanged());
}
}
class ConfigCatLazyModeClient extends ConfigCatClient {
constructor(sdkKey, configCatConfiguration) {
super();
this.client = createClientWithLazyLoad(sdkKey, configCatConfiguration);
}
}
class ConfigCatManualModeClient extends ConfigCatClient {
constructor(sdkKey, configCatConfiguration) {
super();
this.client = createClientWithManualPoll(sdkKey, configCatConfiguration);
}
}
class LazyPollingModeConfiguration extends PollingModeConfiguration {
constructor(options = {}) {
super(options);
this.cacheTimeToLiveSeconds = options.cacheTimeToLive;
}
}
class ManualPollingModeConfiguration extends PollingModeConfiguration {
constructor(options = {}) {
super(options);
}
}
class NgConfigCatService {
constructor(ngConfigCatConfiguration) {
const { sdkKey, configuration = new AutoPollingModeConfiguration() } = ngConfigCatConfiguration;
if (configuration instanceof AutoPollingModeConfiguration) {
this.configCatClient = new ConfigCatAutoModeClient(sdkKey, configuration);
}
else if (configuration instanceof LazyPollingModeConfiguration) {
this.configCatClient = new ConfigCatLazyModeClient(sdkKey, configuration);
}
else if (configuration instanceof ManualPollingModeConfiguration) {
this.configCatClient = new ConfigCatManualModeClient(sdkKey, configuration);
}
else {
throw new TypeError('NgConfigCat configuration should be represent by an instance of PollingModeConfiguration class');
}
}
getValue(key, defaultValue, user) {
return this.configCatClient.getValue(key, defaultValue, user);
}
getAllKeys() {
return this.configCatClient.getAllKeys();
}
forceRefresh() {
return this.configCatClient.forceRefresh();
}
}
NgConfigCatService.ɵprov = ɵɵdefineInjectable({ factory: function NgConfigCatService_Factory() { return new NgConfigCatService(ɵɵinject(NG_CONFIG_CAT_OPTIONS_TOKEN)); }, token: NgConfigCatService, providedIn: "root" });
NgConfigCatService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
NgConfigCatService.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [NG_CONFIG_CAT_OPTIONS_TOKEN,] }] }
];
class NgConfigCatFeatureBaseDirective {
constructor(viewContainerRef, templateRef, ngConfigCatService) {
this.viewContainerRef = viewContainerRef;
this.templateRef = templateRef;
this.ngConfigCatService = ngConfigCatService;
this.destroy$ = new Subject();
this.viewRef = null;
}
ngOnInit() {
if (!this.featureName) {
throw new Error('Attribute `ngConfigCatFeatureEnabled` should not be null or empty');
}
this.render();
}
ngOnDestroy() {
this.destroy$.next();
}
render() {
this.ngConfigCatService.getValue(this.featureName, this.defaultValue, this.user).pipe(takeUntil(this.destroy$), map((isFeatureEnabled) => isFeatureEnabled === this.shouldFeatureBeEnabled)).subscribe((isVisible) => {
if (isVisible) {
this.viewRef = this.viewContainerRef.createEmbeddedView(this.templateRef);
this.viewRef.markForCheck();
}
else {
this.viewContainerRef.clear();
if (this.viewRef) {
this.viewRef.destroy();
this.viewRef = null;
}
}
});
}
}
NgConfigCatFeatureBaseDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngConfigCatFeatureBase]'
},] }
];
NgConfigCatFeatureBaseDirective.ctorParameters = () => [
{ type: ViewContainerRef },
{ type: TemplateRef },
{ type: NgConfigCatService }
];
class NgConfigCatFeatureEnabledDirective extends NgConfigCatFeatureBaseDirective {
constructor(viewContainerRef, templateRef, ngConfigCatService) {
super(viewContainerRef, templateRef, ngConfigCatService);
this.defaultValue = false;
this.shouldFeatureBeEnabled = true;
}
}
NgConfigCatFeatureEnabledDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngConfigCatFeatureEnabled]'
},] }
];
NgConfigCatFeatureEnabledDirective.ctorParameters = () => [
{ type: ViewContainerRef },
{ type: TemplateRef },
{ type: NgConfigCatService }
];
NgConfigCatFeatureEnabledDirective.propDecorators = {
featureName: [{ type: Input, args: ['ngConfigCatFeatureEnabled',] }],
defaultValue: [{ type: Input, args: ['ngConfigCatDefault',] }],
user: [{ type: Input, args: ['ngConfigCatUser',] }]
};
class NgConfigCatFeatureDisabledDirective extends NgConfigCatFeatureBaseDirective {
constructor(viewContainerRef, templateRef, ngConfigCatService) {
super(viewContainerRef, templateRef, ngConfigCatService);
this.defaultValue = false;
this.shouldFeatureBeEnabled = false;
}
}
NgConfigCatFeatureDisabledDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngConfigCatFeatureDisabled]'
},] }
];
NgConfigCatFeatureDisabledDirective.ctorParameters = () => [
{ type: ViewContainerRef },
{ type: TemplateRef },
{ type: NgConfigCatService }
];
NgConfigCatFeatureDisabledDirective.propDecorators = {
featureName: [{ type: Input, args: ['ngConfigCatFeatureDisabled',] }],
defaultValue: [{ type: Input, args: ['ngConfigCatDefault',] }],
user: [{ type: Input, args: ['ngConfigCatUser',] }]
};
class NgConfigCatModule {
constructor(parentModule) {
if (parentModule) {
throw new Error('NgConfigCatModule is already loaded. Import it in the AppModule only.');
}
}
static forRoot(ngConfigCatConfiguration) {
return {
ngModule: NgConfigCatModule,
providers: [
{
provide: NG_CONFIG_CAT_OPTIONS_TOKEN,
useValue: ngConfigCatConfiguration
},
],
};
}
}
NgConfigCatModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule,
],
declarations: [
NgConfigCatFeatureEnabledDirective,
NgConfigCatFeatureDisabledDirective,
],
exports: [
NgConfigCatFeatureEnabledDirective,
NgConfigCatFeatureDisabledDirective,
],
},] }
];
NgConfigCatModule.ctorParameters = () => [
{ type: NgConfigCatModule, decorators: [{ type: Optional }, { type: SkipSelf }] }
];
/**
* Generated bundle index. Do not edit.
*/
export { AutoPollingModeConfiguration, LazyPollingModeConfiguration, ManualPollingModeConfiguration, NgConfigCatFeatureDisabledDirective, NgConfigCatFeatureEnabledDirective, NgConfigCatModule, NgConfigCatService, NG_CONFIG_CAT_OPTIONS_TOKEN as ɵa };
//# sourceMappingURL=ng-config-cat.js.map