Skip to content

Commit 40ff143

Browse files
authored
Merge pull request #1013 from firebase/colerogers.remote-config
Adds v2 Remote Config Triggers
2 parents c1451e3 + 3c1a5f2 commit 40ff143

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

2nd-gen/remote-config-diff/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Get the difference between the previous and current Remote Config templates.
2+
3+
Use the Firebase Remote Config Cloud Function trigger to take action when your template is updated.
4+
5+
This sample demonstrates how to use the Remote Config Cloud Function trigger to log the difference between the previous and current Remote Config templates.
6+
7+
* See [eBay's real world implementation](https://github.com/eBay/firebase-remote-config-monitor).
8+
9+
* See the [docs](https://firebase.google.com/docs/functions/rc-events) for more on Remote Config Triggers.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
es6: true,
5+
node: true,
6+
},
7+
extends: [
8+
"eslint:recommended",
9+
"google",
10+
],
11+
rules: {
12+
quotes: ["error", "double"],
13+
},
14+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2022 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
// [START all]
19+
// [START import]
20+
// The Cloud Functions for Firebase SDK to create v2 Cloud Functions and set up triggers.
21+
const { onConfigUpdated } = require('firebase-functions/v2/remoteConfig');
22+
// The Firebase Admin SDK to obtain access tokens.
23+
const admin = require('firebase-admin');
24+
admin.initializeApp();
25+
const rp = require('request-promise');
26+
const jsonDiff = require('json-diff');
27+
// [END import]
28+
29+
// [START showconfigdiff]
30+
exports.showconfigdiff = onConfigUpdated((event) => {
31+
// Obtain the access token from the admin SDK
32+
return admin.credential.applicationDefault().getAccessToken()
33+
.then(accessTokenObj => {
34+
return accessTokenObj.access_token;
35+
})
36+
.then(accessToken => {
37+
// Get the version number from the event object
38+
const currentVersion = event.data.versionNumber;
39+
const templatePromises = [];
40+
templatePromises.push(getTemplate(currentVersion, accessToken));
41+
templatePromises.push(getTemplate(currentVersion - 1, accessToken));
42+
// Get the templates
43+
return Promise.all(templatePromises);
44+
})
45+
.then(results => {
46+
const currentTemplate = results[0];
47+
const previousTemplate = results[1];
48+
// Figure out the differences of the templates
49+
const diff = jsonDiff.diffString(previousTemplate, currentTemplate);
50+
// Log the difference
51+
functions.logger.log(diff);
52+
53+
return null;
54+
}).catch(error => {
55+
functions.logger.error(error);
56+
return null;
57+
});
58+
});
59+
// [END showconfigdiff]
60+
61+
// [START getTemplate]
62+
function getTemplate(version, accessToken) {
63+
const options = {
64+
uri: 'https://firebaseremoteconfig.googleapis.com/v1/projects/remote-config-function/remoteConfig',
65+
qs: {
66+
versionNumber: version
67+
},
68+
headers: {
69+
Authorization: 'Bearer ' + accessToken
70+
},
71+
json: true // Automatically parses the JSON string in the response
72+
};
73+
74+
// Obtain the template from the rest API
75+
return rp(options).then(resp => {
76+
return Promise.resolve(resp);
77+
}).catch(err => {
78+
functions.logger.error(err);
79+
return Promise.resolve(null);
80+
});
81+
}
82+
// [START getTemplate]
83+
// [END all]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "remote-config-diff",
3+
"description": "Get the difference between the previous and current Remote Config template",
4+
"scripts": {
5+
"lint": "eslint .",
6+
"serve": "firebase emulators:start --only functions",
7+
"shell": "firebase functions:shell",
8+
"start": "npm run shell",
9+
"deploy": "firebase deploy --only functions",
10+
"logs": "firebase functions:log",
11+
"compile": "cp ../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
12+
},
13+
"dependencies": {
14+
"firebase-admin": "^10.2.0",
15+
"firebase-functions": "^4.0.0",
16+
"json-diff": "^0.5.4",
17+
"request": "^2.88.2",
18+
"request-promise": "^4.2.5"
19+
},
20+
"devDependencies": {
21+
"eslint": "^6.8.0",
22+
"eslint-plugin-promise": "^4.2.1"
23+
},
24+
"engines": {
25+
"node": "16"
26+
},
27+
"private": true
28+
}

0 commit comments

Comments
 (0)