1- import { IUniversalFlagConfigResponse , IBanditParametersResponse } from '../http-client' ;
1+ import { IBanditParametersResponse , IUniversalFlagConfigResponse } from '../http-client' ;
22import {
33 Environment ,
44 FormatEnum ,
@@ -9,6 +9,20 @@ import {
99import { obfuscatePrecomputedBanditMap , obfuscatePrecomputedFlags } from '../obfuscation' ;
1010import { ContextAttributes , FlagKey , HashedFlagKey } from '../types' ;
1111
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+
1226// Base interface for all configuration responses
1327interface IBasePrecomputedConfigurationResponse {
1428 readonly format : FormatEnum . PRECOMPUTED ;
@@ -169,28 +183,17 @@ export interface IConfigurationWire {
169183type UfcResponseType = IUniversalFlagConfigResponse | IBanditParametersResponse ;
170184
171185// 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 > ;
176187
177188/**
178189 * A wrapper around a server response that includes the response, etag, and fetchedAt timestamp.
179190 */
180191interface IConfigResponse < T extends UfcResponseType > {
181- readonly response : ResponseString < T > ; // JSON-encoded server response
192+ readonly response : UFCResponseString < T > ; // JSON-encoded server response
182193 readonly etag ?: string ; // Entity Tag - denotes a snapshot or version of the config.
183194 readonly fetchedAt ?: string ; // ISO timestamp for when this config was fetched
184195}
185196
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-
194197export class ConfigurationWireV1 implements IConfigurationWire {
195198 public readonly version = 1 ;
196199
@@ -200,10 +203,6 @@ export class ConfigurationWireV1 implements IConfigurationWire {
200203 readonly bandits ?: IConfigResponse < IBanditParametersResponse > ,
201204 ) { }
202205
203- public static fromString ( stringifiedPayload : string ) : IConfigurationWire {
204- return JSON . parse ( stringifiedPayload ) as IConfigurationWire ;
205- }
206-
207206 public static fromResponses (
208207 flagConfig : IUniversalFlagConfigResponse ,
209208 banditConfig ?: IBanditParametersResponse ,
@@ -213,13 +212,13 @@ export class ConfigurationWireV1 implements IConfigurationWire {
213212 return new ConfigurationWireV1 (
214213 undefined ,
215214 {
216- response : deflateResponse ( flagConfig ) ,
215+ response : deflateJsonObject ( flagConfig ) ,
217216 fetchedAt : new Date ( ) . toISOString ( ) ,
218217 etag : flagConfigEtag ,
219218 } ,
220219 banditConfig
221220 ? {
222- response : deflateResponse ( banditConfig ) ,
221+ response : deflateJsonObject ( banditConfig ) ,
223222 fetchedAt : new Date ( ) . toISOString ( ) ,
224223 etag : banditConfigEtag ,
225224 }
0 commit comments