Skip to content

Commit e61f34e

Browse files
authored
feat: Export BaseFlag, FlagsmithConfig, FlagsmithValue, TraitConfig types (#188)
* Export BaseFlag, FlagsmithConfig, FlagsmithValue, TraitConfig types * tsdoc
1 parent 6e391ed commit e61f34e

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ export {
55
FlagsmithClientError,
66
EnvironmentDataPollingManager,
77
FlagsmithCache,
8+
BaseFlag,
89
DefaultFlag,
910
Flags,
1011
Flagsmith
1112
} from './sdk/index.js';
1213

1314
export { BaseOfflineHandler, LocalFileHandler } from './sdk/offline_handlers.js';
1415

15-
export { FlagsmithConfig } from './sdk/types.js';
16+
export { FlagsmithConfig, FlagsmithValue, TraitConfig } from './sdk/types.js';
1617

1718
export {
1819
EnvironmentModel,

sdk/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import {
2222
FlagsmithCache,
2323
FlagsmithConfig,
2424
FlagsmithTraitValue,
25-
ITraitConfig
25+
TraitConfig
2626
} from './types.js';
2727
import { pino, Logger } from 'pino';
2828

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

32-
export { DefaultFlag, Flags } from './models.js';
32+
export { BaseFlag, DefaultFlag, Flags } from './models.js';
3333
export { EnvironmentDataPollingManager } from './polling_manager.js';
3434
export { FlagsmithCache, FlagsmithConfig } from './types.js';
3535

@@ -206,13 +206,13 @@ export class Flagsmith {
206206
*
207207
* @param {string} identifier a unique identifier for the identity in the current
208208
environment, e.g. email address, username, uuid
209-
* @param {{[key:string]:any | ITraitConfig}} traits? a dictionary of traits to add / update on the identity in
209+
* @param {{[key:string]:any | TraitConfig}} traits? a dictionary of traits to add / update on the identity in
210210
Flagsmith, e.g. {"num_orders": 10} or {age: {value: 30, transient: true}}
211211
* @returns Flags object holding all the flags for the given identity.
212212
*/
213213
async getIdentityFlags(
214214
identifier: string,
215-
traits?: { [key: string]: FlagsmithTraitValue | ITraitConfig },
215+
traits?: { [key: string]: FlagsmithTraitValue | TraitConfig },
216216
transient: boolean = false
217217
): Promise<Flags> {
218218
if (!identifier) {
@@ -456,7 +456,7 @@ export class Flagsmith {
456456

457457
private async getIdentityFlagsFromApi(
458458
identifier: string,
459-
traits: { [key: string]: FlagsmithTraitValue | ITraitConfig },
459+
traits: { [key: string]: FlagsmithTraitValue | TraitConfig },
460460
transient: boolean = false
461461
) {
462462
if (!this.identitiesUrl) {

sdk/models.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ import { AnalyticsProcessor } from './analytics.js';
33

44
type FlagValue = string | number | boolean | undefined;
55

6+
/**
7+
* A Flagsmith feature. It has an enabled/disabled state, and an optional {@link FlagValue}.
8+
*/
69
export class BaseFlag {
10+
/**
11+
* Indicates whether this feature is enabled.
12+
*/
713
enabled: boolean;
14+
/**
15+
* An optional {@link FlagValue} for this feature.
16+
*/
817
value: FlagValue;
18+
/**
19+
* If true, the state for this feature was determined by a default flag handler. See {@link DefaultFlag}.
20+
*/
921
isDefault: boolean;
1022

1123
constructor(value: FlagValue, enabled: boolean, isDefault: boolean) {
@@ -15,14 +27,27 @@ export class BaseFlag {
1527
}
1628
}
1729

30+
/**
31+
* A {@link BaseFlag} returned by a default flag handler when flag evaluation fails.
32+
* @see FlagsmithConfig#defaultFlagHandler
33+
*/
1834
export class DefaultFlag extends BaseFlag {
1935
constructor(value: FlagValue, enabled: boolean) {
2036
super(value, enabled, true);
2137
}
2238
}
2339

40+
/**
41+
* A Flagsmith feature retrieved from a successful flag evaluation.
42+
*/
2443
export class Flag extends BaseFlag {
44+
/**
45+
* An identifier for this feature, unique in a single Flagsmith installation.
46+
*/
2547
featureId: number;
48+
/**
49+
* The programmatic name for this feature, unique per Flagsmith project.
50+
*/
2651
featureName: string;
2752

2853
constructor(params: {

sdk/types.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { Logger } from 'pino';
55
import { BaseOfflineHandler } from './offline_handlers.js';
66
import { Flagsmith } from './index.js';
77

8-
export type IFlagsmithValue<T = string | number | boolean | null> = T;
8+
/**
9+
* A concrete type for the possible values of a feature.
10+
*/
11+
export type FlagsmithValue<T = string | number | boolean | null> = T;
912

1013
/**
1114
* Stores and retrieves {@link Flags} from a cache.
@@ -95,7 +98,7 @@ export interface FlagsmithConfig {
9598
* const defaultHandler = () => new DefaultFlag(undefined, false)
9699
*
97100
* // Enable only VIP flags by default
98-
* const vipDefaultHandler = (key: string) => new Default(undefined, key.startsWith('vip_'))
101+
* const vipDefaultHandler = (key: string) => new DefaultFlag(undefined, key.startsWith('vip_'))
99102
*/
100103
defaultFlagHandler?: (flagKey: string) => DefaultFlag;
101104
cache?: FlagsmithCache;
@@ -116,9 +119,16 @@ export interface FlagsmithConfig {
116119
offlineHandler?: BaseOfflineHandler;
117120
}
118121

119-
export interface ITraitConfig {
122+
/**
123+
* Represents the configuration for a trait in Flagsmith.
124+
*
125+
* @property value The {@link FlagsmithTraitValue} for this trait.
126+
* @property [transient] Indicates whether the trait should be persisted when used in a remote flag evaluation context.
127+
* Defaults to false.
128+
*/
129+
export interface TraitConfig {
120130
value: FlagsmithTraitValue;
121131
transient?: boolean;
122132
}
123133

124-
export declare type FlagsmithTraitValue = IFlagsmithValue;
134+
export declare type FlagsmithTraitValue = FlagsmithValue;

sdk/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Fetch, FlagsmithTraitValue, ITraitConfig } from './types.js';
1+
import { Fetch, FlagsmithTraitValue, TraitConfig } from './types.js';
22
import { Dispatcher } from 'undici-types';
33

4-
type Traits = { [key: string]: ITraitConfig | FlagsmithTraitValue };
4+
type Traits = { [key: string]: TraitConfig | FlagsmithTraitValue };
55

66
export function isTraitConfig(
7-
traitValue: ITraitConfig | FlagsmithTraitValue
8-
): traitValue is ITraitConfig {
7+
traitValue: TraitConfig | FlagsmithTraitValue
8+
): traitValue is TraitConfig {
99
return !!traitValue && typeof traitValue == 'object' && traitValue.value !== undefined;
1010
}
1111

0 commit comments

Comments
 (0)