diff --git a/CHANGES.txt b/CHANGES.txt index 8d31f0a..7f32a73 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -15,6 +15,7 @@ - `useSplitTreatments` optimizes feature flag evaluations by using the `useMemo` hook to memoize `getTreatmentsWithConfig` method calls from the SDK. This avoids re-evaluating feature flags when the hook is called with the same options and the feature flag definitions have not changed. - They fixed a bug in the deprecated `useClient` and `useTreatments` hooks, which caused them to not re-render and re-evaluate feature flags when they access a different SDK client than the context and its status updates (i.e., when it emits SDK_READY or other event). - Added TypeScript types and interfaces to the library index exports, allowing them to be imported from the library index, e.g., `import type { ISplitFactoryProps } from '@splitsoftware/splitio-react'` (Related to issue https://github.com/splitio/react-client/issues/162). + - Added `defaultTreatment` property to the `SplitView` object returned by the `split` and `splits` methods of the SDK manager (Related to issue https://github.com/splitio/javascript-commons/issues/225). - Updated type declarations of the library components to not restrict the type of the `children` prop to ReactElement, allowing to pass any valid ReactNode value (Related to issue https://github.com/splitio/react-client/issues/164). - Updated linter and other dependencies for vulnerability fixes. - Bugfixing - Removed conditional code within hooks to adhere to the rules of hooks and prevent React warnings. Previously, this code checked for the availability of the hooks API (available in React version 16.8.0 or above) and logged an error message. Now, using hooks with React versions below 16.8.0 will throw an error. diff --git a/src/__tests__/constants.test.ts b/src/__tests__/utils.test.ts similarity index 86% rename from src/__tests__/constants.test.ts rename to src/__tests__/utils.test.ts index c51913a..b5ef021 100644 --- a/src/__tests__/constants.test.ts +++ b/src/__tests__/utils.test.ts @@ -1,4 +1,5 @@ -import { getControlTreatmentsWithConfig, CONTROL_WITH_CONFIG } from '../constants'; +import { CONTROL_WITH_CONFIG } from '../constants'; +import { getControlTreatmentsWithConfig } from '../utils'; import SplitIO from '@splitsoftware/splitio/types/splitio'; describe('getControlTreatmentsWithConfig', () => { diff --git a/src/__tests__/withSplitTreatments.test.tsx b/src/__tests__/withSplitTreatments.test.tsx index d1e665f..f5d523b 100644 --- a/src/__tests__/withSplitTreatments.test.tsx +++ b/src/__tests__/withSplitTreatments.test.tsx @@ -12,7 +12,7 @@ import { sdkBrowser } from './testUtils/sdkConfigs'; import { withSplitFactory } from '../withSplitFactory'; import { withSplitClient } from '../withSplitClient'; import { withSplitTreatments } from '../withSplitTreatments'; -import { getControlTreatmentsWithConfig } from '../constants'; +import { getControlTreatmentsWithConfig } from '../utils'; describe('withSplitTreatments', () => { diff --git a/src/constants.ts b/src/constants.ts index 47020ef..7d81db7 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,3 @@ -import { validateFeatureFlags } from './utils'; - // The string below is a marker and will be replaced by the real version number. DO NOT CHANGE export const VERSION: string = 'react-' + 'REACT_SDK_VERSION_NUMBER'; @@ -15,20 +13,6 @@ export const CONTROL_WITH_CONFIG: SplitIO.TreatmentWithConfig = { config: null, }; -export const getControlTreatmentsWithConfig = (featureFlagNames: unknown): SplitIO.TreatmentsWithConfig => { - // validate featureFlags Names - const validatedFeatureFlagNames = validateFeatureFlags(featureFlagNames); - - // return empty object if the returned value is false - if (!validatedFeatureFlagNames) return {}; - - // return control treatments for each validated feature flag name - return validatedFeatureFlagNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => { - pValue[cValue] = CONTROL_WITH_CONFIG; - return pValue; - }, {}); -}; - // Warning and error messages export const WARN_SF_CONFIG_AND_FACTORY: string = '[WARN] Both a config and factory props were provided to SplitFactory. Config prop will be ignored.'; diff --git a/src/utils.ts b/src/utils.ts index ee42523..760077a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import memoizeOne from 'memoize-one'; import shallowEqual from 'shallowequal'; import { SplitFactory as SplitSdk } from '@splitsoftware/splitio/client'; -import { VERSION, WARN_NAMES_AND_FLAGSETS, getControlTreatmentsWithConfig } from './constants'; +import { CONTROL_WITH_CONFIG, VERSION, WARN_NAMES_AND_FLAGSETS } from './constants'; import { ISplitStatus } from './types'; // Utils used to access singleton instances of Split factories and clients, and to gracefully shutdown all clients together. @@ -97,9 +97,16 @@ export function getStatus(client: SplitIO.IBrowserClient | null): ISplitStatus { }; } +/** + * Manage client attributes binding + */ +export function initAttributes(client: SplitIO.IBrowserClient | null, attributes?: SplitIO.Attributes) { + if (client && attributes) client.setAttributes(attributes); +} + // Input validation utils that will be replaced eventually -export function validateFeatureFlags(maybeFeatureFlags: unknown, listName = 'feature flag names'): false | string[] { +function validateFeatureFlags(maybeFeatureFlags: unknown, listName = 'feature flag names'): false | string[] { if (Array.isArray(maybeFeatureFlags) && maybeFeatureFlags.length > 0) { const validatedArray: string[] = []; // Remove invalid values @@ -116,13 +123,6 @@ export function validateFeatureFlags(maybeFeatureFlags: unknown, listName = 'fea return false; } -/** - * Manage client attributes binding - */ -export function initAttributes(client: SplitIO.IBrowserClient | null, attributes?: SplitIO.Attributes) { - if (client && attributes) client.setAttributes(attributes); -} - const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/; function validateFeatureFlag(maybeFeatureFlag: unknown, item = 'feature flag name'): false | string { @@ -146,6 +146,20 @@ function validateFeatureFlag(maybeFeatureFlag: unknown, item = 'feature flag nam return false; } +export function getControlTreatmentsWithConfig(featureFlagNames: unknown): SplitIO.TreatmentsWithConfig { + // validate featureFlags Names + const validatedFeatureFlagNames = validateFeatureFlags(featureFlagNames); + + // return empty object if the returned value is false + if (!validatedFeatureFlagNames) return {}; + + // return control treatments for each validated feature flag name + return validatedFeatureFlagNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => { + pValue[cValue] = CONTROL_WITH_CONFIG; + return pValue; + }, {}); +} + /** * Removes duplicate items on an array of strings. */