Skip to content

Feature Request/Proposal: Expanding Functions #447

@justjish

Description

@justjish

A few thoughts on expanding the Functions API.

I was thinking if it would be worthwhile to expand the functions API a bit, mainly in regard to handling loading and error states.
And potentially expose some helpful options. (I haven't fully thought through the various options, but wanted to start a discussion on them)

// Sorta risk combining types like this, but can't think of a cleaner way to keep things simple. 
type ReactFireHttpsCallableOptions & HttpsCallableOptions = {
   suspense: boolean, // Enable suspense? Maybe just use the internal settings to determine if we should suspend.
   start: boolean, // Calls the functions right away. 
   data?: RequestData // Firebase Typing, To use in conjunction with start option
   warmup: boolean // See Below
}
type Status = {
loading: boolean
error: FunctionsError // Firebase Typing
}
const [data, send, status] = useHttpsCallable<Req,Res>('calculate', {}:ReactFireHttpsCallableOptions) ;

ReactFireHttpsCallableOptions Breakdown

  • warmup: I know cold boot can be a massive pain point for some. To help alleviate this problem, I was thinking ReactFire can just make an OPTIONS request when useHttpsCallable is called. Since httpsCallable functions are just POST endpoints, this should be safe. Recreating the code to determine the url should be fairly trivial (just need to take into account; custom domains and region)
  • timeout: Comes from firebase's HttpsCallableOptions
  • start/data/suspense: Not fully sold on these, just wanted to mention them

useHttpsCallable / useFunctionsCallable / useFunctionsHttpsCallable

  • data: The payload of the function
  • send: Invokes the function. Optionally takes in ResponseData
  • status: An object with two properties, loading: boolean and error: FunctionsError | undefined

Example Use Cases

Simple

const [data, send, {loading, error} ] = useHttpsCallable('MyFunctions');

if (loading) return <LoadingComponent/>
if (error) return <ErrorFallback {...error}/>
return <button onClick={()=> send('my payload')}> Start </button>

Call function right away.

const [data, send, {loading, error} ] = useHttpsCallable('MyFunctions', { start: true, data: props.id });

if (loading) return <LoadingComponent/>
if (error) return <ErrorFallback {...error}/>
return <div>{data.message}</div>

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions