|
1 | | -export interface IBulletTrainFeature { |
2 | | - enabled: boolean; |
3 | | - value?: string | number | boolean; |
| 1 | +export interface IFlagsmithFeature { |
| 2 | + enabled: boolean |
| 3 | + value?: string|number|boolean |
4 | 4 | } |
5 | | -export interface IFlagsmithFeature extends IBulletTrainFeature { |
6 | | -} |
7 | | -export declare type IFlagsmithTrait = string | number | boolean; |
| 5 | + |
| 6 | +export type IFlagsmithTrait = string|number|boolean |
| 7 | + |
| 8 | +/* |
| 9 | + example: {hero:{enabled:true, value:"blue"}, myCoolFeature:{enabled:true}} |
| 10 | +* */ |
8 | 11 | export interface IFlags { |
9 | | - [key: string]: IFlagsmithFeature; |
| 12 | + [key: string]: IFlagsmithFeature |
10 | 13 | } |
| 14 | + |
| 15 | +/* |
| 16 | + example: {favourite_color: "blue", age: 21} |
| 17 | +* */ |
11 | 18 | export interface ITraits { |
12 | | - [key: string]: IFlagsmithTrait; |
13 | | -} |
14 | | -export interface IUserIdentity { |
15 | | - flags: IFlagsmithFeature; |
16 | | - traits: ITraits; |
| 19 | + [key: string]: IFlagsmithTrait |
17 | 20 | } |
| 21 | + |
18 | 22 | export interface IRetrieveInfo { |
19 | | - isFromServer: boolean; |
20 | | - flagsChanged: boolean; |
21 | | - traitsChanged: boolean; |
| 23 | + isFromServer: boolean |
| 24 | + flagsChanged: boolean |
| 25 | + traitsChanged: boolean |
22 | 26 | } |
23 | 27 | export interface IState { |
24 | | - api: string; |
25 | | - environmentID: string; |
26 | | - flags?: IFlags; |
27 | | - identity?: string; |
28 | | - traits: ITraits; |
| 28 | + api: string |
| 29 | + environmentID: string |
| 30 | + flags?: IFlags |
| 31 | + identity?: string |
| 32 | + traits: ITraits |
| 33 | +} |
| 34 | +type ICacheOptions = { |
| 35 | + ttl?:number, // how long to persist the cache in ms (defaults to 0 which is infinite) |
| 36 | + /* |
| 37 | + If this is true and there's cache available, it will skip hitting the API as if preventFetch was true |
| 38 | + Note: this is just for flagsmith.init(), Calls to identify, getFlags etc will still hit the API regardless |
| 39 | + */ |
| 40 | + skipAPI?:boolean |
| 41 | +} |
| 42 | +export interface IInitConfig { |
| 43 | + AsyncStorage?: any // an AsyncStorage implementation |
| 44 | + api?: string // the api you wish to use, important if self hosting |
| 45 | + cacheFlags?: boolean // whether to local storage flags, needs AsyncStorage defined |
| 46 | + cacheOptions?: ICacheOptions // A ttl in ms (default to 0 which is infinite) and option to skip hitting the API in flagsmith.init if there's cache available. |
| 47 | + defaultFlags?: IFlags // |
| 48 | + enableAnalytics?: boolean // Enable sending flag analytics for getValue and hasFeature evaluations. |
| 49 | + enableDynatrace?: boolean // Enables the Dynatrace RUM integration |
| 50 | + enableLogs?: boolean // whether to enable logs |
| 51 | + angularHttpClient?: any // an angular http client to support ssr |
| 52 | + environmentID: string // your Flagsmith environment id |
| 53 | + headers?: object // pass custom headers for flagsmith api calls |
| 54 | + identity?: string // Initialise with a given identity |
| 55 | + traits?: ITraits // Initialise with a given set of traits |
| 56 | + onChange?: (previousFlags:IFlags, params:IRetrieveInfo)=> void // triggered when the flags are retrieved |
| 57 | + onError?: (res:{message:string}) => void // triggered if there was an api error |
| 58 | + preventFetch?: boolean // whether to prevent fetching flags on init |
| 59 | + state?: IState // set a predefined state, useful for isomorphic applications |
| 60 | + _trigger?: ()=>void // Used internally, this function will callback separately to onChange whenever flags are updated |
29 | 61 | } |
30 | 62 | export interface IFlagsmith { |
31 | 63 | /** |
32 | 64 | * Initialise the sdk against a particular environment |
33 | 65 | */ |
34 | | - init: (config: { |
35 | | - environmentID: string; |
36 | | - api?: string; |
37 | | - headers?: object; |
38 | | - AsyncStorage?: any; |
39 | | - cacheFlags?: boolean; |
40 | | - preventFetch?: boolean; |
41 | | - enableAnalytics?: boolean; |
42 | | - enableLogs?: boolean; |
43 | | - onChange?: (previousFlags: IFlags, params: IRetrieveInfo) => void; |
44 | | - state?: IState; |
45 | | - onError?: (res: { |
46 | | - message: string; |
47 | | - }) => void; |
48 | | - defaultFlags?: IFlags; |
49 | | - }) => Promise<void>; |
| 66 | + init:(config: IInitConfig) => Promise<void> |
| 67 | + |
50 | 68 | /** |
51 | 69 | * Trigger a manual fetch of the environment features |
52 | 70 | */ |
53 | | - getFlags: () => Promise<null>; |
| 71 | + getFlags:()=> Promise<null> |
| 72 | + |
54 | 73 | /** |
55 | 74 | * Returns the current flags |
56 | 75 | */ |
57 | | - getAllFlags: () => IFlags; |
| 76 | + getAllFlags:()=> IFlags |
| 77 | + |
58 | 78 | /** |
59 | 79 | * Identify user, triggers a call to get flags if flagsmith.init has been called |
60 | 80 | */ |
61 | | - identify: (userId: string, traits?: Record<string, string | number | boolean>) => Promise<void>; |
| 81 | + identify:(userId:string, traits?: Record<string, string|number|boolean>,) => Promise<void> |
| 82 | + |
62 | 83 | /** |
63 | 84 | * Retrieves the current state of flagsmith |
64 | 85 | */ |
65 | | - getState: () => IState; |
| 86 | + getState:()=> IState |
| 87 | + |
66 | 88 | /** |
67 | 89 | * Clears the identity, triggers a call to getFlags |
68 | 90 | */ |
69 | | - logout: () => Promise<null>; |
| 91 | + logout:()=> Promise<null> |
| 92 | + |
70 | 93 | /** |
71 | 94 | * Polls the flagsmith API, specify interval in ms |
72 | 95 | */ |
73 | | - startListening: (interval?: number) => void; |
| 96 | + startListening:(interval?:number)=> void |
| 97 | + |
74 | 98 | /** |
75 | 99 | * Stops polling |
76 | 100 | */ |
77 | | - stopListening: () => void; |
| 101 | + stopListening:()=> void |
| 102 | + |
78 | 103 | /** |
79 | 104 | * Get the whether a flag is enabled e.g. flagsmith.hasFeature("powerUserFeature") |
80 | 105 | */ |
81 | | - hasFeature: (key: string) => boolean; |
| 106 | + hasFeature:(key: string)=> boolean |
| 107 | + |
82 | 108 | /** |
83 | 109 | * Get the value of a particular remote config e.g. flagsmith.getValue("font_size") |
84 | 110 | */ |
85 | | - getValue: (key: string) => string | number | boolean; |
| 111 | + getValue:(key: string) => string|number|boolean |
| 112 | + |
86 | 113 | /** |
87 | 114 | * Get the value of a particular trait for the identified user |
88 | 115 | */ |
89 | | - getTrait: (key: string) => string | number | boolean; |
| 116 | + getTrait:(key: string) => string|number|boolean |
| 117 | + |
90 | 118 | /** |
91 | 119 | * Set a specific trait for a given user id, triggers a call to get flags |
92 | 120 | */ |
93 | | - setTrait: (key: string, value: string | number | boolean) => Promise<null>; |
| 121 | + setTrait:( |
| 122 | + key: string, |
| 123 | + value: string|number|boolean |
| 124 | + )=> Promise<null> |
| 125 | + |
94 | 126 | /** |
95 | 127 | * Set a key value set of traits for a given user, triggers a call to get flags |
96 | 128 | */ |
97 | | - setTraits: (traits: Record<string, string | number | boolean>) => Promise<null>; |
| 129 | + setTraits:( |
| 130 | + traits: Record<string, string|number|boolean>, |
| 131 | + )=> Promise<null> |
| 132 | + |
98 | 133 | /** |
99 | 134 | * The stored identity of the user |
100 | 135 | */ |
101 | | - identity?: string; |
| 136 | + identity?:string |
| 137 | + |
| 138 | + /** |
| 139 | + * Whether the flagsmith SDK is initialised |
| 140 | + */ |
| 141 | + initialised?:boolean |
| 142 | + |
| 143 | + /** |
| 144 | + * Used internally, this function will callback separately to onChange whenever flags are updated |
| 145 | + */ |
| 146 | + trigger?:()=>{} |
| 147 | + |
| 148 | + cacheOptions: { ttl: number, skipAPI: boolean } |
102 | 149 | } |
0 commit comments