Skip to content

Commit

Permalink
fix!: Allow ConfigCat provider to be used in server applications (#796)
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Simon <adam@configcat.com>
Co-authored-by: Adam Simon <adam@configcat.com>
  • Loading branch information
adams85 and Adam Simon authored Mar 14, 2024
1 parent 5113e6f commit 190946f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
11 changes: 6 additions & 5 deletions libs/providers/config-cat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ The OpenFeature SDK is required as peer dependency.

The minimum required version of `@openfeature/server-sdk` currently is `1.6.0`.

The minimum required version of `configcat-js` currently is `8.0.0`.
The minimum required version of `configcat-js-ssr` currently is `7.1.2`.

```
$ npm install @openfeature/server-sdk configcat-js
$ npm install @openfeature/server-sdk configcat-js-ssr
```

## Usage

The ConfigCat provider uses the [ConfigCat Javascript SDK](https://configcat.com/docs/sdk-reference/js/).
The ConfigCat provider uses the [ConfigCat JavaScript (SSR) SDK](https://configcat.com/docs/sdk-reference/js-ssr/).
This means that the provider can be used in both server (e.g. Node.js) and client (browser) applications.

It can either be created by passing the ConfigCat SDK options to ```ConfigCatProvider.create``` or
the ```ConfigCatProvider``` constructor.

The available options can be found in the [ConfigCat Javascript SDK docs](https://configcat.com/docs/sdk-reference/js/).
The available options can be found in the [ConfigCat JavaScript (SSR) SDK](https://configcat.com/docs/sdk-reference/js-ssr/).

### Example using the default configuration

Expand All @@ -53,7 +54,7 @@ OpenFeature.setProvider(provider);
## Evaluation Context

ConfigCat only supports string values in its "evaluation
context", [there known as user](https://configcat.com/docs/advanced/user-object/).
context", [there known as User Object](https://configcat.com/docs/advanced/user-object/).

This means that every value is converted to a string. This is trivial for numbers and booleans. Objects and arrays are
converted to JSON strings that can be interpreted in ConfigCat.
Expand Down
2 changes: 1 addition & 1 deletion libs/providers/config-cat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"peerDependencies": {
"@openfeature/server-sdk": "^1.8.0",
"configcat-js": "^8.0.0 || ^9.0.0"
"configcat-js-ssr": ">=7.1.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
LogLevel,
OverrideBehaviour,
PollingMode,
} from 'configcat-js';
} from 'configcat-js-ssr';

import { IEventEmitter } from 'configcat-common/lib/EventEmitter';

Expand Down
10 changes: 5 additions & 5 deletions libs/providers/config-cat/src/lib/config-cat-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
StandardResolutionReasons,
TypeMismatchError,
} from '@openfeature/server-sdk';
import { getClient, IConfig, IConfigCatClient, IEvaluationDetails, SettingValue } from 'configcat-js';
import { getClient, IConfig, IConfigCatClient, IEvaluationDetails, SettingValue } from 'configcat-js-ssr';
import { transformContext } from './context-transformer';

export class ConfigCatProvider implements Provider {
Expand Down Expand Up @@ -99,7 +99,7 @@ export class ConfigCatProvider implements Provider {
const { value, ...evaluationData } = await this._client.getValueDetailsAsync<SettingValue>(
flagKey,
undefined,
transformContext(context),
context.targetingKey != null ? transformContext(context) : undefined,
);

const validatedValue = validateFlagType('boolean', value);
Expand All @@ -121,7 +121,7 @@ export class ConfigCatProvider implements Provider {
const { value, ...evaluationData } = await this._client.getValueDetailsAsync<SettingValue>(
flagKey,
undefined,
transformContext(context),
context.targetingKey != null ? transformContext(context) : undefined,
);

const validatedValue = validateFlagType('string', value);
Expand All @@ -143,7 +143,7 @@ export class ConfigCatProvider implements Provider {
const { value, ...evaluationData } = await this._client.getValueDetailsAsync<SettingValue>(
flagKey,
undefined,
transformContext(context),
context.targetingKey != null ? transformContext(context) : undefined,
);

const validatedValue = validateFlagType('number', value);
Expand All @@ -165,7 +165,7 @@ export class ConfigCatProvider implements Provider {
const { value, ...evaluationData } = await this._client.getValueDetailsAsync(
flagKey,
undefined,
transformContext(context),
context.targetingKey != null ? transformContext(context) : undefined,
);

if (typeof value === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion libs/providers/config-cat/src/lib/context-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EvaluationContext, EvaluationContextValue, TargetingKeyMissingError } from '@openfeature/server-sdk';
import { User as ConfigCatUser } from 'configcat-js';
import { User as ConfigCatUser } from 'configcat-js-ssr';

function contextValueToString(contextValue: EvaluationContextValue): string | undefined {
if (typeof contextValue === 'string') {
Expand Down
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@swc/helpers": "0.5.3",
"ajv": "^8.12.0",
"axios": "1.6.7",
"configcat-js": "^9.0.0",
"configcat-js-ssr": "^8.3.0",
"copy-anything": "^3.0.5",
"imurmurhash": "^0.1.4",
"json-logic-engine": "1.3.1",
Expand Down

0 comments on commit 190946f

Please sign in to comment.