1

I am writing a firebase function.

And in there, I need a default value.

And since I set up a Remote Config in Firebase, I try to use it inside my firebase function (to have one source of truth as for the default-values)

Using the following code to retrieve a remoteConfig-parameter, how can you convert it to a typescript number ?

const remoteConfigTemplate = await admin.remoteConfig().getTemplate();

const defaultVal = remoteConfigTemplate.parameters["video_max_duration"].defaultValue

const actualValue = defaultVal.value;

const finalValue: number = Number(actualValue);

In the code above, I see that defaultVal is of type RemoteConfigParameterValue?. But how do I convert it to a number in typescript ??

I tried to cast the defaultValue but this does not work...

const defaultVal = remoteConfigTemplate.parameters["video_max_duration"].defaultValue as ExplicitParameterValue

The error is Cannot find name 'ExplicitParameterValue'.ts(2304)

What is still wrong ?

Is there another way of getting a Firebase Remote Config defaultValue into a firebase function ?

iKK
  • 6,394
  • 10
  • 58
  • 131

1 Answers1

1

defaultValuereturns a JSON object of Type RemoteConfigParameterValue

    type RemoteConfigParameterValue = ExplicitParameterValue|InAppDefaultValue; 
    interface ExplicitParameterValue { value: string };

You have to access the value field of this object:

    const defaultVal = (remoteConfigTemplate.parameters['video_max_duration']?.
      defaultValue as any).
      value as number