feature flags
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
/**
|
||||
* FeatureFlagService - Manages feature flags for both server and client
|
||||
*
|
||||
* Automatic Alpha Mode Integration:
|
||||
* When NEXT_PUBLIC_GRIDPILOT_MODE=alpha, all features are automatically enabled.
|
||||
* This eliminates the need to manually set FEATURE_FLAGS for alpha deployments.
|
||||
* API-Driven Integration:
|
||||
* Fetches feature flags from the API endpoint GET /features
|
||||
* Returns empty flags on error (secure by default)
|
||||
*
|
||||
* Server: Reads from process.env.FEATURE_FLAGS (comma-separated)
|
||||
* OR auto-enables all features if in alpha mode
|
||||
* Server: Fetches from API at ${NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001'}/features
|
||||
* Client: Reads from session context or provides mock implementation
|
||||
*/
|
||||
|
||||
@@ -18,7 +17,7 @@ export class FeatureFlagService {
|
||||
if (flags) {
|
||||
this.flags = new Set(flags);
|
||||
} else {
|
||||
// Parse from environment variable
|
||||
// Parse from environment variable (fallback for backward compatibility)
|
||||
const flagsEnv = process.env.FEATURE_FLAGS;
|
||||
this.flags = flagsEnv
|
||||
? new Set(flagsEnv.split(',').map(f => f.trim()))
|
||||
@@ -41,33 +40,44 @@ export class FeatureFlagService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create service with environment flags
|
||||
* Automatically enables all features if in alpha mode
|
||||
* FEATURE_FLAGS can override alpha mode defaults
|
||||
* Factory method to create service by fetching from API
|
||||
* Fetches from ${NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001'}/features
|
||||
* On error, returns empty flags (secure by default)
|
||||
*/
|
||||
static fromEnv(): FeatureFlagService {
|
||||
const mode = process.env.NEXT_PUBLIC_GRIDPILOT_MODE;
|
||||
const flagsEnv = process.env.FEATURE_FLAGS;
|
||||
|
||||
// If FEATURE_FLAGS is explicitly set, use it (overrides alpha mode)
|
||||
if (flagsEnv) {
|
||||
return new FeatureFlagService();
|
||||
static async fromAPI(): Promise<FeatureFlagService> {
|
||||
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';
|
||||
const url = `${baseUrl}/features`;
|
||||
|
||||
try {
|
||||
// Use next: { revalidate: 0 } for Next.js server runtime
|
||||
// This is equivalent to cache: 'no-store' but is the preferred Next.js convention
|
||||
const response = await fetch(url, {
|
||||
next: { revalidate: 0 },
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Parse JSON { features: Record<string, string> }
|
||||
// Enable flags whose value is 'enabled'
|
||||
const enabledFlags: string[] = [];
|
||||
if (data.features && typeof data.features === 'object') {
|
||||
Object.entries(data.features).forEach(([flag, value]) => {
|
||||
if (value === 'enabled') {
|
||||
enabledFlags.push(flag);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new FeatureFlagService(enabledFlags);
|
||||
} catch (error) {
|
||||
// Log error but return empty flags (secure by default)
|
||||
console.error('Failed to fetch feature flags from API:', error);
|
||||
return new FeatureFlagService([]);
|
||||
}
|
||||
|
||||
// If in alpha mode, automatically enable all features
|
||||
if (mode === 'alpha') {
|
||||
return new FeatureFlagService([
|
||||
'driver_profiles',
|
||||
'team_profiles',
|
||||
'wallets',
|
||||
'sponsors',
|
||||
'team_feature',
|
||||
'alpha_features'
|
||||
]);
|
||||
}
|
||||
|
||||
// Otherwise, use FEATURE_FLAGS environment variable (empty if not set)
|
||||
return new FeatureFlagService();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user