-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfeatureProvider.ts
More file actions
58 lines (48 loc) · 2.17 KB
/
featureProvider.ts
File metadata and controls
58 lines (48 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { IGettable } from "./gettable.js";
import { FeatureFlag, FeatureManagementConfiguration, FEATURE_MANAGEMENT_KEY, FEATURE_FLAGS_KEY } from "./model.js";
export interface IFeatureFlagProvider {
/**
* Get all feature flags.
*/
getFeatureFlags(): Promise<FeatureFlag[]>;
/**
* Get a feature flag by name.
* @param featureName The name of the feature flag.
*/
getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined>;
}
/**
* A feature flag provider that uses a map-like configuration to provide feature flags.
*/
export class ConfigurationMapFeatureFlagProvider implements IFeatureFlagProvider {
#configuration: IGettable;
constructor(configuration: IGettable) {
this.#configuration = configuration;
}
async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> {
const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY);
return featureConfig?.[FEATURE_FLAGS_KEY]?.findLast((feature) => feature.id === featureName);
}
async getFeatureFlags(): Promise<FeatureFlag[]> {
const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY);
return featureConfig?.[FEATURE_FLAGS_KEY] ?? [];
}
}
/**
* A feature flag provider that uses an object-like configuration to provide feature flags.
*/
export class ConfigurationObjectFeatureFlagProvider implements IFeatureFlagProvider {
#configuration: Record<string, unknown>;
constructor(configuration: Record<string, unknown>) {
this.#configuration = configuration;
}
async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> {
const featureFlags = this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY];
return featureFlags?.findLast((feature: FeatureFlag) => feature.id === featureName);
}
async getFeatureFlags(): Promise<FeatureFlag[]> {
return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? [];
}
}