N Sharma
N Sharma

Reputation: 34497

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 ?

Upvotes: 14

Views: 6209

Answers (2)

Divyansh Singh
Divyansh Singh

Reputation: 401

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.

Upvotes: 11

Frank van Puffelen
Frank van Puffelen

Reputation: 599081

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.

Upvotes: 13

Related Questions