diff --git a/.changeset/cli-rules-openfeature.md b/.changeset/cli-rules-openfeature.md new file mode 100644 index 000000000..e1a061af7 --- /dev/null +++ b/.changeset/cli-rules-openfeature.md @@ -0,0 +1,5 @@ +--- +"@reflag/cli": patch +--- + +The coding-agent rules no longer say the OpenFeature providers use SDK v1. diff --git a/.changeset/openfeature-sdk-v2.md b/.changeset/openfeature-sdk-v2.md new file mode 100644 index 000000000..ca0ad733b --- /dev/null +++ b/.changeset/openfeature-sdk-v2.md @@ -0,0 +1,6 @@ +--- +"@reflag/openfeature-browser-provider": major +"@reflag/openfeature-node-provider": major +--- + +Use Reflag SDK v2. Boolean flags resolve with `getBooleanValue()`, and multivariate flags resolve with the method that matches their value's type: `getStringValue()`, `getNumberValue()` or `getObjectValue()`. String and object reads no longer return a Boolean flag's Remote config key or payload; move Remote config to a multivariate flag first. Number reads now work. A read returns your default with `TYPE_MISMATCH` when the value has a different JSON type, and with `DEFAULT` when the flag has no value. The Browser provider reports the selected variant's key as `variant`, and updates the client's context on context changes instead of creating a new client. diff --git a/packages/browser-sdk/test/release.test.ts b/packages/browser-sdk/test/release.test.ts index 2de176c51..a8b8081ed 100644 --- a/packages/browser-sdk/test/release.test.ts +++ b/packages/browser-sdk/test/release.test.ts @@ -42,9 +42,11 @@ describe("framework v1/v2 release isolation", () => { ); }); - test("keeps the unchanged OpenFeature Browser provider on published v1", () => { - expect(openfeature.dependencies["@reflag/browser-sdk"]).toBe("1.7.4"); - expect(openfeatureRequire.resolve("@reflag/browser-sdk")).not.toBe( + test("uses workspace v2 for the OpenFeature Browser provider", () => { + expect(openfeature.dependencies["@reflag/browser-sdk"]).toBe( + `^${browser.version}`, + ); + expect(openfeatureRequire.resolve("@reflag/browser-sdk")).toBe( resolve(__dirname, "../dist/reflag-browser-sdk.umd.js"), ); }); diff --git a/packages/cli/services/rules.ts b/packages/cli/services/rules.ts index a2d6efac4..003170c0f 100644 --- a/packages/cli/services/rules.ts +++ b/packages/cli/services/rules.ts @@ -16,7 +16,7 @@ export function getCopilotInstructions() { const rules = /* markdown */ ` # Reflag SDK v2 -These instructions apply to the Node.js, Browser, React, Vue and React Native SDKs, version 2. The OpenFeature providers still use SDK v1; don't mix their code or generated types with v2. +These instructions apply to the Node.js, Browser, React, Vue and React Native SDKs, version 2. ## Reading flags diff --git a/packages/flag-evaluation/README.md b/packages/flag-evaluation/README.md index c4b2b84c2..6d66e865f 100644 --- a/packages/flag-evaluation/README.md +++ b/packages/flag-evaluation/README.md @@ -100,8 +100,7 @@ rules or a single flag. It returns an object with `evaluateAll(context)` and `evaluateFlag(context, flagKey)` methods, rather than a callable evaluator. Publishers and consumers must adopt the collection/reference protocol together. Node SDK v2 uses the collection evaluator, including in the matching v2 React -bootstrap demo. The OpenFeature Node provider remains pinned to published Node -SDK 1.8.3 and its v1 evaluator. +bootstrap demo and the OpenFeature Node provider. V1's `evaluateFlagRules`, scalar `evaluate`, `flattenJSON`, `unflattenJSON`, and value-rule types are removed. There is no v2 one-shot evaluation wrapper. diff --git a/packages/flag-evaluation/test/release.test.ts b/packages/flag-evaluation/test/release.test.ts index df96593ef..fadc3776f 100644 --- a/packages/flag-evaluation/test/release.test.ts +++ b/packages/flag-evaluation/test/release.test.ts @@ -29,15 +29,15 @@ describe("v1/v2 release isolation", () => { ); }); - it("keeps the unchanged OpenFeature provider on published Node SDK v1", () => { - // Matching the v2 workspace's version would silently link this alias locally. - expect(require.resolve("@reflag/node-sdk-v1/package.json")).not.toBe( - resolve(__dirname, "../../node-sdk/package.json"), + it("moves the OpenFeature provider onto workspace Node SDK v2", () => { + expect(openfeature.dependencies["@reflag/node-sdk"]).toBe( + `^${nodeSdk.version}`, ); - expect(require("@reflag/node-sdk-v1/package.json").version).toBe("1.8.3"); - expect(openfeature.dependencies["@reflag/node-sdk-v1"]).toBe( - "npm:@reflag/node-sdk@1.8.3", - ); - expect(openfeature.dependencies).not.toHaveProperty("@reflag/node-sdk"); + expect(openfeature.dependencies).not.toHaveProperty("@reflag/node-sdk-v1"); + expect( + require.resolve("@reflag/node-sdk/package.json", { + paths: [resolve(__dirname, "../../openfeature-node-provider")], + }), + ).toBe(resolve(__dirname, "../../node-sdk/package.json")); }); }); diff --git a/packages/openfeature-browser-provider/README.md b/packages/openfeature-browser-provider/README.md index 7a97afb81..08b9555c9 100644 --- a/packages/openfeature-browser-provider/README.md +++ b/packages/openfeature-browser-provider/README.md @@ -53,7 +53,7 @@ const client = OpenFeature.getClient(); // use client const boolValue = client.getBooleanValue("huddles", false); -// use more complex, dynamic config-enabled functionality. +// read a multivariate flag's value const feedbackConfig = client.getObjectValue("ask-feedback", { question: "How are you enjoying this feature?", }); @@ -62,58 +62,35 @@ const feedbackConfig = client.getObjectValue("ask-feedback", { Initializing the Reflag Browser Provider will also initialize [automatic feedback surveys](https://github.com/reflagcom/javascript/tree/main/packages/browser-sdk#qualitative-feedback). -## Feature resolution methods +## Flag resolution methods -The Reflag OpenFeature Provider implements the OpenFeature evaluation interface for different value types. Each method handles the resolution of flags according to the OpenFeature specification. - -### Common behavior - -All resolution methods share these behaviors: - -- Return default value with `PROVIDER_NOT_READY` if client is not initialized, -- Return default value with `FLAG_NOT_FOUND` if flag doesn't exist, -- Return default value with `ERROR` if there was a type mismatch, -- Return evaluated value with `TARGETING_MATCH` on successful resolution. - -### Type-Specific Methods - -#### Boolean Resolution - -```ts -client.getBooleanValue("my-flag", false); -``` - -Returns the flag's enabled state. This is the most common use case for flags. - -#### String Resolution +The provider reads flags with Reflag SDK v2. Boolean flags resolve with `getBooleanValue()`. Multivariate flags resolve with the method that matches their value's JSON type. ```ts -client.getStringValue("my-flag", "default"); +client.getBooleanValue("huddles", false); // a Boolean flag +client.getStringValue("model", "quick"); // a multivariate flag with string values +client.getNumberValue("max-participants", 10); // ... with number values +client.getObjectValue("huddle-settings", { maxParticipants: 10 }); // ... with object or array values ``` -Returns the flag's remote config key (also known as "variant"). Useful for multi-variate use cases. - -#### Number Resolution - -```ts -client.getNumberValue("my-flag", 0); -``` +All methods: -Not directly supported by Reflag. Use `getObjectValue` instead for numeric configurations. +- return the flag's value with `TARGETING_MATCH`. In the Browser provider, `variant` is the key of the selected variant; +- return your default with `DEFAULT` if the flag has no value; +- return your default with `FLAG_NOT_FOUND` if the flag doesn't exist; +- return your default with `TYPE_MISMATCH` if the value has a different JSON type from your default. Pass `null` to `getObjectValue()` to accept any JSON value; +- return your default with `PROVIDER_NOT_READY` before the provider is initialized. -#### Object Resolution +Each evaluation records one check in Reflag. -```ts -// works for any type: -client.getObjectValue("my-flag", { defaultValue: true }); -client.getObjectValue("my-flag", "string-value"); -client.getObjectValue("my-flag", 199); -``` +## Migrating from provider 1.x -Returns the flag's remote config payload with type validation. This is the most flexible method, -allowing for complex configuration objects or simple types. +Version 2 of the provider uses Reflag SDK v2 instead of v1. -The object resolution performs runtime type checking between the default value and the flag payload to ensure type safety. +- `getStringValue()` and `getObjectValue()` read a multivariate flag's value instead of a Boolean flag's Remote config key and payload. Move Remote config to a multivariate flag first. See [Migrating from Remote config](https://docs.reflag.com/product-handbook/multivariate-flags#migrating-from-remote-config). +- `getNumberValue()` now works for multivariate flags with number values. +- A Boolean read of a multivariate flag returns your default with `TYPE_MISMATCH`. +- The `fallbackFlags` option is removed. See the SDK v2 [migration guide](https://docs.reflag.com/supported-languages/migrating-from-v1). ## Context diff --git a/packages/openfeature-browser-provider/example/app/featureManagement.ts b/packages/openfeature-browser-provider/example/app/featureManagement.ts index 683a152a6..844a63ab5 100644 --- a/packages/openfeature-browser-provider/example/app/featureManagement.ts +++ b/packages/openfeature-browser-provider/example/app/featureManagement.ts @@ -19,17 +19,7 @@ export async function initOpenFeature() { console.error("No publishable key set for Reflag"); return; } - reflagProvider = new ReflagBrowserSDKProvider({ - publishableKey, - fallbackFlags: { - huddle: { - key: "zoom", // huddleMeetingProvider - payload: { - joinUrl: "https://zoom.us/join", - }, - }, - }, - }); + reflagProvider = new ReflagBrowserSDKProvider({ publishableKey }); return OpenFeature.setProviderAndWait(reflagProvider); } diff --git a/packages/openfeature-browser-provider/example/components/HuddleFeature.tsx b/packages/openfeature-browser-provider/example/components/HuddleFeature.tsx index 6a1886e5b..bb3105182 100644 --- a/packages/openfeature-browser-provider/example/components/HuddleFeature.tsx +++ b/packages/openfeature-browser-provider/example/components/HuddleFeature.tsx @@ -9,11 +9,13 @@ import React from "react"; import { track } from "@/app/featureManagement"; const flagKey = "huddle"; +// A multivariate flag whose variants hold the huddle settings. +const settingsFlagKey = "huddle-settings"; export const HuddleFeature = () => { const isEnabled = useBooleanFlagValue(flagKey, false); const { variant: huddleMeetingProvider, value: config } = - useObjectFlagDetails(flagKey, { + useObjectFlagDetails(settingsFlagKey, { joinUrl: "https://zoom.us/join", }); diff --git a/packages/openfeature-browser-provider/package.json b/packages/openfeature-browser-provider/package.json index 81c0528b3..51819f01d 100644 --- a/packages/openfeature-browser-provider/package.json +++ b/packages/openfeature-browser-provider/package.json @@ -36,7 +36,7 @@ "fmt:fix": "oxfmt --write ." }, "dependencies": { - "@reflag/browser-sdk": "1.7.4" + "@reflag/browser-sdk": "^2.0.0-next.1" }, "devDependencies": { "@openfeature/core": "1.5.0", diff --git a/packages/openfeature-browser-provider/src/index.test.ts b/packages/openfeature-browser-provider/src/index.test.ts index 928529cdf..dbbecc848 100644 --- a/packages/openfeature-browser-provider/src/index.test.ts +++ b/packages/openfeature-browser-provider/src/index.test.ts @@ -23,8 +23,10 @@ describe("ReflagBrowserSDKProvider", () => { let provider: ReflagBrowserSDKProvider; let ofClient: Client; const reflagClientMock = { - getFlags: vi.fn(), + getFlagResults: vi.fn(), getFlag: vi.fn(), + setContext: vi.fn().mockResolvedValue(undefined), + getContext: vi.fn().mockReturnValue({ user: { id: "user-1" } }), initialize: vi.fn().mockResolvedValue({}), track: vi.fn(), stop: vi.fn(), @@ -75,7 +77,7 @@ describe("ReflagBrowserSDKProvider", () => { expect(reflagClientMock.stop).toHaveBeenCalledTimes(1); }); - it("onContextChange re-initializes client", async () => { + it("onContextChange initializes the client if needed", async () => { const p = new ReflagBrowserSDKProvider({ publishableKey }); expect(p["_client"]).toBeUndefined(); expect(mockReflagClient).toHaveBeenCalledTimes(0); @@ -84,6 +86,68 @@ describe("ReflagBrowserSDKProvider", () => { expect(mockReflagClient).toHaveBeenCalledTimes(1); expect(p["_client"]).toBeDefined(); }); + + it("onContextChange updates the existing client's context", async () => { + await provider.initialize(); + vi.clearAllMocks(); + + await provider.onContextChange({}, { targetingKey: "user-2" }); + expect(mockReflagClient).not.toHaveBeenCalled(); + expect(reflagClientMock.setContext).toHaveBeenCalledWith( + defaultContextTranslator({ targetingKey: "user-2" }), + ); + }); + + it("onContextChange drops bootstrapped data when the first user ID arrives", async () => { + const bootstrappedState = { + protocolVersion: 2 as const, + context: {}, + flags: {}, + }; + const p = new ReflagBrowserSDKProvider({ + publishableKey, + bootstrappedState, + }); + await p.initialize(); + reflagClientMock.getContext.mockReturnValueOnce({}); + vi.clearAllMocks(); + + await p.onContextChange({}, { targetingKey: "user-2" }); + expect(mockReflagClient).toHaveBeenCalledWith({ + publishableKey, + ...defaultContextTranslator({ targetingKey: "user-2" }), + }); + }); + + it("onContextChange keeps context from the provider options", async () => { + const p = new ReflagBrowserSDKProvider({ + publishableKey, + other: { environment: "production" }, + }); + await p.initialize({ targetingKey: "user-1" }); + vi.clearAllMocks(); + + await p.onContextChange({}, { targetingKey: "user-2" }); + expect(reflagClientMock.setContext).toHaveBeenCalledWith({ + other: { environment: "production" }, + ...defaultContextTranslator({ targetingKey: "user-2" }), + }); + }); + + it("onContextChange starts a new client when the first user ID arrives", async () => { + await provider.initialize(); + reflagClientMock.getContext.mockReturnValueOnce({}); + vi.clearAllMocks(); + + await provider.onContextChange({}, { targetingKey: "user-2" }); + expect(reflagClientMock.stop).toHaveBeenCalledTimes(1); + expect(mockReflagClient).toHaveBeenCalledWith({ + publishableKey, + ...defaultContextTranslator({ targetingKey: "user-2" }), + }); + expect(reflagClientMock.setContext).not.toHaveBeenCalled(); + expect(provider.status).toBe("READY"); + }); }); describe("contextTranslator", () => { @@ -177,29 +241,19 @@ describe("ReflagBrowserSDKProvider", () => { await provider.initialize(); }); - function mockFlag( - enabled: boolean, - configKey?: string | null, - configPayload?: any, - ) { - const config = { - key: configKey, - payload: configPayload, - }; - - reflagClientMock.getFlag = vi.fn().mockReturnValue({ - isEnabled: enabled, - config, + function mockFlag(value: unknown, variantKey?: string) { + reflagClientMock.getFlagResults = vi.fn().mockReturnValue({ + [testFlagKey]: { value, variantKey, resolved: value !== undefined }, }); - - reflagClientMock.getFlags = vi.fn().mockReturnValue({ - [testFlagKey]: { - isEnabled: enabled, - config: { - key: "key", - payload: configPayload, - }, - }, + // Mirrors the SDK: the default is used when the flag has no value or a + // different JSON type. + reflagClientMock.getFlag = vi.fn((_key: string, def: unknown) => { + const type = (v: unknown) => + v === null ? "null" : Array.isArray(v) ? "array" : typeof v; + return value === undefined || + (def !== null && type(value) !== type(def)) + ? def + : value; }); } @@ -218,7 +272,7 @@ describe("ReflagBrowserSDKProvider", () => { }); it("returns error if flag is not found", async () => { - mockFlag(true, "key", true); + mockFlag(true, "on"); const val = ofClient.getBooleanDetails("missing-key", true); expect(val).toMatchObject({ @@ -228,10 +282,11 @@ describe("ReflagBrowserSDKProvider", () => { errorCode: "FLAG_NOT_FOUND", value: true, }); + expect(reflagClientMock.getFlag).not.toHaveBeenCalled(); }); - it("calls the client correctly when evaluating", async () => { - mockFlag(true, "key", true); + it("reads the flag through getFlag so a check is recorded", async () => { + mockFlag(true, "on"); const val = ofClient.getBooleanDetails(testFlagKey, false); @@ -239,90 +294,89 @@ describe("ReflagBrowserSDKProvider", () => { flagKey: testFlagKey, flagMetadata: {}, reason: "TARGETING_MATCH", - variant: "key", + variant: "on", value: true, }); + expect(reflagClientMock.getFlag).toHaveBeenCalledWith(testFlagKey, false); + }); + + it("returns the default when the flag has no value", async () => { + mockFlag(undefined); - expect(reflagClientMock.getFlags).toHaveBeenCalled(); - expect(reflagClientMock.getFlag).toHaveBeenCalledWith(testFlagKey); + expect(ofClient.getStringDetails(testFlagKey, "default")).toMatchObject({ + reason: "DEFAULT", + value: "default", + }); }); it.each([ - [true, false, true, "TARGETING_MATCH", undefined], - [undefined, true, true, "ERROR", "FLAG_NOT_FOUND"], - [undefined, false, false, "ERROR", "FLAG_NOT_FOUND"], - ])( - "should return the correct result when evaluating boolean. enabled: %s, value: %s, default: %s, expected: %s, reason: %s, errorCode: %s`", - (enabled, def, expected, reason, errorCode) => { - const configKey = enabled !== undefined ? "variant-1" : undefined; - const flagKey = enabled ? testFlagKey : "missing-key"; - - mockFlag(enabled ?? false, configKey); - - expect(ofClient.getBooleanDetails(flagKey, def)).toMatchObject({ - flagKey, - flagMetadata: {}, - reason, - value: expected, - ...(errorCode ? { errorCode } : {}), - ...(configKey ? { variant: configKey } : {}), - }); - }, - ); - - it("should return error when evaluating number", async () => { - expect(ofClient.getNumberDetails(testFlagKey, 1)).toMatchObject({ + ["boolean", true, () => ofClient.getBooleanDetails(testFlagKey, false)], + [ + "string", + "quick", + () => ofClient.getStringDetails(testFlagKey, "default"), + ], + ["number", 15, () => ofClient.getNumberDetails(testFlagKey, 10)], + [ + "object", + { model: "quick" }, + () => ofClient.getObjectDetails(testFlagKey, { model: "default" }), + ], + ["array", [1, 2], () => ofClient.getObjectDetails(testFlagKey, [])], + ])("resolves a %s value with its variant", (_type, value, read) => { + mockFlag(value, "variant-1"); + + expect(read()).toMatchObject({ flagKey: testFlagKey, - flagMetadata: {}, - reason: "ERROR", - errorCode: "GENERAL", - value: 1, + reason: "TARGETING_MATCH", + variant: "variant-1", + value, }); }); it.each([ - ["key-1", "default", "key-1", "TARGETING_MATCH"], - [null, "default", "default", "DEFAULT"], - [undefined, "default", "default", "DEFAULT"], - ])( - "should return the correct result when evaluating string. variant: %s, def: %s, expected: %s, reason: %s, errorCode: %s`", - (variant, def, expected, reason) => { - mockFlag(true, variant, {}); - expect(ofClient.getStringDetails(testFlagKey, def)).toMatchObject({ - flagKey: testFlagKey, - flagMetadata: {}, - reason, - value: expected, - ...(variant ? { variant } : {}), - }); - }, - ); + [ + "boolean read of a string", + "quick", + () => ofClient.getBooleanDetails(testFlagKey, true), + true, + ], + [ + "string read of a boolean", + true, + () => ofClient.getStringDetails(testFlagKey, "default"), + "default", + ], + [ + "number read of a string", + "15", + () => ofClient.getNumberDetails(testFlagKey, 10), + 10, + ], + [ + "object read of an array", + [1], + () => ofClient.getObjectDetails(testFlagKey, { a: 1 }), + { a: 1 }, + ], + ])("returns TYPE_MISMATCH for a %s", (_name, value, read, expected) => { + mockFlag(value, "variant-1"); + + expect(read()).toMatchObject({ + reason: "ERROR", + errorCode: "TYPE_MISMATCH", + value: expected, + }); + }); - it.each([ - ["one", {}, { a: 1 }, {}, "TARGETING_MATCH", undefined], - ["two", "string", "default", "string", "TARGETING_MATCH", undefined], - ["three", 15, 16, 15, "TARGETING_MATCH", undefined], - ["four", true, true, true, "TARGETING_MATCH", undefined], - ["five", 100, "string", "string", "ERROR", "TYPE_MISMATCH"], - ["six", 1337, true, true, "ERROR", "TYPE_MISMATCH"], - ["seven", "string", 1337, 1337, "ERROR", "TYPE_MISMATCH"], - [undefined, null, { a: 2 }, { a: 2 }, "ERROR", "TYPE_MISMATCH"], - [undefined, undefined, "a", "a", "ERROR", "TYPE_MISMATCH"], - ])( - "should return the correct result when evaluating object. variant: %s, value: %s, default: %s, expected: %s, reason: %s, errorCode: %s`", - (variant, value, def, expected, reason, errorCode) => { - mockFlag(true, variant, value); - - expect(ofClient.getObjectDetails(testFlagKey, def)).toMatchObject({ - flagKey: testFlagKey, - flagMetadata: {}, - reason, - value: expected, - ...(errorCode ? { errorCode } : {}), - ...(variant && !errorCode ? { variant } : {}), - }); - }, - ); + it("accepts any JSON value with a null object default", async () => { + mockFlag({ a: 1 }, "variant-1"); + + expect(ofClient.getObjectDetails(testFlagKey, null)).toMatchObject({ + reason: "TARGETING_MATCH", + value: { a: 1 }, + }); + }); }); describe("track", () => { diff --git a/packages/openfeature-browser-provider/src/index.ts b/packages/openfeature-browser-provider/src/index.ts index 187af1ddf..c1d51e429 100644 --- a/packages/openfeature-browser-provider/src/index.ts +++ b/packages/openfeature-browser-provider/src/index.ts @@ -11,7 +11,24 @@ import { TrackingEventDetails, } from "@openfeature/web-sdk"; -import { Flag, InitOptions, ReflagClient } from "@reflag/browser-sdk"; +import { InitOptions, ReflagClient } from "@reflag/browser-sdk"; + +function jsonType(value: JsonValue): string { + return value === null + ? "null" + : Array.isArray(value) + ? "array" + : typeof value; +} + +// A `null` default accepts any JSON value, as in the Reflag SDKs. +function hasId(id: unknown) { + return id !== undefined && id !== null && id !== ""; +} + +function isTypeMismatch(value: JsonValue, defaultValue: JsonValue) { + return defaultValue !== null && jsonType(value) !== jsonType(defaultValue); +} export type ContextTranslationFn = ( context?: EvaluationContext, @@ -73,8 +90,15 @@ export class ReflagBrowserSDKProvider implements Provider { } async initialize(context?: EvaluationContext): Promise { + await this.startClient(this._clientOptions, context); + } + + private async startClient( + options: InitOptions, + context?: EvaluationContext, + ): Promise { const client = new ReflagClient({ - ...this._clientOptions, + ...options, ...this._contextTranslator(context), }); @@ -98,13 +122,38 @@ export class ReflagBrowserSDKProvider implements Provider { _oldContext: EvaluationContext, newContext: EvaluationContext, ): Promise { - await this.initialize(newContext); + // Match initialize(): context from the provider options, overridden by + // the translated OpenFeature context. + const { user, company, other, otherContext } = this._clientOptions; + const context = { + user, + company, + other, + otherContext, + ...this._contextTranslator(newContext), + }; + const client = this._client; + if (!client) { + await this.initialize(newContext); + return; + } + // The Browser SDK sets up automatic feedback only when the client starts + // with a user, so start a new client when the first user ID arrives. + if (!hasId(client.getContext().user?.id) && hasId(context.user?.id)) { + this._client = undefined; + await client.stop(); + // Bootstrapped data belongs to the anonymous context, and the SDK would + // prefer its context and skip fetching, so start without it. + const { bootstrappedState: _bootstrap, ...options } = this._clientOptions; + await this.startClient(options, newContext); + return; + } + await client.setContext(context); } private resolveFlag( flagKey: string, defaultValue: T, - resolveFn: (feature: Flag) => ResolutionDetails, ): ResolutionDetails { if (!this._client) { return { @@ -115,88 +164,57 @@ export class ReflagBrowserSDKProvider implements Provider { } satisfies ResolutionDetails; } - const features = this._client.getFlags(); - if (flagKey in features) { - return resolveFn(this._client.getFlag(flagKey)); + const result = this._client.getFlagResults()[flagKey]; + if (!result) { + return { + value: defaultValue, + reason: StandardResolutionReasons.ERROR, + errorCode: ErrorCode.FLAG_NOT_FOUND, + errorMessage: `Flag ${flagKey} not found`, + }; } - return { - value: defaultValue, - reason: StandardResolutionReasons.DEFAULT, - errorCode: ErrorCode.FLAG_NOT_FOUND, - errorMessage: `Flag ${flagKey} not found`, - }; - } + // Records a check. Returns the default if the flag has no value or a + // different JSON type. + const value = this._client.getFlag(flagKey, defaultValue) as unknown as T; - resolveBooleanEvaluation(flagKey: string, defaultValue: boolean) { - return this.resolveFlag(flagKey, defaultValue, (feature) => { + if (result.value === undefined) { + return { value: defaultValue, reason: StandardResolutionReasons.DEFAULT }; + } + + if (isTypeMismatch(result.value, defaultValue)) { return { - value: feature.isEnabled, - variant: feature.config.key, - reason: StandardResolutionReasons.TARGETING_MATCH, + value: defaultValue, + reason: StandardResolutionReasons.ERROR, + errorCode: ErrorCode.TYPE_MISMATCH, + errorMessage: `Expected a value of type \`${jsonType(defaultValue)}\` but got \`${jsonType(result.value)}\`.`, }; - }); - } + } - resolveNumberEvaluation(_flagKey: string, defaultValue: number) { return { - value: defaultValue, - reason: StandardResolutionReasons.ERROR, - errorCode: ErrorCode.GENERAL, - errorMessage: - "Reflag doesn't support this method. Use `resolveObjectEvaluation` instead.", + value, + variant: result.variantKey, + reason: StandardResolutionReasons.TARGETING_MATCH, }; } - resolveStringEvaluation( - flagKey: string, - defaultValue: string, - ): ResolutionDetails { - return this.resolveFlag(flagKey, defaultValue, (feature) => { - if (!feature.config.key) { - return { - value: defaultValue, - reason: StandardResolutionReasons.DEFAULT, - }; - } + resolveBooleanEvaluation(flagKey: string, defaultValue: boolean) { + return this.resolveFlag(flagKey, defaultValue); + } - return { - value: feature.config.key as string, - variant: feature.config.key, - reason: StandardResolutionReasons.TARGETING_MATCH, - }; - }); + resolveNumberEvaluation(flagKey: string, defaultValue: number) { + return this.resolveFlag(flagKey, defaultValue); + } + + resolveStringEvaluation(flagKey: string, defaultValue: string) { + return this.resolveFlag(flagKey, defaultValue); } resolveObjectEvaluation( flagKey: string, defaultValue: T, ) { - return this.resolveFlag(flagKey, defaultValue, (feature) => { - const expType = typeof defaultValue; - - const payloadType = typeof feature.config.payload; - - if ( - feature.config.payload === undefined || - feature.config.payload === null || - payloadType !== expType - ) { - return { - value: defaultValue, - reason: StandardResolutionReasons.ERROR, - variant: feature.config.key, - errorCode: ErrorCode.TYPE_MISMATCH, - errorMessage: `Expected remote config payload of type \`${expType}\` but got \`${payloadType}\`.`, - }; - } - - return { - value: feature.config.payload, - variant: feature.config.key, - reason: StandardResolutionReasons.TARGETING_MATCH, - }; - }); + return this.resolveFlag(flagKey, defaultValue); } track( diff --git a/packages/openfeature-node-provider/README.md b/packages/openfeature-node-provider/README.md index 08b9274e0..562132f50 100644 --- a/packages/openfeature-node-provider/README.md +++ b/packages/openfeature-node-provider/README.md @@ -34,7 +34,7 @@ If you have been using the Bucket SDKs, the following list will help you migrate ## Usage The provider uses the [Reflag Node.js SDK](https://docs.reflag.com/quickstart/supported-languages-frameworks/node.js-sdk). -The available options can be found in the [Reflag Node.js SDK](https://github.com/reflagcom/javascript/tree/main/packages/node-sdk#initialization-options). +The available options can be found in the [Reflag Node.js SDK](https://github.com/reflagcom/javascript/tree/main/packages/node-sdk#configuration). ### Example using the default configuration @@ -66,58 +66,35 @@ const enterpriseFlagEnabled = await client.getBooleanValue( ); ``` -## Feature resolution methods +## Flag resolution methods -The Reflag OpenFeature Provider implements the OpenFeature evaluation interface for different value types. Each method handles the resolution of flags according to the OpenFeature specification. - -### Common behavior - -All resolution methods share these behaviors: - -- Return default value with `PROVIDER_NOT_READY` if client is not initialized, -- Return default value with `FLAG_NOT_FOUND` if flag doesn't exist, -- Return default value with `ERROR` if there was a type mismatch, -- Return evaluated value with `TARGETING_MATCH` on successful resolution. - -### Type-Specific Methods - -#### Boolean Resolution - -```ts -client.getBooleanValue("my-flag", false); -``` - -Returns the flags's enabled state. This is the most common use case for flags. - -#### String Resolution +The provider reads flags with Reflag SDK v2. Boolean flags resolve with `getBooleanValue()`. Multivariate flags resolve with the method that matches their value's JSON type. ```ts -client.getStringValue("my-flag", "default"); +client.getBooleanValue("huddles", false); // a Boolean flag +client.getStringValue("model", "quick"); // a multivariate flag with string values +client.getNumberValue("max-participants", 10); // ... with number values +client.getObjectValue("huddle-settings", { maxParticipants: 10 }); // ... with object or array values ``` -Returns the flags's remote config key (also known as "variant"). Useful for multi-variate use cases. - -#### Number Resolution - -```ts -client.getNumberValue("my-flag", 0); -``` +All methods: -Not directly supported by Reflag. Use `getObjectValue` instead for numeric configurations. +- return the flag's value with `TARGETING_MATCH`; +- return your default with `DEFAULT` if the flag has no value; +- return your default with `FLAG_NOT_FOUND` if the flag doesn't exist; +- return your default with `TYPE_MISMATCH` if the value has a different JSON type from your default. Pass `null` to `getObjectValue()` to accept any JSON value; +- return your default with `PROVIDER_NOT_READY` before the provider is initialized. -#### Object Resolution +Each evaluation records one check in Reflag. -```ts -// works for any type: -client.getObjectValue("my-flag", { defaultValue: true }); -client.getObjectValue("my-flag", "string-value"); -client.getObjectValue("my-flag", 199); -``` +## Migrating from provider 1.x -Returns the flag's remote config payload with type validation. This is the most flexible method, -allowing for complex configuration objects or simple types. +Version 2 of the provider uses Reflag SDK v2 instead of v1. -The object resolution performs runtime type checking between the default value and the flag payload to ensure type safety. +- `getStringValue()` and `getObjectValue()` read a multivariate flag's value instead of a Boolean flag's Remote config key and payload. Move Remote config to a multivariate flag first. See [Migrating from Remote config](https://docs.reflag.com/product-handbook/multivariate-flags#migrating-from-remote-config). +- `getNumberValue()` now works for multivariate flags with number values. +- A Boolean read of a multivariate flag returns your default with `TYPE_MISMATCH`. +- The `fallbackFlags` option is removed. See the SDK v2 [migration guide](https://docs.reflag.com/supported-languages/migrating-from-v1). ## Translating Evaluation Context diff --git a/packages/openfeature-node-provider/example/app.ts b/packages/openfeature-node-provider/example/app.ts index ab6fbdaa4..b7bcef6ff 100644 --- a/packages/openfeature-node-provider/example/app.ts +++ b/packages/openfeature-node-provider/example/app.ts @@ -79,10 +79,10 @@ app.post("/todos", async (req, res) => { // Check if the user has the "create-todos" feature enabled. if (isEnabled) { - // Get the configuration for the "create-todos" feature. - // We expect the configuration to be a JSON object with a `maxLength` property. + // Read the settings from the "todo-settings" multivariate flag. + // Each variant's value is a JSON object with a `maxLength` property. const config = await ofClient.getObjectValue( - "create-todos", + "todo-settings", { maxLength: 100 }, res.locals.context, ); diff --git a/packages/openfeature-node-provider/example/reflag.ts b/packages/openfeature-node-provider/example/reflag.ts index cbb17047e..74bc860bf 100644 --- a/packages/openfeature-node-provider/example/reflag.ts +++ b/packages/openfeature-node-provider/example/reflag.ts @@ -12,20 +12,6 @@ export type CreateTodosConfig = { const provider = new ReflagNodeProvider({ secretKey: process.env.REFLAG_SECRET_KEY!, - fallbackFlags: { - "show-todos": { - isEnabled: true, - }, - "create-todos": { - isEnabled: true, - config: { - key: "default", - payload: { - maxLength: 100, - }, - }, - }, - }, logger: console, }); diff --git a/packages/openfeature-node-provider/package.json b/packages/openfeature-node-provider/package.json index 06c492a73..cf39adfa8 100644 --- a/packages/openfeature-node-provider/package.json +++ b/packages/openfeature-node-provider/package.json @@ -37,7 +37,7 @@ "fmt:fix": "oxfmt --write ." }, "dependencies": { - "@reflag/node-sdk-v1": "npm:@reflag/node-sdk@1.8.3" + "@reflag/node-sdk": "^2.0.0-next.3" }, "devDependencies": { "@babel/core": "~7.29.6", diff --git a/packages/openfeature-node-provider/src/index.test.ts b/packages/openfeature-node-provider/src/index.test.ts index 8d2607976..4f8dc45b3 100644 --- a/packages/openfeature-node-provider/src/index.test.ts +++ b/packages/openfeature-node-provider/src/index.test.ts @@ -1,12 +1,12 @@ import { ProviderStatus } from "@openfeature/server-sdk"; import { beforeEach, describe, expect, it, Mock, vi } from "vitest"; -import { ReflagClient } from "@reflag/node-sdk-v1"; +import { ReflagClient } from "@reflag/node-sdk"; import { defaultContextTranslator, ReflagNodeProvider } from "./index"; -vi.mock("@reflag/node-sdk-v1", () => { - const actualModule = vi.importActual("@reflag/node-sdk-v1"); +vi.mock("@reflag/node-sdk", () => { + const actualModule = vi.importActual("@reflag/node-sdk"); return { __esModule: true, @@ -17,7 +17,8 @@ vi.mock("@reflag/node-sdk-v1", () => { const reflagClientMock = { getFlag: vi.fn(), - getFlagDefinitions: vi.fn().mockReturnValue([]), + getFlags: vi.fn().mockReturnValue({}), + getFlagDefinitions: vi.fn().mockReturnValue({ flags: [], segments: [] }), initialize: vi.fn().mockResolvedValue({}), flush: vi.fn(), track: vi.fn(), @@ -56,31 +57,23 @@ describe("ReflagNodeProvider", () => { let mockTranslatorFn: Mock; - function mockFlag( - enabled: boolean, - configKey?: string | null, - configPayload?: any, - flagKey = testFlagKey, - ) { - const config = { - key: configKey, - payload: configPayload, - }; - - reflagClientMock.getFlag = vi.fn().mockReturnValue({ - isEnabled: enabled, - config, - }); - - // Mock getFlagDefinitions to return feature definitions that include the specified flag - reflagClientMock.getFlagDefinitions = vi.fn().mockReturnValue([ - { - key: flagKey, - description: "Test flag", - flag: {}, - config: {}, + function mockFlag(value: unknown, flagKey = testFlagKey) { + reflagClientMock.getFlagDefinitions = vi + .fn() + .mockReturnValue({ flags: [{ key: flagKey }], segments: [] }); + reflagClientMock.getFlags = vi.fn().mockReturnValue({ [flagKey]: value }); + // Mirrors the SDK: the default is used when the flag has no value or a + // different JSON type. A null default accepts any value. + reflagClientMock.getFlag = vi.fn( + (_context: unknown, _key: string, def: unknown) => { + const type = (v: unknown) => + v === null ? "null" : Array.isArray(v) ? "array" : typeof v; + return value === undefined || + (def !== null && type(value) !== type(def)) + ? def + : value; }, - ]); + ); } beforeEach(async () => { @@ -192,6 +185,7 @@ describe("ReflagNodeProvider", () => { expect(reflagClientMock.getFlag).toHaveBeenCalledWith( reflagContext, testFlagKey, + false, ); }); }); @@ -221,7 +215,7 @@ describe("ReflagNodeProvider", () => { }); it("returns error if flag is not found", async () => { - mockFlag(true, "key", true); + mockFlag(true); const val = await provider.resolveBooleanEvaluation( "missing-key", true, @@ -233,52 +227,27 @@ describe("ReflagNodeProvider", () => { errorCode: "FLAG_NOT_FOUND", value: true, }); + expect(reflagClientMock.getFlag).not.toHaveBeenCalled(); }); - it("calls the client correctly when evaluating", async () => { - mockFlag(true, "key", true); + it("records exactly one check per evaluation", async () => { + mockFlag(true); - const val = await provider.resolveBooleanEvaluation( - testFlagKey, - false, - context, - ); + expect( + await provider.resolveBooleanEvaluation(testFlagKey, false, context), + ).toMatchObject({ reason: "TARGETING_MATCH", value: true }); - expect(val).toMatchObject({ - reason: "TARGETING_MATCH", - value: true, + expect(reflagClientMock.getFlags).toHaveBeenCalledExactlyOnceWith({ + ...reflagContext, + enableTracking: false, }); - - expect(reflagClientMock.getFlagDefinitions).toHaveBeenCalled(); - expect(reflagClientMock.getFlag).toHaveBeenCalledWith( + expect(reflagClientMock.getFlag).toHaveBeenCalledExactlyOnceWith( reflagContext, testFlagKey, + false, ); }); - it.each([ - [true, false, true, "TARGETING_MATCH", undefined], - [undefined, true, true, "ERROR", "FLAG_NOT_FOUND"], - [undefined, false, false, "ERROR", "FLAG_NOT_FOUND"], - ])( - "should return the correct result when evaluating boolean. enabled: %s, value: %s, default: %s, expected: %s, reason: %s, errorCode: %s`", - async (enabled, def, expected, reason, errorCode) => { - const configKey = enabled !== undefined ? "variant-1" : undefined; - - mockFlag(enabled ?? false, configKey); - const flagKey = enabled ? testFlagKey : "missing-key"; - - expect( - await provider.resolveBooleanEvaluation(flagKey, def, context), - ).toMatchObject({ - reason, - value: expected, - ...(configKey ? { variant: configKey } : {}), - ...(errorCode ? { errorCode } : {}), - }); - }, - ); - it("should return error when context is missing user ID", async () => { mockTranslatorFn.mockReturnValue({ user: {} }); @@ -291,58 +260,124 @@ describe("ReflagNodeProvider", () => { }); }); - it("should return error when evaluating number", async () => { + it("returns the default when the flag has no value", async () => { + mockFlag(undefined); + expect( - await provider.resolveNumberEvaluation(testFlagKey, 1), - ).toMatchObject({ - reason: "ERROR", - errorCode: "GENERAL", - value: 1, - }); + await provider.resolveStringEvaluation(testFlagKey, "default", context), + ).toMatchObject({ reason: "DEFAULT", value: "default" }); + }); + + it("returns the default when the flag has no value and the default is null", async () => { + mockFlag(undefined); + + expect( + await provider.resolveObjectEvaluation(testFlagKey, null, context), + ).toMatchObject({ reason: "DEFAULT", value: null }); }); it.each([ - ["key-1", "default", "key-1", "TARGETING_MATCH"], - [null, "default", "default", "DEFAULT"], - [undefined, "default", "default", "DEFAULT"], - ])( - "should return the correct result when evaluating string. variant: %s, def: %s, expected: %s, reason: %s, errorCode: %s`", - async (variant, def, expected, reason) => { - mockFlag(true, variant, {}); - expect( - await provider.resolveStringEvaluation(testFlagKey, def, context), - ).toMatchObject({ - reason, - value: expected, - ...(variant ? { variant } : {}), - }); - }, - ); + [ + "boolean", + true, + (p: ReflagNodeProvider) => + p.resolveBooleanEvaluation(testFlagKey, false, context), + ], + [ + "string", + "quick", + (p: ReflagNodeProvider) => + p.resolveStringEvaluation(testFlagKey, "default", context), + ], + [ + "number", + 15, + (p: ReflagNodeProvider) => + p.resolveNumberEvaluation(testFlagKey, 10, context), + ], + [ + "object", + { model: "quick" }, + (p: ReflagNodeProvider) => + p.resolveObjectEvaluation(testFlagKey, { model: "default" }, context), + ], + [ + "array", + [1, 2], + (p: ReflagNodeProvider) => + p.resolveObjectEvaluation(testFlagKey, [], context), + ], + [ + "JSON null", + null, + (p: ReflagNodeProvider) => + p.resolveObjectEvaluation(testFlagKey, null, context), + ], + ])("resolves a %s value", async (_type, value, read) => { + mockFlag(value); + + expect(await read(provider)).toMatchObject({ + reason: "TARGETING_MATCH", + value, + }); + }); it.each([ - [{}, { a: 1 }, {}, "TARGETING_MATCH", undefined], - ["string", "default", "string", "TARGETING_MATCH", undefined], - [15, -15, 15, "TARGETING_MATCH", undefined], - [true, false, true, "TARGETING_MATCH", undefined], - [null, { a: 2 }, { a: 2 }, "ERROR", "TYPE_MISMATCH"], - [100, "string", "string", "ERROR", "TYPE_MISMATCH"], - [true, 1337, 1337, "ERROR", "TYPE_MISMATCH"], - ["string", 1337, 1337, "ERROR", "TYPE_MISMATCH"], - [undefined, "default", "default", "ERROR", "TYPE_MISMATCH"], + [ + "boolean read of a string", + "quick", + (p: ReflagNodeProvider) => + p.resolveBooleanEvaluation(testFlagKey, true, context), + true, + ], + [ + "string read of a boolean", + true, + (p: ReflagNodeProvider) => + p.resolveStringEvaluation(testFlagKey, "default", context), + "default", + ], + [ + "number read of a string", + "15", + (p: ReflagNodeProvider) => + p.resolveNumberEvaluation(testFlagKey, 10, context), + 10, + ], + [ + "string read of JSON null", + null, + (p: ReflagNodeProvider) => + p.resolveStringEvaluation(testFlagKey, "default", context), + "default", + ], + [ + "object read of an array", + [1], + (p: ReflagNodeProvider) => + p.resolveObjectEvaluation(testFlagKey, { a: 1 }, context), + { a: 1 }, + ], ])( - "should return the correct result when evaluating object. payload: %s, default: %s, expected: %s, reason: %s, errorCode: %s`", - async (value, def, expected, reason, errorCode) => { - const configKey = value === undefined ? undefined : "config-key"; - mockFlag(true, configKey, value); - expect( - await provider.resolveObjectEvaluation(testFlagKey, def, context), - ).toMatchObject({ - reason, + "returns TYPE_MISMATCH for a %s", + async (_name, value, read, expected) => { + mockFlag(value); + + expect(await read(provider)).toMatchObject({ + reason: "ERROR", + errorCode: "TYPE_MISMATCH", value: expected, - ...(errorCode ? { errorCode, variant: configKey } : {}), }); }, ); + + it("accepts any JSON value with a null object default", async () => { + mockFlag({ a: 1 }); + + expect( + await provider.resolveObjectEvaluation(testFlagKey, null, context), + ).toMatchObject({ reason: "TARGETING_MATCH", value: { a: 1 } }); + }); }); describe("track", () => { diff --git a/packages/openfeature-node-provider/src/index.ts b/packages/openfeature-node-provider/src/index.ts index a677b8ab6..9c18f56d0 100644 --- a/packages/openfeature-node-provider/src/index.ts +++ b/packages/openfeature-node-provider/src/index.ts @@ -15,7 +15,20 @@ import { ClientOptions, Context as ReflagContext, ReflagClient, -} from "@reflag/node-sdk-v1"; +} from "@reflag/node-sdk"; + +function jsonType(value: JsonValue): string { + return value === null + ? "null" + : Array.isArray(value) + ? "array" + : typeof value; +} + +// A `null` default accepts any JSON value, as in the Reflag SDKs. +function isTypeMismatch(value: JsonValue, defaultValue: JsonValue) { + return defaultValue !== null && jsonType(value) !== jsonType(defaultValue); +} type ProviderOptions = ClientOptions & { contextTranslator?: (context: EvaluationContext) => ReflagContext; @@ -77,10 +90,7 @@ export class ReflagNodeProvider implements Provider { private resolveFlag( flagKey: string, defaultValue: T, - context: ReflagContext, - resolveFn: ( - feature: ReturnType, - ) => Promise>, + evaluationContext: EvaluationContext, ): Promise> { if (this.status !== ServerProviderStatus.READY) { return Promise.resolve({ @@ -91,6 +101,7 @@ export class ReflagNodeProvider implements Provider { }); } + const context = this.contextTranslator(evaluationContext); if (!context.user?.id) { return Promise.resolve({ value: defaultValue, @@ -100,16 +111,52 @@ export class ReflagNodeProvider implements Provider { }); } - const featureDefs = this._client.getFlagDefinitions(); - if (featureDefs.some(({ key }) => key === flagKey)) { - return resolveFn(this._client.getFlag(context, flagKey)); + const exists = this._client + .getFlagDefinitions() + .flags.some(({ key }) => key === flagKey); + if (!exists) { + return Promise.resolve({ + value: defaultValue, + reason: StandardResolutionReasons.ERROR, + errorCode: ErrorCode.FLAG_NOT_FOUND, + errorMessage: `Flag ${flagKey} not found`, + }); + } + + // Read the raw value without recording a check. getFlags() keeps a missing + // value (undefined) apart from an explicit JSON null. Then read again with + // the default so the SDK records one check for the value we return. + const raw = ( + this._client.getFlags({ ...context, enableTracking: false }) as Record< + string, + JsonValue | undefined + > + )[flagKey]; + const value = this._client.getFlag( + context, + flagKey, + defaultValue, + ) as unknown as T; + + if (raw === undefined) { + return Promise.resolve({ + value: defaultValue, + reason: StandardResolutionReasons.DEFAULT, + }); + } + + if (isTypeMismatch(raw, defaultValue)) { + return Promise.resolve({ + value: defaultValue, + reason: StandardResolutionReasons.ERROR, + errorCode: ErrorCode.TYPE_MISMATCH, + errorMessage: `Expected a value of type \`${jsonType(defaultValue)}\` but got \`${jsonType(raw)}\`.`, + }); } return Promise.resolve({ - value: defaultValue, - reason: StandardResolutionReasons.ERROR, - errorCode: ErrorCode.FLAG_NOT_FOUND, - errorMessage: `Flag ${flagKey} not found`, + value, + reason: StandardResolutionReasons.TARGETING_MATCH, }); } @@ -118,18 +165,7 @@ export class ReflagNodeProvider implements Provider { defaultValue: boolean, context: EvaluationContext, ): Promise> { - return this.resolveFlag( - flagKey, - defaultValue, - this.contextTranslator(context), - (feature) => { - return Promise.resolve({ - value: feature.isEnabled, - variant: feature.config?.key, - reason: StandardResolutionReasons.TARGETING_MATCH, - }); - }, - ); + return this.resolveFlag(flagKey, defaultValue, context); } resolveStringEvaluation( @@ -137,38 +173,15 @@ export class ReflagNodeProvider implements Provider { defaultValue: string, context: EvaluationContext, ): Promise> { - return this.resolveFlag( - flagKey, - defaultValue, - this.contextTranslator(context), - (feature) => { - if (!feature.config.key) { - return Promise.resolve({ - value: defaultValue, - reason: StandardResolutionReasons.DEFAULT, - }); - } - - return Promise.resolve({ - value: feature.config.key as string, - variant: feature.config.key, - reason: StandardResolutionReasons.TARGETING_MATCH, - }); - }, - ); + return this.resolveFlag(flagKey, defaultValue, context); } resolveNumberEvaluation( - _flagKey: string, + flagKey: string, defaultValue: number, + context: EvaluationContext, ): Promise> { - return Promise.resolve({ - value: defaultValue, - reason: StandardResolutionReasons.ERROR, - errorCode: ErrorCode.GENERAL, - errorMessage: - "Reflag doesn't support this method. Use `resolveObjectEvaluation` instead.", - }); + return this.resolveFlag(flagKey, defaultValue, context); } resolveObjectEvaluation( @@ -176,35 +189,7 @@ export class ReflagNodeProvider implements Provider { defaultValue: T, context: EvaluationContext, ): Promise> { - return this.resolveFlag( - flagKey, - defaultValue, - this.contextTranslator(context), - (feature) => { - const expType = typeof defaultValue; - const payloadType = typeof feature.config.payload; - - if ( - feature.config.payload === undefined || - feature.config.payload === null || - payloadType !== expType - ) { - return Promise.resolve({ - value: defaultValue, - variant: feature.config.key, - reason: StandardResolutionReasons.ERROR, - errorCode: ErrorCode.TYPE_MISMATCH, - errorMessage: `Expected remote config payload of type \`${expType}\` but got \`${payloadType}\`.`, - }); - } - - return Promise.resolve({ - value: feature.config.payload, - variant: feature.config.key, - reason: StandardResolutionReasons.TARGETING_MATCH, - }); - }, - ); + return this.resolveFlag(flagKey, defaultValue, context); } track( diff --git a/packages/react-native-sdk/README.md b/packages/react-native-sdk/README.md index a01ecb5e3..6a3196ea9 100644 --- a/packages/react-native-sdk/README.md +++ b/packages/react-native-sdk/README.md @@ -208,6 +208,4 @@ See the [Expo](https://github.com/reflagcom/javascript/tree/main/packages/react- - If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time. - If you generate type declarations, regenerate them with the v2 CLI. -The OpenFeature providers still use SDK v1. - See the [migration guide](https://docs.reflag.com/supported-languages/migrating-from-v1) for the full steps. diff --git a/yarn.lock b/yarn.lock index 2f93a2802..fc829ca09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6050,18 +6050,6 @@ __metadata: languageName: node linkType: hard -"@reflag/browser-sdk@npm:1.7.4": - version: 1.7.4 - resolution: "@reflag/browser-sdk@npm:1.7.4" - dependencies: - "@floating-ui/dom": "npm:^1.6.8" - fast-equals: "npm:^5.2.2" - js-cookie: "npm:^3.0.5" - preact: "npm:^10.22.1" - checksum: 10c0/11347fc0bae8e77f9e2207b520ae3e227fb88d050ff23f94614c6b5d5886c731c17634844a653fb6827e47e074a1651a9bd5f92e1f2e5704e29f0b259589d55b - languageName: node - linkType: hard - "@reflag/browser-sdk@npm:^2.0.0-next.1, @reflag/browser-sdk@workspace:packages/browser-sdk": version: 0.0.0-use.local resolution: "@reflag/browser-sdk@workspace:packages/browser-sdk" @@ -6174,19 +6162,7 @@ __metadata: languageName: unknown linkType: soft -"@reflag/node-sdk-v1@npm:@reflag/node-sdk@1.8.3": - version: 1.8.3 - resolution: "@reflag/node-sdk@npm:1.8.3" - dependencies: - "@aws-sdk/client-s3": "npm:^3.888.0" - "@googleapis/storage": "npm:21.2.0" - "@redis/client": "npm:^5.11.0" - "@reflag/flag-evaluation-v1": "npm:@reflag/flag-evaluation@1.1.2" - checksum: 10c0/9bf81b4186de1a4bfbcbc0123873bb6aef02dcf1b996e905ab92905168cfdc5e85e89033dcd5f4ab87358ef131956775359e01bf3e535328c8706e63cf948f4b - languageName: node - linkType: hard - -"@reflag/node-sdk@npm:2.0.0-next.3, @reflag/node-sdk@workspace:*, @reflag/node-sdk@workspace:^, @reflag/node-sdk@workspace:packages/node-sdk": +"@reflag/node-sdk@npm:2.0.0-next.3, @reflag/node-sdk@npm:^2.0.0-next.3, @reflag/node-sdk@workspace:*, @reflag/node-sdk@workspace:^, @reflag/node-sdk@workspace:packages/node-sdk": version: 0.0.0-use.local resolution: "@reflag/node-sdk@workspace:packages/node-sdk" dependencies: @@ -6214,7 +6190,7 @@ __metadata: dependencies: "@openfeature/core": "npm:1.5.0" "@openfeature/web-sdk": "npm:^1.3.0" - "@reflag/browser-sdk": "npm:1.7.4" + "@reflag/browser-sdk": "npm:^2.0.0-next.1" "@reflag/tsconfig": "npm:0.0.2" "@types/node": "npm:^22.12.0" jsdom: "npm:^24.1.0" @@ -6234,7 +6210,7 @@ __metadata: "@babel/core": "npm:~7.29.6" "@openfeature/core": "npm:^1.5.0" "@openfeature/server-sdk": "npm:>=1.16.1" - "@reflag/node-sdk-v1": "npm:@reflag/node-sdk@1.8.3" + "@reflag/node-sdk": "npm:^2.0.0-next.3" "@reflag/tsconfig": "npm:~0.0.2" "@types/node": "npm:^22.12.0" flush-promises: "npm:~1.0.2"