diff --git a/libs/providers/config-cat-web/README.md b/libs/providers/config-cat-web/README.md index 0f1dd4c11..c40e4f7eb 100644 --- a/libs/providers/config-cat-web/README.md +++ b/libs/providers/config-cat-web/README.md @@ -14,18 +14,18 @@ The OpenFeature SDK is required as peer dependency. The minimum required version of `@openfeature/web-sdk` currently is `1.0.0`. -The minimum required version of `configcat-js-ssr` currently is `8.4.3`. +The minimum required version of `@configcat/sdk` currently is `1.0.1`. ``` -$ npm install @openfeature/web-sdk configcat-js-ssr +$ npm install @openfeature/web-sdk @configcat/sdk ``` ## Usage -The ConfigCat provider uses the [ConfigCat JavaScript SSR SDK](https://configcat.com/docs/sdk-reference/js-ssr/). +The ConfigCat provider uses the [ConfigCat Browser (JavaScript) SDK](https://configcat.com/docs/sdk-reference/js/browser/). It can be created by passing the ConfigCat SDK options to ```ConfigCatWebProvider.create```. -The available options can be found in the [ConfigCat JavaScript SSR SDK](https://configcat.com/docs/sdk-reference/js-ssr/#creating-the-configcat-client). +The available options can be found in the [ConfigCat JavaScript SSR SDK](https://configcat.com/docs/sdk-reference/js/browser/#creating-the-configcat-client). The ConfigCat Web Provider only supports the `AutoPolling` mode because it caches all evaluation data to support synchronous evaluation of feature flags. @@ -54,7 +54,7 @@ await OpenFeature.clearProviders(); ```javascript import { OpenFeature } from "@openfeature/web-sdk"; import { ConfigCatWebProvider } from '@openfeature/config-cat-web-provider'; -import { createConsoleLogger, LogLevel } from 'configcat-js-ssr'; +import { createConsoleLogger, LogLevel } from '@configcat/sdk'; // Create and set the provider. const provider = ConfigCatWebProvider.create('', { @@ -99,7 +99,7 @@ User: ```json { - "targetingKey": "test", + "targetingKey": "userId", "email": "email", "country": "country", "customString": "customString", @@ -125,10 +125,11 @@ User: ```json { - "identifier": "test", + "identifier": "userId", "email": "email", "country": "country", "custom": { + "targetingKey": "userId", "customString": "customString", "customBoolean": "true", "customNumber": 1, diff --git a/libs/providers/config-cat-web/package-lock.json b/libs/providers/config-cat-web/package-lock.json index 3697f786c..ce580a123 100644 --- a/libs/providers/config-cat-web/package-lock.json +++ b/libs/providers/config-cat-web/package-lock.json @@ -7,9 +7,33 @@ "": { "name": "@openfeature/config-cat-web-provider", "version": "0.1.6", + "license": "Apache-2.0", + "dependencies": { + "@openfeature/config-cat-core": "0.1.1" + }, "peerDependencies": { - "@openfeature/web-sdk": "^1.0.0", + "@configcat/sdk": "^1.0.1", + "@openfeature/web-sdk": "^1.0.0" + } + }, + "node_modules/@configcat/sdk": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@configcat/sdk/-/sdk-1.0.1.tgz", + "integrity": "sha512-XTUEx84ZUNKMZ/SkA2LcoweVbTOW6DzIzJXSuOEytEMSmvUOiTZJPdRdnv6HzK3BKgvAGEUEITsKGv695R1A/w==", + "license": "MIT", + "peer": true + }, + "node_modules/@openfeature/config-cat-core": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@openfeature/config-cat-core/-/config-cat-core-0.1.1.tgz", + "integrity": "sha512-H1aL77z/ZgG5gQYpMsY9ZpzDzm64DHRouGxAmpBqJI1JFQQhNY0d6bQs6eeIYN8rHrgIpHAPEqjWSAYKrmyFxw==", + "license": "Apache-2.0", + "dependencies": { + "configcat-common": "9.3.1", "configcat-js-ssr": "^8.4.3" + }, + "peerDependencies": { + "@openfeature/core": "^1.6.0" } }, "node_modules/@openfeature/core": { @@ -30,25 +54,36 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "peer": true + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.7.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz", - "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==", - "peer": true, + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.11.0.tgz", + "integrity": "sha512-1Lx3WLFQWm3ooKDYZD1eXmoGO9fxYQjrycfHFC8P0sCfQVXyROp0p9PFWBehewBOdCwHc+f/b8I0fMto5eSfwA==", + "license": "MIT", "dependencies": { "follow-redirects": "^1.15.6", - "form-data": "^4.0.0", + "form-data": "^4.0.4", "proxy-from-env": "^1.1.0" } }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "peer": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -60,7 +95,6 @@ "version": "9.3.1", "resolved": "https://registry.npmjs.org/configcat-common/-/configcat-common-9.3.1.tgz", "integrity": "sha512-yVkIbluksD/kZfVyKjLIOpwLrq3/ZRM7Lwrsz89JmbpQ6VtbnelrTQynSPElTtKjrPRZx56v3IZYk3nWTnnM6A==", - "peer": true, "dependencies": { "tslib": "^2.4.1" } @@ -69,7 +103,6 @@ "version": "8.4.3", "resolved": "https://registry.npmjs.org/configcat-js-ssr/-/configcat-js-ssr-8.4.3.tgz", "integrity": "sha512-9tNM61cgJOE9C1MO8wBK1QglrnlT8VpiAW/KgGdFdOuIPs3ky62EThgAE+HYSRYEv4JrRNB4i7G0v1Qgbf18Hw==", - "peer": true, "dependencies": { "axios": "^1.7.4", "configcat-common": "9.3.1", @@ -80,11 +113,69 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "peer": true, "engines": { "node": ">=0.4.0" } }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-set-tostringtag": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", + "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.6", + "has-tostringtag": "^1.0.2", + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/follow-redirects": { "version": "1.15.6", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", @@ -95,7 +186,6 @@ "url": "https://github.com/sponsors/RubenVerborgh" } ], - "peer": true, "engines": { "node": ">=4.0" }, @@ -106,24 +196,131 @@ } }, "node_modules/form-data": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", - "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "peer": true, + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz", + "integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==", + "license": "MIT", "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.2", "mime-types": "^2.1.12" }, "engines": { "node": ">= 6" } }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-tostringtag": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", + "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", + "license": "MIT", + "dependencies": { + "has-symbols": "^1.0.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", + "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "peer": true, "engines": { "node": ">= 0.6" } @@ -132,7 +329,6 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "peer": true, "dependencies": { "mime-db": "1.52.0" }, @@ -143,14 +339,12 @@ "node_modules/proxy-from-env": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", - "peer": true + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" }, "node_modules/tslib": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.7.0.tgz", - "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==", - "peer": true + "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==" } } } diff --git a/libs/providers/config-cat-web/package.json b/libs/providers/config-cat-web/package.json index a3ca6df43..bae0d6201 100644 --- a/libs/providers/config-cat-web/package.json +++ b/libs/providers/config-cat-web/package.json @@ -7,8 +7,10 @@ "current-version": "echo $npm_package_version" }, "peerDependencies": { - "@openfeature/web-sdk": "^1.0.0", - "configcat-js-ssr": "^8.4.3", + "@configcat/sdk": "^1.0.1", + "@openfeature/web-sdk": "^1.0.0" + }, + "dependencies": { "@openfeature/config-cat-core": "0.1.1" } } diff --git a/libs/providers/config-cat-web/src/lib/config-cat-web-provider.spec.ts b/libs/providers/config-cat-web/src/lib/config-cat-web-provider.spec.ts index 0dbd6c189..da5ebb003 100644 --- a/libs/providers/config-cat-web/src/lib/config-cat-web-provider.spec.ts +++ b/libs/providers/config-cat-web/src/lib/config-cat-web-provider.spec.ts @@ -1,6 +1,14 @@ import { ConfigCatWebProvider } from './config-cat-web-provider'; -import type { HookEvents, IConfigCatCache, ISettingUnion } from 'configcat-js-ssr'; -import { createConsoleLogger, createFlagOverridesFromMap, LogLevel, OverrideBehaviour } from 'configcat-js-ssr'; +import type { HookEvents, IConfigCatConfigFetcher } from '@configcat/sdk'; +import { + createConsoleLogger, + createFlagOverridesFromMap, + FetchResponse, + LogLevel, + OverrideBehaviour, + prepareConfig, + SettingType, +} from '@configcat/sdk'; import type { EventEmitter } from 'events'; import { ProviderEvents, ParseError, FlagNotFoundError, TypeMismatchError } from '@openfeature/web-sdk'; @@ -65,7 +73,7 @@ describe('ConfigCatWebProvider', () => { describe('events', () => { it('should emit PROVIDER_CONFIGURATION_CHANGED event', () => { const handler = jest.fn(); - const eventData = { settings: { myFlag: {} as ISettingUnion }, salt: undefined, segments: [] }; + const eventData = prepareConfig({ f: { myFlag: { t: SettingType.Boolean, v: { b: true }, i: '' } } }); provider.events.addHandler(ProviderEvents.ConfigurationChanged, handler); configCatEmitter.emit('configChanged', eventData); @@ -76,47 +84,36 @@ describe('ConfigCatWebProvider', () => { }); it("should emit PROVIDER_READY event when underlying client is initialized after provider's initialize", async () => { - const cacheValue = '253370761200000\nW/"12345678-90a"\n{"f":{"booleanTrue":{"t":0,"v":{"b":true}}}}'; + let currentFetch = () => new FetchResponse(503, 'Service Unavailable', []); - const fakeSharedCache = new (class implements IConfigCatCache { - private _value?: string; - get(key: string) { - return this._value; - } - set(key: string, value: string) { - this._value = value; + const fakeConfigFetcher = new (class implements IConfigCatConfigFetcher { + fetchAsync(): Promise { + return Promise.resolve(currentFetch()); } })(); const provider = ConfigCatWebProvider.create('configcat-sdk-1/1234567890123456789012/1234567890123456789012', { - cache: fakeSharedCache, + configFetcher: fakeConfigFetcher, logger: createConsoleLogger(LogLevel.Off), - offline: true, maxInitWaitTimeSeconds: 1, }); const readyHandler = jest.fn(); provider.events.addHandler(ProviderEvents.Ready, readyHandler); - try { - await provider.initialize(); - } catch (err) { - expect((err as Error).message).toContain('underlying ConfigCat client could not initialize'); - } + await expect(provider.initialize()).rejects.toThrow(/underlying ConfigCat client could not initialize/); expect(readyHandler).toHaveBeenCalledTimes(0); - fakeSharedCache.set('', cacheValue); + currentFetch = () => new FetchResponse(200, 'OK', [], '{"f":{"booleanTrue":{"t":0,"v":{"b":true}}}}'); // Make sure that the internal cache is refreshed. await provider.configCatClient?.forceRefreshAsync(); - provider.resolveBooleanEvaluation('booleanTrue', false, { targetingKey }); - - // Wait a little while for the Ready event to be emitted. - await new Promise((resolve) => setTimeout(resolve, 100)); - expect(readyHandler).toHaveBeenCalled(); + + const resolutionDetails = provider.resolveBooleanEvaluation('booleanTrue', false, { targetingKey }); + expect(resolutionDetails.value).toBe(true); }); }); diff --git a/libs/providers/config-cat-web/src/lib/config-cat-web-provider.ts b/libs/providers/config-cat-web/src/lib/config-cat-web-provider.ts index 31abb2bc9..35e891baf 100644 --- a/libs/providers/config-cat-web/src/lib/config-cat-web-provider.ts +++ b/libs/providers/config-cat-web/src/lib/config-cat-web-provider.ts @@ -7,9 +7,9 @@ import { TypeMismatchError, } from '@openfeature/web-sdk'; import type { PrimitiveType, PrimitiveTypeName } from '@openfeature/config-cat-core'; -import { isType, parseError, toResolutionDetails, transformContext } from '@openfeature/config-cat-core'; -import type { IConfig, IConfigCatClient, OptionsForPollingMode, SettingValue } from 'configcat-js-ssr'; -import { ClientCacheState, getClient, PollingMode } from 'configcat-js-ssr'; +import { isType, toResolutionDetails, transformContext, translateError } from '@openfeature/config-cat-core'; +import type { Config, IConfigCatClient, IJSAutoPollOptions, SettingValue } from '@configcat/sdk'; +import { ClientCacheState, EvaluationErrorCode, getClient, PollingMode } from '@configcat/sdk'; export class ConfigCatWebProvider implements Provider { public readonly events = new OpenFeatureEventEmitter(); @@ -27,7 +27,7 @@ export class ConfigCatWebProvider implements Provider { this._clientFactory = clientFactory; } - public static create(sdkKey: string, options?: OptionsForPollingMode) { + public static create(sdkKey: string, options?: IJSAutoPollOptions) { // Let's create a shallow copy to not mess up caller's options object. options = options ? { ...options } : {}; @@ -37,11 +37,16 @@ export class ConfigCatWebProvider implements Provider { options.setupHooks = (hooks) => { oldSetupHooks?.(hooks); - hooks.on('configChanged', (config: IConfig) => + hooks.on('configChanged', (config: Config) => { + if (provider._client && !provider._isProviderReady) { + provider._isProviderReady = true; + provider.events.emit(ProviderEvents.Ready); + } + provider.events.emit(ProviderEvents.ConfigurationChanged, { - flagsChanged: Object.keys(config.settings), - }), - ); + flagsChanged: config.f ? Object.keys(config.f) : [], + }); + }); }; return getClient(sdkKey, PollingMode.AutoPoll, options); @@ -132,18 +137,8 @@ export class ConfigCatWebProvider implements Provider { transformContext(context), ); - if (!this._isProviderReady && snapshot.cacheState !== ClientCacheState.NoFlagData) { - // Ideally, we would check ConfigCat client's initialization state in its "background" polling loop. - // This is not possible at the moment, so as a workaround, we do the check on feature flag evaluation. - // There are plans to improve this situation, so let's revise this - // as soon as ConfigCat SDK implements the necessary event. - - this._isProviderReady = true; - setTimeout(() => this.events.emit(ProviderEvents.Ready), 0); - } - - if (evaluationData.isDefaultValue) { - throw parseError(evaluationData.errorMessage); + if (evaluationData.errorCode !== EvaluationErrorCode.None) { + throw translateError(evaluationData.errorCode); } if (flagType !== 'object') { diff --git a/libs/providers/config-cat/README.md b/libs/providers/config-cat/README.md index dc842aeca..feb0c2e7a 100644 --- a/libs/providers/config-cat/README.md +++ b/libs/providers/config-cat/README.md @@ -14,19 +14,19 @@ The OpenFeature SDK is required as peer dependency. The minimum required version of `@openfeature/server-sdk` currently is `1.13.5`. -The minimum required version of `configcat-node` currently is `11.3.1`. +The minimum required version of `@configcat/sdk` currently is `1.0.1`. ``` -$ npm install @openfeature/server-sdk configcat-node +$ npm install @openfeature/server-sdk @configcat/sdk ``` ## Usage -The ConfigCat provider uses the [ConfigCat Node.js SDK](https://configcat.com/docs/sdk-reference/node/). +The ConfigCat provider uses the [ConfigCat Node.js SDK](https://configcat.com/docs/sdk-reference/js/node/). It can be created by passing the ConfigCat SDK options to ```ConfigCatProvider.create```. -The available options can be found in the [ConfigCat Node.js SDK](https://configcat.com/docs/sdk-reference/node/#creating-the-configcat-client). +The available options can be found in the [ConfigCat Node.js SDK](https://configcat.com/docs/sdk-reference/js/node/#creating-the-configcat-client). ### Example using the default configuration @@ -53,7 +53,7 @@ await OpenFeature.clearProviders(); ```javascript import { OpenFeature } from "@openfeature/server-sdk"; import { ConfigCatProvider } from '@openfeature/config-cat-provider'; -import { createConsoleLogger, LogLevel, PollingMode } from 'configcat-node'; +import { createConsoleLogger, LogLevel, PollingMode } from '@configcat/sdk'; // Create and set the provider. const provider = ConfigCatProvider.create('', PollingMode.LazyLoad, { @@ -98,7 +98,7 @@ User: ```json { - "targetingKey": "test", + "targetingKey": "userId", "email": "email", "country": "country", "customString": "customString", @@ -121,10 +121,11 @@ User: ```json { - "identifier": "test", + "identifier": "userId", "email": "email", "country": "country", "custom": { + "targetingKey": "userId", "customString": "customString", "customBoolean": "true", "customNumber": 1, diff --git a/libs/providers/config-cat/package-lock.json b/libs/providers/config-cat/package-lock.json index 3f377cc41..e98890709 100644 --- a/libs/providers/config-cat/package-lock.json +++ b/libs/providers/config-cat/package-lock.json @@ -7,9 +7,33 @@ "": { "name": "@openfeature/config-cat-provider", "version": "0.7.6", + "license": "Apache-2.0", + "dependencies": { + "@openfeature/config-cat-core": "0.1.1" + }, "peerDependencies": { - "@openfeature/server-sdk": "^1.13.5", - "configcat-node": "^11.3.1" + "@configcat/sdk": "^1.0.1", + "@openfeature/server-sdk": "^1.13.5" + } + }, + "node_modules/@configcat/sdk": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@configcat/sdk/-/sdk-1.0.1.tgz", + "integrity": "sha512-XTUEx84ZUNKMZ/SkA2LcoweVbTOW6DzIzJXSuOEytEMSmvUOiTZJPdRdnv6HzK3BKgvAGEUEITsKGv695R1A/w==", + "license": "MIT", + "peer": true + }, + "node_modules/@openfeature/config-cat-core": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@openfeature/config-cat-core/-/config-cat-core-0.1.1.tgz", + "integrity": "sha512-H1aL77z/ZgG5gQYpMsY9ZpzDzm64DHRouGxAmpBqJI1JFQQhNY0d6bQs6eeIYN8rHrgIpHAPEqjWSAYKrmyFxw==", + "license": "Apache-2.0", + "dependencies": { + "configcat-common": "9.3.1", + "configcat-js-ssr": "^8.4.3" + }, + "peerDependencies": { + "@openfeature/core": "^1.6.0" } }, "node_modules/@openfeature/core": { @@ -30,43 +54,317 @@ "@openfeature/core": "1.2.0" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" + }, + "node_modules/axios": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.11.0.tgz", + "integrity": "sha512-1Lx3WLFQWm3ooKDYZD1eXmoGO9fxYQjrycfHFC8P0sCfQVXyROp0p9PFWBehewBOdCwHc+f/b8I0fMto5eSfwA==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.4", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/configcat-common": { "version": "9.3.1", "resolved": "https://registry.npmjs.org/configcat-common/-/configcat-common-9.3.1.tgz", "integrity": "sha512-yVkIbluksD/kZfVyKjLIOpwLrq3/ZRM7Lwrsz89JmbpQ6VtbnelrTQynSPElTtKjrPRZx56v3IZYk3nWTnnM6A==", - "peer": true, "dependencies": { "tslib": "^2.4.1" } }, - "node_modules/configcat-node": { - "version": "11.3.1", - "resolved": "https://registry.npmjs.org/configcat-node/-/configcat-node-11.3.1.tgz", - "integrity": "sha512-7XJbgBpcxlwzlRLmvCtHTkO247Ban2ZkBqlmk+T0wVEt5tXfltgd53SYLYpw7RBWX0ma/QyP5E+/k/UDdMrOCw==", - "peer": true, + "node_modules/configcat-js-ssr": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/configcat-js-ssr/-/configcat-js-ssr-8.5.0.tgz", + "integrity": "sha512-UZdoMEVBZpRlAbpnTMqfZaqs15UFL8++Dfn8zwgbGD5ZdSgGnLGqQXH5wdIZ0+KzBOoLYTR4nsyzCS8j+ZjNog==", + "license": "MIT", "dependencies": { - "configcat-common": "9.3.1", - "tslib": "^2.4.1", - "tunnel": "0.0.6" + "axios": "^1.8.4", + "configcat-common": "9.4.0", + "tslib": "^2.4.1" + } + }, + "node_modules/configcat-js-ssr/node_modules/configcat-common": { + "version": "9.4.0", + "resolved": "https://registry.npmjs.org/configcat-common/-/configcat-common-9.4.0.tgz", + "integrity": "sha512-rKFawlrEXY1NQ1jxX5sYXN4fhT4MhfllC1ELd+3k2tY+z1DQju+PoopNra1wyfegLn3Aa0X8l3JpXFFO/gyo0g==", + "license": "MIT", + "dependencies": { + "tslib": "^2.4.1" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" }, "engines": { - "node": ">=14" + "node": ">= 0.4" } }, - "node_modules/tslib": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.7.0.tgz", - "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==", - "peer": true + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } }, - "node_modules/tunnel": { - "version": "0.0.6", - "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz", - "integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==", - "peer": true, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, "engines": { - "node": ">=0.6.11 <=0.7.0 || >=0.7.3" + "node": ">= 0.4" } + }, + "node_modules/es-set-tostringtag": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", + "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.6", + "has-tostringtag": "^1.0.2", + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.11", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.11.tgz", + "integrity": "sha512-deG2P0JfjrTxl50XGCDyfI97ZGVCxIpfKYmfyrQ54n5FO/0gfIES8C/Psl6kWVDolizcaaxZJnTS0QSMxvnsBQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz", + "integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.2", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-tostringtag": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", + "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", + "license": "MIT", + "dependencies": { + "has-symbols": "^1.0.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", + "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" + }, + "node_modules/tslib": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.7.0.tgz", + "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==" } } } diff --git a/libs/providers/config-cat/package.json b/libs/providers/config-cat/package.json index 5a0c2701b..5fb0af397 100644 --- a/libs/providers/config-cat/package.json +++ b/libs/providers/config-cat/package.json @@ -7,9 +7,10 @@ "current-version": "echo $npm_package_version" }, "peerDependencies": { - "@openfeature/server-sdk": "^1.13.5", - "configcat-node": "^11.3.1", - "@openfeature/config-cat-core": "0.1.1", - "configcat-common": "9.4.0" + "@configcat/sdk": "^1.0.1", + "@openfeature/server-sdk": "^1.13.5" + }, + "dependencies": { + "@openfeature/config-cat-core": "0.1.1" } } diff --git a/libs/providers/config-cat/src/lib/config-cat-provider.spec.ts b/libs/providers/config-cat/src/lib/config-cat-provider.spec.ts index 529b4cdc5..4af1c7de2 100644 --- a/libs/providers/config-cat/src/lib/config-cat-provider.spec.ts +++ b/libs/providers/config-cat/src/lib/config-cat-provider.spec.ts @@ -1,13 +1,16 @@ import { ConfigCatProvider } from './config-cat-provider'; -import { ProviderEvents, ParseError, FlagNotFoundError, TypeMismatchError } from '@openfeature/web-sdk'; -import type { HookEvents, IConfigCatCache, ISettingUnion } from 'configcat-js-ssr'; +import { ProviderEvents, ParseError, FlagNotFoundError, TypeMismatchError } from '@openfeature/server-sdk'; +import type { HookEvents, IConfigCatConfigFetcher } from '@configcat/sdk'; import { createConsoleLogger, createFlagOverridesFromMap, + FetchResponse, LogLevel, OverrideBehaviour, PollingMode, -} from 'configcat-js-ssr'; + prepareConfig, + SettingType, +} from '@configcat/sdk'; import type { EventEmitter } from 'events'; describe('ConfigCatProvider', () => { @@ -71,7 +74,7 @@ describe('ConfigCatProvider', () => { describe('events', () => { it('should emit PROVIDER_CONFIGURATION_CHANGED event', () => { const handler = jest.fn(); - const eventData = { settings: { myFlag: {} as ISettingUnion }, salt: undefined, segments: [] }; + const eventData = prepareConfig({ f: { myFlag: { t: SettingType.Boolean, v: { b: true }, i: '' } } }); provider.events.addHandler(ProviderEvents.ConfigurationChanged, handler); configCatEmitter.emit('configChanged', eventData); @@ -82,15 +85,11 @@ describe('ConfigCatProvider', () => { }); it("should emit PROVIDER_READY event when underlying client is initialized after provider's initialize", async () => { - const cacheValue = '253370761200000\nW/"12345678-90a"\n{"f":{"booleanTrue":{"t":0,"v":{"b":true}}}}'; + let currentFetch = () => new FetchResponse(503, 'Service Unavailable', []); - const fakeSharedCache = new (class implements IConfigCatCache { - private _value?: string; - get(key: string) { - return this._value; - } - set(key: string, value: string) { - this._value = value; + const fakeConfigFetcher = new (class implements IConfigCatConfigFetcher { + fetchAsync(): Promise { + return Promise.resolve(currentFetch()); } })(); @@ -98,9 +97,8 @@ describe('ConfigCatProvider', () => { 'configcat-sdk-1/1234567890123456789012/1234567890123456789012', PollingMode.AutoPoll, { - cache: fakeSharedCache, + configFetcher: fakeConfigFetcher, logger: createConsoleLogger(LogLevel.Off), - offline: true, maxInitWaitTimeSeconds: 1, }, ); @@ -108,25 +106,19 @@ describe('ConfigCatProvider', () => { const readyHandler = jest.fn(); provider.events.addHandler(ProviderEvents.Ready, readyHandler); - try { - await provider.initialize(); - } catch (err) { - expect((err as Error).message).toContain('underlying ConfigCat client could not initialize'); - } + await expect(provider.initialize()).rejects.toThrow(/underlying ConfigCat client could not initialize/); expect(readyHandler).toHaveBeenCalledTimes(0); - fakeSharedCache.set('', cacheValue); + currentFetch = () => new FetchResponse(200, 'OK', [], '{"f":{"booleanTrue":{"t":0,"v":{"b":true}}}}'); // Make sure that the internal cache is refreshed. await provider.configCatClient?.forceRefreshAsync(); - provider.resolveBooleanEvaluation('booleanTrue', false, { targetingKey }); - - // Wait a little while for the Ready event to be emitted. - await new Promise((resolve) => setTimeout(resolve, 100)); - expect(readyHandler).toHaveBeenCalled(); + + const resolutionDetails = await provider.resolveBooleanEvaluation('booleanTrue', false, { targetingKey }); + expect(resolutionDetails.value).toBe(true); }); }); diff --git a/libs/providers/config-cat/src/lib/config-cat-provider.ts b/libs/providers/config-cat/src/lib/config-cat-provider.ts index d47404b94..7a534449e 100644 --- a/libs/providers/config-cat/src/lib/config-cat-provider.ts +++ b/libs/providers/config-cat/src/lib/config-cat-provider.ts @@ -7,11 +7,26 @@ import { ParseError, } from '@openfeature/server-sdk'; import type { PrimitiveType, PrimitiveTypeName } from '@openfeature/config-cat-core'; -import { isType, parseError, toResolutionDetails, transformContext } from '@openfeature/config-cat-core'; -import type { SettingValue } from 'configcat-common'; -import { ClientCacheState, PollingMode } from 'configcat-common'; -import type { IConfigCatClient, IConfig, OptionsForPollingMode } from 'configcat-node'; -import { getClient } from 'configcat-node'; +import { isType, toResolutionDetails, transformContext, translateError } from '@openfeature/config-cat-core'; +import type { + Config, + IConfigCatClient, + INodeAutoPollOptions, + INodeLazyLoadingOptions, + INodeManualPollOptions, + SettingValue, +} from '@configcat/sdk'; +import { ClientCacheState, EvaluationErrorCode, getClient, PollingMode } from '@configcat/sdk'; + +type OptionsForPollingMode = TMode extends PollingMode.AutoPoll + ? INodeAutoPollOptions + : TMode extends PollingMode.ManualPoll + ? INodeManualPollOptions + : TMode extends PollingMode.LazyLoad + ? INodeLazyLoadingOptions + : TMode extends undefined + ? INodeAutoPollOptions + : never; export class ConfigCatProvider implements Provider { public readonly events = new OpenFeatureEventEmitter(); @@ -44,11 +59,16 @@ export class ConfigCatProvider implements Provider { options.setupHooks = (hooks) => { oldSetupHooks?.(hooks); - hooks.on('configChanged', (config: IConfig) => + hooks.on('configChanged', (config: Config) => { + if (provider._client && !provider._isProviderReady) { + provider._isProviderReady = true; + provider.events.emit(ProviderEvents.Ready); + } + provider.events.emit(ProviderEvents.ConfigurationChanged, { - flagsChanged: Object.keys(config.settings), - }), - ); + flagsChanged: config.f ? Object.keys(config.f) : [], + }); + }); }; return getClient(sdkKey, pollingMode, options); @@ -137,18 +157,8 @@ export class ConfigCatProvider implements Provider { transformContext(context), ); - if (!this._isProviderReady && this._client.snapshot().cacheState !== ClientCacheState.NoFlagData) { - // Ideally, we would check ConfigCat client's initialization state in its "background" polling loop. - // This is not possible at the moment, so as a workaround, we do the check on feature flag evaluation. - // There are plans to improve this situation, so let's revise this - // as soon as ConfigCat SDK implements the necessary event. - - this._isProviderReady = true; - setTimeout(() => this.events.emit(ProviderEvents.Ready), 0); - } - - if (evaluationData.isDefaultValue) { - throw parseError(evaluationData.errorMessage); + if (evaluationData.errorCode !== EvaluationErrorCode.None) { + throw translateError(evaluationData.errorCode); } if (flagType !== 'object') { diff --git a/libs/shared/config-cat-core/package-lock.json b/libs/shared/config-cat-core/package-lock.json index ba813e96d..9172744f0 100644 --- a/libs/shared/config-cat-core/package-lock.json +++ b/libs/shared/config-cat-core/package-lock.json @@ -9,324 +9,24 @@ "version": "0.1.1", "license": "Apache-2.0", "dependencies": { - "configcat-common": "9.4.0", - "configcat-js-ssr": "^8.4.3" + "@configcat/sdk": "^1.0.1" }, "peerDependencies": { "@openfeature/core": "^1.6.0" } }, + "node_modules/@configcat/sdk": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@configcat/sdk/-/sdk-1.0.1.tgz", + "integrity": "sha512-XTUEx84ZUNKMZ/SkA2LcoweVbTOW6DzIzJXSuOEytEMSmvUOiTZJPdRdnv6HzK3BKgvAGEUEITsKGv695R1A/w==", + "license": "MIT" + }, "node_modules/@openfeature/core": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/@openfeature/core/-/core-1.6.0.tgz", "integrity": "sha512-QYAtwdreZU9Mi/LXLRzXsUA7PhbtT7+UJfRBMIAy6MidZjMgIbNfoh6+MncXb3UocThn0OsYa8WLfWD9q43eCQ==", "license": "Apache-2.0", "peer": true - }, - "node_modules/asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "license": "MIT" - }, - "node_modules/axios": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.10.0.tgz", - "integrity": "sha512-/1xYAC4MP/HEG+3duIhFr4ZQXR4sQXOIe+o6sdqzeykGLx6Upp/1p8MHqhINOvGeP7xyNHe7tsiJByc4SSVUxw==", - "license": "MIT", - "dependencies": { - "follow-redirects": "^1.15.6", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - }, - "node_modules/call-bind-apply-helpers": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", - "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", - "license": "MIT", - "dependencies": { - "es-errors": "^1.3.0", - "function-bind": "^1.1.2" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "license": "MIT", - "dependencies": { - "delayed-stream": "~1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, - "node_modules/configcat-common": { - "version": "9.4.0", - "resolved": "https://registry.npmjs.org/configcat-common/-/configcat-common-9.4.0.tgz", - "integrity": "sha512-rKFawlrEXY1NQ1jxX5sYXN4fhT4MhfllC1ELd+3k2tY+z1DQju+PoopNra1wyfegLn3Aa0X8l3JpXFFO/gyo0g==", - "license": "MIT", - "dependencies": { - "tslib": "^2.4.1" - } - }, - "node_modules/configcat-js-ssr": { - "version": "8.5.0", - "resolved": "https://registry.npmjs.org/configcat-js-ssr/-/configcat-js-ssr-8.5.0.tgz", - "integrity": "sha512-UZdoMEVBZpRlAbpnTMqfZaqs15UFL8++Dfn8zwgbGD5ZdSgGnLGqQXH5wdIZ0+KzBOoLYTR4nsyzCS8j+ZjNog==", - "license": "MIT", - "dependencies": { - "axios": "^1.8.4", - "configcat-common": "9.4.0", - "tslib": "^2.4.1" - } - }, - "node_modules/delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "license": "MIT", - "engines": { - "node": ">=0.4.0" - } - }, - "node_modules/dunder-proto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", - "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", - "license": "MIT", - "dependencies": { - "call-bind-apply-helpers": "^1.0.1", - "es-errors": "^1.3.0", - "gopd": "^1.2.0" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-define-property": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", - "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-errors": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", - "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-object-atoms": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", - "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", - "license": "MIT", - "dependencies": { - "es-errors": "^1.3.0" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/es-set-tostringtag": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", - "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", - "license": "MIT", - "dependencies": { - "es-errors": "^1.3.0", - "get-intrinsic": "^1.2.6", - "has-tostringtag": "^1.0.2", - "hasown": "^2.0.2" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/follow-redirects": { - "version": "1.15.9", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", - "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "license": "MIT", - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, - "node_modules/form-data": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.3.tgz", - "integrity": "sha512-qsITQPfmvMOSAdeyZ+12I1c+CKSstAFAwu+97zrnWAbIr5u8wfsExUzCesVLC8NgHuRUqNN4Zy6UPWUTRGslcA==", - "license": "MIT", - "dependencies": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "es-set-tostringtag": "^2.1.0", - "hasown": "^2.0.2", - "mime-types": "^2.1.12" - }, - "engines": { - "node": ">= 6" - } - }, - "node_modules/function-bind": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", - "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/get-intrinsic": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", - "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", - "license": "MIT", - "dependencies": { - "call-bind-apply-helpers": "^1.0.2", - "es-define-property": "^1.0.1", - "es-errors": "^1.3.0", - "es-object-atoms": "^1.1.1", - "function-bind": "^1.1.2", - "get-proto": "^1.0.1", - "gopd": "^1.2.0", - "has-symbols": "^1.1.0", - "hasown": "^2.0.2", - "math-intrinsics": "^1.1.0" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/get-proto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", - "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", - "license": "MIT", - "dependencies": { - "dunder-proto": "^1.0.1", - "es-object-atoms": "^1.0.0" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/gopd": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", - "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/has-symbols": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", - "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/has-tostringtag": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", - "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", - "license": "MIT", - "dependencies": { - "has-symbols": "^1.0.3" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/hasown": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", - "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", - "license": "MIT", - "dependencies": { - "function-bind": "^1.1.2" - }, - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/math-intrinsics": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", - "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", - "license": "MIT", - "engines": { - "node": ">= 0.4" - } - }, - "node_modules/mime-db": { - "version": "1.52.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", - "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "license": "MIT", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/mime-types": { - "version": "2.1.35", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", - "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "license": "MIT", - "dependencies": { - "mime-db": "1.52.0" - }, - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/proxy-from-env": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", - "license": "MIT" - }, - "node_modules/tslib": { - "version": "2.8.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", - "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", - "license": "0BSD" } } } diff --git a/libs/shared/config-cat-core/package.json b/libs/shared/config-cat-core/package.json index 809fe2b19..7bc9abd51 100644 --- a/libs/shared/config-cat-core/package.json +++ b/libs/shared/config-cat-core/package.json @@ -10,7 +10,6 @@ "@openfeature/core": "^1.6.0" }, "dependencies": { - "configcat-common": "9.4.0", - "configcat-js-ssr": "^8.4.3" + "@configcat/sdk": "^1.0.1" } } diff --git a/libs/shared/config-cat-core/src/lib/context-transformer.spec.ts b/libs/shared/config-cat-core/src/lib/context-transformer.spec.ts index 05308f59d..985461dfc 100644 --- a/libs/shared/config-cat-core/src/lib/context-transformer.spec.ts +++ b/libs/shared/config-cat-core/src/lib/context-transformer.spec.ts @@ -10,9 +10,11 @@ describe('context-transformer', () => { const user = { identifier: context['targetingKey'], - country: undefined, - custom: {}, email: undefined, + country: undefined, + custom: { + targetingKey: context['targetingKey'], + }, }; expect(transformContext(context)).toEqual(user); @@ -29,7 +31,11 @@ describe('context-transformer', () => { identifier: context['targetingKey'], email: context['email'], country: context['country'], - custom: {}, + custom: { + targetingKey: context['targetingKey'], + email: context['email'], + country: context['country'], + }, }; expect(transformContext(context)).toEqual(user); @@ -44,6 +50,7 @@ describe('context-transformer', () => { const user = { identifier: context['targetingKey'], custom: { + targetingKey: context['targetingKey'], customString: context['customString'], }, }; @@ -51,7 +58,7 @@ describe('context-transformer', () => { expect(transformContext(context)).toEqual(user); }); - it('map custom property with number to number', () => { + it('map custom property with number', () => { const context: EvaluationContext = { targetingKey: 'test', customNumber: 1, @@ -60,14 +67,15 @@ describe('context-transformer', () => { const user = { identifier: context['targetingKey'], custom: { - customNumber: 1, + targetingKey: context['targetingKey'], + customNumber: context['customNumber'], }, }; expect(transformContext(context)).toEqual(user); }); - it('map custom property with boolean to string', () => { + it('map custom property with boolean', () => { const context: EvaluationContext = { targetingKey: 'test', customBoolean: true, @@ -76,7 +84,8 @@ describe('context-transformer', () => { const user = { identifier: context['targetingKey'], custom: { - customBoolean: 'true', + targetingKey: context['targetingKey'], + customBoolean: context['customBoolean'], }, }; @@ -95,6 +104,7 @@ describe('context-transformer', () => { const user = { identifier: context['targetingKey'], custom: { + targetingKey: context['targetingKey'], customObject: JSON.stringify(context['customObject']), }, }; @@ -111,7 +121,8 @@ describe('context-transformer', () => { const user = { identifier: context['targetingKey'], custom: { - customArray: ['one', 'two', 'three'], + targetingKey: context['targetingKey'], + customArray: context['customArray'], }, }; @@ -127,6 +138,7 @@ describe('context-transformer', () => { const user = { identifier: context['targetingKey'], custom: { + targetingKey: context['targetingKey'], customArray: JSON.stringify(context['customArray']), }, }; @@ -154,11 +166,29 @@ describe('context-transformer', () => { email: 'email', country: 'country', custom: { - customString: 'customString', - customBoolean: 'true', - customNumber: 1, - customObject: '{"prop1":"1","prop2":2}', - customArray: '[1,"2",false]', + targetingKey: context['targetingKey'], + email: context['email'], + country: context['country'], + customString: context['customString'], + customBoolean: context['customBoolean'], + customNumber: context['customNumber'], + customObject: JSON.stringify(context['customObject']), + customArray: JSON.stringify(context['customArray']), + }, + }; + + expect(transformContext(context)).toEqual(user); + }); + + it('map identifier if targetingKey is not present', () => { + const context: EvaluationContext = { + identifier: 'test', + }; + + const user = { + identifier: 'test', + custom: { + identifier: context['identifier'], }, }; diff --git a/libs/shared/config-cat-core/src/lib/context-transformer.ts b/libs/shared/config-cat-core/src/lib/context-transformer.ts index 39497b2c3..2c565d042 100644 --- a/libs/shared/config-cat-core/src/lib/context-transformer.ts +++ b/libs/shared/config-cat-core/src/lib/context-transformer.ts @@ -1,27 +1,28 @@ import type { EvaluationContext, EvaluationContextValue } from '@openfeature/core'; -import type { User as ConfigCatUser, UserAttributeValue } from 'configcat-common'; +import type { IUser as ConfigCatUser, UserAttributeValue } from '@configcat/sdk'; function toUserAttributeValue(value: EvaluationContextValue): UserAttributeValue { - if (typeof value === 'string' || typeof value === 'number' || value instanceof Date) { - return value; - } else if (Array.isArray(value) && value.every((item) => typeof item === 'string')) { - return value as ReadonlyArray; + // NOTE: The ConfigCat SDK doesn't support objects and non-string arrays as user attribute values + // but we special-case these for backward compatibility. + if (typeof value === 'object' && (!Array.isArray(value) || !value.every((item) => typeof item === 'string'))) { + return JSON.stringify(value); } - return JSON.stringify(value); -} -function transformCustomContextValues(contextValue: EvaluationContextValue): ConfigCatUser['custom'] { - if (typeof contextValue !== 'object' || contextValue === null) { - return {}; - } + // NOTE: No need to check for unsupported attribute values as the ConfigCat SDK handles those internally. + return value as UserAttributeValue; +} - return Object.entries(contextValue).reduce( - (context, [key, value]) => { +function transformCustomContextValues(values: Record): ConfigCatUser['custom'] { + let attributes: ConfigCatUser['custom']; + for (const [key, value] of Object.entries(values)) { + if (value != null) { const transformedValue = toUserAttributeValue(value); - return transformedValue ? { ...context, [key]: transformedValue } : context; - }, - {} as ConfigCatUser['custom'], - ); + // NOTE: No need to check for `identifier`, `email` and `country` user attributes + // as the ConfigCat SDK ignores those as custom user attributes. + (attributes ??= {})[key] = transformedValue; + } + } + return attributes; } function stringOrUndefined(param?: unknown): string | undefined { @@ -33,16 +34,13 @@ function stringOrUndefined(param?: unknown): string | undefined { } export function transformContext(context: EvaluationContext): ConfigCatUser | undefined { - const { targetingKey, email, country, ...attributes } = context; - - if (!targetingKey) { - return undefined; - } + const { targetingKey, email, country } = context; return { - identifier: targetingKey, + // NOTE: The ConfigCat SDK handles missing or unsupported identifier values. + identifier: targetingKey ?? (context['identifier'] as string), email: stringOrUndefined(email), country: stringOrUndefined(country), - custom: transformCustomContextValues(attributes), + custom: transformCustomContextValues(context), }; } diff --git a/libs/shared/config-cat-core/src/lib/result-mapping.ts b/libs/shared/config-cat-core/src/lib/result-mapping.ts index 25d05bd17..a190b1827 100644 --- a/libs/shared/config-cat-core/src/lib/result-mapping.ts +++ b/libs/shared/config-cat-core/src/lib/result-mapping.ts @@ -1,17 +1,18 @@ -import type { IEvaluationDetails } from 'configcat-js-ssr'; +import type { EvaluationDetails } from '@configcat/sdk'; +import { EvaluationErrorCode } from '@configcat/sdk'; import type { ResolutionDetails, ResolutionReason, JsonValue, OpenFeatureError } from '@openfeature/core'; import { TypeMismatchError, StandardResolutionReasons, ParseError, - TargetingKeyMissingError, GeneralError, FlagNotFoundError, + ProviderNotReadyError, } from '@openfeature/core'; export function toResolutionDetails( value: T, - data: Omit, + data: Omit, reason?: ResolutionReason, ): ResolutionDetails { const matchedTargeting = data.matchedTargetingRule; @@ -28,27 +29,19 @@ export function toResolutionDetails( }; } -export function parseError(errorMessage: string | undefined): OpenFeatureError { - // Detecting the error type by checking the error message is awkward and fragile, - // but ConfigCat SDK doesn't allow a better way at the moment. - // However, there are plans to improve this situation, so let's revise this - // as soon as ConfigCat SDK implements returning error codes. - - if (errorMessage) { - if (errorMessage.includes('Config JSON is not present')) { +export function translateError(errorCode: Exclude): OpenFeatureError { + switch (errorCode) { + case EvaluationErrorCode.InvalidConfigModel: return new ParseError(); - } - if (errorMessage.includes('the key was not found in config JSON')) { - return new FlagNotFoundError(); - } - if ( - errorMessage.includes('The type of a setting must match the type of the specified default value') || - /Setting value (?:is null|is undefined|'.*' is of an unsupported type)/.test(errorMessage) - ) { + case EvaluationErrorCode.SettingValueTypeMismatch: return new TypeMismatchError(); - } + case EvaluationErrorCode.ConfigJsonNotAvailable: + return new ProviderNotReadyError(); + case EvaluationErrorCode.SettingKeyMissing: + return new FlagNotFoundError(); + default: + return new GeneralError(); } - return new GeneralError(); } export type PrimitiveTypeName = 'string' | 'boolean' | 'number' | 'object'; diff --git a/package-lock.json b/package-lock.json index c8199b948..dba5a3e34 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@aws-sdk/client-ssm": "^3.759.0", "@bufbuild/protobuf": "^1.2.0", + "@configcat/sdk": "^1.0.1", "@connectrpc/connect": "^1.4.0", "@connectrpc/connect-web": "^1.4.0", "@flipt-io/flipt": "^1.2.0", @@ -23,9 +24,6 @@ "@swc/helpers": "0.5.17", "ajv": "^8.12.0", "axios": "1.11.0", - "configcat-common": "9.4.0", - "configcat-js-ssr": "^8.4.3", - "configcat-node": "^11.3.1", "copy-anything": "^3.0.5", "flagsmith": "^4.1.4", "imurmurhash": "^0.1.4", @@ -92,7 +90,8 @@ "verdaccio": "^5.0.4" }, "engines": { - "node": ">=24.0.0" + "node": ">=24.0.0", + "npm": "^8.0.0" } }, "node_modules/@ampproject/remapping": { @@ -2829,6 +2828,12 @@ "tough-cookie": "^4.1.4" } }, + "node_modules/@configcat/sdk": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@configcat/sdk/-/sdk-1.0.1.tgz", + "integrity": "sha512-XTUEx84ZUNKMZ/SkA2LcoweVbTOW6DzIzJXSuOEytEMSmvUOiTZJPdRdnv6HzK3BKgvAGEUEITsKGv695R1A/w==", + "license": "MIT" + }, "node_modules/@connectrpc/connect": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/@connectrpc/connect/-/connect-1.6.1.tgz", @@ -10715,58 +10720,6 @@ "node": ">=0.10.0" } }, - "node_modules/configcat-common": { - "version": "9.4.0", - "resolved": "https://registry.npmjs.org/configcat-common/-/configcat-common-9.4.0.tgz", - "integrity": "sha512-rKFawlrEXY1NQ1jxX5sYXN4fhT4MhfllC1ELd+3k2tY+z1DQju+PoopNra1wyfegLn3Aa0X8l3JpXFFO/gyo0g==", - "license": "MIT", - "dependencies": { - "tslib": "^2.4.1" - } - }, - "node_modules/configcat-js-ssr": { - "version": "8.4.4", - "resolved": "https://registry.npmjs.org/configcat-js-ssr/-/configcat-js-ssr-8.4.4.tgz", - "integrity": "sha512-vkc3rfGltlwa5Fi62BDSyCDfFW/onBTmR6rddJTk/9cLUPZMW+p9dn7GABCOnT0Ke7K6UV1pHsCCuPuxTP0PKA==", - "license": "MIT", - "dependencies": { - "axios": "^1.7.8", - "configcat-common": "9.3.1", - "tslib": "^2.4.1" - } - }, - "node_modules/configcat-js-ssr/node_modules/configcat-common": { - "version": "9.3.1", - "resolved": "https://registry.npmjs.org/configcat-common/-/configcat-common-9.3.1.tgz", - "integrity": "sha512-yVkIbluksD/kZfVyKjLIOpwLrq3/ZRM7Lwrsz89JmbpQ6VtbnelrTQynSPElTtKjrPRZx56v3IZYk3nWTnnM6A==", - "license": "MIT", - "dependencies": { - "tslib": "^2.4.1" - } - }, - "node_modules/configcat-node": { - "version": "11.3.1", - "resolved": "https://registry.npmjs.org/configcat-node/-/configcat-node-11.3.1.tgz", - "integrity": "sha512-7XJbgBpcxlwzlRLmvCtHTkO247Ban2ZkBqlmk+T0wVEt5tXfltgd53SYLYpw7RBWX0ma/QyP5E+/k/UDdMrOCw==", - "license": "MIT", - "dependencies": { - "configcat-common": "9.3.1", - "tslib": "^2.4.1", - "tunnel": "0.0.6" - }, - "engines": { - "node": ">=14" - } - }, - "node_modules/configcat-node/node_modules/configcat-common": { - "version": "9.3.1", - "resolved": "https://registry.npmjs.org/configcat-common/-/configcat-common-9.3.1.tgz", - "integrity": "sha512-yVkIbluksD/kZfVyKjLIOpwLrq3/ZRM7Lwrsz89JmbpQ6VtbnelrTQynSPElTtKjrPRZx56v3IZYk3nWTnnM6A==", - "license": "MIT", - "dependencies": { - "tslib": "^2.4.1" - } - }, "node_modules/confusing-browser-globals": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/confusing-browser-globals/-/confusing-browser-globals-1.0.11.tgz", @@ -20644,15 +20597,6 @@ "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "license": "0BSD" }, - "node_modules/tunnel": { - "version": "0.0.6", - "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz", - "integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==", - "license": "MIT", - "engines": { - "node": ">=0.6.11 <=0.7.0 || >=0.7.3" - } - }, "node_modules/tunnel-agent": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", diff --git a/package.json b/package.json index ac33a1cd5..74d58dc0e 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "dependencies": { "@aws-sdk/client-ssm": "^3.759.0", "@bufbuild/protobuf": "^1.2.0", + "@configcat/sdk": "^1.0.1", "@connectrpc/connect": "^1.4.0", "@connectrpc/connect-web": "^1.4.0", "@flipt-io/flipt": "^1.2.0", @@ -32,9 +33,6 @@ "@swc/helpers": "0.5.17", "ajv": "^8.12.0", "axios": "1.11.0", - "configcat-common": "9.4.0", - "configcat-js-ssr": "^8.4.3", - "configcat-node": "^11.3.1", "copy-anything": "^3.0.5", "flagsmith": "^4.1.4", "imurmurhash": "^0.1.4",