Skip to content

Commit

Permalink
feat!: use "domain" instead of "clientName" (#826)
Browse files Browse the repository at this point in the history
## This PR

- adds support for domains
- removes named clients

---------

Signed-off-by: Michael Beemer <[email protected]>
Signed-off-by: Michael Beemer <[email protected]>
  • Loading branch information
beeme1mr authored Feb 27, 2024
1 parent 5637cb2 commit 427ba88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
15 changes: 9 additions & 6 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The OpenFeature React SDK adds React-specific functionality to the [OpenFeature

In addition to the feature provided by the [web sdk](https://openfeature.dev/docs/reference/technologies/client/web), capabilities include:

- [Multiple Providers and Scoping](#multiple-providers-and-scoping)
- [Multiple Providers and domains](#multiple-providers-and-domains)
- [Re-rendering with Context Changes](#re-rendering-with-context-changes)
- [Re-rendering with Flag Configuration Changes](#re-rendering-with-flag-configuration-changes)
- [Suspense Support](#suspense-support)
Expand Down Expand Up @@ -130,15 +130,16 @@ const {
} = useBooleanFlagDetails('new-message', false);
```

### Multiple Providers and Scoping
### Multiple Providers and Domains

Multiple providers and scoped clients can be configured by passing a `clientName` to the `OpenFeatureProvider`:

Multiple providers can be used by passing a `domain` to the `OpenFeatureProvider`:

```tsx
// Flags within this scope will use the a client/provider associated with `myClient`,
// Flags within this domain will use the a client/provider associated with `my-domain`,
function App() {
return (
<OpenFeatureProvider clientName={'myClient'}>
<OpenFeatureProvider domain={'my-domain'}>
<Page></Page>
</OpenFeatureProvider>
);
Expand All @@ -148,9 +149,11 @@ function App() {
This is analogous to:

```ts
OpenFeature.getClient('myClient');
OpenFeature.getClient('my-domain');
```

For more information about `domains`, refer to the [web SDK](https://github.com/open-feature/js-sdk/blob/main/packages/client/README.md).

### Re-rendering with Context Changes

By default, if the OpenFeature [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context) is modified, components will be re-rendered.
Expand Down
21 changes: 7 additions & 14 deletions packages/react/src/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
import * as React from 'react';
import { Client, OpenFeature } from '@openfeature/web-sdk';

type ClientOrClientName =
type ClientOrDomain =
| {
/**
* The name of the client.
* An identifier which logically binds clients with providers
* @see OpenFeature.setProvider() and overloads.
*/
clientName: string;
/**
* OpenFeature client to use.
*/
domain: string;
client?: never;
}
| {
/**
* OpenFeature client to use.
*/
client: Client;
/**
* The name of the client.
* @see OpenFeature.setProvider() and overloads.
*/
clientName?: never;
domain?: never;
};

type ProviderProps = {
children?: React.ReactNode;
} & ClientOrClientName;
} & ClientOrDomain;

const Context = React.createContext<Client | undefined>(undefined);

export const OpenFeatureProvider = ({ client, clientName, children }: ProviderProps) => {
export const OpenFeatureProvider = ({ client, domain, children }: ProviderProps) => {
if (!client) {
client = OpenFeature.getClient(clientName);
client = OpenFeature.getClient(domain);
}

return <Context.Provider value={client}>{children}</Context.Provider>;
Expand Down

0 comments on commit 427ba88

Please sign in to comment.