Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
FlagsmithClientError,
EnvironmentDataPollingManager,
FlagsmithCache,
BaseFlag,
DefaultFlag,
Flags,
Flagsmith,
Expand All @@ -16,8 +17,10 @@ export {
} from './sdk/offline_handlers.js';

export {
FlagsmithConfig
} from './sdk/types.js'
FlagsmithConfig,
FlagsmithValue,
TraitConfig,
} from './sdk/types.js';

export {
EnvironmentModel,
Expand Down
10 changes: 5 additions & 5 deletions sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import { EnvironmentDataPollingManager } from './polling_manager.js';
import { Deferred, generateIdentitiesData, retryFetch } from './utils.js';
import { SegmentModel } from '../flagsmith-engine/index.js';
import { getIdentitySegments } from '../flagsmith-engine/segments/evaluators.js';
import { Fetch, FlagsmithCache, FlagsmithConfig, FlagsmithTraitValue, ITraitConfig } from './types.js';
import { Fetch, FlagsmithCache, FlagsmithConfig, FlagsmithTraitValue, TraitConfig } from './types.js';
import { pino, Logger } from 'pino';

export { AnalyticsProcessor, AnalyticsProcessorOptions } from './analytics.js';
export { FlagsmithAPIError, FlagsmithClientError } from './errors.js';

export { DefaultFlag, Flags } from './models.js';
export { BaseFlag, DefaultFlag, Flags } from './models.js';
export { EnvironmentDataPollingManager } from './polling_manager.js';
export { FlagsmithCache, FlagsmithConfig } from './types.js';

Expand Down Expand Up @@ -191,13 +191,13 @@ export class Flagsmith {
*
* @param {string} identifier a unique identifier for the identity in the current
environment, e.g. email address, username, uuid
* @param {{[key:string]:any | ITraitConfig}} traits? a dictionary of traits to add / update on the identity in
* @param {{[key:string]:any | TraitConfig}} traits? a dictionary of traits to add / update on the identity in
Flagsmith, e.g. {"num_orders": 10} or {age: {value: 30, transient: true}}
* @returns Flags object holding all the flags for the given identity.
*/
async getIdentityFlags(
identifier: string,
traits?: { [key: string]: FlagsmithTraitValue | ITraitConfig },
traits?: { [key: string]: FlagsmithTraitValue | TraitConfig },
transient: boolean = false
): Promise<Flags> {
if (!identifier) {
Expand Down Expand Up @@ -438,7 +438,7 @@ export class Flagsmith {

private async getIdentityFlagsFromApi(
identifier: string,
traits: { [key: string]: FlagsmithTraitValue | ITraitConfig },
traits: { [key: string]: FlagsmithTraitValue | TraitConfig },
transient: boolean = false
) {
if (!this.identitiesUrl) {
Expand Down
25 changes: 25 additions & 0 deletions sdk/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ import { AnalyticsProcessor } from './analytics.js';

type FlagValue = string | number | boolean | undefined;

/**
* A Flagsmith feature. It has an enabled/disabled state, and an optional {@link FlagValue}.
*/
export class BaseFlag {
/**
* Indicates whether this feature is enabled.
*/
enabled: boolean;
/**
* An optional {@link FlagValue} for this feature.
*/
value: FlagValue;
/**
* If true, the state for this feature was determined by a default flag handler. See {@link DefaultFlag}.
*/
isDefault: boolean;

constructor(value: FlagValue, enabled: boolean, isDefault: boolean) {
Expand All @@ -15,14 +27,27 @@ export class BaseFlag {
}
}

/**
* A {@link BaseFlag} returned by a default flag handler when flag evaluation fails.
* @see FlagsmithConfig#defaultFlagHandler
*/
export class DefaultFlag extends BaseFlag {
constructor(value: FlagValue, enabled: boolean) {
super(value, enabled, true);
}
}

/**
* A Flagsmith feature retrieved from a successful flag evaluation.
*/
export class Flag extends BaseFlag {
/**
* An identifier for this feature, unique in a single Flagsmith installation.
*/
featureId: number;
/**
* The programmatic name for this feature, unique per Flagsmith project.
*/
featureName: string;

constructor(params: {
Expand Down
18 changes: 14 additions & 4 deletions sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Logger } from 'pino';
import { BaseOfflineHandler } from './offline_handlers.js';
import { Flagsmith } from './index.js'

export type IFlagsmithValue<T = string | number | boolean | null> = T;
/**
* A concrete type for the possible values of a feature.
*/
export type FlagsmithValue<T = string | number | boolean | null> = T;


/**
Expand Down Expand Up @@ -96,7 +99,7 @@ export interface FlagsmithConfig {
* const defaultHandler = () => new DefaultFlag(undefined, false)
*
* // Enable only VIP flags by default
* const vipDefaultHandler = (key: string) => new Default(undefined, key.startsWith('vip_'))
* const vipDefaultHandler = (key: string) => new DefaultFlag(undefined, key.startsWith('vip_'))
*/
defaultFlagHandler?: (flagKey: string) => DefaultFlag;
cache?: FlagsmithCache;
Expand All @@ -117,9 +120,16 @@ export interface FlagsmithConfig {
offlineHandler?: BaseOfflineHandler;
}

export interface ITraitConfig {
/**
* Represents the configuration for a trait in Flagsmith.
*
* @property value The {@link FlagsmithTraitValue} for this trait.
* @property [transient] Indicates whether the trait should be persisted when used in a remote flag evaluation context.
* Defaults to false.
*/
export interface TraitConfig {
value: FlagsmithTraitValue;
transient?: boolean;
}

export declare type FlagsmithTraitValue = IFlagsmithValue;
export declare type FlagsmithTraitValue = FlagsmithValue;
8 changes: 4 additions & 4 deletions sdk/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Fetch, FlagsmithTraitValue, ITraitConfig} from './types.js';
import {Fetch, FlagsmithTraitValue, TraitConfig} from './types.js';
import {Dispatcher} from "undici-types";

type Traits = { [key: string]: ITraitConfig | FlagsmithTraitValue };
type Traits = { [key: string]: TraitConfig | FlagsmithTraitValue };

export function isTraitConfig(
traitValue: ITraitConfig | FlagsmithTraitValue
): traitValue is ITraitConfig {
traitValue: TraitConfig | FlagsmithTraitValue
): traitValue is TraitConfig {
return !!traitValue && typeof traitValue == 'object' && traitValue.value !== undefined;
}

Expand Down