@splitsoftware/openfeature-web-split-provider 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.txt +13 -2
- package/README.md +190 -16
- package/es/lib/context.js +55 -0
- package/es/lib/evaluation.js +21 -0
- package/es/lib/events.js +69 -0
- package/es/lib/js-split-provider.js +80 -142
- package/es/lib/parsers.js +38 -0
- package/es/lib/types.js +2 -0
- package/lib/lib/context.js +61 -0
- package/lib/lib/evaluation.js +25 -0
- package/lib/lib/events.js +74 -0
- package/lib/lib/js-split-provider.js +78 -140
- package/lib/lib/parsers.js +44 -0
- package/lib/lib/types.js +5 -0
- package/package.json +2 -2
- package/src/__tests__/bucketingKey.spec.js +145 -0
- package/src/__tests__/context.spec.js +146 -0
- package/src/__tests__/evaluation.spec.js +76 -0
- package/src/__tests__/events.spec.js +182 -0
- package/src/__tests__/integration/bucketing-key.test.js +81 -0
- package/src/__tests__/integration/e2e-events.test.js +1 -8
- package/src/__tests__/integration/openfeature-evaluation.test.js +162 -0
- package/src/__tests__/parsers.spec.js +115 -0
- package/src/__tests__/provider.spec.js +59 -3
- package/src/__tests__/types.spec.js +15 -0
- package/src/lib/context.ts +62 -0
- package/src/lib/evaluation.ts +33 -0
- package/src/lib/events.ts +105 -0
- package/src/lib/js-split-provider.ts +103 -180
- package/src/lib/parsers.ts +43 -0
- package/src/lib/types.ts +12 -0
- package/types/lib/context.d.ts +15 -0
- package/types/lib/evaluation.d.ts +7 -0
- package/types/lib/events.d.ts +20 -0
- package/types/lib/js-split-provider.d.ts +11 -7
- package/types/lib/parsers.d.ts +8 -0
- package/types/lib/types.d.ts +10 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ParseError } from "@openfeature/web-sdk";
|
|
2
|
+
import type { JsonValue } from "@openfeature/web-sdk";
|
|
3
|
+
|
|
4
|
+
export function parseValidNumber(stringValue: string | undefined): number {
|
|
5
|
+
if (stringValue === undefined) {
|
|
6
|
+
throw new ParseError(`Invalid 'undefined' value.`);
|
|
7
|
+
}
|
|
8
|
+
const result = Number.parseFloat(stringValue);
|
|
9
|
+
if (Number.isNaN(result)) {
|
|
10
|
+
throw new ParseError(`Invalid numeric value ${stringValue}`);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function parseValidJsonObject<T extends JsonValue>(
|
|
16
|
+
stringValue: string | undefined
|
|
17
|
+
): T {
|
|
18
|
+
if (stringValue === undefined) {
|
|
19
|
+
throw new ParseError(`Invalid 'undefined' JSON value.`);
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const value = JSON.parse(stringValue);
|
|
23
|
+
if (typeof value !== "object") {
|
|
24
|
+
throw new ParseError(
|
|
25
|
+
`Flag value ${stringValue} had unexpected type ${typeof value}, expected "object"`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
} catch (err) {
|
|
30
|
+
throw new ParseError(`Error parsing ${stringValue} as JSON, ${err}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Map a string treatment to a boolean for boolean flag resolution.
|
|
36
|
+
* @throws ParseError if treatment is not a valid boolean representation
|
|
37
|
+
*/
|
|
38
|
+
export function parseBooleanTreatment(treatment: string): boolean {
|
|
39
|
+
const lower = treatment.toLowerCase();
|
|
40
|
+
if (lower === 'on' || lower === 'true') return true;
|
|
41
|
+
if (lower === 'off' || lower === 'false') return false;
|
|
42
|
+
throw new ParseError(`Invalid boolean value for ${treatment}`);
|
|
43
|
+
}
|
package/src/lib/types.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
2
|
+
|
|
3
|
+
export type Consumer = {
|
|
4
|
+
targetingKey: string | undefined;
|
|
5
|
+
/** Trimmed bucketing key, or undefined when missing or unusable (blank, not a string, > 250 chars). */
|
|
6
|
+
bucketingKey: string | undefined;
|
|
7
|
+
trafficType: string;
|
|
8
|
+
attributes: SplitIO.Attributes;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const CONTROL_TREATMENT = 'control';
|
|
12
|
+
export const CONTROL_VALUE_ERROR_MESSAGE = "Received the 'control' value from Split.";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { EvaluationContext } from "@openfeature/web-sdk";
|
|
2
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
3
|
+
import type { Consumer } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Warning for a bucketingKey in the context that will not be used, or undefined when there is nothing to report.
|
|
6
|
+
* Names the reason only, never the value.
|
|
7
|
+
*/
|
|
8
|
+
export declare function bucketingKeyWarning(context: EvaluationContext): string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Transform OpenFeature evaluation context into a consumer object for the Split API.
|
|
11
|
+
* `bucketingKey` is reserved: it is never forwarded as an attribute.
|
|
12
|
+
*/
|
|
13
|
+
export declare function transformContext(context: EvaluationContext, defaultTrafficType: string): Consumer;
|
|
14
|
+
/** Split key for a consumer: `{ matchingKey, bucketingKey }` or the targeting key alone. */
|
|
15
|
+
export declare function toSplitKey({ targetingKey, bucketingKey }: Consumer): SplitIO.SplitKey | undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ResolutionDetails } from "@openfeature/web-sdk";
|
|
2
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
3
|
+
import type { Consumer } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Evaluate a flag with the Split client and return string resolution details.
|
|
6
|
+
*/
|
|
7
|
+
export declare function evaluateTreatment(client: SplitIO.IBrowserClient, flagKey: string, consumer: Consumer): ResolutionDetails<string>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { OpenFeatureEventEmitter } from "@openfeature/web-sdk";
|
|
2
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
3
|
+
/**
|
|
4
|
+
* Split SDK emits SDK_READY with SdkReadyMetadata:
|
|
5
|
+
* - initialCacheLoad: boolean — true when fresh install / no cache, false when ready from cache
|
|
6
|
+
* - lastUpdateTimestamp?: number — ms since epoch when cache was last updated (undefined if initialCacheLoad is true)
|
|
7
|
+
*/
|
|
8
|
+
export type ReadyCallbacks = {
|
|
9
|
+
onSdkReady: () => void;
|
|
10
|
+
onSdkTimedOut: () => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Attach Split SDK event listeners and resolve/reject the ready promise based on client status.
|
|
14
|
+
* When the Split SDK becomes ready, emits ProviderEvents.Ready with Split ready metadata before resolving.
|
|
15
|
+
*/
|
|
16
|
+
export declare function attachSplitReadyHandlers(client: SplitIO.IBrowserClient, eventsEmitter: OpenFeatureEventEmitter, callbacks: ReadyCallbacks, providerName: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* Subscribe to Split SDK_UPDATE and emit OpenFeature ConfigurationChanged with details.
|
|
19
|
+
*/
|
|
20
|
+
export declare function attachSplitUpdateHandler(client: SplitIO.IBrowserClient, eventsEmitter: OpenFeatureEventEmitter, providerName: string): void;
|
|
@@ -7,19 +7,23 @@ export declare class OpenFeatureSplitProvider implements Provider {
|
|
|
7
7
|
private client;
|
|
8
8
|
private factory;
|
|
9
9
|
private trafficType;
|
|
10
|
+
/** Readiness of the bound client (rejects if it timed out). */
|
|
11
|
+
private ready;
|
|
10
12
|
events: OpenFeatureEventEmitter;
|
|
11
|
-
onContextChange(oldContext: EvaluationContext, newContext: EvaluationContext): Promise<void>;
|
|
12
13
|
constructor(splitFactory: SplitIO.IBrowserSDK);
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Warn through the Split SDK logger, so it follows the SDK log settings (`debug`, `factory.Logger`).
|
|
16
|
+
* `settings.log` is internal: the public typings only expose `factory.Logger`, which has no `warn`.
|
|
17
|
+
*/
|
|
18
|
+
private warn;
|
|
19
|
+
/** Warn once per context set; evaluations reuse the context and would repeat it. */
|
|
20
|
+
private warnIfBucketingKeyIgnored;
|
|
21
|
+
initialize(context?: EvaluationContext): Promise<void>;
|
|
22
|
+
onContextChange(oldContext: EvaluationContext, newContext: EvaluationContext): Promise<void>;
|
|
14
23
|
resolveBooleanEvaluation(flagKey: string, _: boolean, context: EvaluationContext, _logger: Logger): ResolutionDetails<boolean>;
|
|
15
24
|
resolveStringEvaluation(flagKey: string, _: string, context: EvaluationContext, _logger: Logger): ResolutionDetails<string>;
|
|
16
25
|
resolveNumberEvaluation(flagKey: string, _: number, context: EvaluationContext, _logger: Logger): ResolutionDetails<number>;
|
|
17
26
|
resolveObjectEvaluation<U extends JsonValue>(flagKey: string, _: U, context: EvaluationContext, _logger: Logger): ResolutionDetails<U>;
|
|
18
|
-
private evaluateTreatment;
|
|
19
27
|
track(trackingEventName: string, context: EvaluationContext, details: TrackingEventDetails): void;
|
|
20
28
|
onClose?(): Promise<void>;
|
|
21
|
-
private transformContext;
|
|
22
|
-
private parseValidNumber;
|
|
23
|
-
private parseValidJsonObject;
|
|
24
|
-
private eventsHandler;
|
|
25
29
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { JsonValue } from "@openfeature/web-sdk";
|
|
2
|
+
export declare function parseValidNumber(stringValue: string | undefined): number;
|
|
3
|
+
export declare function parseValidJsonObject<T extends JsonValue>(stringValue: string | undefined): T;
|
|
4
|
+
/**
|
|
5
|
+
* Map a string treatment to a boolean for boolean flag resolution.
|
|
6
|
+
* @throws ParseError if treatment is not a valid boolean representation
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseBooleanTreatment(treatment: string): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
2
|
+
export type Consumer = {
|
|
3
|
+
targetingKey: string | undefined;
|
|
4
|
+
/** Trimmed bucketing key, or undefined when missing or unusable (blank, not a string, > 250 chars). */
|
|
5
|
+
bucketingKey: string | undefined;
|
|
6
|
+
trafficType: string;
|
|
7
|
+
attributes: SplitIO.Attributes;
|
|
8
|
+
};
|
|
9
|
+
export declare const CONTROL_TREATMENT = "control";
|
|
10
|
+
export declare const CONTROL_VALUE_ERROR_MESSAGE = "Received the 'control' value from Split.";
|