Line data Source code
1 : const FF_PREFIX = 'ff:'
2 :
3 : /**
4 : * Gets a dev-time feature flag override from localStorage.
5 : * Stripped from production builds via import.meta.env.DEV tree-shaking.
6 : *
7 : * Returns undefined (not null) as the "no override" sentinel because
8 : * null is a valid JSON value — JSON.parse('null') returns null.
9 : * Using undefined avoids ambiguity between "no override set" and
10 : * "override explicitly set to null".
11 : *
12 : * Usage in browser console:
13 : * localStorage.setItem('ff:example_enabled', 'true')
14 : * localStorage.removeItem('ff:example_enabled')
15 : */
16 7110332 : export function getDevOverride<T>(
17 : flagKey: string & { readonly valueType?: T }
18 : ): T | undefined {
19 : if (!import.meta.env.DEV || typeof localStorage === 'undefined')
20 : return undefined
21 : let raw: string | null
22 : try {
23 : raw = localStorage.getItem(`${FF_PREFIX}${flagKey}`)
24 : } catch {
25 : return undefined
26 : }
27 : if (raw === null) return undefined
28 : try {
29 : return JSON.parse(raw) as T
30 : } catch {
31 : console.warn(`[ff] Invalid JSON for override "${flagKey}":`, raw)
32 : return undefined
33 : }
34 : }
|