Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing functions hook "useHttpsCallableFromUrl" #287

Open
nipunasudha opened this issue Jan 4, 2023 · 0 comments
Open

Missing functions hook "useHttpsCallableFromUrl" #287

nipunasudha opened this issue Jan 4, 2023 · 0 comments

Comments

@nipunasudha
Copy link

I noticed the functions hook "useHttpsCallableFromUrl" is missing in the library. I'm not able to PR at the moment but here's a custom hook I use for that:

import {Functions, httpsCallableFromURL, HttpsCallableResult,} from 'firebase/functions';
import {useCallback, useState} from 'react';

export type HttpsCallableHook<
    RequestData = unknown,
    ResponseData = unknown
> = Readonly<
    [
        (
            data?: RequestData
        ) => Promise<HttpsCallableResult<ResponseData> | undefined>,
        boolean,
            Error | undefined
    ]
>;

export function useHttpsCallableFromUrl<RequestData = unknown, ResponseData = unknown>(
    functions: Functions,
    name: string
): HttpsCallableHook<RequestData, ResponseData> {
    const [error, setError] = useState<Error>();
    const [loading, setLoading] = useState<boolean>(false);

    const callCallable = useCallback(
        async (
            data?: RequestData
        ): Promise<HttpsCallableResult<ResponseData> | undefined> => {
            const callable = httpsCallableFromURL<RequestData, ResponseData>(
                functions,
                name
            );
            setLoading(true);
            setError(undefined);
            try {
                return await callable(data);
            } catch (err) {
                setError(err as Error);
            } finally {
                setLoading(false);
            }
        },
        [functions, name]
    );

    return [callCallable, loading, error] as const;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant