Skip to content
5 changes: 3 additions & 2 deletions .github/workflows/sdk-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ permissions:
jobs:
sdk-spec:
# Keep the ref in `uses:` and `spec-ref` in sync.
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.3.0
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.9.0
with:
spec-ref: v0.3.0
spec-ref: v0.9.0
tags: core events local-evaluation
language: nodejs
87 changes: 86 additions & 1 deletion sdk-spec/steps.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Each function implements the Gherkin step in the comment above it.
*/

import { Flagsmith, type Flags, type FlagsmithValue } from '@flagsmith/nodejs';
import { Flag, Flagsmith, type Flags, type FlagsmithValue } from '@flagsmith/nodejs';

function asSpecFlags(flags: Flags) {
return flags.allFlags().map(flag => ({
Expand All @@ -24,6 +24,35 @@ export function createClientWithLocalEvaluation(environmentKey: string, apiUrl:
return new Flagsmith({ environmentKey, apiUrl, enableLocalEvaluation: true });
}

// Create the SDK client with events enabled
export function createClientWithEvents(
environmentKey: string,
apiUrl: string,
eventsApiUrl: string
) {
return new Flagsmith({
environmentKey,
apiUrl,
enableEvents: true,
eventProcessorConfig: { eventsApiUrl }
});
}

// Create the SDK client with local evaluation and events enabled
export function createClientWithLocalEvaluationAndEvents(
environmentKey: string,
apiUrl: string,
eventsApiUrl: string
) {
return new Flagsmith({
environmentKey,
apiUrl,
enableLocalEvaluation: true,
enableEvents: true,
eventProcessorConfig: { eventsApiUrl }
});
}

// When the application gets the environment flags
export async function getEnvironmentFlags(flagsmith: Flagsmith) {
return asSpecFlags(await flagsmith.getEnvironmentFlags());
Expand All @@ -37,3 +66,59 @@ export async function getIdentityFlags(
) {
return asSpecFlags(await flagsmith.getIdentityFlags(identifier, traits));
}

// When the application gets the experiment flag "{feature}" for identity "{identifier}" with traits:
export async function getExperimentFlag(
flagsmith: Flagsmith,
feature: string,
identifier: string,
traits: Record<string, FlagsmithValue>
) {
const flag = await flagsmith.getExperimentFlag(feature, identifier, traits);
const featureName = flag instanceof Flag ? flag.featureName : feature; // A default flag has no name of its own
return [{ feature: featureName, enabled: flag.enabled, value: flag.value }];
}

// When the application tracks event "{event}" for identity "{identifier}" with value {value} and metadata:
export function trackEvent(
flagsmith: Flagsmith,
event: string,
identifier: string,
value: FlagsmithValue,
metadata: Record<string, unknown>
) {
flagsmith.trackEvent(event, { identifier, value, metadata });
}

// When the application tracks an exposure of feature "{feature}" for identity "{identifier}" with value {value} and metadata:
export function trackExposureEvent(
flagsmith: Flagsmith,
feature: string,
identifier: string,
value: FlagsmithValue,
metadata: Record<string, unknown>
) {
flagsmith.trackExposureEvent(feature, { identifier, value, metadata });
}

// When the application tracks events "event_{first}" to "event_{last}" for identity "{identifier}"
export function trackNumberedEvents(
flagsmith: Flagsmith,
first: number,
last: number,
identifier: string
) {
for (let number = Number(first); number <= Number(last); number++) {
flagsmith.trackEvent(`event_${number}`, { identifier });
}
}

// When the application flushes events
export async function flushEvents(flagsmith: Flagsmith) {
await flagsmith.flushEvents();
}

// When the application closes the client with a timeout of {timeout} seconds
export async function close(flagsmith: Flagsmith, timeout: number) {
await flagsmith.close(timeout * 1000);
}
Loading
Loading