diff --git a/Node-1st-gen/delete-unused-accounts-cron/functions/index.js b/Node-1st-gen/delete-unused-accounts-cron/functions/index.js index c912b60518..47d75c15fa 100644 --- a/Node-1st-gen/delete-unused-accounts-cron/functions/index.js +++ b/Node-1st-gen/delete-unused-accounts-cron/functions/index.js @@ -16,6 +16,8 @@ 'use strict'; const functions = require('firebase-functions/v1'); +const { onSchedule } = require("firebase-functions/v2/scheduler"); + const admin = require('firebase-admin'); admin.initializeApp(); const PromisePool = require('es6-promise-pool').default; @@ -26,7 +28,8 @@ const MAX_CONCURRENT = 3; * Run once a day at midnight, to cleanup the users * Manually run the task here https://console.cloud.google.com/cloudscheduler */ -exports.accountcleanup = functions.pubsub.schedule('every day 00:00').onRun(async context => { +exports.accountcleanup = onSchedule('every day 00:00', async (event) => { + // Fetch all user details. const inactiveUsers = await getInactiveUsers(); diff --git a/Node-1st-gen/presence-firestore/functions/index.js b/Node-1st-gen/presence-firestore/functions/index.js index f91e958640..808933bc19 100644 --- a/Node-1st-gen/presence-firestore/functions/index.js +++ b/Node-1st-gen/presence-firestore/functions/index.js @@ -16,6 +16,8 @@ // [START presence_sync_function] const functions = require('firebase-functions/v1'); +const { onValueUpdated } = require("firebase-functions/v2/database"); + const admin = require('firebase-admin'); admin.initializeApp(); @@ -26,8 +28,8 @@ const firestore = admin.firestore(); // Create a new function which is triggered on changes to /status/{uid} // Note: This is a Realtime Database trigger, *not* Firestore. -exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate( - async (change, context) => { +exports.onUserStatusChanged = onValueUpdated('/status/{uid}', async ({ change, context }) => { + // Get the data written to Realtime Database const eventStatus = change.after.val(); diff --git a/Node-1st-gen/quickstarts/auth-blocking-functions/functions/index.js b/Node-1st-gen/quickstarts/auth-blocking-functions/functions/index.js index d8deda2ba5..0ea63dcc87 100644 --- a/Node-1st-gen/quickstarts/auth-blocking-functions/functions/index.js +++ b/Node-1st-gen/quickstarts/auth-blocking-functions/functions/index.js @@ -15,6 +15,8 @@ */ const {functions} = require("firebase-functions/v1"); +const { beforeUserCreated, beforeUserSignedIn } = require("firebase-functions/v2/identity"); + const {admin} = require("firebase-admin"); admin.initializeApp(); @@ -23,9 +25,8 @@ const db = admin.firestore(); // [START v1ValidateNewUser] // [START v1beforeCreateFunctionTrigger] // Block account creation with any non-acme email address. -exports.validateNewUser = functions.auth - .user() - .beforeCreate((user, context) => { +exports.validateNewUser = beforeUserCreated(({ data: user }) => { + // [END v1beforeCreateFunctionTrigger] // [START v1readEmailData] // Email passed from the User object. @@ -48,9 +49,8 @@ exports.validateNewUser = functions.auth // [START v1CheckForBan] // [START v1beforeSignInFunctionTrigger] // Block account sign in with any banned account. -exports.checkForBan = functions.auth - .user() - .beforeSignIn(async (user, context) => { +exports.checkForBan = beforeUserSignedIn(async ({ data: user }) => { + // [END v1beforeSignInFunctionTrigger] // [START v1readEmailData] // Email passed from the User object. diff --git a/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js b/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js index ba00b0ad13..681f64ae25 100644 --- a/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js +++ b/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js @@ -17,6 +17,8 @@ // [START import] const functions = require('firebase-functions/v1'); +const { onMessagePublished } = require("firebase-functions/v2/pubsub"); + // [END import] // [START helloWorld] @@ -25,7 +27,8 @@ const functions = require('firebase-functions/v1'); * topic. */ // [START trigger] -exports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) => { +exports.helloPubSub = onMessagePublished('topic-name', ({ message, context }) => { + // [END trigger] // [START readBase64] // Decode the PubSub Message body. @@ -41,7 +44,8 @@ exports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) = * Cloud Function to be triggered by Pub/Sub that logs a message using the data published to the * topic as JSON. */ -exports.helloPubSubJson = functions.pubsub.topic('another-topic-name').onPublish((message) => { +exports.helloPubSubJson = onMessagePublished('another-topic-name', ({ message, context }) => { + // [START readJson] // Get the `name` attribute of the PubSub message JSON body. let name = null; @@ -60,7 +64,8 @@ exports.helloPubSubJson = functions.pubsub.topic('another-topic-name').onPublish * Cloud Function to be triggered by Pub/Sub that logs a message using the data attributes * published to the topic. */ -exports.helloPubSubAttributes = functions.pubsub.topic('yet-another-topic-name').onPublish((message) => { +exports.helloPubSubAttributes = onMessagePublished('yet-another-topic-name', ({ message, context }) => { + // [START readAttributes] // Get the `name` attribute of the message. const name = message.attributes.name; diff --git a/Node-1st-gen/quickstarts/thumbnails/functions/index.js b/Node-1st-gen/quickstarts/thumbnails/functions/index.js index 0a3f73970d..e2cbacedc9 100644 --- a/Node-1st-gen/quickstarts/thumbnails/functions/index.js +++ b/Node-1st-gen/quickstarts/thumbnails/functions/index.js @@ -17,6 +17,8 @@ // [START import] const functions = require('firebase-functions/v1'); +const { onObjectFinalized } = require("firebase-functions/v2/storage"); + const admin = require('firebase-admin'); admin.initializeApp() const path = require('path'); @@ -31,7 +33,8 @@ const sharp = require('sharp'); * generate a thumbnail automatically using sharp. */ // [START generateThumbnailTrigger] -exports.firstGenGenerateThumbnail = functions.storage.object().onFinalize(async (object) => { +exports.firstGenGenerateThumbnail = onObjectFinalized(async ({ object, context }) => { + // [END generateThumbnailTrigger] // [START eventAttributes] const fileBucket = object.bucket; // The Storage bucket that contains the file. diff --git a/Node-1st-gen/quickstarts/uppercase-firestore/functions/index.js b/Node-1st-gen/quickstarts/uppercase-firestore/functions/index.js index c5033d399e..f6cb7984b8 100644 --- a/Node-1st-gen/quickstarts/uppercase-firestore/functions/index.js +++ b/Node-1st-gen/quickstarts/uppercase-firestore/functions/index.js @@ -19,6 +19,9 @@ // [START import] // The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers. const functions = require('firebase-functions/v1'); +const { onRequest } = require("firebase-functions/v2/https"); +const { onDocumentCreated } = require("firebase-functions/v2/firestore"); + // The Firebase Admin SDK to access Firestore. const admin = require("firebase-admin"); @@ -29,7 +32,8 @@ admin.initializeApp(); // Take the text parameter passed to this HTTP endpoint and insert it into // Firestore under the path /messages/:documentId/original // [START addMessageTrigger] -exports.addMessage = functions.https.onRequest(async (req, res) => { +exports.addMessage = onRequest(async (req, res) => { + // [END addMessageTrigger] // Grab the text parameter. const original = req.query.text; @@ -49,9 +53,8 @@ exports.addMessage = functions.https.onRequest(async (req, res) => { // Listens for new messages added to /messages/:documentId/original and creates an // uppercase version of the message to /messages/:documentId/uppercase // [START makeUppercaseTrigger] -exports.makeUppercase = functions.firestore - .document("/messages/{documentId}") - .onCreate((snap, context) => { +exports.makeUppercase = onDocumentCreated("/messages/{documentId}", ({ snapshot: snap, context }) => { + // [END makeUppercaseTrigger] // [START makeUppercaseBody] // Grab the current value of what was written to Firestore. diff --git a/Node-1st-gen/quickstarts/uppercase-rtdb/functions/index.js b/Node-1st-gen/quickstarts/uppercase-rtdb/functions/index.js index 16ad1ebc33..fe60a407fa 100644 --- a/Node-1st-gen/quickstarts/uppercase-rtdb/functions/index.js +++ b/Node-1st-gen/quickstarts/uppercase-rtdb/functions/index.js @@ -19,6 +19,9 @@ // [START import] // The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers. const functions = require('firebase-functions/v1'); +const { onRequest } = require("firebase-functions/v2/https"); +const { onValueCreated } = require("firebase-functions/v2/database"); + // The Firebase Admin SDK to access the Firebase Realtime Database. const admin = require('firebase-admin'); @@ -29,7 +32,8 @@ admin.initializeApp(); // Take the text parameter passed to this HTTP endpoint and insert it into the // Realtime Database under the path /messages/:pushId/original // [START addMessageTrigger] -exports.addMessage = functions.https.onRequest(async (req, res) => { +exports.addMessage = onRequest(async (req, res) => { + // [END addMessageTrigger] // Grab the text parameter. const original = req.query.text; @@ -45,8 +49,8 @@ exports.addMessage = functions.https.onRequest(async (req, res) => { // [START makeUppercase] // Listens for new messages added to /messages/:pushId/original and creates an // uppercase version of the message to /messages/:pushId/uppercase -exports.makeUppercase = functions.database.ref('/messages/{pushId}/original') - .onCreate((snapshot, context) => { +exports.makeUppercase = onValueCreated('/messages/{pushId}/original', ({ snapshot, context }) => { + // Grab the current value of what was written to the Realtime Database. const original = snapshot.val(); functions.logger.log('Uppercasing', context.params.pushId, original); diff --git a/Node-1st-gen/remote-config-diff/functions/index.js b/Node-1st-gen/remote-config-diff/functions/index.js index ef8e1c3d01..1940fb8160 100644 --- a/Node-1st-gen/remote-config-diff/functions/index.js +++ b/Node-1st-gen/remote-config-diff/functions/index.js @@ -15,13 +15,16 @@ */ const functions = require('firebase-functions/v1'); +const { onConfigUpdated } = require("firebase-functions/v2/remoteConfig"); + const admin = require('firebase-admin'); const jsonDiff = require('json-diff'); admin.initializeApp(); // [START remote_config_function] -exports.showConfigDiff = functions.remoteConfig.onUpdate(versionMetadata => { +exports.showConfigDiff = onConfigUpdated(({ version: versionMetadata, context }) => { + return admin.credential.applicationDefault().getAccessToken() .then(accessTokenObj => { return accessTokenObj.access_token;