Skip to content

Commit eeaf1f6

Browse files
committed
Move functionality from #load to #set
1 parent c68610a commit eeaf1f6

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ export type ServerConfig = {
176176
// @public
177177
export interface ServerTemplate {
178178
evaluate(context?: EvaluationContext): ServerConfig;
179-
load(template?: ServerTemplateData | string): Promise<void>;
179+
load(): Promise<void>;
180+
set(template: ServerTemplateData | string): void;
180181
// (undocumented)
181182
toJSON(): ServerTemplateData;
182183
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,17 @@ export interface ServerTemplate {
390390
*/
391391
evaluate(context?: EvaluationContext): ServerConfig;
392392

393+
/**
394+
* Sets and caches a {@link ServerTemplateData} or a JSON string representing
395+
* the server template
396+
*/
397+
set(template: ServerTemplateData | string): void;
398+
393399
/**
394400
* Fetches and caches the current active version of the
395401
* project's {@link ServerTemplate}.
396402
*/
397-
load(template?: ServerTemplateData | string): Promise<void>;
403+
load(): Promise<void>;
398404

399405
/**
400406
* @returns JSON representation of {@link ServerTemplateData}

src/remote-config/remote-config.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -322,32 +322,34 @@ class ServerTemplateImpl implements ServerTemplate {
322322
}
323323

324324
/**
325-
* Fetches and caches the current active version of the project's {@link ServerTemplate}.
325+
* Takes in either a {@link ServerTemplateData} or a JSON string
326+
* representing the template, parses it, and caches it.
326327
*/
327-
public load(template?: ServerTemplateData | string): Promise<void> {
328-
if (template) {
329-
// Check and instantiates the template via a json string
330-
if (isString(template)) {
331-
try {
332-
this.cache = new ServerTemplateDataImpl(JSON.parse(template));
333-
} catch (e) {
334-
throw new FirebaseRemoteConfigError(
335-
'invalid-argument',
336-
`Failed to parse the JSON string: ${template}. ` + e
337-
);
338-
}
339-
} else {
340-
this.cache = template;
328+
public set(template: ServerTemplateData | string): void {
329+
if (isString(template)) {
330+
try {
331+
this.cache = new ServerTemplateDataImpl(JSON.parse(template));
332+
} catch (e) {
333+
throw new FirebaseRemoteConfigError(
334+
'invalid-argument',
335+
`Failed to parse the JSON string: ${template}. ` + e
336+
);
341337
}
342-
return Promise.resolve();
343338
} else {
344-
return this.apiClient.getServerTemplate()
345-
.then((template) => {
346-
this.cache = new ServerTemplateDataImpl(template);
347-
});
339+
this.cache = template;
348340
}
349341
}
350342

343+
/**
344+
* Fetches and caches the current active version of the project's {@link ServerTemplate}.
345+
*/
346+
public load(): Promise<void> {
347+
return this.apiClient.getServerTemplate()
348+
.then((template) => {
349+
this.cache = new ServerTemplateDataImpl(template);
350+
});
351+
}
352+
351353
/**
352354
* Evaluates the current template in cache to produce a {@link ServerConfig}.
353355
*/

test/unit/remote-config/remote-config.spec.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,7 @@ describe('RemoteConfig', () => {
752752
}
753753
};
754754

755-
describe('load', () => {
756-
const operationName = 'getServerTemplate';
755+
describe('set', () => {
757756
const INVALID_PARAMETERS: any[] = [null, '', 'abc', 1, true, []];
758757
const INVALID_CONDITIONS: any[] = [null, '', 'abc', 1, true, {}];
759758
let sourceTemplate = deepCopy(SERVER_REMOTE_CONFIG_RESPONSE);
@@ -770,7 +769,7 @@ describe('RemoteConfig', () => {
770769
}
771770
};
772771
const initializedTemplate = remoteConfig.initServerTemplate();
773-
initializedTemplate.load(template);
772+
initializedTemplate.set(template);
774773
const parsed = initializedTemplate.toJSON();
775774
const expectedVersion = deepCopy(VERSION_INFO);
776775
expectedVersion.updateTime = new Date(expectedVersion.updateTime).toUTCString();
@@ -791,7 +790,7 @@ describe('RemoteConfig', () => {
791790
};
792791
const templateJson = JSON.stringify(template);
793792
const initializedTemplate = remoteConfig.initServerTemplate();
794-
initializedTemplate.load(templateJson);
793+
initializedTemplate.set(templateJson);
795794
const parsed = initializedTemplate.toJSON();
796795
const expectedVersion = deepCopy(VERSION_INFO);
797796
expectedVersion.updateTime = new Date(expectedVersion.updateTime).toUTCString();
@@ -802,7 +801,7 @@ describe('RemoteConfig', () => {
802801
it('should throw if template is an invalid JSON', () => {
803802
const jsonString = '{invalidJson: null}';
804803
const initializedTemplate = remoteConfig.initServerTemplate();
805-
expect(() => initializedTemplate.load(jsonString))
804+
expect(() => initializedTemplate.set(jsonString))
806805
.to.throw(/Failed to parse the JSON string: ([\D\w]*)\./);
807806
});
808807

@@ -811,7 +810,7 @@ describe('RemoteConfig', () => {
811810
it(`should throw if the parameters is ${JSON.stringify(invalidParameter)}`, () => {
812811
const jsonString = JSON.stringify(sourceTemplate);
813812
const initializedTemplate = remoteConfig.initServerTemplate();
814-
expect(() => initializedTemplate.load(jsonString))
813+
expect(() => initializedTemplate.set(jsonString))
815814
.to.throw(/Failed to parse the JSON string: ([\D\w]*)\./);
816815
});
817816
});
@@ -822,11 +821,16 @@ describe('RemoteConfig', () => {
822821
it(`should throw if the conditions is ${JSON.stringify(invalidConditions)}`, () => {
823822
const jsonString = JSON.stringify(sourceTemplate);
824823
const initializedTemplate = remoteConfig.initServerTemplate();
825-
expect(() => initializedTemplate.load(jsonString))
824+
expect(() => initializedTemplate.set(jsonString))
826825
.to.throw(/Failed to parse the JSON string: ([\D\w]*)\./);
827826
});
828827
});
829828

829+
});
830+
831+
describe('load', () => {
832+
const operationName = 'getServerTemplate';
833+
830834
it('should propagate API errors', () => {
831835
const stub = sinon
832836
.stub(RemoteConfigApiClient.prototype, operationName)
@@ -1248,7 +1252,7 @@ describe('RemoteConfig', () => {
12481252
},
12491253
}
12501254

1251-
template.load(response as ServerTemplateData);
1255+
template.set(response as ServerTemplateData);
12521256

12531257
let config = template.evaluate();
12541258

@@ -1263,7 +1267,7 @@ describe('RemoteConfig', () => {
12631267
},
12641268
}
12651269

1266-
template.load(response as ServerTemplateData);
1270+
template.set(response as ServerTemplateData);
12671271

12681272
config = template.evaluate();
12691273

0 commit comments

Comments
 (0)