1
- import { IUniversalFlagConfigResponse , IBanditParametersResponse } from '../http-client' ;
1
+ import { IBanditParametersResponse , IUniversalFlagConfigResponse } from '../http-client' ;
2
2
import {
3
3
Environment ,
4
4
FormatEnum ,
@@ -9,6 +9,20 @@ import {
9
9
import { obfuscatePrecomputedBanditMap , obfuscatePrecomputedFlags } from '../obfuscation' ;
10
10
import { ContextAttributes , FlagKey , HashedFlagKey } from '../types' ;
11
11
12
+ import { deflateJsonObject , inflateJsonObject , JsonString } from './json-util' ;
13
+
14
+ /**
15
+ * Builds an `IConfigurationWire` instance from the payload string.
16
+ * To generate the payload string, see `ConfigurationWireHelper.fetchBootstrapConfiguration`.
17
+ *
18
+ * @param payloadString
19
+ */
20
+ export function configurationFromString (
21
+ payloadString : string | JsonString < IConfigurationWire > ,
22
+ ) : IConfigurationWire {
23
+ return inflateJsonObject ( payloadString as JsonString < IConfigurationWire > ) ;
24
+ }
25
+
12
26
// Base interface for all configuration responses
13
27
interface IBasePrecomputedConfigurationResponse {
14
28
readonly format : FormatEnum . PRECOMPUTED ;
@@ -169,28 +183,17 @@ export interface IConfigurationWire {
169
183
type UfcResponseType = IUniversalFlagConfigResponse | IBanditParametersResponse ;
170
184
171
185
// The UFC responses are JSON-encoded strings so we can treat them as opaque blobs, but we also want to enforce type safety.
172
- type ResponseString < T extends UfcResponseType > = string & {
173
- readonly __brand : unique symbol ;
174
- readonly __type : T ;
175
- } ;
186
+ type UFCResponseString < T extends UfcResponseType > = JsonString < T > ;
176
187
177
188
/**
178
189
* A wrapper around a server response that includes the response, etag, and fetchedAt timestamp.
179
190
*/
180
191
interface IConfigResponse < T extends UfcResponseType > {
181
- readonly response : ResponseString < T > ; // JSON-encoded server response
192
+ readonly response : UFCResponseString < T > ; // JSON-encoded server response
182
193
readonly etag ?: string ; // Entity Tag - denotes a snapshot or version of the config.
183
194
readonly fetchedAt ?: string ; // ISO timestamp for when this config was fetched
184
195
}
185
196
186
- export function inflateResponse < T extends UfcResponseType > ( response : ResponseString < T > ) : T {
187
- return JSON . parse ( response ) as T ;
188
- }
189
-
190
- export function deflateResponse < T extends UfcResponseType > ( value : T ) : ResponseString < T > {
191
- return JSON . stringify ( value ) as ResponseString < T > ;
192
- }
193
-
194
197
export class ConfigurationWireV1 implements IConfigurationWire {
195
198
public readonly version = 1 ;
196
199
@@ -200,10 +203,6 @@ export class ConfigurationWireV1 implements IConfigurationWire {
200
203
readonly bandits ?: IConfigResponse < IBanditParametersResponse > ,
201
204
) { }
202
205
203
- public static fromString ( stringifiedPayload : string ) : IConfigurationWire {
204
- return JSON . parse ( stringifiedPayload ) as IConfigurationWire ;
205
- }
206
-
207
206
public static fromResponses (
208
207
flagConfig : IUniversalFlagConfigResponse ,
209
208
banditConfig ?: IBanditParametersResponse ,
@@ -213,13 +212,13 @@ export class ConfigurationWireV1 implements IConfigurationWire {
213
212
return new ConfigurationWireV1 (
214
213
undefined ,
215
214
{
216
- response : deflateResponse ( flagConfig ) ,
215
+ response : deflateJsonObject ( flagConfig ) ,
217
216
fetchedAt : new Date ( ) . toISOString ( ) ,
218
217
etag : flagConfigEtag ,
219
218
} ,
220
219
banditConfig
221
220
? {
222
- response : deflateResponse ( banditConfig ) ,
221
+ response : deflateJsonObject ( banditConfig ) ,
223
222
fetchedAt : new Date ( ) . toISOString ( ) ,
224
223
etag : banditConfigEtag ,
225
224
}
0 commit comments