|
| 1 | +import { useRemoteConfig } from '../firebaseApp'; |
| 2 | +import { useObservable } from '../useObservable'; |
| 3 | +import { |
| 4 | + getValue, |
| 5 | + getString, |
| 6 | + getBoolean, |
| 7 | + getNumber, |
| 8 | + getAll, |
| 9 | + AllParameters |
| 10 | +} from './getValue'; |
| 11 | +import { Observable } from 'rxjs'; |
| 12 | + |
| 13 | +type RemoteConfig = import('firebase/app').remoteConfig.RemoteConfig; |
| 14 | +type RemoteConfigValue = import('firebase/app').remoteConfig.Value; |
| 15 | +type Getter$<T> = (remoteConfig: RemoteConfig, key: string) => Observable<T>; |
| 16 | + |
| 17 | +/** |
| 18 | + * Helper function to construct type safe functions. Since Remote Config has |
| 19 | + * methods that return different types for values, we need to be extra safe |
| 20 | + * to make sure we are not returning improper types by accident. |
| 21 | + * @param key |
| 22 | + * @param getter |
| 23 | + * @param remoteConfig |
| 24 | + */ |
| 25 | +function typeSafeUse<T>( |
| 26 | + key: string, |
| 27 | + getter: Getter$<T>, |
| 28 | + remoteConfig?: RemoteConfig |
| 29 | +): T { |
| 30 | + remoteConfig = remoteConfig || useRemoteConfig()(); |
| 31 | + const $value = getter(remoteConfig, key); |
| 32 | + return useObservable<T>($value, `remoteconfig:${key}`); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Accepts a key and optionally a Remote Config instance. Returns a |
| 37 | + * Remote Config Value. |
| 38 | + * |
| 39 | + * @param key The parameter key in Remote Config |
| 40 | + * @param remoteConfig Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
| 41 | + */ |
| 42 | +export function useRemoteConfigValue( |
| 43 | + key: string, |
| 44 | + remoteConfig?: RemoteConfig |
| 45 | +): RemoteConfigValue { |
| 46 | + return typeSafeUse<RemoteConfigValue>(key, getValue, remoteConfig); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Convience method similar to useRemoteConfigValue. Returns a `string` from a Remote Config parameter. |
| 51 | + * @param key The parameter key in Remote Config |
| 52 | + * @param remoteConfig Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
| 53 | + */ |
| 54 | +export function useRemoteConfigString( |
| 55 | + key: string, |
| 56 | + remoteConfig?: RemoteConfig |
| 57 | +): string { |
| 58 | + return typeSafeUse<string>(key, getString, remoteConfig); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Convience method similar to useRemoteConfigValue. Returns a `number` from a Remote Config parameter. |
| 63 | + * @param key The parameter key in Remote Config |
| 64 | + * @param remoteConfig Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
| 65 | + */ |
| 66 | +export function useRemoteConfigNumber( |
| 67 | + key: string, |
| 68 | + remoteConfig?: RemoteConfig |
| 69 | +): number { |
| 70 | + return typeSafeUse<number>(key, getNumber, remoteConfig); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Convience method similar to useRemoteConfigValue. Returns a `boolean` from a Remote Config parameter. |
| 75 | + * @param key The parameter key in Remote Config |
| 76 | + * @param remoteConfig Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
| 77 | + */ |
| 78 | +export function useRemoteConfigBoolean( |
| 79 | + key: string, |
| 80 | + remoteConfig?: RemoteConfig |
| 81 | +) { |
| 82 | + return typeSafeUse<boolean>(key, getBoolean, remoteConfig); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Convience method similar to useRemoteConfigValue. Returns allRemote Config parameters. |
| 87 | + * @param key The parameter key in Remote Config |
| 88 | + * @param remoteConfig Optional instance. If not provided ReactFire will either grab the default instance or lazy load. |
| 89 | + */ |
| 90 | +export function useRemoteConfigAll( |
| 91 | + key: string, |
| 92 | + remoteConfig?: RemoteConfig |
| 93 | +): AllParameters { |
| 94 | + return typeSafeUse<AllParameters>(key, getAll, remoteConfig); |
| 95 | +} |
0 commit comments