Skip to content

Commit 01e023f

Browse files
author
Ace Nassri
authored
GCF: Add Firebase remote config samples (GoogleCloudPlatform#927)
* Add Firebase remote config sample * Add docstrings
1 parent e753263 commit 01e023f

4 files changed

Lines changed: 53 additions & 19 deletions

File tree

functions/firebase/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,18 @@ exports.helloAnalytics = (event) => {
115115
console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`);
116116
};
117117
// [END functions_firebase_analytics]
118+
119+
// [START functions_firebase_remote_config]
120+
/**
121+
* Triggered by a change to a Firebase Remote Config value.
122+
*
123+
* @param {object} data The Cloud Functions event data.
124+
*/
125+
exports.helloRemoteConfig = (event) => {
126+
const data = event.data;
127+
128+
console.log(`Update type: ${data.updateType}`);
129+
console.log(`Origin: ${data.updateOrigin}`);
130+
console.log(`Version: ${data.versionNumber}`);
131+
};
132+
// [END functions_firebase_remote_config]

functions/firebase/test/index.test.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ test(`should listen to Auth events`, t => {
107107
t.true(console.log.calledWith(`Email: me@example.com`));
108108
});
109109

110-
test.serial('should monitor Analytics', t => {
110+
test.serial('should listen to Analytics events', t => {
111111
const date = new Date();
112112
const event = {
113113
data: {
@@ -137,30 +137,20 @@ test.serial('should monitor Analytics', t => {
137137
t.is(console.log.args[4][0], `Location: London, UK`);
138138
});
139139

140-
test(`should update data in response to Firestore events`, t => {
140+
test(`should listen to Remote Config events`, t => {
141141
const sample = getSample();
142142

143-
const date = Date.now();
144143
const event = {
145-
resource: '/documents/some/path',
146144
data: {
147-
email: 'me@example.com',
148-
metadata: {
149-
createdAt: date
150-
},
151-
value: {
152-
fields: {
153-
original: {
154-
stringValue: 'foobar'
155-
}
156-
}
157-
}
145+
updateOrigin: 'CONSOLE',
146+
updateType: 'INCREMENTAL_UPDATE',
147+
versionNumber: '1'
158148
}
159149
};
160150

161-
sample.program.makeUpperCase(event);
151+
sample.program.helloRemoteConfig(event);
162152

163-
t.true(sample.mocks.firestore.doc.calledWith('some/path'));
164-
t.true(console.log.calledWith(`Replacing value: foobar --> FOOBAR`));
165-
t.true(sample.mocks.firestore.set.calledWith({'original': 'FOOBAR'}));
153+
t.true(console.log.calledWith(`Update type: INCREMENTAL_UPDATE`));
154+
t.true(console.log.calledWith(`Origin: CONSOLE`));
155+
t.true(console.log.calledWith(`Version: 1`));
166156
});

functions/node8/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,16 @@ exports.makeUpperCase = (data, context) => {
281281
});
282282
};
283283
// [END functions_firebase_reactive_node8]
284+
285+
// [START functions_firebase_remote_config_node8]
286+
/**
287+
* Triggered by a change to a Firebase Remote Config value.
288+
*
289+
* @param {object} data The Cloud Functions event data.
290+
*/
291+
exports.helloRemoteConfig = (data) => {
292+
console.log(`Update type: ${data.updateType}`);
293+
console.log(`Origin: ${data.updateOrigin}`);
294+
console.log(`Version: ${data.versionNumber}`);
295+
};
296+
// [END functions_firebase_remote_config_node8]

functions/node8/test/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,19 @@ test(`should update data in response to Firestore events`, t => {
252252
t.true(console.log.calledWith(`Replacing value: foobar --> FOOBAR`));
253253
t.true(sample.mocks.firestore.set.calledWith({'original': 'FOOBAR'}));
254254
});
255+
256+
test(`should listen to Firebase Remote Config events`, t => {
257+
const sample = getSample();
258+
259+
const data = {
260+
updateOrigin: 'CONSOLE',
261+
updateType: 'INCREMENTAL_UPDATE',
262+
versionNumber: '1'
263+
};
264+
265+
sample.program.helloRemoteConfig(data);
266+
267+
t.true(console.log.calledWith(`Update type: INCREMENTAL_UPDATE`));
268+
t.true(console.log.calledWith(`Origin: CONSOLE`));
269+
t.true(console.log.calledWith(`Version: 1`));
270+
});

0 commit comments

Comments
 (0)