@@ -22,62 +22,54 @@ const {onConfigUpdated} = require("firebase-functions/v2/remoteConfig");
22
22
const logger = require ( "firebase-functions/logger" ) ;
23
23
// The Firebase Admin SDK to obtain access tokens.
24
24
const admin = require ( "firebase-admin" ) ;
25
- admin . initializeApp ( ) ;
25
+ const app = admin . initializeApp ( ) ;
26
26
const fetch = require ( "node-fetch" ) ;
27
27
const jsonDiff = require ( "json-diff" ) ;
28
28
// [END import]
29
29
30
30
// [START showconfigdiff]
31
- exports . showconfigdiff = onConfigUpdated ( ( event ) => {
32
- // Obtain the access token from the admin SDK
33
- return admin . credential . applicationDefault ( ) . getAccessToken ( )
34
- . then ( ( accessTokenObj ) => {
35
- return accessTokenObj . access_token ;
36
- } )
37
- . then ( ( accessToken ) => {
38
- // Get the version number from the event object
39
- const currentVersion = event . data . versionNumber ;
40
- const templatePromises = [ ] ;
41
- templatePromises . push ( getTemplate ( currentVersion , accessToken ) ) ;
42
- templatePromises . push ( getTemplate ( currentVersion - 1 , accessToken ) ) ;
43
- // Get the templates
44
- return Promise . all ( templatePromises ) ;
45
- } )
46
- . then ( ( results ) => {
47
- const currentTemplate = results [ 0 ] ;
48
- const previousTemplate = results [ 1 ] ;
49
- // Figure out the differences of the templates
50
- const diff = jsonDiff . diffString ( previousTemplate , currentTemplate ) ;
51
- // Log the difference
52
- logger . log ( diff ) ;
31
+ exports . showconfigdiff = onConfigUpdated ( async ( event ) => {
32
+ try {
33
+ // Obtain the access token from the Admin SDK
34
+ const accessTokenObj = await admin . credential . applicationDefault ( )
35
+ . getAccessToken ( ) ;
36
+ const accessToken = accessTokenObj . access_token ;
53
37
54
- return null ;
55
- } ) . catch ( ( error ) => {
56
- logger . error ( error ) ;
57
- return null ;
58
- } ) ;
38
+ // Get the version number from the event object
39
+ const remoteConfigApi = "https://firebaseremoteconfig.googleapis.com/v1/" +
40
+ `projects/${ app . options . projectId } /remoteConfig` ;
41
+ const currentVersion = event . data . versionNumber ;
42
+ const prevVersion = currentVersion - 1 ;
43
+ const templatePromises = [ ] ;
44
+ templatePromises . push ( fetch (
45
+ remoteConfigApi ,
46
+ {
47
+ method : "POST" ,
48
+ body : new URLSearchParams ( [ [ "versionNumber" , currentVersion + "" ] ] ) ,
49
+ headers : { Authorization : "Bearer " + accessToken } ,
50
+ } ,
51
+ ) ) ;
52
+ templatePromises . push ( fetch (
53
+ remoteConfigApi ,
54
+ {
55
+ method : "POST" ,
56
+ body : new URLSearchParams ( [ [ "versionNumber" , prevVersion + "" ] ] ) ,
57
+ headers : { Authorization : "Bearer " + accessToken } ,
58
+ } ,
59
+ ) ) ;
60
+
61
+ // Get the templates
62
+ const responses = await Promise . all ( templatePromises ) ;
63
+ const results = responses . map ( ( r ) => r . json ( ) ) ;
64
+ const currentTemplate = results [ 0 ] ;
65
+ const previousTemplate = results [ 1 ] ;
66
+ // Figure out the differences of the templates
67
+ const diff = jsonDiff . diffString ( previousTemplate , currentTemplate ) ;
68
+ // Log the difference
69
+ logger . log ( diff ) ;
70
+ } catch ( error ) {
71
+ logger . error ( error ) ;
72
+ }
59
73
} ) ;
60
74
// [END showconfigdiff]
61
-
62
- // [START getTemplate]
63
- /**
64
- * Get a specific version of a Remote Config template
65
- * @param {number } version
66
- * @param {string } accessToken
67
- * @return {Promise<string> } the template as JSON
68
- */
69
- async function getTemplate ( version , accessToken ) {
70
- const params = new URLSearchParams ( ) ;
71
- params . append ( "versionNumber" , version + "" ) ;
72
- const response = await fetch (
73
- "https://firebaseremoteconfig.googleapis.com/v1/projects/remote-config-function/remoteConfig" ,
74
- {
75
- method : "POST" ,
76
- body : params ,
77
- headers : { Authorization : "Bearer " + accessToken } ,
78
- } ,
79
- ) ;
80
- return response . json ( ) ;
81
- }
82
- // [END getTemplate]
83
75
// [END all]
0 commit comments