Skip to content

Commit b8b2751

Browse files
committed
updating the fetch call
1 parent 2653ebb commit b8b2751

File tree

8 files changed

+63
-26
lines changed

8 files changed

+63
-26
lines changed

packages/remote-config/src/api.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export function getRemoteConfig(
6868
rc._initializePromise = Promise.all([
6969
rc._storage.setLastSuccessfulFetchResponse(options.initialFetchResponse),
7070
rc._storage.setActiveConfigEtag(options.initialFetchResponse?.eTag || ''),
71+
rc._storage.setActiveConfigTemplateVersion(
72+
options.initialFetchResponse.templateVersion || 0
73+
),
7174
rc._storageCache.setLastSuccessfulFetchTimestampMillis(Date.now()),
7275
rc._storageCache.setLastFetchStatus('success'),
7376
rc._storageCache.setActiveConfig(
@@ -100,6 +103,7 @@ export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
100103
!lastSuccessfulFetchResponse ||
101104
!lastSuccessfulFetchResponse.config ||
102105
!lastSuccessfulFetchResponse.eTag ||
106+
!lastSuccessfulFetchResponse.templateVersion ||
103107
lastSuccessfulFetchResponse.eTag === activeConfigEtag
104108
) {
105109
// Either there is no successful fetched config, or is the same as current active
@@ -108,7 +112,10 @@ export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
108112
}
109113
await Promise.all([
110114
rc._storageCache.setActiveConfig(lastSuccessfulFetchResponse.config),
111-
rc._storage.setActiveConfigEtag(lastSuccessfulFetchResponse.eTag)
115+
rc._storage.setActiveConfigEtag(lastSuccessfulFetchResponse.eTag),
116+
rc._storage.setActiveConfigTemplateVersion(
117+
lastSuccessfulFetchResponse.templateVersion
118+
)
112119
]);
113120
return true;
114121
}

packages/remote-config/src/client/realtime_handler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
FetchRequest,
3333
RemoteConfigAbortSignal
3434
} from './remote_config_fetch_client';
35-
import { RestClient } from './rest_client';
35+
import { CachingClient } from './caching_client';
3636

3737
const API_KEY_HEADER = 'X-Goog-Api-Key';
3838
const INSTALLATIONS_AUTH_TOKEN_HEADER = 'X-Goog-Firebase-Installations-Auth';
@@ -55,7 +55,7 @@ export class RealtimeHandler {
5555
private readonly appId: string,
5656
private readonly logger: Logger,
5757
private readonly storageCache: StorageCache,
58-
private readonly restClient: RestClient
58+
private readonly cachingClient: CachingClient
5959
) {
6060
void this.setRetriesRemaining();
6161
void VisibilityMonitor.getInstance().on(
@@ -178,7 +178,7 @@ export class RealtimeHandler {
178178
): Promise<Response> {
179179
const eTagValue = await this.storage.getActiveConfigEtag();
180180
const lastKnownVersionNumber =
181-
await this.storage.getLastKnownTemplateVersion();
181+
await this.storage.getActiveConfigTemplateVersion();
182182

183183
const headers = {
184184
[API_KEY_HEADER]: this.apiKey,
@@ -340,7 +340,7 @@ export class RealtimeHandler {
340340
fetchAttempt: currentAttempt
341341
};
342342

343-
const fetchResponse: FetchResponse = await this.restClient.fetch(
343+
const fetchResponse: FetchResponse = await this.cachingClient.fetch(
344344
fetchRequest
345345
);
346346
let activatedConfigs = await this.storage.getActiveConfig();
@@ -465,7 +465,7 @@ export class RealtimeHandler {
465465

466466
if (TEMPLATE_VERSION_KEY in jsonObject) {
467467
const oldTemplateVersion =
468-
await this.storage.getLastKnownTemplateVersion();
468+
await this.storage.getActiveConfigTemplateVersion();
469469
const targetTemplateVersion = Number(
470470
jsonObject[TEMPLATE_VERSION_KEY]
471471
);

packages/remote-config/src/client/rest_client.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
import { ERROR_FACTORY, ErrorCode } from '../errors';
2828
import { getUserLanguage } from '../language';
2929
import { _FirebaseInstallationsInternal } from '@firebase/installations';
30-
import { Storage } from '../storage/storage';
3130

3231
/**
3332
* Defines request body parameters required to call the fetch API:
@@ -59,8 +58,7 @@ export class RestClient implements RemoteConfigFetchClient {
5958
private readonly namespace: string,
6059
private readonly projectId: string,
6160
private readonly apiKey: string,
62-
private readonly appId: string,
63-
private readonly storage: Storage
61+
private readonly appId: string
6462
) {}
6563

6664
/**
@@ -162,10 +160,6 @@ export class RestClient implements RemoteConfigFetchClient {
162160
config = responseBody['entries'];
163161
state = responseBody['state'];
164162
templateVersion = responseBody['templateVersion'];
165-
166-
if (templateVersion !== undefined) {
167-
await this.storage.setLastKnownTemplateVersion(templateVersion);
168-
}
169163
}
170164

171165
// Normalizes based on legacy state.

packages/remote-config/src/register.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ export function registerRemoteConfig(): void {
9898
namespace,
9999
projectId,
100100
apiKey,
101-
appId,
102-
storage
101+
appId
103102
);
104103
const retryingClient = new RetryingClient(restClient, storage);
105104
const cachingClient = new CachingClient(
@@ -119,7 +118,7 @@ export function registerRemoteConfig(): void {
119118
appId,
120119
logger,
121120
storageCache,
122-
restClient
121+
cachingClient
123122
);
124123

125124
const remoteConfigInstance = new RemoteConfigImpl(

packages/remote-config/src/storage/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ export abstract class Storage {
201201
);
202202
}
203203

204-
getLastKnownTemplateVersion(): Promise<number | undefined> {
204+
getActiveConfigTemplateVersion(): Promise<number | undefined> {
205205
return this.get<number>('last_known_template_version');
206206
}
207207

208-
setLastKnownTemplateVersion(version: number): Promise<void> {
208+
setActiveConfigTemplateVersion(version: number): Promise<void> {
209209
return this.set<number>('last_known_template_version', version);
210210
}
211211
}

packages/remote-config/test/api.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ describe('Remote Config API', () => {
5757
const STUB_FETCH_RESPONSE: FetchResponse = {
5858
status: 200,
5959
eTag: 'asdf',
60-
config: { 'foobar': 'hello world' }
60+
config: { 'foobar': 'hello world' },
61+
templateVersion: 1
6162
};
6263
let fetchStub: sinon.SinonStub;
6364

@@ -94,7 +95,8 @@ describe('Remote Config API', () => {
9495
json: () =>
9596
Promise.resolve({
9697
entries: response.config,
97-
state: 'OK'
98+
state: 'OK',
99+
templateVersion: response.templateVersion
98100
})
99101
} as Response)
100102
);

packages/remote-config/test/client/rest_client.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ describe('RestClient', () => {
4545
'namespace',
4646
'project-id',
4747
'api-key',
48-
'app-id',
49-
storage
48+
'app-id'
5049
);
5150
firebaseInstallations.getId = sinon
5251
.stub()
5352
.returns(Promise.resolve('fis-id'));
5453
firebaseInstallations.getToken = sinon
5554
.stub()
5655
.returns(Promise.resolve('fis-token'));
57-
storage.setLastKnownTemplateVersion = sinon.stub();
56+
storage.setActiveConfigTemplateVersion = sinon.stub();
5857
});
5958

6059
describe('fetch', () => {

packages/remote-config/test/remote_config.test.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,39 +390,58 @@ describe('RemoteConfig', () => {
390390
const ETAG = 'etag';
391391
const CONFIG = { key: 'val' };
392392
const NEW_ETAG = 'new_etag';
393+
const TEMPLATE_VERSION = 1;
393394

394395
let getLastSuccessfulFetchResponseStub: sinon.SinonStub;
395396
let getActiveConfigEtagStub: sinon.SinonStub;
397+
let getActiveConfigTemplateVersionStub: sinon.SinonStub;
396398
let setActiveConfigEtagStub: sinon.SinonStub;
397399
let setActiveConfigStub: sinon.SinonStub;
400+
let setActiveConfigTemplateVersionStub: sinon.SinonStub;
398401

399402
beforeEach(() => {
400403
getLastSuccessfulFetchResponseStub = sinon.stub();
401404
getActiveConfigEtagStub = sinon.stub();
405+
getActiveConfigTemplateVersionStub = sinon.stub();
402406
setActiveConfigEtagStub = sinon.stub();
403407
setActiveConfigStub = sinon.stub();
408+
setActiveConfigTemplateVersionStub = sinon.stub();
404409

405410
storage.getLastSuccessfulFetchResponse =
406411
getLastSuccessfulFetchResponseStub;
407412
storage.getActiveConfigEtag = getActiveConfigEtagStub;
413+
storage.getActiveConfigTemplateVersion =
414+
getActiveConfigTemplateVersionStub;
408415
storage.setActiveConfigEtag = setActiveConfigEtagStub;
409416
storageCache.setActiveConfig = setActiveConfigStub;
417+
storage.setActiveConfigTemplateVersion =
418+
setActiveConfigTemplateVersionStub;
410419
});
411420

412421
it('does not activate if last successful fetch response is undefined', async () => {
413422
getLastSuccessfulFetchResponseStub.returns(Promise.resolve());
414423
getActiveConfigEtagStub.returns(Promise.resolve(ETAG));
424+
getActiveConfigTemplateVersionStub.returns(
425+
Promise.resolve(TEMPLATE_VERSION)
426+
);
415427

416428
const activateResponse = await activate(rc);
417429

418430
expect(activateResponse).to.be.false;
419431
expect(storage.setActiveConfigEtag).to.not.have.been.called;
420432
expect(storageCache.setActiveConfig).to.not.have.been.called;
433+
expect(storage.setActiveConfigTemplateVersion).to.not.have.been.calledWith(
434+
TEMPLATE_VERSION
435+
);
421436
});
422437

423438
it('does not activate if fetched and active etags are the same', async () => {
424439
getLastSuccessfulFetchResponseStub.returns(
425-
Promise.resolve({ config: {}, etag: ETAG })
440+
Promise.resolve({
441+
config: {},
442+
eTag: ETAG,
443+
templateVersion: TEMPLATE_VERSION
444+
})
426445
);
427446
getActiveConfigEtagStub.returns(Promise.resolve(ETAG));
428447

@@ -431,11 +450,18 @@ describe('RemoteConfig', () => {
431450
expect(activateResponse).to.be.false;
432451
expect(storage.setActiveConfigEtag).to.not.have.been.called;
433452
expect(storageCache.setActiveConfig).to.not.have.been.called;
453+
expect(storage.setActiveConfigTemplateVersion).to.not.have.been.calledWith(
454+
TEMPLATE_VERSION
455+
);
434456
});
435457

436458
it('activates if fetched and active etags are different', async () => {
437459
getLastSuccessfulFetchResponseStub.returns(
438-
Promise.resolve({ config: CONFIG, eTag: NEW_ETAG })
460+
Promise.resolve({
461+
config: CONFIG,
462+
eTag: NEW_ETAG,
463+
templateVersion: TEMPLATE_VERSION
464+
})
439465
);
440466
getActiveConfigEtagStub.returns(Promise.resolve(ETAG));
441467

@@ -444,11 +470,18 @@ describe('RemoteConfig', () => {
444470
expect(activateResponse).to.be.true;
445471
expect(storage.setActiveConfigEtag).to.have.been.calledWith(NEW_ETAG);
446472
expect(storageCache.setActiveConfig).to.have.been.calledWith(CONFIG);
473+
expect(storage.setActiveConfigTemplateVersion).to.have.been.calledWith(
474+
TEMPLATE_VERSION
475+
);
447476
});
448477

449478
it('activates if fetched is defined but active config is not', async () => {
450479
getLastSuccessfulFetchResponseStub.returns(
451-
Promise.resolve({ config: CONFIG, eTag: NEW_ETAG })
480+
Promise.resolve({
481+
config: CONFIG,
482+
eTag: NEW_ETAG,
483+
templateVersion: TEMPLATE_VERSION
484+
})
452485
);
453486
getActiveConfigEtagStub.returns(Promise.resolve());
454487

@@ -457,6 +490,9 @@ describe('RemoteConfig', () => {
457490
expect(activateResponse).to.be.true;
458491
expect(storage.setActiveConfigEtag).to.have.been.calledWith(NEW_ETAG);
459492
expect(storageCache.setActiveConfig).to.have.been.calledWith(CONFIG);
493+
expect(storage.setActiveConfigTemplateVersion).to.have.been.calledWith(
494+
TEMPLATE_VERSION
495+
);
460496
});
461497
});
462498

0 commit comments

Comments
 (0)