15
15
*/
16
16
'use strict' ;
17
17
18
- const functions = require ( 'firebase-functions' ) ;
18
+ const { onCall, HttpsError} = require ( 'firebase-functions/v2/https' )
19
+ const { initializeApp} = require ( "firebase-admin/app" ) ;
20
+
19
21
const sanitizer = require ( './sanitizer' ) ;
20
- const admin = require ( 'firebase-admin' ) ;
21
- admin . initializeApp ( ) ;
22
+
23
+ initializeApp ( ) ;
22
24
23
25
// [START allAdd]
24
26
// [START addFunctionTrigger]
25
27
// Adds two numbers to each other.
26
- exports . addNumbers = functions . https . onCall ( ( data ) => {
28
+ exports . addNumbers = onCall ( ( data ) => {
27
29
// [END addFunctionTrigger]
28
30
// [START readAddData]
29
31
// Numbers passed from the client.
@@ -35,7 +37,7 @@ exports.addNumbers = functions.https.onCall((data) => {
35
37
// Checking that attributes are present and are numbers.
36
38
if ( ! Number . isFinite ( firstNumber ) || ! Number . isFinite ( secondNumber ) ) {
37
39
// 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 ' +
39
41
'two arguments "firstNumber" and "secondNumber" which must both be numbers.' ) ;
40
42
}
41
43
// [END addHttpsError]
@@ -54,7 +56,7 @@ exports.addNumbers = functions.https.onCall((data) => {
54
56
55
57
// [START messageFunctionTrigger]
56
58
// 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 ) => {
58
60
// [START_EXCLUDE]
59
61
// [START readMessageData]
60
62
// Message text passed from the client.
@@ -64,13 +66,13 @@ exports.addMessage = functions.https.onCall((data, context) => {
64
66
// Checking attribute.
65
67
if ( ! ( typeof text === 'string' ) || text . length === 0 ) {
66
68
// 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 ' +
68
70
'one arguments "text" containing the message text to add.' ) ;
69
71
}
70
72
// Checking that the user is authenticated.
71
73
if ( ! context . auth ) {
72
74
// 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 ' +
74
76
'while authenticated.' ) ;
75
77
}
76
78
// [END messageHttpsErrors]
@@ -85,7 +87,7 @@ exports.addMessage = functions.https.onCall((data, context) => {
85
87
86
88
// [START returnMessageAsync]
87
89
// 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.
89
91
return admin . database ( ) . ref ( '/messages' ) . push ( {
90
92
text : sanitizedMessage ,
91
93
author : { uid, name, picture, email } ,
@@ -102,7 +104,7 @@ exports.addMessage = functions.https.onCall((data, context) => {
102
104
return sanitizedMessage ;
103
105
} ) . catch ( ( error ) => {
104
106
// 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 ) ;
106
108
} ) ;
107
109
// [END returnMessageAsync]
108
110
// [END_EXCLUDE]
0 commit comments