-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenfeature-bootstrap.ts
More file actions
42 lines (38 loc) · 1.69 KB
/
openfeature-bootstrap.ts
File metadata and controls
42 lines (38 loc) · 1.69 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
/**
* Registers Split OpenFeature providers for `anon-web` and `user-web`, waits until ready, seeds domain context.
* See README: “Why `openfeature-bootstrap.ts`?” for rationale.
*/
import { OpenFeature, type Hook } from '@openfeature/react-sdk';
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
const authKey =
(import.meta.env.VITE_SPLIT_BROWSER_KEY as string | undefined)?.trim() || '<YOUR_AUTHORIZATION_KEY>';
async function registerSplitDomain(domain: string, splitCustomerKey: string): Promise<void> {
const factory = SplitFactory({
core: { authorizationKey: authKey, key: splitCustomerKey },
debug: Boolean(import.meta.env.DEV),
});
await OpenFeature.setProviderAndWait(domain, new OpenFeatureSplitProvider(factory));
}
/** Call from `main.tsx` before `createRoot(…).render` so flags are safe to read on first paint. */
export async function bootstrapOpenFeature(): Promise<void> {
if (import.meta.env.DEV) {
OpenFeature.setLogger(console);
const hook: Hook = {
before(hc) {
console.debug('[OpenFeature]', hc.flagKey, hc.clientMetadata.domain, hc.context.targetingKey);
},
after(hc, d) {
console.debug('[OpenFeature]', hc.flagKey, d.value, d.reason);
},
error(hc, err) {
console.warn('[OpenFeature]', hc.flagKey, err);
},
};
OpenFeature.addHooks(hook);
}
await registerSplitDomain('anon-web', 'anonymous');
await registerSplitDomain('user-web', 'user-1');
await OpenFeature.setContext('anon-web', { targetingKey: 'anonymous' });
await OpenFeature.setContext('user-web', { targetingKey: 'user-1' });
}