Cloud Functions
Call a function by name
Section titled “Call a function by name”firekitCallable wraps a Cloud Function in a typed, reactive caller. Pass the function name as registered in Firebase.
import { firekitCallable } from 'svelte-firekit';
// Type parameters: <InputType, OutputType>const sendWelcome = firekitCallable<{ userId: string }, { sent: boolean }>('sendWelcomeEmail');
// Call the functionconst result = await sendWelcome.call({ userId: 'abc123' });console.log(result.sent); // trueInstance properties
Section titled “Instance properties”| Property | Type | Description |
|---|---|---|
loading | boolean | true while the call is in flight |
error | Error | null | Set if the function call fails |
result | OutputType | null | The resolved return value of the last call |
Using in a component
Section titled “Using in a component”<script lang="ts"> import { firekitCallable } from 'svelte-firekit';
const invite = firekitCallable<{ email: string }, { success: boolean }>('inviteUser');
let email = $state('');
async function handleInvite() { await invite.call({ email }); }</script>
<button onclick={handleInvite} disabled={invite.loading}> {invite.loading ? 'Sending…' : 'Send Invite'}</button>
{#if invite.error} <p class="error">{invite.error.message}</p>{/if}
{#if invite.result?.success} <p>Invite sent!</p>{/if}Call a function by URL
Section titled “Call a function by URL”Use firekitCallableFromURL when you need to call a function at a specific endpoint (e.g. a different region, or a non-default Firebase project).
import { firekitCallableFromURL } from 'svelte-firekit';
const fn = firekitCallableFromURL<{ message: string }, { reply: string }>( 'https://us-east1-my-project.cloudfunctions.net/myFunction');
const result = await fn.call({ message: 'hello' });firekitCallableFromURL exposes the same loading, error, and result properties as firekitCallable.
Error handling
Section titled “Error handling”Firebase Functions errors include a code and details field. Cast to FunctionsError for typed access:
import type { FunctionsError } from 'firebase/functions';
try { await myFn.call(input);} catch (err) { const fnErr = err as FunctionsError; console.log(fnErr.code); // e.g. 'functions/permission-denied' console.log(fnErr.details); // custom error payload from the function}