3

I am new to firebase functions. I am trying redirect functions under a new url instead of using default url from firebase functions.

My firebase.json looks like this. Is there a way to improve "rewrites"?

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  },
  "hosting": {
    "cleanUrls": true,
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],

    "rewrites": [
       {
        "source": "/.well-known/acme-challenge/wVJAlodX0whlzQQxznOSngFXGWFOLtsjk2F9l7oQDXc",
        "destination": "/firebaseHostingVerification"   
        },
      {
        "source": "api/v1/currencies", 
        "function": "currencies"
      },
        {
        "source": "api/v1/stop", 
        "function": "stop"
      },
        {
        "source": "api/v1/track", 
        "function": "track"
      },
        {
        "source": "api/v1/bookmark", 
        "function": "bookmark"
      },
        {
        "source": "api/v1/authenticate", 
        "function": "authenticate"
      },
        {
        "source": "api/v1/add-token", 
        "function": "addUserToken"
      },
        {
        "source": "api/v1/update-history", 
        "function": "updateHistory"
      },
        {
        "source": "api/v1/update-rates", 
        "function": "updateLatestRatesWithCodes"
      },
        {
        "source": "api/v1/createMoneyChanger", 
        "function": "createMoneyChanger"
      },
        {
        "source": "api/v1/moneyChanger", 
        "function": "moneyChanger"
      },
        {
        "source": "api/v1/updateRates", 
        "function": "updateRates"
      }
        //
        {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
  "database": {
    "rules": "database.rules.json"
  },
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "storage": {
    "rules": "storage.rules"
  }
}

Can I write it in such a way, whenever I add a new functions, it is will always be under my predefined url/function-name?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Devi
  • 55
  • 5

1 Answers1

0

If you're asking if it's possible to stop having to type "api/vi/" repeatedly, that is not an option. The rewrite rules are limited in this respect. You are required to call out the path in the URL for every rewrite.

It is possible to use rewrites to wildcard everything under a path to a specific function, but then in that function, you will have to look at the URL path to figure out what specifically you want to do with that URL. Some people use an express app to help with that. If you do this, you will have to deploy that entire function/app if you want to change even one of its options.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you @Doug Strevenson. Is there a better way to call functions from web app? If I don;t redirect, web app cannot call functions unless i allow CORS for all the functions. – Devi Apr 20 '19 at 03:01
  • You don't have to write HTTP triggers. You could write database triggers instead, and invoke them by writing into the database. – Doug Stevenson Apr 20 '19 at 03:16
  • How to write database triggers? I wanted to have APIs so that all font-end platforms:iOS,Android etc., can be controlled from one place. – Devi Apr 20 '19 at 03:32
  • Ok. I found it. https://firebase.google.com/docs/functions/database-events is it possible to have onRead() event? – Devi Apr 20 '19 at 03:39
  • No, that would not scale at all. – Doug Stevenson Apr 20 '19 at 03:49
  • I don't understand. Maybe my question is not clear. Eg, client needs to list items only which meet sudden criteria such as it is published and has more than 3 sub-items. In this situation, I was wondering if I could handle it from onRead() event. – Devi Apr 20 '19 at 03:59
  • There is no onRead event. You would have to write the response back into the document and read it on the client with a listener. You originally asked if there is a better way, and this is the only other way I can think of. Maybe there isn't a better way, by however you define "better". – Doug Stevenson Apr 20 '19 at 04:08
  • You're welcome. Please accept this answer as correct if you found it helpful. – Doug Stevenson Apr 20 '19 at 05:28