-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathfeature-flags.server.ts
More file actions
130 lines (111 loc) · 3.09 KB
/
Copy pathfeature-flags.server.ts
File metadata and controls
130 lines (111 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import 'server-only'
import type { FeatureFlagContext } from '@/core/modules/feature-flags/context'
import {
type BooleanFeatureFlagId,
FEATURE_FLAGS,
type FeatureFlagId,
} from '@/core/modules/feature-flags/definitions'
import type {
EvaluatedFeatureFlag,
FeatureFlagDefinition,
PayloadFeatureFlagDefinition,
} from '@/core/modules/feature-flags/types'
import { l, serializeErrorForLog } from '@/core/shared/clients/logger/logger'
import { launchDarklyOpenFeatureProvider } from './launchdarkly-openfeature-provider.server'
import type {
FeatureFlagProvider,
FeatureFlagSnapshot,
} from './provider.server'
export type FeatureFlagService = {
isEnabled(
flagId: BooleanFeatureFlagId,
context: FeatureFlagContext
): Promise<boolean>
evaluateAll(context: FeatureFlagContext): Promise<EvaluatedFeatureFlag[]>
}
function logUnexpectedFlagValue(flag: FeatureFlagDefinition, value: unknown) {
if (value === undefined) {
return
}
l.warn(
{
key: 'feature_flags:unexpected_value',
context: {
flagKey: flag.key,
expectedKind: flag.kind,
actualType: typeof value,
},
},
'Feature flag returned an unexpected value'
)
}
function parsePayload<T>(
flag: PayloadFeatureFlagDefinition<T>,
payload: unknown
): T {
if (payload === undefined) {
return flag.defaultValue
}
const parsed = flag.schema.safeParse(payload)
if (!parsed.success) {
l.warn(
{
key: 'feature_flags:invalid_payload',
context: { flagKey: flag.key },
error: serializeErrorForLog(parsed.error),
},
'Feature flag payload has invalid shape'
)
return flag.defaultValue
}
return parsed.data
}
function getEvaluatedValue(
flag: FeatureFlagDefinition,
snapshot: FeatureFlagSnapshot
) {
if (flag.kind === 'boolean') {
const value = snapshot.getFlagValue(flag.key)
if (typeof value === 'boolean') {
return value
}
logUnexpectedFlagValue(flag, value)
return flag.defaultValue
}
return parsePayload(flag, snapshot.getPayload(flag.key))
}
export function createFeatureFlagService(
provider: FeatureFlagProvider = launchDarklyOpenFeatureProvider
): FeatureFlagService {
return {
async isEnabled(flagId, context) {
const flag = FEATURE_FLAGS[flagId]
const snapshot = await provider.evaluate(context, [flag])
const value = snapshot.getFlagValue(flag.key)
if (typeof value === 'boolean') {
return value
}
logUnexpectedFlagValue(flag, value)
return flag.defaultValue
},
async evaluateAll(context) {
const flags = Object.entries(FEATURE_FLAGS) as [
FeatureFlagId,
FeatureFlagDefinition,
][]
const snapshot = await provider.evaluate(
context,
flags.map(([, flag]) => flag)
)
return flags.map(([id, flag]) => ({
id,
key: flag.key,
kind: flag.kind,
description: flag.description,
defaultValue: flag.defaultValue,
value: getEvaluatedValue(flag, snapshot),
}))
},
}
}
export const featureFlags = createFeatureFlagService()