-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce feature toggles (#1121)
feat: introduce feature toggles
- Loading branch information
Showing
9 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { config } from './app-config'; | ||
|
||
export { isFeatureEnabled } from './is-feature-enabled'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { config } from './app-config'; | ||
|
||
export function isFeatureEnabled(featureName: string) { | ||
return config.features && config.features[featureName]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const FEATURES_PREFIX = 'FLOGO_FEATURES_'; | ||
const FEATURES_PREFIX_LENGTH = FEATURES_PREFIX.length; | ||
|
||
function featuresReducer(features, [envKey, envValue]: [string, any]) { | ||
if (envKey.startsWith(FEATURES_PREFIX)) { | ||
features[envKey.slice(FEATURES_PREFIX_LENGTH)] = envValue; | ||
} | ||
return features; | ||
} | ||
|
||
export function parseFeatureToggles(fromEnv: NodeJS.ProcessEnv) { | ||
return Object.entries(fromEnv).reduce(featuresReducer, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Injectable, APP_INITIALIZER, FactoryProvider } from '@angular/core'; | ||
import { tap, catchError } from 'rxjs/operators'; | ||
import { EMPTY } from 'rxjs'; | ||
import { RestApiService } from './restapi'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class FeatureToggleService { | ||
features: { [featureName: string]: any }; | ||
|
||
isFeatureEnabled(featureName: string) { | ||
return this.features && !!this.features[featureName]; | ||
} | ||
} | ||
|
||
// needed to be exported because of AoT | ||
export function featureToggleServiceFactory( | ||
apiService: RestApiService, | ||
featureService: FeatureToggleService | ||
) { | ||
return () => { | ||
return apiService | ||
.get('_/features') | ||
.pipe( | ||
tap(features => (featureService.features = features)), | ||
catchError(() => EMPTY) | ||
) | ||
.toPromise(); | ||
}; | ||
} | ||
|
||
export const FEATURE_TOGGLES_APP_INITIALIZER: FactoryProvider = { | ||
provide: APP_INITIALIZER, | ||
useFactory: featureToggleServiceFactory, | ||
deps: [RestApiService, FeatureToggleService], | ||
multi: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters