-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtypes.ts
193 lines (175 loc) · 5.56 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
export interface Target {
identifier: string
name?: string
anonymous?: boolean
attributes?: object
}
export interface StreamEvent {
event: string
domain: string
identifier: string
version: number
evaluations?: Evaluation[]
}
export enum Event {
READY = 'ready',
CONNECTED = 'connected',
DISCONNECTED = 'disconnected',
STOPPED = 'stopped',
POLLING = 'polling',
POLLING_STOPPED = 'polling stopped',
FLAGS_LOADED = 'flags loaded',
CACHE_LOADED = 'cache loaded',
CHANGED = 'changed',
ERROR = 'error',
ERROR_CACHE = 'cache error',
ERROR_METRICS = 'metrics error',
ERROR_AUTH = 'auth error',
ERROR_FETCH_FLAGS = 'fetch flags error',
ERROR_FETCH_FLAG = 'fetch flag error',
ERROR_STREAM = 'stream error'
}
export type VariationValue = boolean | string | number | object | undefined
// Used when callers, such as the Flutter SDK for Web, require to know if the variation failed
// and the default value was returned.
export interface VariationValueWithDebug {
value: VariationValue
isDefaultValue: boolean
}
export interface Evaluation {
flag: string // Feature flag identifier
identifier: string // variation identifier
value: VariationValue // variation value
kind: string // boolean | json | string | int
deleted?: boolean // mark that feature flag is deleted
}
export type FetchFlagsResult = { type: 'success'; data: Evaluation[] } | { type: 'error'; error: any }
export interface EventCallbackMapping {
[Event.READY]: (flags: Record<string, VariationValue>) => void
[Event.CONNECTED]: () => void
[Event.STOPPED]: () => void
[Event.POLLING]: () => void
[Event.POLLING_STOPPED]: () => void
[Event.DISCONNECTED]: () => void
[Event.FLAGS_LOADED]: (evaluations: Evaluation[]) => void
[Event.CACHE_LOADED]: (evaluations: Evaluation[]) => void
[Event.CHANGED]: (flag: Evaluation) => void
[Event.ERROR]: (error: unknown) => void
[Event.ERROR_AUTH]: (error: unknown) => void
[Event.ERROR_FETCH_FLAGS]: (error: unknown) => void
[Event.ERROR_FETCH_FLAG]: (error: unknown) => void
[Event.ERROR_STREAM]: (error: unknown) => void
[Event.ERROR_METRICS]: (error: unknown) => void
}
export type EventOnBinding = <K extends keyof EventCallbackMapping>(event: K, callback: EventCallbackMapping[K]) => void
export type EventOffBinding = <K extends keyof EventCallbackMapping>(
event?: K,
callback?: EventCallbackMapping[K]
) => void
export type VariationFn = {
(identifier: string, defaultValue: any): VariationValue
(identifier: string, defaultValue: any, withDebug?: boolean): VariationValue | VariationValueWithDebug
(identifier: string, defaultValue: any, withDebug: false): VariationValue
(identifier: string, defaultValue: any, withDebug: true): VariationValueWithDebug
}
export interface Result {
on: EventOnBinding
off: EventOffBinding
variation: VariationFn
close: () => void
setEvaluations: (evaluations: Evaluation[]) => void
registerAPIRequestMiddleware: (middleware: APIRequestMiddleware) => void
refreshEvaluations: () => void
}
type FetchArgs = Parameters<typeof fetch>
export type APIRequestMiddleware = (req: FetchArgs) => FetchArgs
export interface Options {
/**
* Override the default base URL for the SDK to communicate with the Harness Feature Flags service.
* @default https://config.ff.harness.io/api/1.0
*/
baseUrl?: string
/**
* Override the default metrics URL for the SDK to communicate with the Harness Feature Flags
* metrics service.
* @default https://events.ff.harness.io/api/1.0
*/
eventUrl?: string
/**
* The interval in milliseconds to sync metrics with the Harness Feature Flags metrics service.
* @default 60000
*/
eventsSyncInterval?: number
/**
* The interval in milliseconds to poll the Harness Feature Flags service for updates when polling is enabled
* and streaming is disabled.
* @default 60000
*/
pollingInterval?: number
/**
* Whether to enable the streaming feature. If set to `false` and polling enabled, the SDK will use polling to
* check for updates.
* @default true
*/
streamEnabled?: boolean
/**
* Whether to enable polling. If set to `false`, the SDK will not poll for updates.
* @default false
*/
pollingEnabled?: boolean
/**
* Whether to enable debug logging.
* @default false
*/
debug?: boolean
/**
* Whether to enable caching.
* @default false
*/
cache?: boolean | CacheOptions
/**
* Logger to use instead of the default console.log, console.error and console.info functions
* @default console
*/
logger?: Logger
/**
* By default, the stream will attempt to reconnect indefinitely if it disconnects. Use this option to limit
* the number of attempts it will make.
*/
maxStreamRetries?: number
}
export interface MetricsInfo {
featureIdentifier: string
featureValue: any
variationIdentifier: string
count: number
lastAccessed: number
}
export interface SyncStorage {
getItem: (key: string) => string | null
setItem: (key: string, value: string) => void
removeItem: (key: string) => void
}
export interface AsyncStorage {
getItem: (key: string) => Promise<string | null>
setItem: (key: string, value: string) => Promise<void>
removeItem: (key: string) => Promise<void>
}
export interface CacheOptions {
/**
* Time to live in milliseconds
* @default Infinity
*/
ttl?: number
/**
* Storage to use for caching
* @default localStorage
*/
storage?: AsyncStorage | SyncStorage
}
export interface Logger {
debug: (...data: any[]) => void
error: (...data: any[]) => void
warn: (...data: any[]) => void
info: (...data: any[]) => void
}