|
1 | | -import { IGettable, isGettable } from "./gettable"; |
2 | | -import { FeatureDefinition, FeatureConfiguration, FEATURE_MANAGEMENT_KEY, FEATURE_FLAGS_KEY } from "./model"; |
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
3 | 3 |
|
4 | | -export interface IFeatureProvider { |
5 | | - getFeatureFlags(): Promise<FeatureDefinition[]>; |
| 4 | +import { IGettable } from "./gettable"; |
| 5 | +import { FeatureFlag, FeatureManagementConfiguration, FEATURE_MANAGEMENT_KEY, FEATURE_FLAGS_KEY } from "./model"; |
| 6 | + |
| 7 | +export interface IFeatureFlagProvider { |
| 8 | + /** |
| 9 | + * Get all feature flags. |
| 10 | + */ |
| 11 | + getFeatureFlags(): Promise<FeatureFlag[]>; |
| 12 | + |
| 13 | + /** |
| 14 | + * Get a feature flag by name. |
| 15 | + * @param featureName The name of the feature flag. |
| 16 | + */ |
| 17 | + getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined>; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * A feature flag provider that uses a map-like configuration to provide feature flags. |
| 22 | + */ |
| 23 | +export class ConfigurationMapFeatureFlagProvider implements IFeatureFlagProvider { |
| 24 | + #configuration: IGettable; |
| 25 | + |
| 26 | + constructor(configuration: IGettable) { |
| 27 | + this.#configuration = configuration; |
| 28 | + } |
| 29 | + async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> { |
| 30 | + const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY); |
| 31 | + return featureConfig?.[FEATURE_FLAGS_KEY]?.find((feature) => feature.id === featureName); |
| 32 | + } |
| 33 | + |
| 34 | + async getFeatureFlags(): Promise<FeatureFlag[]> { |
| 35 | + const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY); |
| 36 | + return featureConfig?.[FEATURE_FLAGS_KEY] ?? []; |
| 37 | + } |
6 | 38 | } |
7 | 39 |
|
8 | | -export class ConfigurationFeatureProvider implements IFeatureProvider { |
9 | | - #configuration: IGettable | Record<string, unknown>; |
| 40 | +/** |
| 41 | + * A feature flag provider that uses an object-like configuration to provide feature flags. |
| 42 | + */ |
| 43 | +export class ConfigurationObjectFeatureFlagProvider implements IFeatureFlagProvider { |
| 44 | + #configuration: Record<string, unknown>; |
10 | 45 |
|
11 | | - constructor(configuration: Record<string, unknown> | IGettable) { |
12 | | - if (typeof configuration !== "object") { |
13 | | - throw new Error("Configuration must be an object."); |
14 | | - } |
| 46 | + constructor(configuration: Record<string, unknown>) { |
15 | 47 | this.#configuration = configuration; |
16 | 48 | } |
17 | 49 |
|
18 | | - async getFeatureFlags(): Promise<FeatureDefinition[]> { |
19 | | - if (isGettable(this.#configuration)) { |
20 | | - const featureConfig = this.#configuration.get<FeatureConfiguration>(FEATURE_MANAGEMENT_KEY); |
21 | | - return featureConfig?.[FEATURE_FLAGS_KEY] ?? []; |
22 | | - } else { |
23 | | - return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? []; |
24 | | - } |
| 50 | + async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> { |
| 51 | + const featureFlags = this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY]; |
| 52 | + return featureFlags?.find((feature: FeatureFlag) => feature.id === featureName); |
| 53 | + } |
| 54 | + |
| 55 | + async getFeatureFlags(): Promise<FeatureFlag[]> { |
| 56 | + return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? []; |
25 | 57 | } |
26 | 58 | } |
0 commit comments