Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add version in cloud function for firebase

Hi I have created one cloud function getUsers. Its can be accessed like https://xyz.cloudfunctions.net/getUsers

exports.getUsers = functions.https.onRequest((request, response) => {

});

I need to do some changes in the function and want to keep same function name. I want to add version in the cloud function so that it can be accessed like this

https://xyz.cloudfunctions.net/getUsers/v2

What is the way to do this in cloud function for Firebase ?

like image 768
N Sharma Avatar asked Aug 07 '17 10:08

N Sharma


People also ask

How do I check my Firebase version?

Run firebase tools --version to check version. And as per the prompt, run npm install -g firebase-tools to update.

What version of node does Firebase use?

You can choose to run all functions in a project exclusively on the runtime environment corresponding to one of these supported Node. js versions: Node. js 16.


2 Answers

There is no built-in versioning for Cloud Functions. The closest you can get on the default URLs is writing the version into the function name: getUsers_v2.

If you're using Firebase Hosting to give friendly URLs to your Cloud Functions, you can of course map each individual function to the URL you want.

like image 62
Frank van Puffelen Avatar answered Oct 26 '22 01:10

Frank van Puffelen


One way of doing is this to make your own path handler. Of course, you can use express but I chose not to use it just for this.

exports.getUsers = functions.https.onRequest((request, response) => {

   const path = request.path;               // '/getUsers/v2'

   const version = path.split('/')[2];      // ['', 'getUsers', 'v2']

   switch ( version ) {

      case 'v1': // Handle for v1; 
             break;

      case 'v2': // Handle for v2; 
             break;
   }
});

This will expose your endpoint till https://xyz.cloudfunctions.net/getUsers but for trailing versions or any other stuff in url, you can use a handler like this.

like image 44
Divyansh Singh Avatar answered Oct 26 '22 01:10

Divyansh Singh