Skip to content

Commit b70bbd3

Browse files
update FeatureFlag type to match v2 schema (#12)
Co-authored-by: Zhiyuan Liang <[email protected]>
1 parent c541c29 commit b70bbd3

File tree

2 files changed

+136
-25
lines changed

2 files changed

+136
-25
lines changed

src/featureManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ export class FeatureManager {
5050
return true;
5151
}
5252

53-
const requirementType = featureFlag.conditions?.requirement_type ?? RequirementType.Any; // default to any.
53+
const requirementType: RequirementType = featureFlag.conditions?.requirement_type ?? "Any"; // default to any.
5454

5555
/**
5656
* While iterating through the client filters, we short-circuit the evaluation based on the requirement type.
5757
* - When requirement type is "All", the feature is enabled if all client filters are matched. If any client filter is not matched, the feature is disabled, otherwise it is enabled. `shortCircuitEvaluationResult` is false.
5858
* - When requirement type is "Any", the feature is enabled if any client filter is matched. If any client filter is matched, the feature is enabled, otherwise it is disabled. `shortCircuitEvaluationResult` is true.
5959
*/
60-
const shortCircuitEvaluationResult: boolean = requirementType === RequirementType.Any;
60+
const shortCircuitEvaluationResult: boolean = requirementType === "Any";
6161

6262
for (const clientFilter of clientFilters) {
6363
const matchedFeatureFilter = this.#featureFilters.get(clientFilter.name);

src/model.ts

+134-23
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,172 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
// Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureFlag.v1.1.0.schema.json
4+
// Converted from:
5+
// https://github.com/Azure/AppConfiguration/blob/6e544296a5607f922a423df165f60801717c7800/docs/FeatureManagement/FeatureFlag.v2.0.0.schema.json
56

7+
/**
8+
* A feature flag is a named property that can be toggled to enable or disable some feature of an application.
9+
*/
610
export interface FeatureFlag {
711
/**
812
* An ID used to uniquely identify and reference the feature.
913
*/
10-
id: string
11-
14+
id: string;
1215
/**
1316
* A description of the feature.
1417
*/
15-
description?: string
16-
18+
description?: string;
1719
/**
1820
* A display name for the feature to use for display rather than the ID.
1921
*/
20-
display_name?: string
21-
22+
display_name?: string;
2223
/**
2324
* 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.
2425
*/
25-
enabled: boolean
26+
enabled?: boolean;
27+
/**
28+
* The declaration of conditions used to dynamically enable the feature.
29+
*/
30+
conditions?: FeatureEnablementConditions;
31+
/**
32+
* The list of variants defined for this feature. A variant represents a configuration value of a feature flag that can be a string, a number, a boolean, or a JSON object.
33+
*/
34+
variants?: Variant[];
35+
/**
36+
* Determines how variants should be allocated for the feature to various users.
37+
*/
38+
allocation?: VariantAllocation;
39+
/**
40+
* The declaration of options used to configure telemetry for this feature.
41+
*/
42+
telemetry?: TelemetryOptions
43+
}
2644

45+
/**
46+
* The declaration of conditions used to dynamically enable the feature
47+
*/
48+
interface FeatureEnablementConditions {
2749
/**
28-
* The declaration of conditions used to dynamically enable features.
50+
* Determines whether any or all registered client filters must be evaluated as true for the feature to be considered enabled.
2951
*/
30-
conditions?: FeatureEnablementConditions
52+
requirement_type?: RequirementType;
53+
/**
54+
* Filters that must run on the client and be evaluated as true for the feature to be considered enabled.
55+
*/
56+
client_filters?: ClientFilter[];
3157
}
3258

33-
export enum RequirementType {
34-
Any = "Any",
35-
All = "All"
59+
export type RequirementType = "Any" | "All";
60+
61+
interface ClientFilter {
62+
/**
63+
* The name used to refer to a client filter.
64+
*/
65+
name: string;
66+
/**
67+
* Parameters for a given client filter. A client filter can require any set of parameters of any type.
68+
*/
69+
parameters?: Record<string, unknown>;
3670
}
3771

38-
export interface FeatureEnablementConditions {
72+
interface Variant {
3973
/**
40-
* Determines whether any or all registered client filters must be evaluated as true for the feature to be considered enabled.
74+
* The name used to refer to a feature variant.
4175
*/
42-
requirement_type?: RequirementType
76+
name: string;
77+
/**
78+
* The configuration value for this feature variant.
79+
*/
80+
configuration_value?: unknown;
81+
/**
82+
* The path to a configuration section used as the configuration value for this feature variant.
83+
*/
84+
configuration_reference?: string;
85+
/**
86+
* Overrides the enabled state of the feature if the given variant is assigned. Does not override the state if value is None.
87+
*/
88+
status_override?: "None" | "Enabled" | "Disabled";
89+
}
4390

91+
/**
92+
* Determines how variants should be allocated for the feature to various users.
93+
*/
94+
interface VariantAllocation {
4495
/**
45-
* Filters that must run on the client and be evaluated as true for the feature to be considered enabled.
96+
* Specifies which variant should be used when the feature is considered disabled.
97+
*/
98+
default_when_disabled?: string;
99+
/**
100+
* Specifies which variant should be used when the feature is considered enabled and no other allocation rules are applicable.
101+
*/
102+
default_when_enabled?: string;
103+
/**
104+
* A list of objects, each containing a variant name and list of users for whom that variant should be used.
105+
*/
106+
user?: UserAllocation[];
107+
/**
108+
* A list of objects, each containing a variant name and list of groups for which that variant should be used.
109+
*/
110+
group?: GroupAllocation[];
111+
/**
112+
* A list of objects, each containing a variant name and percentage range for which that variant should be used.
113+
*/
114+
percentile?: PercentileAllocation[]
115+
/**
116+
* The value percentile calculations are based on. The calculated percentile is consistent across features for a given user if the same nonempty seed is used.
117+
*/
118+
seed?: string;
119+
}
120+
121+
interface UserAllocation {
122+
/**
123+
* The name of the variant to use if the user allocation matches the current user.
124+
*/
125+
variant: string;
126+
/**
127+
* Collection of users where if any match the current user, the variant specified in the user allocation is used.
128+
*/
129+
users: string[];
130+
}
131+
132+
interface GroupAllocation {
133+
/**
134+
* The name of the variant to use if the group allocation matches a group the current user is in.
135+
*/
136+
variant: string;
137+
/**
138+
* Collection of groups where if the current user is in any of these groups, the variant specified in the group allocation is used.
139+
*/
140+
groups: string[];
141+
}
142+
143+
interface PercentileAllocation {
144+
/**
145+
* The name of the variant to use if the calculated percentile for the current user falls in the provided range.
146+
*/
147+
variant: string;
148+
/**
149+
* The lower end of the percentage range for which this variant will be used.
150+
*/
151+
from: number;
152+
/**
153+
* The upper end of the percentage range for which this variant will be used.
46154
*/
47-
client_filters?: ClientFilter[]
155+
to: number;
48156
}
49157

50-
export interface ClientFilter {
158+
/**
159+
* The declaration of options used to configure telemetry for this feature.
160+
*/
161+
interface TelemetryOptions {
51162
/**
52-
* The name used to refer to and require a client filter.
163+
* Indicates if telemetry is enabled.
53164
*/
54-
name: string
165+
enabled?: boolean;
55166
/**
56-
* Custom parameters for a given client filter. A client filter can require any set of parameters of any type.
167+
* A container for metadata that should be bundled with flag telemetry.
57168
*/
58-
parameters?: unknown
169+
metadata?: Record<string, string>;
59170
}
60171

61172
// Feature Management Section fed into feature manager.

0 commit comments

Comments
 (0)