Function Signature
Parameters
The environment variable name that points to the secret file path
Array of allowed string values for the enum
Returns
Promise<T[number] | undefined> - One of the allowed enum values, or undefined if not found
How It Works
This is the optional variant ofsecretEnum. It reads secrets from the filesystem without throwing errors when the file is missing:
- Checks if the environment variable
keycontains a file path - If no path is set, defaults to
/run/secrets/{key} - Reads the file contents and validates it against allowed values
- Returns
undefinedif the secret file doesn’t exist (instead of throwing) - Still throws an error if the file exists but contains a value not in the allowed list
Difference from secretEnum
- maybeSecretEnum: Returns
undefinedwhen the secret file is missing - secretEnum: Throws an error when the secret file is missing (unless a fallback is provided)
maybeSecretEnum for truly optional enum secrets where absence is a valid state.
Example
Error Handling
Returnsundefined if:
- The secret file doesn’t exist
- The file is empty
- The file exists but the value is not one of the allowed enum values
