-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[PM-2282] Make feature flags type safe #8612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
e6a6319
87eb7e3
31f6a5c
1877773
31a3da0
31135c0
926e451
1434cdc
734eda4
835a8a2
ae94ee5
46a51ac
1b18c78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,31 @@ export enum FeatureFlag { | |
AC1795_UpdatedSubscriptionStatusSection = "AC-1795_updated-subscription-status-section", | ||
} | ||
|
||
// Replace this with a type safe lookup of the feature flag values in PM-2282 | ||
export type FeatureFlagValue = number | string | boolean; | ||
// Map of feature flags to their value type. `string`, `number` and `boolean` are the only supported types. | ||
export const FeatureFlagRuntimeTypes = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we technically don't need these for this PR, #8561 requires it if we want to introduce a default |
||
[FeatureFlag.BrowserFilelessImport]: "boolean", | ||
[FeatureFlag.ItemShare]: "boolean", | ||
[FeatureFlag.FlexibleCollectionsV1]: "boolean", | ||
[FeatureFlag.VaultOnboarding]: "boolean", | ||
[FeatureFlag.GeneratorToolsModernization]: "boolean", | ||
[FeatureFlag.KeyRotationImprovements]: "boolean", | ||
[FeatureFlag.FlexibleCollectionsMigration]: "boolean", | ||
[FeatureFlag.ShowPaymentMethodWarningBanners]: "boolean", | ||
[FeatureFlag.EnableConsolidatedBilling]: "boolean", | ||
[FeatureFlag.AC1795_UpdatedSubscriptionStatusSection]: "boolean", | ||
} as const; | ||
|
||
// Helpers for mapping between runtime and static types | ||
type RuntimeType = "boolean" | "string" | "number"; | ||
type RuntimeToStatic<T extends RuntimeType> = T extends "boolean" | ||
? boolean | ||
: T extends "string" | ||
? string | ||
: T extends "number" | ||
? number | ||
: never; | ||
|
||
// Typescript type of the feature flag values | ||
export type FeatureFlagType<Flag extends FeatureFlag> = RuntimeToStatic< | ||
(typeof FeatureFlagRuntimeTypes)[Flag] | ||
>; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 Since the default config service has test cases, they should be updated. |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,7 +13,7 @@ import { | |||||||||
} from "rxjs"; | ||||||||||
import { SemVer } from "semver"; | ||||||||||
|
||||||||||
import { FeatureFlag, FeatureFlagValue } from "../../../enums/feature-flag.enum"; | ||||||||||
import { FeatureFlag, FeatureFlagType } from "../../../enums/feature-flag.enum"; | ||||||||||
import { UserId } from "../../../types/guid"; | ||||||||||
import { ConfigApiServiceAbstraction } from "../../abstractions/config/config-api.service.abstraction"; | ||||||||||
import { ConfigService } from "../../abstractions/config/config.service"; | ||||||||||
|
@@ -90,19 +90,20 @@ export class DefaultConfigService implements ConfigService { | |||||||||
map((config) => config?.environment?.cloudRegion ?? Region.US), | ||||||||||
); | ||||||||||
} | ||||||||||
getFeatureFlag$<T extends FeatureFlagValue>(key: FeatureFlag, defaultValue?: T) { | ||||||||||
|
||||||||||
getFeatureFlag$<Flag extends FeatureFlag>(key: Flag, defaultValue?: FeatureFlagType<Flag>) { | ||||||||||
return this.serverConfig$.pipe( | ||||||||||
map((serverConfig) => { | ||||||||||
if (serverConfig?.featureStates == null || serverConfig.featureStates[key] == null) { | ||||||||||
return defaultValue; | ||||||||||
} | ||||||||||
|
||||||||||
return serverConfig.featureStates[key] as T; | ||||||||||
return serverConfig.featureStates[key] as FeatureFlagType<Flag>; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎨 suggestion: Since we now have the types available during runtime, maybe we should add some guards here to verify that we actually get the types we expect?
Suggested change
Or maybe this check should be done when fetching the server config, in which case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was planning on using it in my other PR. |
||||||||||
}), | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
async getFeatureFlag<T extends FeatureFlagValue>(key: FeatureFlag, defaultValue?: T) { | ||||||||||
async getFeatureFlag<Flag extends FeatureFlag>(key: Flag, defaultValue?: FeatureFlagType<Flag>) { | ||||||||||
return await firstValueFrom(this.getFeatureFlag$(key, defaultValue)); | ||||||||||
} | ||||||||||
|
||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.