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+
1921const 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