Skip to content

Commit fc658bf

Browse files
authored
Update functions used for testing to be v2 (#1701)
Functions v1 have been deprecated for a while, and we should use v2 when writing functions.
1 parent 0ac75a8 commit fc658bf

File tree

3 files changed

+1983
-1320
lines changed

3 files changed

+1983
-1320
lines changed

functions/functions/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*/
1616
'use strict';
1717

18-
const functions = require('firebase-functions');
18+
const {onCall, HttpsError} = require('firebase-functions/v2/https')
19+
const {initializeApp} = require("firebase-admin/app");
20+
1921
const sanitizer = require('./sanitizer');
20-
const admin = require('firebase-admin');
21-
admin.initializeApp();
22+
23+
initializeApp();
2224

2325
// [START allAdd]
2426
// [START addFunctionTrigger]
2527
// Adds two numbers to each other.
26-
exports.addNumbers = functions.https.onCall((data) => {
28+
exports.addNumbers = onCall((data) => {
2729
// [END addFunctionTrigger]
2830
// [START readAddData]
2931
// Numbers passed from the client.
@@ -35,7 +37,7 @@ exports.addNumbers = functions.https.onCall((data) => {
3537
// Checking that attributes are present and are numbers.
3638
if (!Number.isFinite(firstNumber) || !Number.isFinite(secondNumber)) {
3739
// Throwing an HttpsError so that the client gets the error details.
38-
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
40+
throw new HttpsError('invalid-argument', 'The function must be called with ' +
3941
'two arguments "firstNumber" and "secondNumber" which must both be numbers.');
4042
}
4143
// [END addHttpsError]
@@ -54,7 +56,7 @@ exports.addNumbers = functions.https.onCall((data) => {
5456

5557
// [START messageFunctionTrigger]
5658
// Saves a message to the Firebase Realtime Database but sanitizes the text by removing swearwords.
57-
exports.addMessage = functions.https.onCall((data, context) => {
59+
exports.addMessage = onCall((data, context) => {
5860
// [START_EXCLUDE]
5961
// [START readMessageData]
6062
// Message text passed from the client.
@@ -64,13 +66,13 @@ exports.addMessage = functions.https.onCall((data, context) => {
6466
// Checking attribute.
6567
if (!(typeof text === 'string') || text.length === 0) {
6668
// Throwing an HttpsError so that the client gets the error details.
67-
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
69+
throw new HttpsError('invalid-argument', 'The function must be called with ' +
6870
'one arguments "text" containing the message text to add.');
6971
}
7072
// Checking that the user is authenticated.
7173
if (!context.auth) {
7274
// Throwing an HttpsError so that the client gets the error details.
73-
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
75+
throw new HttpsError('failed-precondition', 'The function must be called ' +
7476
'while authenticated.');
7577
}
7678
// [END messageHttpsErrors]
@@ -85,7 +87,7 @@ exports.addMessage = functions.https.onCall((data, context) => {
8587

8688
// [START returnMessageAsync]
8789
// Saving the new message to the Realtime Database.
88-
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
90+
const sanitizedMessage = sanitizeText(text); // Sanitize the message.
8991
return admin.database().ref('/messages').push({
9092
text: sanitizedMessage,
9193
author: { uid, name, picture, email },
@@ -102,7 +104,7 @@ exports.addMessage = functions.https.onCall((data, context) => {
102104
return sanitizedMessage;
103105
}).catch((error) => {
104106
// Re-throwing the error as an HttpsError so that the client gets the error details.
105-
throw new functions.https.HttpsError('unknown', error.message, error);
107+
throw new HttpsError('unknown', error.message, error);
106108
});
107109
// [END returnMessageAsync]
108110
// [END_EXCLUDE]

0 commit comments

Comments
 (0)