-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathprovider.tsx
More file actions
65 lines (61 loc) · 2.17 KB
/
Copy pathprovider.tsx
File metadata and controls
65 lines (61 loc) · 2.17 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
import { withFrameworkMetadata } from '@openfeature/core';
import type { Client, OpenFeatureAPIBase } from '@openfeature/web-sdk';
import { OpenFeature } from '@openfeature/web-sdk';
import * as React from 'react';
import type { ReactFlagEvaluationOptions } from '../options';
import { Context } from '../internal';
type ClientOrDomain =
| {
/**
* An identifier which logically binds clients with providers
* @see OpenFeature.setProvider() and overloads.
*/
domain?: string;
/**
* An isolated OpenFeature API instance to use instead of the global singleton.
* Use this in micro-frontend architectures where different parts of the application
* need isolated OpenFeature instances.
* @see createIsolatedOpenFeatureAPI from '@openfeature/web-sdk/isolated'
* @example
* ```tsx
* import { createIsolatedOpenFeatureAPI } from '@openfeature/web-sdk/isolated';
*
* const MyOpenFeature = createIsolatedOpenFeatureAPI();
* MyOpenFeature.setProvider(myProvider);
*
* function App() {
* return (
* <OpenFeatureProvider openfeature={MyOpenFeature}>
* {children}
* </OpenFeatureProvider>
* );
* }
* ```
*/
openfeature?: OpenFeatureAPIBase;
client?: never;
}
| {
/**
* OpenFeature client to use.
*/
client?: Client;
domain?: never;
openfeature?: never;
};
type ProviderProps = {
children?: React.ReactNode;
} & ClientOrDomain &
ReactFlagEvaluationOptions;
/**
* Provides a scope for evaluating feature flags by binding a client to all child components.
* @param {ProviderProps} properties props for the context provider
* @returns {OpenFeatureProvider} context provider
*/
export function OpenFeatureProvider({ client, domain, openfeature, children, ...options }: ProviderProps) {
const stableClient = React.useMemo(
() => withFrameworkMetadata(client || (openfeature ?? OpenFeature).getClient(domain), 'react'),
[client, domain, openfeature],
);
return <Context.Provider value={{ client: stableClient, options }}>{children}</Context.Provider>;
}