@@ -22,6 +22,7 @@ import {
22
22
} from '../configuration' ;
23
23
import ConfigurationRequestor from '../configuration-requestor' ;
24
24
import { IConfigurationStore , ISyncStore } from '../configuration-store/configuration-store' ;
25
+ import { MemoryOnlyConfigurationStore } from '../configuration-store/memory.store' ;
25
26
import {
26
27
DEFAULT_INITIAL_CONFIG_REQUEST_RETRIES ,
27
28
DEFAULT_POLL_CONFIG_REQUEST_RETRIES ,
@@ -53,6 +54,7 @@ import {
53
54
VariationType ,
54
55
} from '../interfaces' ;
55
56
import { getMD5Hash } from '../obfuscation' ;
57
+ import { OverridePayload , OverrideValidator } from '../override-validator' ;
56
58
import initPoller , { IPoller } from '../poller' ;
57
59
import {
58
60
Attributes ,
@@ -63,6 +65,7 @@ import {
63
65
FlagKey ,
64
66
ValueType ,
65
67
} from '../types' ;
68
+ import { shallowClone } from '../util' ;
66
69
import { validateNotBlank } from '../validation' ;
67
70
import { LIB_VERSION } from '../version' ;
68
71
@@ -125,6 +128,7 @@ export default class EppoClient {
125
128
private isObfuscated : boolean ;
126
129
private requestPoller ?: IPoller ;
127
130
private readonly evaluator = new Evaluator ( ) ;
131
+ private readonly overrideValidator = new OverrideValidator ( ) ;
128
132
129
133
constructor ( {
130
134
eventDispatcher = new NoOpEventDispatcher ( ) ,
@@ -154,6 +158,35 @@ export default class EppoClient {
154
158
this . isObfuscated = isObfuscated ;
155
159
}
156
160
161
+ /**
162
+ * Validates and parses x-eppo-overrides header sent by Eppo's Chrome extension
163
+ */
164
+ async parseOverrides (
165
+ overridePayload : string | undefined ,
166
+ ) : Promise < Record < FlagKey , Variation > | undefined > {
167
+ if ( ! overridePayload ) {
168
+ return undefined ;
169
+ }
170
+ const payload : OverridePayload = this . overrideValidator . parseOverridePayload ( overridePayload ) ;
171
+ await this . overrideValidator . validateOverrideApiKey ( payload . apiKey ) ;
172
+ return payload . overrides ;
173
+ }
174
+
175
+ /**
176
+ * Creates an EppoClient instance that has the specified overrides applied
177
+ * to it without affecting the original EppoClient singleton. Useful for
178
+ * applying overrides in a shared Node instance, such as a web server.
179
+ */
180
+ withOverrides ( overrides : Record < FlagKey , Variation > ) : EppoClient {
181
+ if ( overrides && Object . keys ( overrides ) . length ) {
182
+ const copy = shallowClone ( this ) ;
183
+ copy . overrideStore = new MemoryOnlyConfigurationStore < Variation > ( ) ;
184
+ copy . overrideStore . setEntries ( overrides ) ;
185
+ return copy ;
186
+ }
187
+ return this ;
188
+ }
189
+
157
190
setConfigurationRequestParameters (
158
191
configurationRequestParameters : FlagConfigurationRequestParameters ,
159
192
) {
0 commit comments