Support Feature Flags default values (#7027)

* Support Feature Flags default values

* Update src/services/feature-flags/FeatureFlagsService.ts

Co-authored-by: Bee <68532117+abeatrix@users.noreply.github.com>

* Update FeatureFlag support for unknown values

* refactor isFeatureFlagEnabeld

---------

Co-authored-by: Bee <68532117+abeatrix@users.noreply.github.com>
This commit is contained in:
Tomás BarreiroandBee authored and GitHub committed 2025-10-23 23:30:09 +02:00
1 parent 6f69ffb16f
commit ee1bb2f788
3 files changed
+25 -15

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"claude-dev": patch
---
Support Feature Flags default values
@@ -1,5 +1,5 @@
import { Logger } from "@/services/logging/Logger"
import { FEATURE_FLAGS, FeatureFlag } from "@/shared/services/feature-flags/feature-flags"
import { FEATURE_FLAGS, FeatureFlag, FeatureFlagDefaultValue } from "@/shared/services/feature-flags/feature-flags"
import type { IFeatureFlagsProvider } from "./providers/IFeatureFlagsProvider"
// Default cache time-to-live (TTL) for feature flags - an hour
@@ -18,7 +18,7 @@ export class FeatureFlagsService {
*/
public constructor(private provider: IFeatureFlagsProvider) {}
private cache: Map<FeatureFlag, boolean> = new Map()
private cache: Map<FeatureFlag, unknown> = new Map()
private lastCacheUpdateTime: number = 0
/**
@@ -40,12 +40,12 @@ export class FeatureFlagsService {
Logger.log(`do_nothing flag: ${this.getDoNothingFlag()}`)
}
private async getFeatureFlag(flagName: FeatureFlag): Promise<boolean> {
private async getFeatureFlag(flagName: FeatureFlag): Promise<unknown> {
try {
const flagValue = await this.provider.getFeatureFlag(flagName)
const enabled = flagValue === true
this.cache.set(flagName, enabled)
return enabled
const value = flagValue ?? FeatureFlagDefaultValue[flagName]
this.cache.set(flagName, value)
return value
} catch (error) {
console.error(`Error checking if feature flag ${flagName} is enabled:`, error)
this.cache.set(flagName, false)
@@ -62,10 +62,9 @@ export class FeatureFlagsService {
* @returns Boolean indicating if the feature is enabled
*/
public async isFeatureFlagEnabled(flagName: FeatureFlag): Promise<boolean> {
if (this.cache.has(flagName)) {
return this.cache.get(flagName)!
}
return this.getFeatureFlag(flagName)
const value = this.cache.has(flagName) ? this.cache.get(flagName) : await this.getFeatureFlag(flagName)
return !!value
}
/**
@@ -75,20 +74,20 @@ export class FeatureFlagsService {
* Cache is updated periodically via poll(), and is generated on extension startup,
* and whenever the user logs in.
*/
public getBooleanFlagEnabled(flagName: FeatureFlag, defaultValue = false): boolean {
return this.cache.get(flagName) ?? defaultValue
public getBooleanFlagEnabled(flagName: FeatureFlag): boolean {
return this.cache.get(flagName) === true
}
public getWorkOsAuthEnabled(): boolean {
return this.getBooleanFlagEnabled(FeatureFlag.WORKOS_AUTH, false)
return this.getBooleanFlagEnabled(FeatureFlag.WORKOS_AUTH)
}
public getDoNothingFlag(): boolean {
return this.getBooleanFlagEnabled(FeatureFlag.DO_NOTHING, false)
return this.getBooleanFlagEnabled(FeatureFlag.DO_NOTHING)
}
public getHooksEnabled(): boolean {
return this.getBooleanFlagEnabled(FeatureFlag.HOOKS, false)
return this.getBooleanFlagEnabled(FeatureFlag.HOOKS)
}
/**
@@ -8,4 +8,10 @@ export enum FeatureFlag {
HOOKS = "hooks",
}
export const FeatureFlagDefaultValue: Partial<Record<FeatureFlag, boolean>> = {
[FeatureFlag.WORKOS_AUTH]: true,
[FeatureFlag.DO_NOTHING]: false,
[FeatureFlag.HOOKS]: false,
}
export const FEATURE_FLAGS = Object.values(FeatureFlag)