How to add version in cloud function for firebase

Viewed 4765

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 ?

2 Answers

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.

Related