forked from firebase/functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (48 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const rp = require('request-promise');
const jsonDiff = require('json-diff');
admin.initializeApp();
// [START remote_config_function]
exports.showConfigDiff = functions.remoteConfig.onUpdate(versionMetadata => {
return admin.credential.applicationDefault().getAccessToken()
.then(accessTokenObj => {
return accessTokenObj.access_token;
})
.then(accessToken => {
const currentVersion = versionMetadata.versionNumber;
const templatePromises = [];
templatePromises.push(getTemplate(currentVersion, accessToken));
templatePromises.push(getTemplate(currentVersion - 1, accessToken));
return Promise.all(templatePromises);
})
.then(results => {
const currentTemplate = results[0];
const previousTemplate = results[1];
const diff = jsonDiff.diffString(previousTemplate, currentTemplate);
console.log(diff);
return null;
}).catch(error => {
console.error(error);
return null;
});
});
// [END remote_config_function]
function getTemplate(version, accessToken) {
const options = {
uri: 'https://firebaseremoteconfig.googleapis.com/v1/projects/remote-config-function/remoteConfig',
qs: {
versionNumber: version
},
headers: {
Authorization: 'Bearer ' + accessToken
},
json: true // Automatically parses the JSON string in the response
};
return rp(options).then(resp => {
return Promise.resolve(resp);
}).catch(err => {
console.error(err);
return Promise.resolve(null);
});
}