import { useRemoteConfig } from './'; import { useObservable, ObservableStatus } from './useObservable'; import { getValue, getString, getBoolean, getNumber, getAll, AllParameters } from 'rxfire/remote-config'; import { Observable } from 'rxjs'; import type { RemoteConfig, Value as RemoteConfigValue } from 'firebase/remote-config'; type Getter$ = (remoteConfig: RemoteConfig, key: string) => Observable; /** * Helper function to construct type safe functions. Since Remote Config has * methods that return different types for values, we need to be extra safe * to make sure we are not returning improper types by accident. * @param key * @param getter * @param remoteConfig */ function useRemoteConfigValue_INTERNAL(key: string, getter: Getter$): ObservableStatus { const remoteConfig = useRemoteConfig(); const appName = remoteConfig.app.name; const $value = getter(remoteConfig, key); const observableId = `remoteConfig:${key}:${getter.name}:${appName}`; return useObservable(observableId, $value); } /** * Accepts a key and optionally a Remote Config instance. Returns a * Remote Config Value. * * @param key The parameter key in Remote Config */ export function useRemoteConfigValue(key: string): ObservableStatus { return useRemoteConfigValue_INTERNAL(key, getValue); } /** * Convience method similar to useRemoteConfigValue. Returns a `string` from a Remote Config parameter. * @param key The parameter key in Remote Config */ export function useRemoteConfigString(key: string): ObservableStatus { return useRemoteConfigValue_INTERNAL(key, getString); } /** * Convience method similar to useRemoteConfigValue. Returns a `number` from a Remote Config parameter. * @param key The parameter key in Remote Config */ export function useRemoteConfigNumber(key: string): ObservableStatus { return useRemoteConfigValue_INTERNAL(key, getNumber); } /** * Convience method similar to useRemoteConfigValue. Returns a `boolean` from a Remote Config parameter. * @param key The parameter key in Remote Config */ export function useRemoteConfigBoolean(key: string): ObservableStatus { return useRemoteConfigValue_INTERNAL(key, getBoolean); } /** * Convience method similar to useRemoteConfigValue. Returns allRemote Config parameters. * @param key The parameter key in Remote Config */ export function useRemoteConfigAll(key: string): ObservableStatus { return useRemoteConfigValue_INTERNAL(key, getAll); }