-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix .NET, JavaScript and Node.js OpenFeature provider code samples (#486
) * Correct code samples in OpenFeature Provider for .NET reference * Correct code samples in OpenFeature Provider for JavaScript reference * Correct code samples in OpenFeature Provider for Node.js reference * Kill .NET Core
- Loading branch information
Showing
10 changed files
with
142 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,21 +38,23 @@ dotnet add package OpenFeature.Contrib.Providers.ConfigCat | |
</Tabs> | ||
### 2. Initialize the provider | ||
|
||
The `ConfigCatProvider` constructor takes the SDK key and an optional `ConfigCatClientOptions` argument containing the additional configuration options for the [ConfigCat .NET SDK](../../dotnet/#creating-the-configcat-client): | ||
The `ConfigCatProvider` constructor takes the SDK key and an optional callback that can be used to specify additional configuration options for the [ConfigCat .NET SDK](../../dotnet/#creating-the-configcat-client): | ||
|
||
```cs | ||
using OpenFeature.Contrib.Providers.ConfigCat; | ||
using System; | ||
using ConfigCat.Client; | ||
using OpenFeature.Contrib.ConfigCat; | ||
|
||
// Build options for the ConfigCat SDK. | ||
var options = new ConfigCatClientOptions | ||
// Specify options for the ConfigCat SDK. | ||
Action<ConfigCat.Client.Configuration.ConfigCatClientOptions> configureOptions = (options) => | ||
{ | ||
PollingMode = PollingModes.AutoPoll(pollInterval: TimeSpan.FromSeconds(60)); | ||
Logger = new ConsoleLogger(LogLevel.Warning); | ||
options.PollingMode = PollingModes.AutoPoll(pollInterval: TimeSpan.FromSeconds(60)); | ||
options.Logger = new ConsoleLogger(LogLevel.Warning); | ||
// ... | ||
}; | ||
|
||
// Configure the provider. | ||
OpenFeature.Api.Instance.SetProvider(new ConfigCatProvider("#YOUR-SDK-KEY#", options)); | ||
await OpenFeature.Api.Instance.SetProviderAsync(new ConfigCatProvider("#YOUR-SDK-KEY#", configureOptions)); | ||
|
||
// Create a client. | ||
var client = OpenFeature.Api.Instance.GetClient(); | ||
|
@@ -64,7 +66,7 @@ For more information about all the configuration options, see the [.NET SDK docu | |
|
||
```cs | ||
var isAwesomeFeatureEnabled = await client.GetBooleanValueAsync("isAwesomeFeatureEnabled", false); | ||
if(isAwesomeFeatureEnabled) | ||
if (isAwesomeFeatureEnabled) | ||
{ | ||
doTheNewThing(); | ||
} | ||
|
@@ -74,6 +76,14 @@ else | |
} | ||
``` | ||
|
||
### 4. Cleaning up | ||
|
||
On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client. | ||
|
||
```cs | ||
await OpenFeature.Api.Instance.ShutdownAsync(); | ||
``` | ||
|
||
## Evaluation Context | ||
|
||
An <a href="https://openfeature.dev/docs/reference/concepts/evaluation-context" target="_blank">evaluation context</a> in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. | ||
|
@@ -91,12 +101,12 @@ The following table shows how the different context attributes are mapped to Use | |
To evaluate feature flags for a context, use the <a href="https://openfeature.dev/docs/reference/concepts/evaluation-api/" target="_blank">OpenFeature Evaluation API</a>: | ||
|
||
```cs | ||
var context = new EvaluationContext() | ||
var context = OpenFeature.Model.EvaluationContext.Builder() | ||
.Set("Id", "#SOME-USER-ID#") | ||
.Set("Email", "[email protected]") | ||
.Set("Country", "CountryID") | ||
.Set("Rating", 4.5) | ||
.Set("RegisteredAt", DateTime.Parse("2023-11-22 12:34:56 +00:00", CultureInfo.InvariantCulture)) | ||
.Set("RegisteredAt", DateTime.Parse("2023-11-22 12:34:56 +00:00", System.Globalization.CultureInfo.InvariantCulture)) | ||
.Build(); | ||
|
||
var isAwesomeFeatureEnabled = await client.GetBooleanValueAsync("isAwesomeFeatureEnabled", false, context); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,41 +13,29 @@ import TabItem from '@theme/TabItem'; | |
|
||
### 1. Install the provider | ||
|
||
<Tabs groupId="js-install"> | ||
<TabItem value="NPM" label="NPM"> | ||
|
||
```bash | ||
npm i @openfeature/config-cat-web-provider | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="CDN" label="CDN"> | ||
|
||
```html | ||
<script | ||
type="text/javascript" | ||
src="https://cdn.jsdelivr.net/npm/@openfeature/config-cat-web-provider/index.cjs.min.js"> | ||
</script> | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
### 2. Initialize the provider | ||
|
||
The `ConfigCatWebProvider.create()` function takes the SDK key and an optional `options` argument containing additional configuration options for the [ConfigCat JavaScript SDK](../../js/#creating-the-configcat-client): | ||
|
||
```js | ||
import { OpenFeature } from "@openfeature/web-sdk"; | ||
import { ConfigCatWebProvider } from '@openfeature/config-cat-web-provider'; | ||
import { createConsoleLogger, LogLevel } from "configcat-js-ssr"; | ||
|
||
// Build options for the ConfigCat SDK. | ||
const options = { | ||
setupHooks: (hooks) => hooks.on('clientReady', () => console.log('Client is ready!')), | ||
// ... | ||
const options = { | ||
logger: createConsoleLogger(LogLevel.Info), | ||
setupHooks: (hooks) => hooks.on('clientReady', () => console.log('Client is ready!')), | ||
// ... | ||
} | ||
|
||
// Configure the provider. | ||
OpenFeature.setProvider(ConfigCatWebProvider.create('#YOUR-SDK-KEY#', options)); | ||
await OpenFeature.setProviderAndWait(ConfigCatWebProvider.create('#YOUR-SDK-KEY#', options)); | ||
|
||
// Create a client. | ||
const client = OpenFeature.getClient(); | ||
``` | ||
|
@@ -58,13 +46,21 @@ For more information about all the configuration options, see the [JavaScript SD | |
|
||
```js | ||
const isAwesomeFeatureEnabled = client.getBooleanValue('isAwesomeFeatureEnabled', false); | ||
if(isAwesomeFeatureEnabled) { | ||
doTheNewThing(); | ||
if (isAwesomeFeatureEnabled) { | ||
doTheNewThing(); | ||
} else { | ||
doTheOldThing(); | ||
doTheOldThing(); | ||
} | ||
``` | ||
|
||
### 4. Cleaning up | ||
|
||
On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client. | ||
|
||
```cs | ||
await OpenFeature.clearProviders(); | ||
``` | ||
|
||
## Evaluation Context | ||
|
||
An <a href="https://openfeature.dev/docs/reference/concepts/evaluation-context" target="_blank">evaluation context</a> in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. | ||
|
@@ -82,10 +78,10 @@ The following table shows how the different context attributes are mapped to Use | |
To evaluate feature flags for a context, use the <a href="https://openfeature.dev/docs/reference/concepts/evaluation-api/" target="_blank">OpenFeature Evaluation API</a>: | ||
|
||
```js | ||
await OpenFeature.setContext({ | ||
targetingKey: '#SOME-USER-ID#', | ||
email: '[email protected]', | ||
country: 'CountryID', | ||
await OpenFeature.setContext({ | ||
targetingKey: '#SOME-USER-ID#', | ||
email: '[email protected]', | ||
country: 'CountryID', | ||
}); | ||
|
||
const isAwesomeFeatureEnabled = client.getBooleanValue('isAwesomeFeatureEnabled', false); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
id: node | ||
title: OpenFeature Provider for Node.js | ||
description: ConfigCat OpenFeature Provider for Node. This is a step-by-step guide on how to use ConfigCat with the OpenFeature Node.js SDK. | ||
description: ConfigCat OpenFeature Provider for Node.js. This is a step-by-step guide on how to use ConfigCat with the OpenFeature Node.js SDK. | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
|
@@ -22,17 +22,20 @@ npm i @openfeature/config-cat-provider | |
The `ConfigCatProvider.create()` function takes the SDK key and an optional `options` argument containing additional configuration options for the [ConfigCat Node.js SDK](../../node/#creating-the-configcat-client): | ||
|
||
```js | ||
import { OpenFeature } from "@openfeature/server-sdk"; | ||
import { ConfigCatProvider } from '@openfeature/config-cat-provider'; | ||
import { createConsoleLogger, LogLevel, PollingMode } from "configcat-node"; | ||
|
||
// Build options for the ConfigCat SDK. | ||
const options = { | ||
setupHooks: (hooks) => hooks.on('clientReady', () => console.log('Client is ready!')), | ||
// ... | ||
const options = { | ||
logger: createConsoleLogger(LogLevel.Info), | ||
setupHooks: (hooks) => hooks.on('clientReady', () => console.log('Client is ready!')), | ||
// ... | ||
} | ||
|
||
// Configure the provider. | ||
OpenFeature.setProvider(ConfigCatWebProvider.create('#YOUR-SDK-KEY#', PollingMode.AutoPoll, options)); | ||
await OpenFeature.setProviderAndWait(ConfigCatProvider.create('#YOUR-SDK-KEY#', PollingMode.AutoPoll, options)); | ||
|
||
// Create a client. | ||
const client = OpenFeature.getClient(); | ||
``` | ||
|
@@ -43,13 +46,21 @@ For more information about all the configuration options, see the [Node.js SDK d | |
|
||
```js | ||
const isAwesomeFeatureEnabled = await client.getBooleanValue('isAwesomeFeatureEnabled', false); | ||
if(isAwesomeFeatureEnabled) { | ||
doTheNewThing(); | ||
if (isAwesomeFeatureEnabled) { | ||
doTheNewThing(); | ||
} else { | ||
doTheOldThing(); | ||
doTheOldThing(); | ||
} | ||
``` | ||
|
||
### 4. Cleaning up | ||
|
||
On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client. | ||
|
||
```cs | ||
await OpenFeature.clearProviders(); | ||
``` | ||
|
||
## Evaluation Context | ||
|
||
An <a href="https://openfeature.dev/docs/reference/concepts/evaluation-context" target="_blank">evaluation context</a> in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. | ||
|
@@ -67,10 +78,10 @@ The following table shows how the different context attributes are mapped to Use | |
To evaluate feature flags for a context, use the <a href="https://openfeature.dev/docs/reference/concepts/evaluation-api/" target="_blank">OpenFeature Evaluation API</a>: | ||
|
||
```js | ||
const context = { | ||
targetingKey: '#SOME-USER-ID#', | ||
email: '[email protected]', | ||
country: 'CountryID', | ||
const context = { | ||
targetingKey: '#SOME-USER-ID#', | ||
email: '[email protected]', | ||
country: 'CountryID', | ||
}; | ||
|
||
const isAwesomeFeatureEnabled = await client.getBooleanValue('isAwesomeFeatureEnabled', false, context); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,21 +38,23 @@ dotnet add package OpenFeature.Contrib.Providers.ConfigCat | |
</Tabs> | ||
### 2. Initialize the provider | ||
|
||
The `ConfigCatProvider` constructor takes the SDK key and an optional `ConfigCatClientOptions` argument containing the additional configuration options for the [ConfigCat .NET SDK](../../dotnet/#creating-the-configcat-client): | ||
The `ConfigCatProvider` constructor takes the SDK key and an optional callback that can be used to specify additional configuration options for the [ConfigCat .NET SDK](../../dotnet/#creating-the-configcat-client): | ||
|
||
```cs | ||
using OpenFeature.Contrib.Providers.ConfigCat; | ||
using System; | ||
using ConfigCat.Client; | ||
using OpenFeature.Contrib.ConfigCat; | ||
|
||
// Build options for the ConfigCat SDK. | ||
var options = new ConfigCatClientOptions | ||
// Specify options for the ConfigCat SDK. | ||
Action<ConfigCat.Client.Configuration.ConfigCatClientOptions> configureOptions = (options) => | ||
{ | ||
PollingMode = PollingModes.AutoPoll(pollInterval: TimeSpan.FromSeconds(60)); | ||
Logger = new ConsoleLogger(LogLevel.Warning); | ||
options.PollingMode = PollingModes.AutoPoll(pollInterval: TimeSpan.FromSeconds(60)); | ||
options.Logger = new ConsoleLogger(LogLevel.Warning); | ||
// ... | ||
}; | ||
|
||
// Configure the provider. | ||
OpenFeature.Api.Instance.SetProvider(new ConfigCatProvider("#YOUR-SDK-KEY#", options)); | ||
await OpenFeature.Api.Instance.SetProviderAsync(new ConfigCatProvider("#YOUR-SDK-KEY#", configureOptions)); | ||
|
||
// Create a client. | ||
var client = OpenFeature.Api.Instance.GetClient(); | ||
|
@@ -64,7 +66,7 @@ For more information about all the configuration options, see the [.NET SDK docu | |
|
||
```cs | ||
var isAwesomeFeatureEnabled = await client.GetBooleanValueAsync("isAwesomeFeatureEnabled", false); | ||
if(isAwesomeFeatureEnabled) | ||
if (isAwesomeFeatureEnabled) | ||
{ | ||
doTheNewThing(); | ||
} | ||
|
@@ -74,6 +76,14 @@ else | |
} | ||
``` | ||
|
||
### 4. Cleaning up | ||
|
||
On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client. | ||
|
||
```cs | ||
await OpenFeature.Api.Instance.ShutdownAsync(); | ||
``` | ||
|
||
## Evaluation Context | ||
|
||
An <a href="https://openfeature.dev/docs/reference/concepts/evaluation-context" target="_blank">evaluation context</a> in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. | ||
|
@@ -91,12 +101,12 @@ The following table shows how the different context attributes are mapped to Use | |
To evaluate feature flags for a context, use the <a href="https://openfeature.dev/docs/reference/concepts/evaluation-api/" target="_blank">OpenFeature Evaluation API</a>: | ||
|
||
```cs | ||
var context = new EvaluationContext() | ||
var context = OpenFeature.Model.EvaluationContext.Builder() | ||
.Set("Id", "#SOME-USER-ID#") | ||
.Set("Email", "[email protected]") | ||
.Set("Country", "CountryID") | ||
.Set("Rating", 4.5) | ||
.Set("RegisteredAt", DateTime.Parse("2023-11-22 12:34:56 +00:00", CultureInfo.InvariantCulture)) | ||
.Set("RegisteredAt", DateTime.Parse("2023-11-22 12:34:56 +00:00", System.Globalization.CultureInfo.InvariantCulture)) | ||
.Build(); | ||
|
||
var isAwesomeFeatureEnabled = await client.GetBooleanValueAsync("isAwesomeFeatureEnabled", false, context); | ||
|
Oops, something went wrong.