Skip to content

Commit 8e0c11c

Browse files
committed
refine feature flag definition
1 parent ad3487c commit 8e0c11c

File tree

4 files changed

+51
-70
lines changed

4 files changed

+51
-70
lines changed

src/featureManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ export class FeatureManager {
1616
}
1717

1818
listFeatureNames(): string[] {
19-
// TODO: whether to deduplicate the feature names?
20-
return this.#featureFlags.map((flag) => flag.id);
19+
const featureNameSet = new Set(this.#features.map((feature) => feature.id));
20+
return Array.from(featureNameSet);
2121
}
2222

2323
// If multiple feature flags are found, the first one takes precedence.
2424
async isEnabled(featureId: string, context?: unknown): Promise<boolean> {
25-
const featureFlag = this.#featureFlags.find((flag) => flag.id === featureId);
25+
const featureFlag = this.#features.find((flag) => flag.id === featureId);
2626
if (featureFlag === undefined) {
2727
// If the feature is not found, then it is disabled.
2828
return false;
@@ -61,7 +61,7 @@ export class FeatureManager {
6161
}
6262
}
6363

64-
get #featureFlags(): FeatureDefinition[] {
64+
get #features(): FeatureDefinition[] {
6565
return this.#provider.getFeatureDefinitions();
6666
}
6767

src/filter/FeatureFilter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { ClientFilterParameters } from "../model";
21

32
export interface IFeatureFilter {
43
name: string; //e.g. Microsoft.TimeWindow
5-
evaluate(parameters?: ClientFilterParameters, appContext?: unknown): Promise<boolean> | boolean;
4+
evaluate(parameters?: unknown, appContext?: unknown): Promise<boolean> | boolean;
65
}
76

src/filter/TargetingFilter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { ClientFilterParameters } from "../model";
21

3-
interface TargetingParameters extends ClientFilterParameters {
2+
interface TargetingParameters {
43
// TODO: add targeting parameters.
54
}
65

src/model.ts

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,62 @@
33

44
// Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureFlag.v1.1.0.schema.json
55

6-
/**
7-
* An ID used to uniquely identify and reference the feature.
8-
*/
9-
export type FeatureID = string
10-
/**
11-
* A description of the feature.
12-
*/
13-
export type FeatureDescription = string
14-
/**
15-
* A display name for the feature to use for display rather than the ID.
16-
*/
17-
export type FeatureDisplayName = string
18-
/**
19-
* A feature is OFF if enabled is false. If enabled is true, then the feature is ON if there are no conditions (null or empty) or if the conditions are satisfied.
20-
*/
21-
export type EnabledState = boolean
22-
/**
23-
* Determines whether any or all registered client filters must be enabled for the feature to be considered enabled.
24-
*/
25-
export enum RequirementType {
26-
Any = "Any",
27-
All = "All"
28-
}
29-
/**
30-
* The name used to refer to and require a client filter.
31-
*/
32-
export type ClientFilterName = string
33-
/**
34-
* Filters that must run on the client and be evaluated as true for the feature to be considered enabled.
35-
*/
36-
export type ClientFilterCollection = ClientFilter[]
37-
386
export interface FeatureDefinition {
39-
id: FeatureID
40-
description?: FeatureDescription
41-
display_name?: FeatureDisplayName
42-
enabled: EnabledState
7+
/**
8+
* An ID used to uniquely identify and reference the feature.
9+
*/
10+
id: string
11+
12+
/**
13+
* A description of the feature.
14+
*/
15+
description?: string
16+
17+
/**
18+
* A display name for the feature to use for display rather than the ID.
19+
*/
20+
display_name?: string
21+
22+
/**
23+
* A feature is OFF if enabled is false. If enabled is true, then the feature is ON if there are no conditions (null or empty) or if the conditions are satisfied.
24+
*/
25+
enabled: boolean
26+
27+
/**
28+
* The declaration of conditions used to dynamically enable features.
29+
*/
4330
conditions?: FeatureEnablementConditions
44-
[k: string]: unknown
4531
}
46-
/**
47-
* The declaration of conditions used to dynamically enable features.
48-
*/
32+
33+
export enum RequirementType {
34+
Any = "Any",
35+
All = "All"
36+
}
37+
4938
export interface FeatureEnablementConditions {
39+
/**
40+
* Determines whether any or all registered client filters must be enabled for the feature to be considered enabled.
41+
*/
5042
requirement_type?: RequirementType
51-
client_filters?: ClientFilterCollection
52-
[k: string]: unknown
43+
44+
/**
45+
* Filters that must run on the client and be evaluated as true for the feature to be considered enabled.
46+
*/
47+
client_filters?: ClientFilter[]
5348
}
49+
5450
export interface ClientFilter {
55-
name: ClientFilterName
56-
parameters?: ClientFilterParameters
57-
[k: string]: unknown
58-
}
59-
/**
60-
* Custom parameters for a given client filter. A client filter can require any set of parameters of any type.
61-
*/
62-
export interface ClientFilterParameters {
6351
/**
64-
* This interface was referenced by `ClientFilterParameters`'s JSON-Schema definition
65-
* via the `patternProperty` "^.*$".
52+
* The name used to refer to and require a client filter.
53+
*/
54+
name: string
55+
/**
56+
* Custom parameters for a given client filter. A client filter can require any set of parameters of any type.
6657
*/
67-
[k: string]:
68-
| string
69-
| null
70-
| {
71-
[k: string]: unknown
72-
}
73-
| number
74-
| unknown[]
75-
| boolean
58+
parameters?: unknown
7659
}
7760

78-
// Feature Management definition
61+
// Feature Management Section fed into feature manager.
7962
export const FEATURE_MANAGEMENT_KEY = "FeatureManagement"
8063
export const FEATURE_FLAGS_KEY = "FeatureFlags"
8164
export interface FeatureManagement {

0 commit comments

Comments
 (0)