generated from UK-Export-Finance/nestjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(APIM-468): change how integer config values are parsed
- Loading branch information
Showing
5 changed files
with
192 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { InvalidConfigException } from '@ukef/config/invalid-config.exception'; | ||
|
||
// This helper function is used to get integer from configuration. | ||
export const getIntConfig = (environmentVariable: string, defaultValue?: number): number | undefined => { | ||
if (typeof environmentVariable === 'undefined') { | ||
if (typeof defaultValue === 'undefined') { | ||
throw new InvalidConfigException(`Environment variable is missing and doesn't have default value.`); | ||
} | ||
return defaultValue; | ||
} | ||
if (typeof environmentVariable !== 'string') { | ||
throw new InvalidConfigException(`Input environment variable type for ${environmentVariable} should be string.`); | ||
} | ||
|
||
const integer = parseInt(environmentVariable, 10); | ||
// Check if parseInt is number, decimal base integer and input didn't have anything non-numeric. | ||
if (isNaN(integer) || integer.toString() !== environmentVariable) { | ||
throw new InvalidConfigException(`Invalid integer value "${environmentVariable}" for configuration property.`); | ||
} | ||
return integer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters