Skip to content

Commit 86f4426

Browse files
authored
Add #set and #toJSON to RC ServerTemplate (#2522)
* Add #set on ServerTemplate to allow for setting and caching a server template * Simplify initServerTemplate to make use of the new setter * Update tests * Address comments and feedback * Update API docs * Add export for new type ServerTemplateDataType to index.ts * Update some inline comments
1 parent 8dbf86f commit 86f4426

File tree

5 files changed

+170
-51
lines changed

5 files changed

+170
-51
lines changed

etc/firebase-admin.remote-config.api.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface InAppDefaultValue {
4545

4646
// @public
4747
export interface InitServerTemplateOptions extends GetServerTemplateOptions {
48-
template?: ServerTemplateData | string;
48+
template?: ServerTemplateDataType;
4949
}
5050

5151
// @public
@@ -183,9 +183,10 @@ export interface ServerConfig {
183183

184184
// @public
185185
export interface ServerTemplate {
186-
cache: ServerTemplateData;
187186
evaluate(context?: EvaluationContext): ServerConfig;
188187
load(): Promise<void>;
188+
set(template: ServerTemplateDataType): void;
189+
toJSON(): ServerTemplateData;
189190
}
190191

191192
// @public
@@ -198,6 +199,9 @@ export interface ServerTemplateData {
198199
version?: Version;
199200
}
200201

202+
// @public
203+
export type ServerTemplateDataType = ServerTemplateData | string;
204+
201205
// @public
202206
export type TagColor = 'BLUE' | 'BROWN' | 'CYAN' | 'DEEP_ORANGE' | 'GREEN' | 'INDIGO' | 'LIME' | 'ORANGE' | 'PINK' | 'PURPLE' | 'TEAL';
203207

src/remote-config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export {
5050
ServerConfig,
5151
ServerTemplate,
5252
ServerTemplateData,
53+
ServerTemplateDataType,
5354
TagColor,
5455
Value,
5556
ValueSource,

src/remote-config/remote-config-api.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ export interface GetServerTemplateOptions {
364364
defaultConfig?: DefaultConfig;
365365
}
366366

367+
/**
368+
* Represents the type of a Remote Config server template that can be set on
369+
* {@link ServerTemplate}. This can either be a {@link ServerTemplateData} object
370+
* or a template JSON string.
371+
*/
372+
export type ServerTemplateDataType = ServerTemplateData | string;
373+
367374
/**
368375
* Represents optional arguments that can be used when instantiating
369376
* {@link ServerTemplate} synchonously.
@@ -375,22 +382,14 @@ export interface InitServerTemplateOptions extends GetServerTemplateOptions {
375382
* example, customers can reduce initialization latency by pre-fetching and
376383
* caching template data and then using this option to initialize the SDK with
377384
* that data.
378-
* The template can be initialized with either a {@link ServerTemplateData}
379-
* object or a JSON string.
380385
*/
381-
template?: ServerTemplateData|string,
386+
template?: ServerTemplateDataType,
382387
}
383388

384389
/**
385390
* Represents a stateful abstraction for a Remote Config server template.
386391
*/
387392
export interface ServerTemplate {
388-
389-
/**
390-
* Cached {@link ServerTemplateData}.
391-
*/
392-
cache: ServerTemplateData;
393-
394393
/**
395394
* Evaluates the current template to produce a {@link ServerConfig}.
396395
*/
@@ -401,6 +400,17 @@ export interface ServerTemplate {
401400
* project's {@link ServerTemplate}.
402401
*/
403402
load(): Promise<void>;
403+
404+
/**
405+
* Sets and caches a {@link ServerTemplateData} or a JSON string representing
406+
* the server template
407+
*/
408+
set(template: ServerTemplateDataType): void;
409+
410+
/**
411+
* Returns a JSON representation of {@link ServerTemplateData}
412+
*/
413+
toJSON(): ServerTemplateData;
404414
}
405415

406416
/**

src/remote-config/remote-config.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
DefaultConfig,
4141
GetServerTemplateOptions,
4242
InitServerTemplateOptions,
43+
ServerTemplateDataType,
4344
} from './remote-config-api';
4445
import { isString } from 'lodash';
4546

@@ -200,20 +201,9 @@ export class RemoteConfig {
200201
public initServerTemplate(options?: InitServerTemplateOptions): ServerTemplate {
201202
const template = new ServerTemplateImpl(
202203
this.client, new ConditionEvaluator(), options?.defaultConfig);
204+
203205
if (options?.template) {
204-
// Check and instantiates the template via a json string
205-
if (isString(options?.template)) {
206-
try {
207-
template.cache = new ServerTemplateDataImpl(JSON.parse(options?.template));
208-
} catch (e) {
209-
throw new FirebaseRemoteConfigError(
210-
'invalid-argument',
211-
`Failed to parse the JSON string: ${options?.template}. ` + e
212-
);
213-
}
214-
} else {
215-
template.cache = options?.template;
216-
}
206+
template.set(options?.template);
217207
}
218208
return template;
219209
}
@@ -307,7 +297,7 @@ class RemoteConfigTemplateImpl implements RemoteConfigTemplate {
307297
* Remote Config dataplane template data implementation.
308298
*/
309299
class ServerTemplateImpl implements ServerTemplate {
310-
public cache: ServerTemplateData;
300+
private cache: ServerTemplateData;
311301
private stringifiedDefaultConfig: {[key: string]: string} = {};
312302

313303
constructor(
@@ -333,6 +323,27 @@ class ServerTemplateImpl implements ServerTemplate {
333323
});
334324
}
335325

326+
/**
327+
* Parses a {@link ServerTemplateDataType} and caches it.
328+
*/
329+
public set(template: ServerTemplateDataType): void {
330+
let parsed;
331+
if (isString(template)) {
332+
try {
333+
parsed = JSON.parse(template);
334+
} catch (e) {
335+
// Transforms JSON parse errors to Firebase error.
336+
throw new FirebaseRemoteConfigError(
337+
'invalid-argument',
338+
`Failed to parse the JSON string: ${template}. ` + e);
339+
}
340+
} else {
341+
parsed = template;
342+
}
343+
// Throws template parse errors.
344+
this.cache = new ServerTemplateDataImpl(parsed);
345+
}
346+
336347
/**
337348
* Evaluates the current template in cache to produce a {@link ServerConfig}.
338349
*/
@@ -402,6 +413,13 @@ class ServerTemplateImpl implements ServerTemplate {
402413

403414
return new ServerConfigImpl(configValues);
404415
}
416+
417+
/**
418+
* @returns JSON representation of the server template
419+
*/
420+
public toJSON(): ServerTemplateData {
421+
return this.cache;
422+
}
405423
}
406424

407425
class ServerConfigImpl implements ServerConfig {

0 commit comments

Comments
 (0)