|
| 1 | +# ConfigCat Web Provider |
| 2 | + |
| 3 | +This provider is an implementation for [ConfigCat](https://configcat.com) a managed feature flag service. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +``` |
| 8 | +$ npm install @openfeature/config-cat-web-provider |
| 9 | +``` |
| 10 | + |
| 11 | +#### Required peer dependencies |
| 12 | + |
| 13 | +The OpenFeature SDK is required as peer dependency. |
| 14 | + |
| 15 | +The minimum required version of `@openfeature/web-sdk` currently is `1.0.0`. |
| 16 | + |
| 17 | +The minimum required version of `configcat-js-ssr` currently is `7.1.2`. |
| 18 | + |
| 19 | +``` |
| 20 | +$ npm install @openfeature/client-sdk configcat-js-ssr |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +The ConfigCat provider uses the [ConfigCat JavaScript SSR SDK](https://configcat.com/docs/sdk-reference/js-ssr/). |
| 26 | + |
| 27 | +It can be created by passing the ConfigCat SDK options to ```ConfigCatProvider.create```. |
| 28 | +The available options can be found in the [ConfigCat JavaScript SSR SDK](https://configcat.com/docs/sdk-reference/js-ssr/). |
| 29 | + |
| 30 | +The ConfigCat Web Provider only supports the `AutoPolling` mode because it caches all evaluation data to support synchronous evaluation of feature flags. |
| 31 | + |
| 32 | +### Example using the default configuration |
| 33 | + |
| 34 | +```javascript |
| 35 | +import { ConfigCatProvider } from '@openfeature/config-cat-web-provider'; |
| 36 | + |
| 37 | +const provider = ConfigCatProvider.create('<sdk_key>'); |
| 38 | +OpenFeature.setProvider(provider); |
| 39 | +``` |
| 40 | + |
| 41 | +### Example using different polling options and a setupHook |
| 42 | + |
| 43 | +```javascript |
| 44 | +import { ConfigCatProvider } from '@openfeature/config-cat-web-provider'; |
| 45 | + |
| 46 | +const provider = ConfigCatProvider.create('<sdk_key>', { |
| 47 | + setupHooks: (hooks) => hooks.on('clientReady', () => console.log('Client is ready!')), |
| 48 | +}); |
| 49 | + |
| 50 | +OpenFeature.setProvider(provider); |
| 51 | +``` |
| 52 | + |
| 53 | +## Evaluation Context |
| 54 | + |
| 55 | +The OpenFeature Evaluation Context is mapped to the [ConfigCat user object](https://configcat.com/docs/advanced/user-object/). |
| 56 | + |
| 57 | +The [ConfigCat user object](https://configcat.com/docs/advanced/user-object/) has three known attributes, |
| 58 | +and allows for additional attributes. |
| 59 | +The following shows how the attributes are mapped: |
| 60 | + |
| 61 | +| OpenFeature EvaluationContext Field | ConfigCat User Field | Required | |
| 62 | +|-------------------------------------|----------------------|----------| |
| 63 | +| targetingKey | identifier | yes | |
| 64 | +| email | email | no | |
| 65 | +| country | country | no | |
| 66 | +| _Any Other_ | custom | no | |
| 67 | + |
| 68 | +The custom types are mapped the following way: |
| 69 | + |
| 70 | +| OpenFeature EvaluationContext Field Type | ConfigCat User Field Type | |
| 71 | +|------------------------------------------|---------------------------| |
| 72 | +| string | string | |
| 73 | +| number | number | |
| 74 | +| boolean | string | |
| 75 | +| Array<string> | Array<string> | |
| 76 | +| Array | Array | |
| 77 | +| object | string | |
| 78 | + |
| 79 | +The following example shows the conversion between an OpenFeature Evaluation Context and the corresponding ConfigCat |
| 80 | +User: |
| 81 | + |
| 82 | +#### OpenFeature |
| 83 | + |
| 84 | +```json |
| 85 | +{ |
| 86 | + "targetingKey": "test", |
| 87 | + "email": "email", |
| 88 | + "country": "country", |
| 89 | + "customString": "customString", |
| 90 | + "customNumber": 1, |
| 91 | + "customBoolean": true, |
| 92 | + "customObject": { |
| 93 | + "prop1": "1", |
| 94 | + "prop2": 2 |
| 95 | + }, |
| 96 | + "customStringArray": [ |
| 97 | + "one", |
| 98 | + "two" |
| 99 | + ], |
| 100 | + "customArray": [ |
| 101 | + 1, |
| 102 | + "2", |
| 103 | + false |
| 104 | + ] |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +#### ConfigCat |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "identifier": "test", |
| 113 | + "email": "email", |
| 114 | + "country": "country", |
| 115 | + "custom": { |
| 116 | + "customString": "customString", |
| 117 | + "customBoolean": "true", |
| 118 | + "customNumber": 1, |
| 119 | + "customObject": "{\"prop1\":\"1\",\"prop2\":2}", |
| 120 | + "customStringArray": [ |
| 121 | + "one", |
| 122 | + "two" |
| 123 | + ], |
| 124 | + "customArray": "[1,\"2\",false]" |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +## Events |
| 130 | + |
| 131 | +The ConfigCat provider emits the |
| 132 | +following [OpenFeature events](https://openfeature.dev/specification/types#provider-events): |
| 133 | + |
| 134 | +- PROVIDER_READY |
| 135 | +- PROVIDER_ERROR |
| 136 | +- PROVIDER_CONFIGURATION_CHANGED |
| 137 | + |
| 138 | +## Building |
| 139 | + |
| 140 | +Run `nx package providers-config-cat-web` to build the library. |
| 141 | + |
| 142 | +## Running unit tests |
| 143 | + |
| 144 | +Run `nx test providers-config-cat-web` to execute the unit tests via [Jest](https://jestjs.io). |
0 commit comments