Fredric Cliver
Fredric Cliver

Reputation: 195

How can I change a path automatically for calling firebase functions

I'me making my project base on firebase environment. And for sending GET and DELETE call not only POST call. I'd registered my function with method .onRequest (because .onCall only supports POST call, I've checked this in document)

And now I wanted to call my function from frontend.

So I fetched call with this.

fetch(API_ROOT+"/api/sentences", {
  method: "GET"
})

And so I had to use my condition for not relying on the environment, dev or dist.

let API_ROOT = ""
if (location.hostname == "127.0.0.1" || location.hostname == "localhost") {
  console.log("This is local emulator environment")
  functions.useFunctionsEmulator("http://localhost:5001")
  API_ROOT = "http://localhost:5001/[my-project-id]/us-central1"
}else{
  API_ROOT = "https://us-central1-[my-project-id].cloudfunctions.net" 
}

But should I use this? Or is there any way similar to the .httpsCallable method with .onCall registering.

Because that one doesn't need a full path for calling, like below.

functions
  .httpsCallable("api/sentences")()
  .then((res) => console.log(res))

Upvotes: 0

Views: 641

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317760

The client SDK for callable functions is only intended to work with functions that you declare using onCall (I suggest reviewing that documentation again). It's almost certainly not expected to work with normal HTTP triggers declared with onRequest. Callable functions have their own very specific protocol and do not allow you to change the name or path URI of the function.

If you have an HTTP trigger, skip the Firebase client library and just use a regular HTTP client like you would with any other HTTP API.

Upvotes: 2

Related Questions